summaryrefslogtreecommitdiff
path: root/media-libs/tiff
diff options
context:
space:
mode:
authorV3n3RiX <venerix@redcorelinux.org>2017-10-09 18:53:29 +0100
committerV3n3RiX <venerix@redcorelinux.org>2017-10-09 18:53:29 +0100
commit4f2d7949f03e1c198bc888f2d05f421d35c57e21 (patch)
treeba5f07bf3f9d22d82e54a462313f5d244036c768 /media-libs/tiff
reinit the tree, so we can have metadata
Diffstat (limited to 'media-libs/tiff')
-rw-r--r--media-libs/tiff/Manifest60
-rw-r--r--media-libs/tiff/files/tiff-3.9.7-CVE-2012-4447.patch191
-rw-r--r--media-libs/tiff/files/tiff-3.9.7-CVE-2012-4564.patch77
-rw-r--r--media-libs/tiff/files/tiff-3.9.7-CVE-2012-5581.patch245
-rw-r--r--media-libs/tiff/files/tiff-3.9.7-printdir-width.patch36
-rw-r--r--media-libs/tiff/files/tiff-3.9.7-tiffinfo-exif.patch59
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-CVE-2016-10266.patch46
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-CVE-2016-10267.patch53
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-CVE-2017-5225.patch74
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2130.patch112
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2535.patch54
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2594.patch28
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2597.patch41
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2598.patch31
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2599.patch54
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2604.patch108
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2605.patch55
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2607.patch41
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2608.patch104
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2610.patch46
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2619.patch46
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2620.patch29
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2621.patch49
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2627.patch59
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2631.patch34
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2633-bug2634.patch41
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2635.patch33
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2638.patch29
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2639.patch58
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2640.patch28
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2642-bug2643-bug2646-bug2647.patch278
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2644.patch45
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2648.patch33
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2650-2.patch26
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2650.patch54
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2651.patch86
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2653.patch33
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2658.patch33
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2659-2.patch41
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2659.patch34
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-bug2665.patch43
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-fax2tiff.patch39
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-hylafax-hack.patch38
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0005-Leak-TIFFFetchStripThing.patch20
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0006-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch33
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0007-uninitialized-value.patch26
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0008-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch26
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0013-validate-refblackwhite.patch42
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0017-safe_skews_in_gtTileContig.patch97
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0018-fix-leak-in-PredictorSetupDecode.patch27
-rw-r--r--media-libs/tiff/files/tiff-4.0.7-pdfium-0021-oom-TIFFFillStrip.patch29
-rw-r--r--media-libs/tiff/metadata.xml14
-rw-r--r--media-libs/tiff/tiff-3.9.7-r1.ebuild71
-rw-r--r--media-libs/tiff/tiff-4.0.7-r3.ebuild121
-rw-r--r--media-libs/tiff/tiff-4.0.7.ebuild73
-rw-r--r--media-libs/tiff/tiff-4.0.8.ebuild84
56 files changed, 3367 insertions, 0 deletions
diff --git a/media-libs/tiff/Manifest b/media-libs/tiff/Manifest
new file mode 100644
index 000000000000..22e144c2a46b
--- /dev/null
+++ b/media-libs/tiff/Manifest
@@ -0,0 +1,60 @@
+AUX tiff-3.9.7-CVE-2012-4447.patch 5706 SHA256 373020d6c383778ee40f642d90e5d9f3a878f0c17a529825e43e1647d27332cf SHA512 defb8251401b7d65c2cd8f60df30d35551c1b1d0a1dcf514dd95da89572873177ea116e9373dd07cd260e00434235090e1d8864199d5fdfa84c445cb6905ddd6 WHIRLPOOL ac54215b65806681ae5e337a538ce91357247f28815eee7a29ff5eef179075ec43b473a7f62195056a37318e84ae15a2d503fd94b54a0dee173ac6851858f342
+AUX tiff-3.9.7-CVE-2012-4564.patch 1987 SHA256 525f667e2148229520b50d6136c0ecd345b8db9acc62fde945a5f13dae4d51f3 SHA512 24ebe60ce6361561c15c8c5fb46b47942e58912de5efbf128374defc4382a7e800fae3dc0a9fe04876a5e2f61a109edc1c9533be2f8a15b4b0ed7215d7b08c9b WHIRLPOOL c59dfed1f43b75372e9e6bf3381db608d42291e8613c6f38d8b1310868b4f373b15a279144a8f06cb0d9ba5d147a81ca753b6ef118e23e47893902d1d00cf880
+AUX tiff-3.9.7-CVE-2012-5581.patch 8156 SHA256 f47b30c8fc0578df7285f6cf318f29d410db4b82550b3fbc9582beeb9a834415 SHA512 2e215edeb6f4f5d6e14753874a67d76cfec34b3f6ffc420e1c7ede2007a6b2f64c09505e879e83db1de87f28c82c806c4379b38bf7f8735bb2bae675543683f0 WHIRLPOOL 8f7cdca5ec968eab6ec8749e2185c7416fb2055da56ce3b159a637ede9f296e9a37af7c91ff8da1c743bb05371662725374d3febdde2109f18baa57391ac8e16
+AUX tiff-3.9.7-printdir-width.patch 1523 SHA256 597406f727b26fd06106e1e22a1e4e4620b3ffa54a49c2c4b0b8ee6b1d54908b SHA512 9bf2edcbda2ed5dba01839cf1bb34316801b4c5a2b6c71ed46f8777518cf1bc77084db94eaf1ebde84583fa2e1749a5fc5151e321b4d83975b13c3e9ebe96436 WHIRLPOOL 3f9a830622866cbc1fab8109fa9ad787c50230871286e6bdc3594b0d33c887acdca03b19df8d4537ff0e21a6f6a2e48062ff731616b300ead923d8e61253094b
+AUX tiff-3.9.7-tiffinfo-exif.patch 1847 SHA256 2b333f3161f88aef3f764de316c4e8f10906932d33ee575b98f7723b2bff1db7 SHA512 6f211dc864bfb314a1c7edb8855b68cfbbdbbde1ba9422c1c578acbb15e5769323eec366bef618a8100b0ccb8057b2997762ebbd0f943be10882411861ec72fb WHIRLPOOL 4fb1375cc34c889f2148d8b998929f29f8901c38ac3af0839abcd827adf707045b00e7516e7b92df6cd2968dcbbe98ac135662300bc4fe05fb4b43b30f340baa
+AUX tiff-4.0.7-CVE-2016-10266.patch 2045 SHA256 ba601397311aee6944b2013928c7e633b7b9df8c04747c2b7fa66a958098e547 SHA512 ac7d827e715112f1ce3ab86aa096d8f6957ece3ee51b828dfad97771ed25d4058518035103fe82cf46074b49d374592d73f0ffe2a7d9c277d20086d02c91ef7b WHIRLPOOL 807ddb40ec4858045a86736fc14699f04091777dcb93a8b1609b7f856799089cd5e07b4b023dac4c24473b92fb04ec068d58747d164748ba99112919b3dfaa84
+AUX tiff-4.0.7-CVE-2016-10267.patch 1547 SHA256 c4456c25984ff3a74efd755fed1f5bf0f95b570ffe3a47de797626d7e770d0fb SHA512 2c1bcce682ebefd26347e4bce3f1adeb42ce814927f5bc1f099a193b12359504dc4324051f3bf8411a6a3af775e735551dd4ece5229739d1f7e692ecb7179a79 WHIRLPOOL c065d7bc76e1a6bf43e3996e3fcfd23e9ea29db13f8830ed3348d591adf4ffa0b25d7c375bb19e13747520ece89715ea660ecd5a514292fe7a4d217fe523136b
+AUX tiff-4.0.7-CVE-2017-5225.patch 2528 SHA256 8a56aafe16a27c33319141a6e8af5efc47973fabf3d270ff4963a0fde2da8b31 SHA512 0e8dd037df0e962ae4ea56098c023f443a8d20d850fc08c4454884ee60e139f9600bebc5ae44ae272aabe9e97f99665019e556e2484ea9c98b19b99385134000 WHIRLPOOL 0360e6797ae289e0b16f75bc84de3bdd986b4df823fb0dce7ae51595c46c75459e38803e3053253f0f82e8532ad33a1f06dac490b99c9945118dd4d053032ea1
+AUX tiff-4.0.7-bug2130.patch 3732 SHA256 d7997ea154b8ca281da294cc51ba3ba2d7799e18c096e40bb670055f368f067d SHA512 1430355ece6593e0d690aabef1580fa4d5fdc28240771e2e181dadbbcd3c32cad7793437331db6e6080209a080cf8ed3c4ee08fae0c465e5bdbe83584bc5b34e WHIRLPOOL eaa45155a3a5abe1b47e743659ac196bbf22d85be4587df0ab04f0a17c2875d9bcafd9037726aff5e07af1668ab8bc8d0c9e1ccc72d37e2d19f188d2b6df75df
+AUX tiff-4.0.7-bug2535.patch 1857 SHA256 3be096e7bb4d4b3208da61698f2eeca88336a8714f17996fc07ed113fe731ed9 SHA512 056ebeff43745290af9287358378180c04b3099a7d77fef22a0cc41725ef99d3e5a130c66769ccbec79547a1207853b2d56a8fc8f50e69576a58db02af9167a4 WHIRLPOOL a407ed6cd0645a1aa8502db631a15e1025e87286a9dc6f89dcecd2704fa8ed97edc2ec686f32a31085397d88bac6fd68b0e3c7e3ecbb24866f4744ee3823c294
+AUX tiff-4.0.7-bug2594.patch 920 SHA256 9a624c1fff5a7dabff6d1f2f67bb0fd9b8504a2556639035245e557ee2c96d83 SHA512 301353f930fbb0fd43f1dbd7ea7c31cb069d2ac9db025a9434dd2fe4de3458aa2c8b464b587d2fc25220dc2a8149a0970b96f0d40dc415134c947e4deb3eede8 WHIRLPOOL 758c3edb8441a922867c01a3f3036c78339982e22089f1ffe58f247e6130a11074253c46eec639714c516e5fdc2ede5c5b5d0ab39031031f0b38634bf3e084bb
+AUX tiff-4.0.7-bug2597.patch 1312 SHA256 2972182bd4966581844ad1908c36e0166a06d7832c63569be7f6b2321041993f SHA512 0cf3a6cb80c7421a681104560c2cdbed32fa9d544ae990b3d67c4768d4609db3448444742bf2893aedf03845a1b8c3b3188a8b5a8f6c506c3857b2e3a9e46b8f WHIRLPOOL 79870fc8332677dbc5d77fb92b04417712138ef04540d17a37cf30880a414f2940690ce8fe878f82b8a29735d44bb4af82ad04e516e20d611021520d53a60008
+AUX tiff-4.0.7-bug2598.patch 1085 SHA256 eb1f52d044652d99a117b5a97fe18d2bc1dba99d44ffc8d09d8e5946aa73978c SHA512 fd8228b1e1529153312653bed78e4ac662d8b2f6ac4b3714092c3524363be9a2f1048a874b6b4075acf47bcfb417738eb2b6b3fc8ba302fdcca43a52a9ce8754 WHIRLPOOL 19d858d6f1f7a2e13586cefd3cb538b207656845f809e7bdb8503f7f3dba5a7c753c7a38a342fa41e86f81d6894758eae20f0a61f9471d494bcf619339f36bd9
+AUX tiff-4.0.7-bug2599.patch 2173 SHA256 1649d629657870eb5d050c6c0889271d9c4bf029476e80be814161451d5d0091 SHA512 b24118267d9f6598369240c63f03398d2823499879e89ea5427ac144a29f9eaa246c436aa9393487627f978bcc7669c7d1024e247ddd560b099d9bbfd42a1fbb WHIRLPOOL 40657586ced72688acbd66f7bbfca01e6b1333175bdff2e35a2b50eddff75616cd05888ebbb150e9f4b439fddf94f67b4b0f60f7a6f925e3dec91afb5d2a222c
+AUX tiff-4.0.7-bug2604.patch 4132 SHA256 1021d95174247612bac263ae2d69914f3ac8800f7f59691eea96b0c2cea43bad SHA512 37f17f363c610da317b2bf5792dcd3a0a23de91dafba600dc05a7096e7eca9e310ec5395d81a8696854a9f0a2eeb1fb787dc7828b0c7e4510894fa02354b7311 WHIRLPOOL 34be05a439f56022f29555b71f55ac44f8f9ef21006975393d587ef6793452ee689ecd4e9834dc45f0d31f66bcc8b612b470499a53ffe74eb0af045740974e40
+AUX tiff-4.0.7-bug2605.patch 1617 SHA256 2987d5da4f762b2d880761f632c2225ec97e1c1b260774ce8935427501a90c3d SHA512 26cfba6bbfe06eedd2969eb674dac45f37d1deb1ebd7f1f3a531cbc0caba65efa79161f00dfd6b9adeb2ee5412857f7dd372dd3c4d62db12f29c93d130c0029f WHIRLPOOL 004f35954940a621d74eac54633b02531da3ec1ee83192f72d44aad27022e0b6a6ec41ca0a5ef9ef0824c38d5f8eff1e39f6b2273b19b4a6174ce5c2ba3b31aa
+AUX tiff-4.0.7-bug2607.patch 1326 SHA256 dc572bf83af4a5258c10a6416daf49d44638c578de173fdfd843da611ec5ac98 SHA512 c705564dc39314403a0171ee7a5f96567e14b7c7e23729b1a5853a51a02e9c9d12cbcf1ccf3c46755aafd8df1f35ac9cb462d7997a1122fb2f744e6a7c6241b9 WHIRLPOOL 0d5412237283dff67833113a4a176540de0b0ca8b4dc89155007b3095059cf05f5b569f4e8790791fe32aace3f303b6aa9a92a2b960dffea1a7ccc7402a7f519
+AUX tiff-4.0.7-bug2608.patch 4143 SHA256 1ee769ceba4c8afa0adb27ee5204b33086d5ab39f204ebb64854696c3be26d50 SHA512 19e8cf418f0f485a257ce847c755b52e56f73a2371032263bd121af357a3b1fe01aa462d3d0e7c427a69a2daa7374f721fc766e02a037f6c73cd92615ef39b33 WHIRLPOOL 4d69d4047bf822924c6b1d434959ceb3249149730bf9f91bb9db0544770b59c529f05b7f7e056ae80345bcdf374f2fcc2c3946fd2373bde2028db3bf6156b3d9
+AUX tiff-4.0.7-bug2610.patch 1408 SHA256 730cc8453d91cf82d5f7f245b0247c9436ecb2c21d2035f8e47d62618c7c3fec SHA512 c36f6feab9764985147b55e6f0a13f20fabc327090828f0dccbf948f770ccab0b1320da5a0cede978fe6e44167ed04d73d68db3df55dd5292eaaea245ce69c4c WHIRLPOOL 1f0563bb9eadfe1f30f9420497bd25fc899471a0fa4f8572c52d127f80d2a531d309c1cf6692193b6a05f785dd572b57a4c54a7ea3fc9b5951d57fbcdfa3f44e
+AUX tiff-4.0.7-bug2619.patch 1788 SHA256 08b3d76163636241de25609a2fd0fe763651417421fcf81f46b79e6f9632c42e SHA512 a1b8e93cd4738df92e011645780edc8cf726deb14b6ea89e08816edcd132869e2af7a87b29dc4ec8f047f23a977f71e368c48974ea5f47ac0bf0a46899d1ba3e WHIRLPOOL 811e87d1406b577a401c10fae42bb3ccb7323d332f68e6b79ede271a8fa543df0caa0458509df7f7c58aba750a1d3fb5623e84e8f11ca0c54bc07161fc443f29
+AUX tiff-4.0.7-bug2620.patch 999 SHA256 86bad0b58a62814b2f3d190004c706b0a4edd671df6edacca4242f1a68f2d1dd SHA512 11d1db473aa2c8d095efb98fce44d028761f9d160dad372f2dd9dd94ee7165cee7c38074cbe86ebfc89fac0da8d1ba6c111a90425451d9e29c991b18e3d63590 WHIRLPOOL a8455230f1e5fcd895b30f872aa5ac09f991493401d89a48c87414a0a5f8916c73e83947ca55f7cd7c4f251428f27b8f0f3a4b6f0c71bc1fbc2d558dfc9d6f7d
+AUX tiff-4.0.7-bug2621.patch 1607 SHA256 e65e24840bd34a23b385ba7e267349887d225a8d571b3b7bccecb1a6df0c15ab SHA512 abab14c816df59a353c25651832d14bfce1d4aad5041072e9ba15237db108fb8e81c6b6204c741e59ea1ed45f21da64331baa57064ff912939dbe240554ccf9b WHIRLPOOL a9fdd82095ea10b3a949bcd7a95f8865e2b407a2c72bd895b558c2a8d418f79cb2ec897392f770a68574fba6310f16d387422df4f0160b9fbf7db4f3041efcf1
+AUX tiff-4.0.7-bug2627.patch 1943 SHA256 cdaeb714552b03dc7ea555073814716fb1e26ad4c03df21a89b0d1fe95bb0562 SHA512 144e8fec21dec7c9c52c19908d7f33a46b700513d419784d665fbffe7ae587790b5ca10895295108e985c6897b9d9f61ebdcd3febd23ca069c55b358e96af0fc WHIRLPOOL 768e37a14ad3f4ababd395ab1c945c398eb0ea858fe6a6cf3350a06f3bbf007b56a6f5f9588a936d8b0afa50dd60632e91577bc2d5cad3398da2fd01d92a2b87
+AUX tiff-4.0.7-bug2631.patch 1157 SHA256 b6515fb191a07b676c5c38f809611673560c66730a8b637fe6418375c8a92052 SHA512 c55398609a38c231c084526e9875061e776823e953f0c805cb08c2d04bf9d4345c595eda43feb6d929c747cb5032a57c49be9d556bd85bc66ffb4b943fa17ad0 WHIRLPOOL d83179c7786e90ef4cd64127f507211d201fb79bad645375c716619a7c9ffddee7c87575af5c8ae98d758a4b2d6b83be5b3b16d52841321c204282b419ae0361
+AUX tiff-4.0.7-bug2633-bug2634.patch 1313 SHA256 8b0463f85d3572f7789d040e9f7fc6229073ddf9996d865316b4164bd59eb173 SHA512 b431a052fdc180545c08a5e0ac6e8fe09a84fe2339c00487f96da7ded6e4de496ae8f6a00f5e20e389afb9bcb91f2ec692e0e5442738679b49877f98c719067d WHIRLPOOL 6628ad8b471e77ab71ad2ab83e14d61fd9a695ecf8837bb83e88d2798cd6045da0553523e5a8c95dec6e477f26200bce7274e1337a366d379b63be8b564b98ff
+AUX tiff-4.0.7-bug2635.patch 1221 SHA256 d93cbf3e5609fd441019517313cb323d87fb726c80fb708a76bfe8a0599da479 SHA512 f820081833fa4d03c8d3dcaadef4bbe146b0d34fbca07682f3cdd14dce8b3d0e239524eb3dfdcca9984758b647239791bcd2d1171658370dc04c42a02548e175 WHIRLPOOL 33b39ae3e6301198e4173501c30f8ed9c47d863526490df1060187294c441baa5404fadf3ffbe99933044fde1f34bbdc20de5915f3d53acd29fb3616e644e7ef
+AUX tiff-4.0.7-bug2638.patch 980 SHA256 625069ce41f765d294b944fa047eeb9b1a9b28b68729667db715f419c6c26c9c SHA512 32bc9f7395fed8a3d2c007dd8d716b7ad35feecdec933704c65e3bf56ca2856217a2de46b10001975659b6c35d401a01b3c3465c31e4365507417afe9124921d WHIRLPOOL 7143fcddd445b85ec25615a12c5431b0a0f5f0ca2cf16a12fefbeed272234c28f8f42b416f5cf110118309f90c08e8b5eff5bd479cf3915ee33563b717826c8b
+AUX tiff-4.0.7-bug2639.patch 2354 SHA256 25e62415a86d1ac3641f7dee7a2d3962f77621bbcf4f318611ddc11e1df2eb10 SHA512 5cc200ff4ee63c06a4cff8ca0ecec9623815ad2acaaeb034b27030f2789a8ef071a6cef28974b6f821215a1b0b14a0543792adc4aae11797fa89f2a7c7bcb757 WHIRLPOOL 633e5286f43df469e03c4f50ed40de5fce7c37ea3cafdad637d8564ac91c4eb9bb2e91325202683d3a27041d2b493e6785edecd91e3fa81265efee271b435edb
+AUX tiff-4.0.7-bug2640.patch 988 SHA256 ddfa06afb9f5d5998bb198952bdd06de652a58c3654d48dc9eac805cce61af9b SHA512 b8b4afc23e28218d78f7b6f91dce1183c1615fa8994d288220dfb35a2e7ba700901f3b7aa6159d7f525ae42d7e9c94d6240b21b3875df139cf9c453e947ad70b WHIRLPOOL 50db4c258db9dc77953a3621c38eb121953287dc8be8f2cec1f58f2e55b85ccc9e31a02ba544d903dfba9b99ab16c3d4a98f3415f3a5c5579b34ae013f3aded8
+AUX tiff-4.0.7-bug2642-bug2643-bug2646-bug2647.patch 8547 SHA256 8524ad692ce512cd6b21dae509e0994cb5252073cff4fd749665a621291391d1 SHA512 0273effe1287d5f96eba994d81151e3eefce20e6d1d85bd21cb72a8d54d05bb82bcb60a97c778598856e7b7ed45d12b2e3fec52a6bac2aa2f52dd96365ec7b9a WHIRLPOOL 2c8e01f8e0f91093c9c606921ec4d4db926a6e1dccf204a68b780650de5ee3b7976eddd046f0e70fbf3916b1cf2119164bd9df0c5d90b01c27e2401f781b748f
+AUX tiff-4.0.7-bug2644.patch 1863 SHA256 80f61149983375fcf6fbedb4f90b6a5d254379217bed3860af1b1e686a61a73d SHA512 d50bb84b1842eda41ad8d7eead50ded6be928851d04f4ba71bc003cbf6d8a646539a457bab58d5327422e0b8a4ce53c1bd2cc79125924c21d64cc95ecd0307be WHIRLPOOL 7ccf2a979a98a31513bcfbd07374b4c502266eacbe85d2b6a36b6c7c26a7a196d190cfbc6b3fae3775d5962ab0df8ec2c4ac067d50afc9c6e7674f37ff0b7818
+AUX tiff-4.0.7-bug2648.patch 1200 SHA256 edf80470417a31299ba6b218b6e559b81f313cc43b326e1e4f789301b81e82fc SHA512 082695dadfc13b7efe1761b46b8a3144d151844a6bec620734251221c1d062b14c8c94a641abda9f822f1d7b8715a28bf5311e832d0c5b67679d3f4863463980 WHIRLPOOL c2e44d57761aff34ca94cc9620eba2b8ea6efb562477875e28356653613a651c09874197109154fdec086c6560d914cd6ce901fd79207273ed3b830f6e868ee4
+AUX tiff-4.0.7-bug2650-2.patch 811 SHA256 4ee2aa8c63dca66eebccc747835d91e36a468117233c1d162848e317e3c20d06 SHA512 600b72d84da2428d95da3f439ddee3805f9ac5b0f70a45681536f508847bdbb55b37292c33ea3a5e3d4c11f923554371f5417763c62d50ac5a7dc93a2520a537 WHIRLPOOL 95377f712047835d9dbedbb37f05e0c9999f9207de0b71e7d311d2758d3e4f7afa4eb87c7aae8b6d2fda04227339676f3a84952336d615d4ed65272549b1b131
+AUX tiff-4.0.7-bug2650.patch 1815 SHA256 d677602d992fd743212cb2e5afc9eb4ba15b6c49f3f79f3b4a0ebce4a95d4dc9 SHA512 35c32307adb65ccc64fa9d11662aff1ade1404b46892240bbf695311d89ec9a44f31fa9c8cb06e82aaa455a1afd4f85cb8b71ffa6a59a3ffbb83014df36a5fa7 WHIRLPOOL 4d9cf41de4b515bafeae8db193d03b55e4de25c60ee511b296f7384ba4d3708f7798dbe6aee952ff54ced75156fd5649490d2493c3671454b88134ce92496a17
+AUX tiff-4.0.7-bug2651.patch 2650 SHA256 954f174792dae1338a811f4bfba464b05eefd38d44931a5b57686682dc26e752 SHA512 ce26abcc52e762470e32317cda6884fa78d23f81099f88a1dfd24bcf61a8b69037bb867ecc6e53b6b9395eb399c0e7b78c78701fa63eece4f18258b31dc887c4 WHIRLPOOL 40b5403b18ae66cecc5076fe6d5980f3931f7716be810d8f49ac3f62d3f7cf2cf875fe38015f9c01ad91f360b0c889df7f2f1be978bcb08dd2c09c09c8221361
+AUX tiff-4.0.7-bug2653.patch 1211 SHA256 8ce494118e93e7e7f1e23d2badcd266bd576212328cea4526a00a73245f2b79f SHA512 0b069f3580b33aa0f8d569314f18bf4f493ebee4ab92378acbb098618f01304b70ac80dbb90bb8a700013e8444728ea7c7bc806b43aa7362c673a408a5b5937e WHIRLPOOL e1502cc8c3ee5d1f48d9469ac90e355ce0f7a7adc342f1918f6b5c4844f5e074be988de2cb3e8bf0449fabe4ac58159beaa0c9825629fd0f0a988a3d53b6685d
+AUX tiff-4.0.7-bug2658.patch 1177 SHA256 8154f80a01abf592391e54400d77853e0b12b2819a1fecb5313de47a17fa148c SHA512 2eeb99a4aea2257ae5fa7457d0d9dd4aa5f60a20451641fe149ba7044232b54d2a43686cdbde1f10891efbf9de3bd3036b8836dd23dcc30e0771c60a1ae71548 WHIRLPOOL c77941b48b68c108a20c6a2d06dd96b7f2c7774d4e4338e9cca5b1e6ec939e5dcef58fcedaeb3fe28c7184b875b4913c2da82caa874c0e413b92f7c750cd8005
+AUX tiff-4.0.7-bug2659-2.patch 1314 SHA256 4d71f38b73c87467cefe333d436ce003f617a8c8bfba7787f2a27f49ba27547d SHA512 88847700664d19319ca6789cce5716006591e1e467c43c6fd5bb15295b0a49382cd36b3681d8a3aa69f8fc624265c9c559cccb75b84d6055431f14feedb7f08c WHIRLPOOL cfaf35a392a300d82e0c421f8630dbe668887d1e391e3a48d3d8673388f5ce53e239f3d5cd8be77eab64082fb3b2d9215f2637981e513205c908ec0a3eefc2c7
+AUX tiff-4.0.7-bug2659.patch 1173 SHA256 5a781c94a6700f56c222d147a699f6ae5d13542660c981bb9c8667be0a6d686e SHA512 c43716c63c56fc5815a931e645ab8fe8405363c8364bd1b3b6e06abcca60d3c3497d7945553ade4e9b89f67d37a6a5017cef1601c6408db39a525279f7a03150 WHIRLPOOL e72d98a5bdb079f9b44441e62670a3c38153fa99497e8b59f828380a261d930eafedb48c9a0b204d2ffa12a0146dafd8560fbafc84735d3d0a7f8a908ce5f913
+AUX tiff-4.0.7-bug2665.patch 1634 SHA256 ccc07f5fafd86a5dd6f45fe90b8bc997c4d451b519e2dc01adf18d8e668a253a SHA512 36d56f8bbee3d62e90ca7cf294d0f14b624a95376c1a85a3b51b89176ccde4e1601e895fb85b5d56e06c7de89fee5e7d07d2bc7f6500165199e1715d477deb09 WHIRLPOOL d87ce573fa2760ce29ca10a8dd230f05cd5cfcddba26b94f7151315d168bdf1436ef1a84046b477be3d689736ba13ad99185729f3c72aaeb2452931e2c5bed81
+AUX tiff-4.0.7-fax2tiff.patch 1516 SHA256 36ad571cfd362052c25c1c206daf6abdb20109f4562cce56aee59789b2f2a788 SHA512 0b692df0ef4517b23e183bd4e521db6f97a9d34a59229f66d06ad02e1f021f2e9981f3c5d7c45b3a65fed5755190deb67cd49dd2332f71b1ea5adc07cbc4d4ea WHIRLPOOL f978c02fb25b2fde8dc715440c34635ded17845ecb58c1c34b90b54064be116910df369cdd2dc02ce23fb1a98eaf503aa37eeef3e77ff640cf4cf09188bee1f6
+AUX tiff-4.0.7-hylafax-hack.patch 1626 SHA256 41439abd34c29b0d021c490a2b2e567eed30e46d4e8bdf99cb2a5e8ab3fb02ef SHA512 f6d6da8e60b94560fa3a47b3779160893317817b56ea216482da65113249724b5d9023eb50d30660a2fc2e49b2f9147ce1dc12b198173c5ffc3f44e95308d843 WHIRLPOOL 558d196da71545bdf2030475b03b8764e779873dc6dd7306a5bfada042a80784991ad2201a3e45783f3d2f655c6f7b667b478a960a9ee97c6e014eba00d10d78
+AUX tiff-4.0.7-pdfium-0005-Leak-TIFFFetchStripThing.patch 613 SHA256 6dde45c9e55687c6067666edb58a372272fffd75e96df88477c22e28831b49e1 SHA512 d29d1eefece677e5802c649a41f96af4dbd6c39e487327dd123b905e7fdfb8982b5e82eae365e5df11832c1682008805a5a7cf572577c5483ec530d881024b3b WHIRLPOOL 1022b81b68b1ad744e9a4177949550df514dff2d2420635557f06abc4ca31425f4b5fd10fdbefef278fbe54f953b9da5e338dc3d48b49d12155011b4baf92785
+AUX tiff-4.0.7-pdfium-0006-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch 930 SHA256 72bce2bf5995769bc2c1762654c761a4e908f36ee367fc08d7501a31197afad5 SHA512 822ccc22304d23b4f50efe1f0fb7890fda8b9edf2075b8add814c0bac0a153fa0ed5d7ee5f47e23799b24f11678c8e0bf9c90ad59c5cc61fd9546b2dca4714a9 WHIRLPOOL ddd3c9d98dff75d14446cb94ea3d9b93030f299975849b8fd1740431974d489e17141e30dc2bc74555dadee95d73cb453696de521a8e49177ebf887f2391e9ea
+AUX tiff-4.0.7-pdfium-0007-uninitialized-value.patch 802 SHA256 0c97c3fd78482d276ebfb8d241e9da5d2169cb43f1d5dece50ccd889bbccfdb2 SHA512 198907ad009d5faf2dd079d1a4e7355d439828a23643158531b15821be0cb3fc9b93da1ccf8baae8f4029b08ae2a55a94c7ef97ea5af6c48a9e013822a5f763d WHIRLPOOL b420eaa2e0eb6c92957e3260c279f11928ac64ebbc41200f6424c3e1b88087d8594ab575998b1a2131b052fcb44988510525d2d22b9d2cbe024801644e8ee323
+AUX tiff-4.0.7-pdfium-0008-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch 924 SHA256 23f2e5983edb0292d37df2529a1cea71afa126d322304d7131b3ae63c7ff48f1 SHA512 43973cc5512eb8fe2e0beab1a98ff0d3bd999ed1adbf7c08c344b3442559cc2cdc99a9f2e9c1bb2bffa4be027945b12b120fa27ec6c8006367932039b8d8dd37 WHIRLPOOL ee5e5ce56a5919df25c3fc74b86f44225559c398e386dae4f6b7e8496561ab63b2282e64cdcb136c37ce068c70a8107bfbb9635116352f04fca424266b307dd6
+AUX tiff-4.0.7-pdfium-0013-validate-refblackwhite.patch 1278 SHA256 691f38a8f3bb627981bc80c32dc1b3d6009c25f97d56edd05f3b20748b908c34 SHA512 b99e4147a3a15c7d612c9884df1677e64cdf0cc0f05b36fded9bf1a187e2aa936a921b78adfba790061c4c06e45d78138ea8a3d7192c4cc23b61d12bd1055912 WHIRLPOOL 07062e5b7495a48b94dee7795671af93ddd2d2644517eada2549e067d0b35b237c3e7821c793634d4be9253b7423a9f25aed4140bc6c0e9957302c8975bee6dd
+AUX tiff-4.0.7-pdfium-0017-safe_skews_in_gtTileContig.patch 2992 SHA256 778b29075480088e54436edd2cbc0c80124e266e12bdbe04e377b37292cd6eec SHA512 0fc1b6b8a57629730b10c0c30d915ce8a9575cac5e1daa91ae74be4e866e4c9cf49031897c001b3ade8182274d875988d40c1d4214b2a427d4676762ca7f2c4c WHIRLPOOL f64fb96876fadb5f9bf25bb0391dd194d3b4b771405637d54f27b9f35dbf8f3b138d020e38f539dc3b1ad734af911c5e5022687888bd4c682ccb59b71a9f16f2
+AUX tiff-4.0.7-pdfium-0018-fix-leak-in-PredictorSetupDecode.patch 849 SHA256 fb44bb590ffd32e534790039f96e4247c36e09a1ca0dbb4e524b135963476d21 SHA512 9da30e7223522dfb4d8a8bc8b5bd545615cfe60a509f8583d29817ecbb1ff28ca38a6e00ee845e9484d9bc02666f99b0144ea08e5083eef2035e99b1825f4bd3 WHIRLPOOL 37b314b8d4124151ccbee6584bddfadedc79627726325911291721f06837996bed915250a70ddff40a37570e644cbefbee35f97dc8396ed8e7fd6dc983d911bc
+AUX tiff-4.0.7-pdfium-0021-oom-TIFFFillStrip.patch 1228 SHA256 d422933c693c839b39faa2fcd27da9c44ab501134cd1c876f1233757df2b10ff SHA512 4add933c6b7e2938affb03e00da0bb28789cd9998f5496f4b592ae14d35175f6ce8a4e83ee639ef42211a8683bddad5b4c8375a1ba0a331bc72a40c45e691162 WHIRLPOOL d5cd7a908f699b15b1bab0fbcb1678fa692143a350aa6df4bb62e16572afb05516116b36e248fb59f5517ce88f03340c93a58c4f81c9137be55c5817bf61f111
+DIST tiff-3.9.7.tar.gz 1468097 SHA256 f5d64dd4ce61c55f5e9f6dc3920fbe5a41e02c2e607da7117a35eb5c320cef6a SHA512 ca89584a9ffa33b4986e4bc2165043cec239896f1f0ab73db00818d0442b570efaa6345b2ed422e884202324d359713df849bf14782bb0cf3b959655febddd77 WHIRLPOOL c06b35da66c365c1fe7f0e6e06a400e139d3e2b5b280aa764015c2f0383a6191ffb3d335cdf2211b687bbb0caacf641be409148986a9813dfde5822a650a9b1c
+DIST tiff-4.0.7.tar.gz 2076392 SHA256 9f43a2cfb9589e5cecaa66e16bf87f814c945f22df7ba600d63aac4632c4f019 SHA512 941357bdd5f947cdca41a1d31ae14b3fadc174ae5dce7b7981dbe58f61995f575ac2e97a7cc4fcc435184012017bec0920278263490464644f2cdfad9a6c5ddc WHIRLPOOL 3090a0d8a5ad3595c97888edab3c48379175cad993567d20be5f397b1c5c1d21012de55c5da5e664ee483d294fe9eb5f3464e14f564fb79c1357094ff67e313d
+DIST tiff-4.0.8.tar.gz 2065574 SHA256 59d7a5a8ccd92059913f246877db95a2918e6c04fb9d43fd74e5c3390dac2910 SHA512 5d010ec4ce37aca733f7ab7db9f432987b0cd21664bd9d99452a146833c40f0d1e7309d1870b0395e947964134d5cfeb1366181e761fe353ad585803ff3d6be6 WHIRLPOOL 13fce447c586fef080c6201f0f5b010bc8b0e096bc9b806ab3b80eb6a672c789f88b5fc34a51585aa7072bb8407ecc958d1d7824fad379f86968f051de2fa96d
+EBUILD tiff-3.9.7-r1.ebuild 2084 SHA256 f91faee9c25897294062a3c4c7216b04ff10c2f8dbe8af17304920907d513b45 SHA512 9204dbe59144189fc74619eb9cc87e3cc13bdefb00ff569ab1b7986e8f0112a94283febbceba27ad61cf265977c7cffd653844a86ff59b23446a0fff4740677e WHIRLPOOL eaac4889c4c62dd04f07f15d3f0324287cdbc412d1d33a59cd7011ec982178fb6e669a518505d2cfafe354a870caf4d6240b09a4189c3f808e6b85cf662a3fee
+EBUILD tiff-4.0.7-r3.ebuild 3984 SHA256 3819e6fee147b8921924907bb02fe7fab8d26bde6d75f80c61d7a1778600b3f7 SHA512 914b7b47017c321679df09960acc4aa8baa76a59a85dcdd8eda90ac026f41da5f4d8cee3cc93f330b815fbef76a8364e4b7a1759d68eb026bbb9bab53ace281c WHIRLPOOL 6e126b9fb491b549038074633afb04588248313d767a194f2312a2802019a386dee5630bf77805d2076267c24cfb74013125aa0b3b7662bde0e66288c5de348f
+EBUILD tiff-4.0.7.ebuild 2008 SHA256 7cffa060f0e71b14c2567e04f1063d0cad813b922812d6184e9a61087660c672 SHA512 120485820216c3e2a1eeb5cdb168edb73cbf8e56ca52a1830ba5f305e8d3c2ff25c343b5cdbe492c7ae46f5ba71e83408f0c84a7f4acf1bbd1cc67bb3a59dd0c WHIRLPOOL a8c37e06ca2dc89807c2aba210b17282d1d1dfe268ae603fc9f8c4c2339f394b4f88e7acba3c76e36c39848699d9bd6ea2329243847f19b31ce5316e4a77a6c4
+EBUILD tiff-4.0.8.ebuild 2631 SHA256 ebfe0ae7422e5f3ecddd55f71e48c39f7d3a703cab46d5e87f1ea2322a77c8ea SHA512 0f751cb980e6375726e3d1cf3747c2d36e48f2a6321972d82f54a7732fc92a8d671f673d5b78ed865c3a99f1192890178c6a8891863dce5cbed04b9309ee09b0 WHIRLPOOL 273af4fefa206144023c9a24bd2a7de0e598d5956d20d26761d38473eacc572a9cfe01072c979f377ffc9aa2ea6f2892cda023300f71256ccf9cea1f3938dd1c
+MISC ChangeLog 7141 SHA256 af3ea485ff770a8e47745811a8d9956e6a2e1ecf3331d2fd250cf1184ec0b344 SHA512 e729de79059b9e40c78ae4d888c1c918f49103d4703deba418c7256178a906fe01a3fb39baa4979d9c86794e395899b1892dda5f27070b47d10897bc70c41e38 WHIRLPOOL c89a85615767e526cd6fe414be8fd13a5925c51fe54dde96d1a731e011a6f44b6f61f056b3b1098bf4186055a8ebd83d9775e357fb4996ad47d89bdfc8291b89
+MISC ChangeLog-2015 37881 SHA256 76a38680402174816a0d1f744702de8b9d788bdd7bdc91386769521833446e49 SHA512 e0fc407d273a8e6282de4d9466d9ee3ec19490f72b70ed939a7d8a4d5a4a0534d754473a2a90ce0917024ba7b7c5c4d0b393576116b62841d2796d786183390d WHIRLPOOL c287bc4ba38bb5485c1b2bd6a9080682b9155edebf7d7b42121b2653a04e43060a8f2576b99c4e378d401329eb4d7620174faa94ccc32f3dfe76f9e72d6ab1c3
+MISC metadata.xml 550 SHA256 1410eefa2efb14159708ab807679ea0ee37fad70104fd5f6bf09a42cb84dda66 SHA512 f57f42dda3a4300ad235f36115f9b431f8328308ced2bb2522f0d0031630abecab970ba4648630fc138d31d6d7c624b9028fdbabc97f2b1f9529457b2ca7f63b WHIRLPOOL 5ed169b8ba3f7539eee60b43e8185c374f962a232f8b5e28e40e396b9aec7b3436c85fde2dd38f225484dfc1585d4052fcb66c2c13abc77c45c16b7f46e9c87b
diff --git a/media-libs/tiff/files/tiff-3.9.7-CVE-2012-4447.patch b/media-libs/tiff/files/tiff-3.9.7-CVE-2012-4447.patch
new file mode 100644
index 000000000000..6c28dc6ec9a8
--- /dev/null
+++ b/media-libs/tiff/files/tiff-3.9.7-CVE-2012-4447.patch
@@ -0,0 +1,191 @@
+Upstream patch for CVE-2012-4447. This also covers an out-of-bounds-read
+possibility in the same file, which wasn't given a separate CVE.
+
+
+diff -Naur tiff-3.9.4.orig/libtiff/tif_pixarlog.c tiff-3.9.4/libtiff/tif_pixarlog.c
+--- tiff-3.9.4.orig/libtiff/tif_pixarlog.c 2010-06-08 14:50:42.000000000 -0400
++++ tiff-3.9.4/libtiff/tif_pixarlog.c 2012-12-10 15:50:14.421538317 -0500
+@@ -117,9 +117,9 @@
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+- t0 = ToLinearF[cr = wp[0]];
+- t1 = ToLinearF[cg = wp[1]];
+- t2 = ToLinearF[cb = wp[2]];
++ 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;
+@@ -136,10 +136,10 @@
+ op[2] = t2;
+ }
+ } else if (stride == 4) {
+- t0 = ToLinearF[cr = wp[0]];
+- t1 = ToLinearF[cg = wp[1]];
+- t2 = ToLinearF[cb = wp[2]];
+- t3 = ToLinearF[ca = wp[3]];
++ 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;
+@@ -183,9 +183,9 @@
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+- t0 = ToLinearF[cr = wp[0]] * SCALE12;
+- t1 = ToLinearF[cg = wp[1]] * SCALE12;
+- t2 = ToLinearF[cb = wp[2]] * SCALE12;
++ 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);
+@@ -202,10 +202,10 @@
+ op[2] = CLAMP12(t2);
+ }
+ } else if (stride == 4) {
+- t0 = ToLinearF[cr = wp[0]] * SCALE12;
+- t1 = ToLinearF[cg = wp[1]] * SCALE12;
+- t2 = ToLinearF[cb = wp[2]] * SCALE12;
+- t3 = ToLinearF[ca = wp[3]] * SCALE12;
++ 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);
+@@ -247,9 +247,9 @@
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+- op[0] = ToLinear16[cr = wp[0]];
+- op[1] = ToLinear16[cg = wp[1]];
+- op[2] = ToLinear16[cb = wp[2]];
++ 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;
+@@ -260,10 +260,10 @@
+ op[2] = ToLinear16[(cb += wp[2]) & mask];
+ }
+ } else if (stride == 4) {
+- op[0] = ToLinear16[cr = wp[0]];
+- op[1] = ToLinear16[cg = wp[1]];
+- op[2] = ToLinear16[cb = wp[2]];
+- op[3] = ToLinear16[ca = wp[3]];
++ 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;
+@@ -342,9 +342,9 @@
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+- op[0] = ToLinear8[cr = wp[0]];
+- op[1] = ToLinear8[cg = wp[1]];
+- op[2] = ToLinear8[cb = wp[2]];
++ 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;
+@@ -355,10 +355,10 @@
+ op[2] = ToLinear8[(cb += wp[2]) & mask];
+ }
+ } else if (stride == 4) {
+- op[0] = ToLinear8[cr = wp[0]];
+- op[1] = ToLinear8[cg = wp[1]];
+- op[2] = ToLinear8[cb = wp[2]];
+- op[3] = ToLinear8[ca = wp[3]];
++ 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;
+@@ -393,9 +393,9 @@
+ mask = CODE_MASK;
+ if (stride == 3) {
+ op[0] = 0;
+- t1 = ToLinear8[cb = wp[2]];
+- t2 = ToLinear8[cg = wp[1]];
+- t3 = ToLinear8[cr = wp[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;
+@@ -413,10 +413,10 @@
+ op[3] = t3;
+ }
+ } else if (stride == 4) {
+- t0 = ToLinear8[ca = wp[3]];
+- t1 = ToLinear8[cb = wp[2]];
+- t2 = ToLinear8[cg = wp[1]];
+- t3 = ToLinear8[cr = wp[0]];
++ 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;
+@@ -630,10 +630,10 @@
+ return guess;
+ }
+
+-static uint32
+-multiply(size_t m1, size_t m2)
++static tsize_t
++multiply(tsize_t m1, tsize_t m2)
+ {
+- uint32 bytes = m1 * m2;
++ tsize_t bytes = m1 * m2;
+
+ if (m1 && bytes / m1 != m2)
+ bytes = 0;
+@@ -641,6 +641,20 @@
+ return bytes;
+ }
+
++static tsize_t
++add_ms(tsize_t m1, tsize_t m2)
++{
++ tsize_t bytes = m1 + m2;
++
++ /* if either input is zero, assume overflow already occurred */
++ if (m1 == 0 || m2 == 0)
++ bytes = 0;
++ else if (bytes <= m1 || bytes <= m2)
++ bytes = 0;
++
++ return bytes;
++}
++
+ static int
+ PixarLogSetupDecode(TIFF* tif)
+ {
+@@ -661,6 +675,8 @@
+ td->td_samplesperpixel : 1);
+ tbuf_size = multiply(multiply(multiply(sp->stride, td->td_imagewidth),
+ td->td_rowsperstrip), 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);
+ sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
diff --git a/media-libs/tiff/files/tiff-3.9.7-CVE-2012-4564.patch b/media-libs/tiff/files/tiff-3.9.7-CVE-2012-4564.patch
new file mode 100644
index 000000000000..98a6e6c4409d
--- /dev/null
+++ b/media-libs/tiff/files/tiff-3.9.7-CVE-2012-4564.patch
@@ -0,0 +1,77 @@
+Upstream patch for CVE-2012-4564.
+
+
+diff -Naur tiff-3.9.4.orig/tools/ppm2tiff.c tiff-3.9.4/tools/ppm2tiff.c
+--- tiff-3.9.4.orig/tools/ppm2tiff.c 2010-06-08 14:50:44.000000000 -0400
++++ tiff-3.9.4/tools/ppm2tiff.c 2012-12-10 16:16:05.154045877 -0500
+@@ -68,6 +68,17 @@
+ exit(-2);
+ }
+
++static tsize_t
++multiply_ms(tsize_t m1, tsize_t m2)
++{
++ tsize_t bytes = m1 * m2;
++
++ if (m1 && bytes / m1 != m2)
++ bytes = 0;
++
++ return bytes;
++}
++
+ int
+ main(int argc, char* argv[])
+ {
+@@ -85,6 +96,7 @@
+ int c;
+ extern int optind;
+ extern char* optarg;
++ tsize_t scanline_size;
+
+ if (argc < 2) {
+ fprintf(stderr, "%s: Too few arguments\n", argv[0]);
+@@ -217,7 +229,8 @@
+ }
+ switch (bpp) {
+ case 1:
+- linebytes = (spp * w + (8 - 1)) / 8;
++ /* 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 {
+@@ -226,15 +239,31 @@
+ }
+ break;
+ case 8:
+- linebytes = spp * w;
++ linebytes = multiply_ms(spp, w);
+ TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
+ TIFFDefaultStripSize(out, rowsperstrip));
+ break;
+ }
+- if (TIFFScanlineSize(out) > linebytes)
++ 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(TIFFScanlineSize(out));
++ 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);
diff --git a/media-libs/tiff/files/tiff-3.9.7-CVE-2012-5581.patch b/media-libs/tiff/files/tiff-3.9.7-CVE-2012-5581.patch
new file mode 100644
index 000000000000..a6bdca137029
--- /dev/null
+++ b/media-libs/tiff/files/tiff-3.9.7-CVE-2012-5581.patch
@@ -0,0 +1,245 @@
+Fix unsafe handling of DotRange and related tags. Back-port of upstream
+patch for CVE-2012-5581. (Note: I have not pushed this into upstream CVS
+for the 3.9 branch, because I'm not entirely convinced that it won't create
+application compatibility issues --- tgl)
+
+
+diff -Naur tiff-3.9.7.orig/libtiff/tif_dir.c tiff-3.9.7/libtiff/tif_dir.c
+--- tiff-3.9.7.orig/libtiff/tif_dir.c 2012-09-22 10:48:09.000000000 -0400
++++ tiff-3.9.7/libtiff/tif_dir.c 2012-12-13 13:39:20.448864070 -0500
+@@ -494,32 +494,28 @@
+ goto end;
+ }
+
+- if ((fip->field_passcount
++ 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 v[2];
++ v[0] = (uint16)va_arg(ap, int);
++ v[1] = (uint16)va_arg(ap, int);
++ _TIFFmemcpy(tv->value, v, 4);
++ }
++ else if (fip->field_passcount
+ || fip->field_writecount == TIFF_VARIABLE
+ || fip->field_writecount == TIFF_VARIABLE2
+ || fip->field_writecount == TIFF_SPP
+- || tv->count > 1)
+- && fip->field_tag != TIFFTAG_PAGENUMBER
+- && fip->field_tag != TIFFTAG_HALFTONEHINTS
+- && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+- && fip->field_tag != TIFFTAG_DOTRANGE
+- && fip->field_tag != TIFFTAG_WHITELEVEL) {
++ || tv->count > 1) {
+ _TIFFmemcpy(tv->value, va_arg(ap, void *),
+ tv->count * tv_size);
+ } else {
+- /*
+- * XXX: The following loop required to handle
+- * TIFFTAG_PAGENUMBER, TIFFTAG_HALFTONEHINTS,
+- * TIFFTAG_YCBCRSUBSAMPLING and TIFFTAG_DOTRANGE tags.
+- * These tags are actually arrays and should be passed as
+- * array pointers to TIFFSetField() function, but actually
+- * passed as a list of separate values. This behaviour
+- * must be changed in the future!
+- */
+- int i;
+ char *val = (char *)tv->value;
+
+- for (i = 0; i < tv->count; i++, val += tv_size) {
++ assert( tv->count == 1 );
+ switch (fip->field_type) {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+@@ -578,7 +574,6 @@
+ status = 0;
+ break;
+ }
+- }
+ }
+ }
+ }
+@@ -869,24 +864,27 @@
+ *va_arg(ap, uint16*) = (uint16)tv->count;
+ *va_arg(ap, void **) = tv->value;
+ ret_val = 1;
+- } else {
+- if ((fip->field_type == TIFF_ASCII
++ } 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)
+- && fip->field_tag != TIFFTAG_PAGENUMBER
+- && fip->field_tag != TIFFTAG_HALFTONEHINTS
+- && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+- && fip->field_tag != TIFFTAG_DOTRANGE) {
++ || tv->count > 1) {
+ *va_arg(ap, void **) = tv->value;
+ ret_val = 1;
+ } else {
+- int j;
+ char *val = (char *)tv->value;
+
+- for (j = 0; j < tv->count;
+- j++, val += _TIFFDataSize(tv->info->field_type)) {
++ assert( tv->count == 1 );
+ switch (fip->field_type) {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+@@ -936,7 +934,6 @@
+ ret_val = 0;
+ break;
+ }
+- }
+ }
+ }
+ break;
+diff -Naur tiff-3.9.7.orig/libtiff/tif_print.c tiff-3.9.7/libtiff/tif_print.c
+--- tiff-3.9.7.orig/libtiff/tif_print.c 2010-07-08 12:17:59.000000000 -0400
++++ tiff-3.9.7/libtiff/tif_print.c 2012-12-13 13:42:12.773478278 -0500
+@@ -112,16 +112,22 @@
+ }
+
+ static int
+-_TIFFPrettyPrintField(TIFF* tif, FILE* fd, ttag_t tag,
++_TIFFPrettyPrintField(TIFF* tif, const TIFFFieldInfo *fip, FILE* fd, ttag_t tag,
+ uint32 value_count, void *raw_data)
+ {
+ TIFFDirectory *td = &tif->tif_dir;
+
++ /* do not try to pretty print auto-defined fields */
++ if (strncmp(fip->field_name,"Tag ", 4) == 0) {
++ return 0;
++ }
++
+ switch (tag)
+ {
+ case TIFFTAG_INKSET:
+- fprintf(fd, " Ink Set: ");
+- switch (*((uint16*)raw_data)) {
++ 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;
+@@ -130,11 +136,18 @@
+ *((uint16*)raw_data),
+ *((uint16*)raw_data));
+ break;
++ }
++ return 1;
+ }
+- return 1;
++ return 0;
++
+ case TIFFTAG_WHITEPOINT:
+- fprintf(fd, " White Point: %g-%g\n",
+- ((float *)raw_data)[0], ((float *)raw_data)[1]); return 1;
++ 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_REFERENCEBLACKWHITE:
+ {
+ uint16 i;
+@@ -174,10 +187,13 @@
+ (unsigned long) value_count);
+ return 1;
+ case TIFFTAG_STONITS:
+- fprintf(fd,
+- " Sample to Nits conversion factor: %.4e\n",
+- *((double*)raw_data));
+- return 1;
++ 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;
+@@ -524,44 +540,28 @@
+ value_count = td->td_samplesperpixel;
+ else
+ value_count = fip->field_readcount;
+- if ((fip->field_type == TIFF_ASCII
++ 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)
+- && fip->field_tag != TIFFTAG_PAGENUMBER
+- && fip->field_tag != TIFFTAG_HALFTONEHINTS
+- && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+- && fip->field_tag != TIFFTAG_DOTRANGE) {
++ || value_count > 1) {
+ if(TIFFGetField(tif, tag, &raw_data) != 1)
+ continue;
+- } else if (fip->field_tag != TIFFTAG_PAGENUMBER
+- && fip->field_tag != TIFFTAG_HALFTONEHINTS
+- && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+- && fip->field_tag != TIFFTAG_DOTRANGE) {
+- raw_data = _TIFFmalloc(
+- _TIFFDataSize(fip->field_type)
+- * value_count);
+- mem_alloc = 1;
+- if(TIFFGetField(tif, tag, raw_data) != 1) {
+- _TIFFfree(raw_data);
+- continue;
+- }
+ } else {
+- /*
+- * XXX: Should be fixed and removed, see the
+- * notes related to TIFFTAG_PAGENUMBER,
+- * TIFFTAG_HALFTONEHINTS,
+- * TIFFTAG_YCBCRSUBSAMPLING and
+- * TIFFTAG_DOTRANGE tags in tif_dir.c. */
+- char *tmp;
+ raw_data = _TIFFmalloc(
+ _TIFFDataSize(fip->field_type)
+ * value_count);
+- tmp = raw_data;
+ mem_alloc = 1;
+- if(TIFFGetField(tif, tag, tmp,
+- tmp + _TIFFDataSize(fip->field_type)) != 1) {
++ if(TIFFGetField(tif, tag, raw_data) != 1) {
+ _TIFFfree(raw_data);
+ continue;
+ }
+@@ -574,7 +574,7 @@
+ * _TIFFPrettyPrintField() fall down and print it as any other
+ * tag.
+ */
+- if (_TIFFPrettyPrintField(tif, fd, tag, value_count, raw_data)) {
++ if (_TIFFPrettyPrintField(tif, fip, fd, tag, value_count, raw_data)) {
+ if(mem_alloc)
+ _TIFFfree(raw_data);
+ continue;
diff --git a/media-libs/tiff/files/tiff-3.9.7-printdir-width.patch b/media-libs/tiff/files/tiff-3.9.7-printdir-width.patch
new file mode 100644
index 000000000000..6ad7534ac6fe
--- /dev/null
+++ b/media-libs/tiff/files/tiff-3.9.7-printdir-width.patch
@@ -0,0 +1,36 @@
+Make TIFFPrintDirectory cope with both TIFF_VARIABLE and TIFF_VARIABLE2
+conventions for field_passcount fields, ie, either 16- or 32-bit counts.
+This patch is taken from upstream commits dated 2012-05-23 ("fix crash
+with odd 16bit count types for some custom fields") and 2012-12-12 ("Fix
+TIFF_VARIABLE/TIFF_VARIABLE2 confusion in TIFFPrintDirectory").
+
+This doesn't qualify as a security issue in itself, mainly because
+TIFFPrintDirectory is unlikely to be used in any security-exposed
+scenarios; but we need to fix it so that our test case for CVE-2012-5581
+works on all platforms.
+
+
+diff -Naur tiff-3.9.4.orig/libtiff/tif_print.c tiff-3.9.4/libtiff/tif_print.c
+--- tiff-3.9.4.orig/libtiff/tif_print.c 2010-06-08 14:50:42.000000000 -0400
++++ tiff-3.9.4/libtiff/tif_print.c 2012-12-13 12:17:33.726765771 -0500
+@@ -518,8 +518,19 @@
+ continue;
+
+ if(fip->field_passcount) {
+- if(TIFFGetField(tif, tag, &value_count, &raw_data) != 1)
++ 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)
diff --git a/media-libs/tiff/files/tiff-3.9.7-tiffinfo-exif.patch b/media-libs/tiff/files/tiff-3.9.7-tiffinfo-exif.patch
new file mode 100644
index 000000000000..a326e21e298b
--- /dev/null
+++ b/media-libs/tiff/files/tiff-3.9.7-tiffinfo-exif.patch
@@ -0,0 +1,59 @@
+Teach "tiffinfo -D" to not try to print image data inside an EXIF subdirectory,
+because there isn't any. Back-patched from an upstream 4.0.2 fix.
+
+This is not a security issue in itself (it crashes, but with a simple NULL
+pointer dereference). However, our test case for CVE-2012-5581 tickles this
+bug, so it seems easier to fix this than make a new test case.
+
+
+diff -Naur tiff-3.9.4.orig/tools/tiffinfo.c tiff-3.9.4/tools/tiffinfo.c
+--- tiff-3.9.4.orig/tools/tiffinfo.c 2010-06-08 14:50:44.000000000 -0400
++++ tiff-3.9.4/tools/tiffinfo.c 2012-12-11 16:33:17.062228558 -0500
+@@ -49,7 +49,7 @@
+ int stoponerr = 1; /* stop on first read error */
+
+ static void usage(void);
+-static void tiffinfo(TIFF*, uint16, long);
++static void tiffinfo(TIFF*, uint16, long, int);
+
+ int
+ main(int argc, char* argv[])
+@@ -124,19 +124,20 @@
+ if (tif != NULL) {
+ if (dirnum != -1) {
+ if (TIFFSetDirectory(tif, (tdir_t) dirnum))
+- tiffinfo(tif, order, flags);
++ tiffinfo(tif, order, flags, 1);
+ } else if (diroff != 0) {
+ if (TIFFSetSubDirectory(tif, diroff))
+- tiffinfo(tif, order, flags);
++ tiffinfo(tif, order, flags, 1);
+ } else {
+ do {
+ uint32 offset;
+
+- tiffinfo(tif, order, flags);
++ tiffinfo(tif, order, flags, 1);
+ if (TIFFGetField(tif, TIFFTAG_EXIFIFD,
+ &offset)) {
+- if (TIFFReadEXIFDirectory(tif, offset))
+- tiffinfo(tif, order, flags);
++ if (TIFFReadEXIFDirectory(tif, offset)) {
++ tiffinfo(tif, order, flags, 0);
++ }
+ }
+ } while (TIFFReadDirectory(tif));
+ }
+@@ -426,10 +427,10 @@
+ }
+
+ static void
+-tiffinfo(TIFF* tif, uint16 order, long flags)
++tiffinfo(TIFF* tif, uint16 order, long flags, int is_image)
+ {
+ TIFFPrintDirectory(tif, stdout, flags);
+- if (!readdata)
++ if (!readdata || !is_image)
+ return;
+ if (rawdata) {
+ if (order) {
diff --git a/media-libs/tiff/files/tiff-4.0.7-CVE-2016-10266.patch b/media-libs/tiff/files/tiff-4.0.7-CVE-2016-10266.patch
new file mode 100644
index 000000000000..67e0ca41c995
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-CVE-2016-10266.patch
@@ -0,0 +1,46 @@
+http://bugzilla.maptools.org/show_bug.cgi?id=2596
+
+From d7520d28685b96a28421ef01fb66cea8d1a96dfc Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Fri, 2 Dec 2016 21:56:56 +0000
+Subject: [PATCH] * libtiff/tif_read.c, libtiff/tiffiop.h: fix uint32 overflow
+ in TIFFReadEncodedStrip() that caused an integer division by zero. Reported
+ by Agostino Sarubbo. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2596
+
+---
+ ChangeLog | 7 +++++++
+ libtiff/tif_read.c | 4 ++--
+ libtiff/tiffiop.h | 6 +++++-
+ 3 files changed, 14 insertions(+), 3 deletions(-)
+
+diff --git a/libtiff/tif_read.c b/libtiff/tif_read.c
+index 80035929f033..29a311db0cb7 100644
+--- a/libtiff/tif_read.c
++++ b/libtiff/tif_read.c
+@@ -346,7 +346,7 @@ TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
+ rowsperstrip=td->td_rowsperstrip;
+ if (rowsperstrip>td->td_imagelength)
+ rowsperstrip=td->td_imagelength;
+- stripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip);
++ stripsperplane= TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
+ stripinplane=(strip%stripsperplane);
+ plane=(uint16)(strip/stripsperplane);
+ rows=td->td_imagelength-stripinplane*rowsperstrip;
+diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
+index 8bcd0c172c08..5294ee78ffaf 100644
+--- a/libtiff/tiffiop.h
++++ b/libtiff/tiffiop.h
+@@ -250,6 +250,10 @@ struct tiff {
+ #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)))
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-CVE-2016-10267.patch b/media-libs/tiff/files/tiff-4.0.7-CVE-2016-10267.patch
new file mode 100644
index 000000000000..04d9729ff731
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-CVE-2016-10267.patch
@@ -0,0 +1,53 @@
+http://bugzilla.maptools.org/show_bug.cgi?id=2611
+
+From bd06f6c97dff0b30de0f80227d782ea448c14b19 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 11:15:18 +0000
+Subject: [PATCH] * libtiff/tif_ojpeg.c: make OJPEGDecode() early exit in case
+ of failure in OJPEGPreDecode(). This will avoid a divide by zero, and
+ potential other issues. Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2611
+
+---
+ ChangeLog | 7 +++++++
+ libtiff/tif_ojpeg.c | 10 +++++++++-
+ 2 files changed, 16 insertions(+), 1 deletion(-)
+
+diff --git a/libtiff/tif_ojpeg.c b/libtiff/tif_ojpeg.c
+index 30a1812634e0..93839d8f3e11 100644
+--- a/libtiff/tif_ojpeg.c
++++ b/libtiff/tif_ojpeg.c
+@@ -244,6 +244,7 @@ typedef enum {
+
+ typedef struct {
+ TIFF* tif;
++ int decoder_ok;
+ #ifndef LIBJPEG_ENCAP_EXTERNAL
+ JMP_BUF exit_jmpbuf;
+ #endif
+@@ -722,6 +723,7 @@ OJPEGPreDecode(TIFF* tif, uint16 s)
+ }
+ sp->write_curstrile++;
+ }
++ sp->decoder_ok = 1;
+ return(1);
+ }
+
+@@ -784,8 +786,14 @@ OJPEGPreDecodeSkipScanlines(TIFF* tif)
+ 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)
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-CVE-2017-5225.patch b/media-libs/tiff/files/tiff-4.0.7-CVE-2017-5225.patch
new file mode 100644
index 000000000000..7f961474ba9c
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-CVE-2017-5225.patch
@@ -0,0 +1,74 @@
+https://bugs.gentoo.org/610330
+
+From 24bc05876f5a1a300a3c4eb0fa8e8cea6a256f9f Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 19:25:44 +0000
+Subject: [PATCH] * tools/tiffcp.c: error out cleanly in cpContig2SeparateByRow
+ and cpSeparate2ContigByRow if BitsPerSample != 8 to avoid heap based
+ overflow. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2656 and
+ http://bugzilla.maptools.org/show_bug.cgi?id=2657
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffcp.c | 26 +++++++++++++++++++++++---
+ 2 files changed, 30 insertions(+), 3 deletions(-)
+
+diff --git a/tools/tiffcp.c b/tools/tiffcp.c
+index 49c9d37125a6..489459a7f6a4 100644
+--- a/tools/tiffcp.c
++++ b/tools/tiffcp.c
+@@ -591,7 +591,7 @@ static copyFunc pickCopyFunc(TIFF*, TIFF*, uint16, uint16);
+ static int
+ tiffcp(TIFF* in, TIFF* out)
+ {
+- uint16 bitspersample, samplesperpixel = 1;
++ uint16 bitspersample = 1, samplesperpixel = 1;
+ uint16 input_compression, input_photometric = PHOTOMETRIC_MINISBLACK;
+ copyFunc cf;
+ uint32 width, length;
+@@ -1067,6 +1067,16 @@ DECLAREcpFunc(cpContig2SeparateByRow)
+ register uint32 n;
+ uint32 row;
+ tsample_t s;
++ uint16 bps = 0;
++
++ (void) TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps);
++ if( bps != 8 )
++ {
++ TIFFError(TIFFFileName(in),
++ "Error, can only handle BitsPerSample=8 in %s",
++ "cpContig2SeparateByRow");
++ return 0;
++ }
+
+ inbuf = _TIFFmalloc(scanlinesizein);
+ outbuf = _TIFFmalloc(scanlinesizeout);
+@@ -1120,6 +1130,16 @@ DECLAREcpFunc(cpSeparate2ContigByRow)
+ register uint32 n;
+ uint32 row;
+ tsample_t s;
++ uint16 bps = 0;
++
++ (void) TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps);
++ if( bps != 8 )
++ {
++ TIFFError(TIFFFileName(in),
++ "Error, can only handle BitsPerSample=8 in %s",
++ "cpSeparate2ContigByRow");
++ return 0;
++ }
+
+ inbuf = _TIFFmalloc(scanlinesizein);
+ outbuf = _TIFFmalloc(scanlinesizeout);
+@@ -1784,7 +1804,7 @@ pickCopyFunc(TIFF* in, TIFF* out, uint16 bitspersample, uint16 samplesperpixel)
+ uint32 w, l, tw, tl;
+ int bychunk;
+
+- (void) TIFFGetField(in, TIFFTAG_PLANARCONFIG, &shortv);
++ (void) TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &shortv);
+ if (shortv != config && bitspersample != 8 && samplesperpixel > 1) {
+ fprintf(stderr,
+ "%s: Cannot handle different planar configuration w/ bits/sample != 8\n",
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2130.patch b/media-libs/tiff/files/tiff-4.0.7-bug2130.patch
new file mode 100644
index 000000000000..b565fecc029b
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2130.patch
@@ -0,0 +1,112 @@
+From c2faaeaa7887c24c574297e8e2f36208df9dc229 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 20:33:35 +0000
+Subject: [PATCH] * libtiff/tif_luv.c, tif_lzw.c, tif_packbits.c: return 0 in
+ Encode functions instead of -1 when TIFFFlushData1() fails. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2130
+
+---
+ ChangeLog | 6 ++++++
+ libtiff/tif_luv.c | 12 ++++++------
+ libtiff/tif_lzw.c | 8 +++++---
+ libtiff/tif_packbits.c | 6 +++---
+ 4 files changed, 20 insertions(+), 12 deletions(-)
+
+diff --git a/libtiff/tif_luv.c b/libtiff/tif_luv.c
+index f42ac0131fee..1f6d8ba3ea5a 100644
+--- a/libtiff/tif_luv.c
++++ b/libtiff/tif_luv.c
+@@ -473,7 +473,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+- return (-1);
++ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+@@ -505,7 +505,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+- return (-1);
++ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+@@ -565,7 +565,7 @@ LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+- return (-1);
++ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+@@ -624,7 +624,7 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+- return (-1);
++ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+@@ -656,7 +656,7 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+- return (-1);
++ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+diff --git a/libtiff/tif_lzw.c b/libtiff/tif_lzw.c
+index 240e19c2e058..5ba35ec1305f 100644
+--- a/libtiff/tif_lzw.c
++++ b/libtiff/tif_lzw.c
+@@ -969,7 +969,8 @@ LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+ */
+ if (op > limit) {
+ tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
+- TIFFFlushData1(tif);
++ if( !TIFFFlushData1(tif) )
++ return 0;
+ op = tif->tif_rawdata;
+ }
+ PutNextCode(op, ent);
+@@ -1054,7 +1055,8 @@ LZWPostEncode(TIFF* tif)
+
+ if (op > sp->enc_rawlimit) {
+ tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
+- TIFFFlushData1(tif);
++ if( !TIFFFlushData1(tif) )
++ return 0;
+ op = tif->tif_rawdata;
+ }
+ if (sp->enc_oldcode != (hcode_t) -1) {
+diff --git a/libtiff/tif_packbits.c b/libtiff/tif_packbits.c
+index d2a0165de9dd..0495e688a6be 100644
+--- a/libtiff/tif_packbits.c
++++ b/libtiff/tif_packbits.c
+@@ -99,7 +99,7 @@ PackBitsEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+ slop = (long)(op - lastliteral);
+ tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
+ if (!TIFFFlushData1(tif))
+- return (-1);
++ return (0);
+ op = tif->tif_rawcp;
+ while (slop-- > 0)
+ *op++ = *lastliteral++;
+@@ -107,7 +107,7 @@ PackBitsEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+ } else {
+ tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
+ if (!TIFFFlushData1(tif))
+- return (-1);
++ return (0);
+ op = tif->tif_rawcp;
+ }
+ }
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2535.patch b/media-libs/tiff/files/tiff-4.0.7-bug2535.patch
new file mode 100644
index 000000000000..c44a8f05d20d
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2535.patch
@@ -0,0 +1,54 @@
+From c4e376852d82936885833441169684267983691f Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 12:51:59 +0000
+Subject: [PATCH] * libtiff/tif_dirwrite.c: in
+ TIFFWriteDirectoryTagCheckedRational, replace assertion by runtime check to
+ error out if passed value is strictly negative. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2535
+
+* tools/tiffcrop.c: remove extraneous TIFFClose() in error code path, that
+caused double free.
+Related to http://bugzilla.maptools.org/show_bug.cgi?id=2535
+---
+ ChangeLog | 11 +++++++++++
+ libtiff/tif_dirwrite.c | 11 ++++++++---
+ tools/tiffcrop.c | 3 +--
+ 3 files changed, 20 insertions(+), 5 deletions(-)
+
+diff --git a/libtiff/tif_dirwrite.c b/libtiff/tif_dirwrite.c
+index d34f6f611d39..055324db078f 100644
+--- a/libtiff/tif_dirwrite.c
++++ b/libtiff/tif_dirwrite.c
+@@ -2094,10 +2094,15 @@ TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* d
+ static int
+ TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
+ {
++ static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
+ uint32 m[2];
+- assert(value>=0.0);
+ assert(sizeof(uint32)==4);
+- if (value<=0.0)
++ if( value < 0 )
++ {
++ TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal");
++ return 0;
++ }
++ else if (value==0.0)
+ {
+ m[0]=0;
+ m[1]=1;
+diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
+index 21dd08720d77..c69177e052d4 100644
+--- a/tools/tiffcrop.c
++++ b/tools/tiffcrop.c
+@@ -7996,7 +7996,6 @@ writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image,
+ if (!TIFFWriteDirectory(out))
+ {
+ TIFFError("","Failed to write IFD for page number %d", pagenum);
+- TIFFClose(out);
+ return (-1);
+ }
+
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2594.patch b/media-libs/tiff/files/tiff-4.0.7-bug2594.patch
new file mode 100644
index 000000000000..b2bc26e9064c
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2594.patch
@@ -0,0 +1,28 @@
+From a56820e2022e23610c1ea99fbf621d73d1e36348 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 14:18:48 +0000
+Subject: [PATCH] * tools/tiffinfo.c: fix null pointer dereference in -r mode
+ when the image has no StripByteCount tag. Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2594
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffinfo.c | 4 ++--
+ 2 files changed, 9 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiffinfo.c b/tools/tiffinfo.c
+index b02c7d46bed0..4d58055de85c 100644
+--- a/tools/tiffinfo.c
++++ b/tools/tiffinfo.c
+@@ -417,7 +417,7 @@ TIFFReadRawData(TIFF* tif, int bitrev)
+ uint64* stripbc=NULL;
+
+ TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &stripbc);
+- if (nstrips > 0) {
++ if (stripbc != NULL && nstrips > 0) {
+ uint32 bufsize = (uint32) stripbc[0];
+ tdata_t buf = _TIFFmalloc(bufsize);
+ tstrip_t s;
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2597.patch b/media-libs/tiff/files/tiff-4.0.7-bug2597.patch
new file mode 100644
index 000000000000..9cd29cfab77b
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2597.patch
@@ -0,0 +1,41 @@
+From 5ad5e64f8530a827482645986f5bb4e4613d0aa7 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 14:42:40 +0000
+Subject: [PATCH] * tools/tiffcp.c: avoid potential division by zero is
+ BitsPerSamples tag is missing. Reported by Agostino sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2597
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffcp.c | 10 ++++++++--
+ 2 files changed, 15 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiffcp.c b/tools/tiffcp.c
+index 6dfb9a91bfa9..c8e48c3c2bb3 100644
+--- a/tools/tiffcp.c
++++ b/tools/tiffcp.c
+@@ -1378,7 +1378,7 @@ DECLAREreadFunc(readSeparateTilesIntoBuffer)
+ uint8* bufp = (uint8*) buf;
+ uint32 tw, tl;
+ uint32 row;
+- uint16 bps, bytes_per_sample;
++ uint16 bps = 0, bytes_per_sample;
+
+ tilebuf = _TIFFmalloc(tilesize);
+ if (tilebuf == 0)
+@@ -1387,6 +1387,12 @@ DECLAREreadFunc(readSeparateTilesIntoBuffer)
+ (void) TIFFGetField(in, TIFFTAG_TILEWIDTH, &tw);
+ (void) TIFFGetField(in, TIFFTAG_TILELENGTH, &tl);
+ (void) TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps);
++ if( bps == 0 )
++ {
++ TIFFError(TIFFFileName(in), "Error, cannot read BitsPerSample");
++ status = 0;
++ goto done;
++ }
+ assert( bps % 8 == 0 );
+ bytes_per_sample = bps/8;
+
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2598.patch b/media-libs/tiff/files/tiff-4.0.7-bug2598.patch
new file mode 100644
index 000000000000..c0a0d1a8db1a
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2598.patch
@@ -0,0 +1,31 @@
+http://bugzilla.maptools.org/show_bug.cgi?id=2598
+
+From bc3d7392e43545c7c6375897458a7a3e8ee4d9d8 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Fri, 2 Dec 2016 22:13:32 +0000
+Subject: [PATCH] * tools/tiffcp.c: avoid uint32 underflow in cpDecodedStrips
+ that can cause various issues, such as buffer overflows in the library.
+ Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2598
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffcp.c | 4 ++--
+ 2 files changed, 9 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiffcp.c b/tools/tiffcp.c
+index 338a3d113bf8..6dfb9a91bfa9 100644
+--- a/tools/tiffcp.c
++++ b/tools/tiffcp.c
+@@ -985,7 +985,7 @@ DECLAREcpFunc(cpDecodedStrips)
+ tstrip_t s, ns = TIFFNumberOfStrips(in);
+ uint32 row = 0;
+ _TIFFmemset(buf, 0, stripsize);
+- for (s = 0; s < ns; s++) {
++ for (s = 0; s < ns && row < imagelength; s++) {
+ tsize_t cc = (row + rowsperstrip > imagelength) ?
+ TIFFVStripSize(in, imagelength - row) : stripsize;
+ if (TIFFReadEncodedStrip(in, s, buf, cc) < 0
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2599.patch b/media-libs/tiff/files/tiff-4.0.7-bug2599.patch
new file mode 100644
index 000000000000..929bb447bf71
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2599.patch
@@ -0,0 +1,54 @@
+From 9bbbe303c8e5db20d7f687ee1ca19c98fb852044 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 15:30:31 +0000
+Subject: [PATCH] * tools/tif_dir.c: when TIFFGetField(, TIFFTAG_NUMBEROFINKS,
+ ) is called, limit the return number of inks to SamplesPerPixel, so that code
+ that parses ink names doesn't go past the end of the buffer. Reported by
+ Agostino Sarubbo. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2599
+
+Reported by Agostino Sarubbo.
+---
+ ChangeLog | 10 +++++++++-
+ libtiff/tif_dir.c | 28 +++++++++++++++++++++++++++-
+ 2 files changed, 36 insertions(+), 2 deletions(-)
+
+diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
+index ad21655a6ee9..2574e748b3be 100644
+--- a/libtiff/tif_dir.c
++++ b/libtiff/tif_dir.c
+@@ -854,6 +854,32 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
+ if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
+ return 0;
+
++ if( 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 != tag)
++ continue;
++ 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;
++ }
++
+ /*
+ * We want to force the custom code to be used for custom
+ * fields even if the tag happens to match a well known
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2604.patch b/media-libs/tiff/files/tiff-4.0.7-bug2604.patch
new file mode 100644
index 000000000000..cc3f4cf3ce91
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2604.patch
@@ -0,0 +1,108 @@
+From ebc6029128555df725e6ad77a983134350bfc831 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Fri, 2 Dec 2016 23:05:51 +0000
+Subject: [PATCH] * libtiff/tif_pixarlog.c, libtiff/tif_luv.c: fix heap-based
+ buffer overflow on generation of PixarLog / LUV compressed files, with
+ ColorMap, TransferFunction attached and nasty plays with bitspersample. The
+ fix for LUV has not been tested, but suffers from the same kind of issue of
+ PixarLog. Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2604
+
+---
+ ChangeLog | 10 ++++++++++
+ libtiff/tif_luv.c | 20 +++++++++++++++-----
+ libtiff/tif_pixarlog.c | 19 ++++++++++++++++---
+ 3 files changed, 41 insertions(+), 8 deletions(-)
+
+diff --git a/libtiff/tif_luv.c b/libtiff/tif_luv.c
+index ca08f30a76b6..f42ac0131fee 100644
+--- a/libtiff/tif_luv.c
++++ b/libtiff/tif_luv.c
+@@ -158,6 +158,7 @@
+ 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 */
+@@ -1552,6 +1553,7 @@ LogLuvSetupEncode(TIFF* tif)
+ td->td_photometric, "must be either LogLUV or LogL");
+ break;
+ }
++ sp->encoder_state = 1;
+ return (1);
+ notsupported:
+ TIFFErrorExt(tif->tif_clientdata, module,
+@@ -1563,19 +1565,27 @@ notsupported:
+ 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
+ */
+- td->td_samplesperpixel =
+- (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
+- td->td_bitspersample = 16;
+- td->td_sampleformat = SAMPLEFORMAT_INT;
++ 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
+diff --git a/libtiff/tif_pixarlog.c b/libtiff/tif_pixarlog.c
+index f4af2bab7ce5..9836dce63450 100644
+--- a/libtiff/tif_pixarlog.c
++++ b/libtiff/tif_pixarlog.c
+@@ -1233,8 +1233,10 @@ PixarLogPostEncode(TIFF* tif)
+ 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
+@@ -1243,8 +1245,19 @@ PixarLogClose(TIFF* tif)
+ * readers that don't know about PixarLog, or how to set
+ * the PIXARLOGDATFMT pseudo-tag.
+ */
+- td->td_bitspersample = 8;
+- td->td_sampleformat = SAMPLEFORMAT_UINT;
++
++ 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
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2605.patch b/media-libs/tiff/files/tiff-4.0.7-bug2605.patch
new file mode 100644
index 000000000000..335e4348d3f3
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2605.patch
@@ -0,0 +1,55 @@
+From cd4832257daf222833ae172b3923268fec5b71b9 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 16:50:02 +0000
+Subject: [PATCH] * tools/tiffcp.c: replace assert( (bps % 8) == 0 ) by a non
+ assert check. Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2605
+
+---
+ ChangeLog | 6 ++++++
+ tools/tiffcp.c | 17 +++++++++++++----
+ 2 files changed, 19 insertions(+), 4 deletions(-)
+
+diff --git a/tools/tiffcp.c b/tools/tiffcp.c
+index 6d96bb89f555..49c9d37125a6 100644
+--- a/tools/tiffcp.c
++++ b/tools/tiffcp.c
+@@ -45,7 +45,6 @@
+ #include <string.h>
+
+ #include <ctype.h>
+-#include <assert.h>
+
+ #ifdef HAVE_UNISTD_H
+ # include <unistd.h>
+@@ -1393,7 +1392,12 @@ DECLAREreadFunc(readSeparateTilesIntoBuffer)
+ status = 0;
+ goto done;
+ }
+- assert( bps % 8 == 0 );
++ if( (bps % 8) != 0 )
++ {
++ TIFFError(TIFFFileName(in), "Error, cannot handle BitsPerSample that is not a multiple of 8");
++ status = 0;
++ goto done;
++ }
+ bytes_per_sample = bps/8;
+
+ for (row = 0; row < imagelength; row += tl) {
+@@ -1584,7 +1588,12 @@ DECLAREwriteFunc(writeBufferToSeparateTiles)
+ _TIFFfree(obuf);
+ return 0;
+ }
+- assert( bps % 8 == 0 );
++ if( (bps % 8) != 0 )
++ {
++ TIFFError(TIFFFileName(out), "Error, cannot handle BitsPerSample that is not a multiple of 8");
++ _TIFFfree(obuf);
++ return 0;
++ }
+ bytes_per_sample = bps/8;
+
+ for (row = 0; row < imagelength; row += tl) {
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2607.patch b/media-libs/tiff/files/tiff-4.0.7-bug2607.patch
new file mode 100644
index 000000000000..532259e91cb9
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2607.patch
@@ -0,0 +1,41 @@
+From c99f44478d6f0491da5b98c8cea14f565a021e22 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 15:44:15 +0000
+Subject: [PATCH] * tools/tiffcp.c: avoid potential division by zero is
+ BitsPerSamples tag is missing. Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2607
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffcp.c | 10 ++++++++--
+ 2 files changed, 15 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiffcp.c b/tools/tiffcp.c
+index c8e48c3c2bb3..142cbb0ecfc2 100644
+--- a/tools/tiffcp.c
++++ b/tools/tiffcp.c
+@@ -1569,7 +1569,7 @@ DECLAREwriteFunc(writeBufferToSeparateTiles)
+ uint8* bufp = (uint8*) buf;
+ uint32 tl, tw;
+ uint32 row;
+- uint16 bps, bytes_per_sample;
++ uint16 bps = 0, bytes_per_sample;
+
+ obuf = _TIFFmalloc(TIFFTileSize(out));
+ if (obuf == NULL)
+@@ -1578,6 +1578,12 @@ DECLAREwriteFunc(writeBufferToSeparateTiles)
+ (void) TIFFGetField(out, TIFFTAG_TILELENGTH, &tl);
+ (void) TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw);
+ (void) TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps);
++ if( bps == 0 )
++ {
++ TIFFError(TIFFFileName(out), "Error, cannot read BitsPerSample");
++ _TIFFfree(obuf);
++ return 0;
++ }
+ assert( bps % 8 == 0 );
+ bytes_per_sample = bps/8;
+
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2608.patch b/media-libs/tiff/files/tiff-4.0.7-bug2608.patch
new file mode 100644
index 000000000000..afe2c25a293e
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2608.patch
@@ -0,0 +1,104 @@
+From 92adbddc283782d71d81dbccf72ed2c279f90097 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 11:02:15 +0000
+Subject: [PATCH] * libtiff/tif_dirread.c: modify
+ ChopUpSingleUncompressedStrip() to instanciate compute ntrips as
+ TIFFhowmany_32(td->td_imagelength, rowsperstrip), instead of a logic based on
+ the total size of data. Which is faulty is the total size of data is not
+ sufficient to fill the whole image, and thus results in reading outside of
+ the StripByCounts/StripOffsets arrays when using TIFFReadScanline(). Reported
+ by Agostino Sarubbo. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2608.
+
+* libtiff/tif_strip.c: revert the change in TIFFNumberOfStrips() done
+for http://bugzilla.maptools.org/show_bug.cgi?id=2587 / CVE-2016-9273 since
+the above change is a better fix that makes it unnecessary.
+---
+ ChangeLog | 15 +++++++++++++++
+ libtiff/tif_dirread.c | 24 +++++++++++-------------
+ libtiff/tif_strip.c | 11 +----------
+ 3 files changed, 27 insertions(+), 23 deletions(-)
+
+diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
+index 01070f2ecebd..f2905286c0d0 100644
+--- a/libtiff/tif_dirread.c
++++ b/libtiff/tif_dirread.c
+@@ -5502,8 +5502,7 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
+ uint64 rowblockbytes;
+ uint64 stripbytes;
+ uint32 strip;
+- uint64 nstrips64;
+- uint32 nstrips32;
++ uint32 nstrips;
+ uint32 rowsperstrip;
+ uint64* newcounts;
+ uint64* newoffsets;
+@@ -5534,18 +5533,17 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
+ return;
+
+ /*
+- * never increase the number of strips in an image
++ * never increase the number of rows per strip
+ */
+ if (rowsperstrip >= td->td_rowsperstrip)
+ return;
+- nstrips64 = TIFFhowmany_64(bytecount, stripbytes);
+- if ((nstrips64==0)||(nstrips64>0xFFFFFFFF)) /* something is wonky, do nothing. */
+- return;
+- nstrips32 = (uint32)nstrips64;
++ nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
++ if( nstrips == 0 )
++ return;
+
+- newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
++ newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
+ "for chopped \"StripByteCounts\" array");
+- newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
++ newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
+ "for chopped \"StripOffsets\" array");
+ if (newcounts == NULL || newoffsets == NULL) {
+ /*
+@@ -5562,18 +5560,18 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
+ * Fill the strip information arrays with new bytecounts and offsets
+ * that reflect the broken-up format.
+ */
+- for (strip = 0; strip < nstrips32; strip++) {
++ for (strip = 0; strip < nstrips; strip++) {
+ if (stripbytes > bytecount)
+ stripbytes = bytecount;
+ newcounts[strip] = stripbytes;
+- newoffsets[strip] = offset;
++ newoffsets[strip] = stripbytes ? offset : 0;
+ offset += stripbytes;
+ bytecount -= stripbytes;
+ }
+ /*
+ * Replace old single strip info with multi-strip info.
+ */
+- td->td_stripsperimage = td->td_nstrips = nstrips32;
++ td->td_stripsperimage = td->td_nstrips = nstrips;
+ TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
+
+ _TIFFfree(td->td_stripbytecount);
+diff --git a/libtiff/tif_strip.c b/libtiff/tif_strip.c
+index b6098dd31241..6e9f2ef6ddf2 100644
+--- a/libtiff/tif_strip.c
++++ b/libtiff/tif_strip.c
+@@ -63,15 +63,6 @@ TIFFNumberOfStrips(TIFF* tif)
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 nstrips;
+
+- /* If the value was already computed and store in td_nstrips, then return it,
+- since ChopUpSingleUncompressedStrip might have altered and resized the
+- since the td_stripbytecount and td_stripoffset arrays to the new value
+- after the initial affectation of td_nstrips = TIFFNumberOfStrips() in
+- tif_dirread.c ~line 3612.
+- See http://bugzilla.maptools.org/show_bug.cgi?id=2587 */
+- if( td->td_nstrips )
+- return td->td_nstrips;
+-
+ nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
+ TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2610.patch b/media-libs/tiff/files/tiff-4.0.7-bug2610.patch
new file mode 100644
index 000000000000..f76e83922d6c
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2610.patch
@@ -0,0 +1,46 @@
+From ee00edfbe833647d59ad87cac82f1b4c0c902179 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 16:40:01 +0000
+Subject: [PATCH] * tools/tiffcp.c: fix uint32 underflow/overflow that can
+ cause heap-based buffer overflow. Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2610
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffcp.c | 8 ++++----
+ 2 files changed, 11 insertions(+), 4 deletions(-)
+
+diff --git a/tools/tiffcp.c b/tools/tiffcp.c
+index 142cbb0ecfc2..6d96bb89f555 100644
+--- a/tools/tiffcp.c
++++ b/tools/tiffcp.c
+@@ -1163,7 +1163,7 @@ bad:
+
+ static void
+ cpStripToTile(uint8* out, uint8* in,
+- uint32 rows, uint32 cols, int outskew, int inskew)
++ uint32 rows, uint32 cols, int outskew, int64 inskew)
+ {
+ while (rows-- > 0) {
+ uint32 j = cols;
+@@ -1320,7 +1320,7 @@ DECLAREreadFunc(readContigTilesIntoBuffer)
+ tdata_t tilebuf;
+ uint32 imagew = TIFFScanlineSize(in);
+ uint32 tilew = TIFFTileRowSize(in);
+- int iskew = imagew - tilew;
++ int64 iskew = (int64)imagew - (int64)tilew;
+ uint8* bufp = (uint8*) buf;
+ uint32 tw, tl;
+ uint32 row;
+@@ -1348,7 +1348,7 @@ DECLAREreadFunc(readContigTilesIntoBuffer)
+ status = 0;
+ goto done;
+ }
+- if (colb + tilew > imagew) {
++ if (colb > iskew) {
+ uint32 width = imagew - colb;
+ uint32 oskew = tilew - width;
+ cpStripToTile(bufp + colb,
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2619.patch b/media-libs/tiff/files/tiff-4.0.7-bug2619.patch
new file mode 100644
index 000000000000..0e0053883a32
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2619.patch
@@ -0,0 +1,46 @@
+From cb840651f037c59895b67d44b46a34127bb082dd Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 13:00:03 +0000
+Subject: [PATCH] * tools/tiffcrop.c: fix integer division by zero when
+ BitsPerSample is missing. Reported by Agostina Sarubo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2619
+
+---
+ ChangeLog | 6 ++++++
+ tools/tiffcrop.c | 8 ++++----
+ 2 files changed, 10 insertions(+), 4 deletions(-)
+
+diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
+index 9122aab37530..21dd08720d77 100644
+--- a/tools/tiffcrop.c
++++ b/tools/tiffcrop.c
+@@ -1164,7 +1164,7 @@ writeBufferToSeparateStrips (TIFF* out, uint8* buf,
+ tdata_t obuf;
+
+ (void) TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+- (void) TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps);
++ (void) TIFFGetFieldDefaulted(out, TIFFTAG_BITSPERSAMPLE, &bps);
+ bytes_per_sample = (bps + 7) / 8;
+ if( width == 0 ||
+ (uint32)bps * (uint32)spp > TIFF_UINT32_MAX / width ||
+@@ -4760,7 +4760,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8 *obuf, uint32 length,
+ int i, bytes_per_sample, bytes_per_pixel, shift_width, result = 1;
+ uint32 j;
+ int32 bytes_read = 0;
+- uint16 bps, planar;
++ uint16 bps = 0, planar;
+ uint32 nstrips;
+ uint32 strips_per_sample;
+ uint32 src_rowsize, dst_rowsize, rows_processed, rps;
+@@ -4780,7 +4780,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8 *obuf, uint32 length,
+ }
+
+ memset (srcbuffs, '\0', sizeof(srcbuffs));
+- TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps);
++ TIFFGetFieldDefaulted(in, TIFFTAG_BITSPERSAMPLE, &bps);
+ TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &planar);
+ TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rps);
+ if (rps > length)
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2620.patch b/media-libs/tiff/files/tiff-4.0.7-bug2620.patch
new file mode 100644
index 000000000000..1b37177c5f91
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2620.patch
@@ -0,0 +1,29 @@
+From 76c4b35f114bc9614700accd22cc4a0b4b6b92d3 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 11:35:56 +0000
+Subject: [PATCH] * tools/tiffcrop.c: fix readContigStripsIntoBuffer() in -i
+ (ignore) mode so that the output buffer is correctly incremented to avoid
+ write outside bounds. Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2620
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffcrop.c | 4 ++--
+ 2 files changed, 9 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
+index 722b132cee6d..bdcbd63ed70b 100644
+--- a/tools/tiffcrop.c
++++ b/tools/tiffcrop.c
+@@ -3698,7 +3698,7 @@ static int readContigStripsIntoBuffer (TIFF* in, uint8* buf)
+ (unsigned long) strip, (unsigned long)rows);
+ return 0;
+ }
+- bufp += bytes_read;
++ bufp += stripsize;
+ }
+
+ return 1;
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2621.patch b/media-libs/tiff/files/tiff-4.0.7-bug2621.patch
new file mode 100644
index 000000000000..7bb1d57e3e9f
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2621.patch
@@ -0,0 +1,49 @@
+From d7045ed1501ec99c4e56174813bb1cb5c9a559ef Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 3 Dec 2016 12:19:32 +0000
+Subject: [PATCH] * tools/tiffcrop.c: add 3 extra bytes at end of strip buffer
+ in readSeparateStripsIntoBuffer() to avoid read outside of heap allocated
+ buffer. Reported by Agostina Sarubo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2621
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiffcrop.c | 14 ++++++++++++--
+ 2 files changed, 19 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
+index bdcbd63ed70b..9122aab37530 100644
+--- a/tools/tiffcrop.c
++++ b/tools/tiffcrop.c
+@@ -4815,10 +4815,17 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8 *obuf, uint32 length,
+ nstrips = TIFFNumberOfStrips(in);
+ strips_per_sample = nstrips /spp;
+
++ /* Add 3 padding bytes for combineSeparateSamples32bits */
++ if( (size_t) stripsize > 0xFFFFFFFFU - 3U )
++ {
++ TIFFError("readSeparateStripsIntoBuffer", "Integer overflow when calculating buffer size.");
++ exit(-1);
++ }
++
+ for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++)
+ {
+ srcbuffs[s] = NULL;
+- buff = _TIFFmalloc(stripsize);
++ buff = _TIFFmalloc(stripsize + 3);
+ if (!buff)
+ {
+ TIFFError ("readSeparateStripsIntoBuffer",
+@@ -4827,6 +4834,9 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8 *obuf, uint32 length,
+ _TIFFfree (srcbuffs[i]);
+ return 0;
+ }
++ buff[stripsize] = 0;
++ buff[stripsize+1] = 0;
++ buff[stripsize+2] = 0;
+ srcbuffs[s] = buff;
+ }
+
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2627.patch b/media-libs/tiff/files/tiff-4.0.7-bug2627.patch
new file mode 100644
index 000000000000..11a3f3cd3f5c
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2627.patch
@@ -0,0 +1,59 @@
+From f88bfadb6d1fac1d0d081058216da659e1f5a628 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sun, 18 Dec 2016 22:28:42 +0000
+Subject: [PATCH] * libtiff/tif_getimage.c: fix potential memory leaks in error
+ code path of TIFFRGBAImageBegin(). Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2627
+
+---
+ ChangeLog | 6 ++++++
+ libtiff/tif_getimage.c | 21 +++++++++------------
+ 2 files changed, 15 insertions(+), 12 deletions(-)
+
+diff --git a/libtiff/tif_getimage.c b/libtiff/tif_getimage.c
+index c0eb6df0b09a..2ea838556732 100644
+--- a/libtiff/tif_getimage.c
++++ b/libtiff/tif_getimage.c
+@@ -283,6 +283,13 @@ TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
+ 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;
+@@ -468,13 +475,6 @@ TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
+ photoTag, img->photometric);
+ goto fail_return;
+ }
+- img->Map = NULL;
+- img->BWmap = NULL;
+- img->PALmap = NULL;
+- img->ycbcr = NULL;
+- img->cielab = NULL;
+- img->UaToAa = NULL;
+- img->Bitdepth16To8 = NULL;
+ TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
+ TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
+@@ -494,10 +494,7 @@ TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
+ return 1;
+
+ fail_return:
+- _TIFFfree( img->redcmap );
+- _TIFFfree( img->greencmap );
+- _TIFFfree( img->bluecmap );
+- img->redcmap = img->greencmap = img->bluecmap = NULL;
++ TIFFRGBAImageEnd( img );
+ return 0;
+ }
+
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2631.patch b/media-libs/tiff/files/tiff-4.0.7-bug2631.patch
new file mode 100644
index 000000000000..6e1011b072dc
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2631.patch
@@ -0,0 +1,34 @@
+From 101253c74cde97203dab28c4f3bd0994cea5804c Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 14 Jan 2017 13:12:33 +0000
+Subject: [PATCH] * tools/raw2tiff.c: avoid integer division by zero. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2631
+
+---
+ ChangeLog | 5 +++++
+ tools/raw2tiff.c | 10 ++++++++--
+ 2 files changed, 13 insertions(+), 2 deletions(-)
+
+diff --git a/tools/raw2tiff.c b/tools/raw2tiff.c
+index 7298e80a95c9..083e9ee73f0f 100644
+--- a/tools/raw2tiff.c
++++ b/tools/raw2tiff.c
+@@ -408,8 +408,14 @@ guessSize(int fd, TIFFDataType dtype, _TIFF_off_t hdr_size, uint32 nbands,
+ } else if (*width == 0 && *length == 0) {
+ unsigned int fail = 0;
+ fprintf(stderr, "Image width and height are not specified.\n");
++ w = (uint32) sqrt(imagesize / longt);
++ if( w == 0 )
++ {
++ fprintf(stderr, "Too small image size.\n");
++ return -1;
++ }
+
+- for (w = (uint32) sqrt(imagesize / longt);
++ for (;
+ w < sqrt(imagesize * longt);
+ w++) {
+ if (imagesize % w == 0) {
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2633-bug2634.patch b/media-libs/tiff/files/tiff-4.0.7-bug2633-bug2634.patch
new file mode 100644
index 000000000000..d68e86ebea2f
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2633-bug2634.patch
@@ -0,0 +1,41 @@
+From 95a32fbbadf54e7527c7e3b66fd603503b29dde9 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 17 Dec 2016 19:45:28 +0000
+Subject: [PATCH] * tools/tiff2ps.c: fix 2 heap-based buffer overflows (in
+ PSDataBW and PSDataColorContig). Reported by Agostino Sarubbo. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2633 and
+ http://bugzilla.maptools.org/show_bug.cgi?id=2634.
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiff2ps.c | 9 +++++++--
+ 2 files changed, 14 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiff2ps.c b/tools/tiff2ps.c
+index 82a5d84b41f5..71df4309ee0c 100644
+--- a/tools/tiff2ps.c
++++ b/tools/tiff2ps.c
+@@ -2440,6 +2440,11 @@ PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
+ unsigned char *cp, c;
+
+ (void) w;
++ if( es <= 0 )
++ {
++ TIFFError(filename, "Inconsistent value of es: %d", es);
++ return;
++ }
+ tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow);
+ if (tf_buf == NULL) {
+ TIFFError(filename, "No space for scanline buffer");
+@@ -2692,7 +2697,7 @@ PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
+
+ if (alpha) {
+ int adjust;
+- while (cc-- > 0) {
++ while (cc-- > 1) {
+ DOBREAK(breaklen, 1, fd);
+ /*
+ * For images with alpha, matte against
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2635.patch b/media-libs/tiff/files/tiff-4.0.7-bug2635.patch
new file mode 100644
index 000000000000..8756115c9058
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2635.patch
@@ -0,0 +1,33 @@
+From a7b470d67f2b98599b2c9cd9945db6eea735cc47 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sun, 18 Dec 2016 10:37:59 +0000
+Subject: [PATCH] * tools/tiff2pdf.c: prevent heap-based buffer overflow in -j
+ mode on a paletted image. Note: this fix errors out before the overflow
+ happens. There could probably be a better fix. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2635
+
+---
+ ChangeLog | 7 +++++++
+ tools/tiff2pdf.c | 8 +++++++-
+ 2 files changed, 14 insertions(+), 1 deletion(-)
+
+diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
+index fe8a6ea7e101..afea414bebf6 100644
+--- a/tools/tiff2pdf.c
++++ b/tools/tiff2pdf.c
+@@ -3654,6 +3654,12 @@ tsize_t t2p_sample_realize_palette(T2P* t2p, unsigned char* buffer){
+ uint32 j=0;
+ sample_count=t2p->tiff_width*t2p->tiff_length;
+ component_count=t2p->tiff_samplesperpixel;
++ if( sample_count * component_count > t2p->tiff_datasize )
++ {
++ TIFFError(TIFF2PDF_MODULE, "Error: sample_count * component_count > t2p->tiff_datasize");
++ t2p->t2p_error = T2P_ERR_ERROR;
++ return 1;
++ }
+
+ for(i=sample_count;i>0;i--){
+ palette_offset=buffer[i-1] * component_count;
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2638.patch b/media-libs/tiff/files/tiff-4.0.7-bug2638.patch
new file mode 100644
index 000000000000..15541576c586
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2638.patch
@@ -0,0 +1,29 @@
+From 9f5536843f2ae641542bb81a3023dbc581fac184 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Tue, 20 Dec 2016 17:13:26 +0000
+Subject: [PATCH] * tools/tiff2pdf.c: fix wrong usage of memcpy() that can
+ trigger unspecified behaviour. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2638
+
+---
+ ChangeLog | 6 ++++++
+ tools/tiff2pdf.c | 5 +++--
+ 2 files changed, 9 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
+index afea414bebf6..78ffa77d123a 100644
+--- a/tools/tiff2pdf.c
++++ b/tools/tiff2pdf.c
+@@ -3593,7 +3593,8 @@ void t2p_tile_collapse_left(
+
+ edgescanwidth = (scanwidth * edgetilewidth + (tilewidth - 1))/ tilewidth;
+ for(i=0;i<tilelength;i++){
+- _TIFFmemcpy(
++ /* We use memmove() since there can be overlaps in src and dst buffers for the first items */
++ memmove(
+ &(((char*)buffer)[edgescanwidth*i]),
+ &(((char*)buffer)[scanwidth*i]),
+ edgescanwidth);
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2639.patch b/media-libs/tiff/files/tiff-4.0.7-bug2639.patch
new file mode 100644
index 000000000000..b894775dc703
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2639.patch
@@ -0,0 +1,58 @@
+From 6a61192a98665d870dcb835452cb9c5757ccd27c Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Tue, 20 Dec 2016 17:24:35 +0000
+Subject: [PATCH] * tools/tiff2pdf.c: avoid potential invalid memory read in
+ t2p_writeproc. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2639
+
+---
+ ChangeLog | 6 ++++++
+ tools/tiff2pdf.c | 20 +++++++++++---------
+ 2 files changed, 17 insertions(+), 9 deletions(-)
+
+diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
+index 78ffa77d123a..5348f1a765fe 100644
+--- a/tools/tiff2pdf.c
++++ b/tools/tiff2pdf.c
+@@ -2896,6 +2896,7 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_
+ }
+ if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) {
+ if (count >= 4) {
++ int retTIFFReadRawTile;
+ /* Ignore EOI marker of JpegTables */
+ _TIFFmemcpy(buffer, jpt, count - 2);
+ bufferoffset += count - 2;
+@@ -2903,22 +2904,23 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_
+ table_end[0] = buffer[bufferoffset-2];
+ table_end[1] = buffer[bufferoffset-1];
+ xuint32 = bufferoffset;
+- bufferoffset -= 2;
+- bufferoffset += TIFFReadRawTile(
++ bufferoffset -= 2;
++ retTIFFReadRawTile= TIFFReadRawTile(
+ input,
+ tile,
+ (tdata_t) &(((unsigned char*)buffer)[bufferoffset]),
+ -1);
++ if( retTIFFReadRawTile < 0 )
++ {
++ _TIFFfree(buffer);
++ t2p->t2p_error = T2P_ERR_ERROR;
++ return(0);
++ }
++ bufferoffset += retTIFFReadRawTile;
+ /* Overwrite SOI marker of image scan with previously */
+ /* saved end of JpegTables */
+ buffer[xuint32-2]=table_end[0];
+ buffer[xuint32-1]=table_end[1];
+- } else {
+- bufferoffset += TIFFReadRawTile(
+- input,
+- tile,
+- (tdata_t) &(((unsigned char*)buffer)[bufferoffset]),
+- -1);
+ }
+ }
+ t2pWriteFile(output, (tdata_t) buffer, bufferoffset);
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2640.patch b/media-libs/tiff/files/tiff-4.0.7-bug2640.patch
new file mode 100644
index 000000000000..2569f47a54b0
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2640.patch
@@ -0,0 +1,28 @@
+From 548b62fae49637b621766c721884d59a55c9a2d8 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Tue, 20 Dec 2016 17:28:17 +0000
+Subject: [PATCH] * tools/tiff2pdf.c: avoid potential heap-based overflow in
+ t2p_readwrite_pdf_image_tile(). Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2640
+
+---
+ ChangeLog | 6 ++++++
+ tools/tiff2pdf.c | 4 ++--
+ 2 files changed, 8 insertions(+), 2 deletions(-)
+
+diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
+index 5348f1a765fe..8e4e24ef9e82 100644
+--- a/tools/tiff2pdf.c
++++ b/tools/tiff2pdf.c
+@@ -2895,7 +2895,7 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_
+ return(0);
+ }
+ if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) {
+- if (count >= 4) {
++ if (count > 4) {
+ int retTIFFReadRawTile;
+ /* Ignore EOI marker of JpegTables */
+ _TIFFmemcpy(buffer, jpt, count - 2);
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2642-bug2643-bug2646-bug2647.patch b/media-libs/tiff/files/tiff-4.0.7-bug2642-bug2643-bug2646-bug2647.patch
new file mode 100644
index 000000000000..6f01774b9d53
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2642-bug2643-bug2646-bug2647.patch
@@ -0,0 +1,278 @@
+From f049eba476a1ed60adc6534452ccf0022c2d1908 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 16:09:02 +0000
+Subject: [PATCH] * libtiff/tif_dir.c, tif_dirread.c, tif_dirwrite.c: implement
+ various clampings of double to other data types to avoid undefined behaviour
+ if the output range isn't big enough to hold the input value. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2643
+ http://bugzilla.maptools.org/show_bug.cgi?id=2642
+ http://bugzilla.maptools.org/show_bug.cgi?id=2646
+ http://bugzilla.maptools.org/show_bug.cgi?id=2647
+
+---
+ ChangeLog | 10 ++++++
+ libtiff/tif_dir.c | 20 ++++++++---
+ libtiff/tif_dirread.c | 12 +++++--
+ libtiff/tif_dirwrite.c | 92 ++++++++++++++++++++++++++++++++++++++++++++------
+ 4 files changed, 116 insertions(+), 18 deletions(-)
+
+diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
+index 2574e748b3be..36c7ae57641a 100644
+--- a/libtiff/tif_dir.c
++++ b/libtiff/tif_dir.c
+@@ -31,6 +31,7 @@
+ * (and also some miscellaneous stuff)
+ */
+ #include "tiffiop.h"
++#include <float.h>
+
+ /*
+ * These are used in the backwards compatibility code...
+@@ -154,6 +155,15 @@ bad:
+ 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)
+ {
+@@ -312,13 +322,13 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
+ dblval = va_arg(ap, double);
+ if( dblval < 0 )
+ goto badvaluedouble;
+- td->td_xresolution = (float) dblval;
++ td->td_xresolution = TIFFClampDoubleToFloat( dblval );
+ break;
+ case TIFFTAG_YRESOLUTION:
+ dblval = va_arg(ap, double);
+ if( dblval < 0 )
+ goto badvaluedouble;
+- td->td_yresolution = (float) dblval;
++ td->td_yresolution = TIFFClampDoubleToFloat( dblval );
+ break;
+ case TIFFTAG_PLANARCONFIG:
+ v = (uint16) va_arg(ap, uint16_vap);
+@@ -327,10 +337,10 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
+ td->td_planarconfig = (uint16) v;
+ break;
+ case TIFFTAG_XPOSITION:
+- td->td_xposition = (float) va_arg(ap, double);
++ td->td_xposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
+ break;
+ case TIFFTAG_YPOSITION:
+- td->td_yposition = (float) va_arg(ap, double);
++ td->td_yposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
+ break;
+ case TIFFTAG_RESOLUTIONUNIT:
+ v = (uint16) va_arg(ap, uint16_vap);
+diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
+index eae3430612d0..f8628fd6d5d2 100644
+--- a/libtiff/tif_dirread.c
++++ b/libtiff/tif_dirread.c
+@@ -40,6 +40,7 @@
+ */
+
+ #include "tiffiop.h"
++#include <float.h>
+
+ #define IGNORE 0 /* tag placeholder used below */
+ #define FAILED_FII ((uint32) -1)
+@@ -2406,7 +2407,14 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEnt
+ ma=(double*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+- *mb++=(float)(*ma++);
++ {
++ double val = *ma++;
++ if( val > FLT_MAX )
++ val = FLT_MAX;
++ else if( val < -FLT_MAX )
++ val = -FLT_MAX;
++ *mb++=(float)val;
++ }
+ }
+ break;
+ }
+diff --git a/libtiff/tif_dirwrite.c b/libtiff/tif_dirwrite.c
+index 055324db078f..f7339685130d 100644
+--- a/libtiff/tif_dirwrite.c
++++ b/libtiff/tif_dirwrite.c
+@@ -30,6 +30,7 @@
+ * Directory Write Support Routines.
+ */
+ #include "tiffiop.h"
++#include <float.h>
+
+ #ifdef HAVE_IEEEFP
+ #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
+@@ -939,6 +940,69 @@ bad:
+ 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)
+ {
+@@ -959,7 +1023,7 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
+ if (tif->tif_dir.td_bitspersample<=32)
+ {
+ for (i = 0; i < count; ++i)
+- ((float*)conv)[i] = (float)value[i];
++ ((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]);
+ ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
+ }
+ else
+@@ -971,19 +1035,19 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
+ if (tif->tif_dir.td_bitspersample<=8)
+ {
+ for (i = 0; i < count; ++i)
+- ((int8*)conv)[i] = (int8)value[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] = (int16)value[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] = (int32)value[i];
++ ((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
+ ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
+ }
+ break;
+@@ -991,19 +1055,19 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
+ if (tif->tif_dir.td_bitspersample<=8)
+ {
+ for (i = 0; i < count; ++i)
+- ((uint8*)conv)[i] = (uint8)value[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] = (uint16)value[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] = (uint32)value[i];
++ ((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
+ ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
+ }
+ break;
+@@ -2102,12 +2166,17 @@ TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir,
+ 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==(double)(uint32)value)
++ else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
+ {
+ m[0]=(uint32)value;
+ m[1]=1;
+@@ -2148,12 +2217,13 @@ TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry*
+ }
+ for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
+ {
+- if (*na<=0.0)
++ if (*na<=0.0 || *na != *na)
+ {
+ nb[0]=0;
+ nb[1]=1;
+ }
+- else if (*na==(float)(uint32)(*na))
++ else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
++ *na==(float)(uint32)(*na))
+ {
+ nb[0]=(uint32)(*na);
+ nb[1]=1;
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2644.patch b/media-libs/tiff/files/tiff-4.0.7-bug2644.patch
new file mode 100644
index 000000000000..b4ec01a32177
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2644.patch
@@ -0,0 +1,45 @@
+From 699097af4e22e48fc78ae7ae02807ec37f0d31fe Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 13:28:01 +0000
+Subject: [PATCH] * libtiff/tif_dirread.c: avoid division by floating point 0
+ in TIFFReadDirEntryCheckedRational() and TIFFReadDirEntryCheckedSrational(),
+ and return 0 in that case (instead of infinity as before presumably)
+ Apparently some sanitizers do not like those divisions by zero. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2644
+
+---
+ ChangeLog | 8 ++++++++
+ libtiff/tif_dirread.c | 12 +++++++++---
+ 2 files changed, 17 insertions(+), 3 deletions(-)
+
+diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
+index f2905286c0d0..eae3430612d0 100644
+--- a/libtiff/tif_dirread.c
++++ b/libtiff/tif_dirread.c
+@@ -2872,7 +2872,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFD
+ m.l = direntry->tdir_offset.toff_long8;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(m.i,2);
+- if (m.i[0]==0)
++ /* 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];
+@@ -2900,7 +2903,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFF
+ m.l=direntry->tdir_offset.toff_long8;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(m.i,2);
+- if ((int32)m.i[0]==0)
++ /* 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];
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2648.patch b/media-libs/tiff/files/tiff-4.0.7-bug2648.patch
new file mode 100644
index 000000000000..a3e2f59dc275
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2648.patch
@@ -0,0 +1,33 @@
+From 569ffefa61f3237fa2221730621c869216c465a6 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 16:13:50 +0000
+Subject: [PATCH] * libtiff/tif_jpeg.c: validate BitsPerSample in
+ JPEGSetupEncode() to avoid undefined behaviour caused by invalid shift
+ exponent. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2648
+
+---
+ ChangeLog | 6 ++++++
+ libtiff/tif_jpeg.c | 9 ++++++++-
+ 2 files changed, 14 insertions(+), 1 deletion(-)
+
+diff --git a/libtiff/tif_jpeg.c b/libtiff/tif_jpeg.c
+index 09ef4949f9ee..e45e2a4e17f8 100644
+--- a/libtiff/tif_jpeg.c
++++ b/libtiff/tif_jpeg.c
+@@ -1632,6 +1632,13 @@ JPEGSetupEncode(TIFF* tif)
+ "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
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2650-2.patch b/media-libs/tiff/files/tiff-4.0.7-bug2650-2.patch
new file mode 100644
index 000000000000..eba5b8f50bac
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2650-2.patch
@@ -0,0 +1,26 @@
+From 08e5d199b0a1c80fc81a1cc718e5d9d019517e37 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 17:48:11 +0000
+Subject: [PATCH] Initialize variable to fix MSVC warning (caused by previous
+ commit)
+
+---
+ libtiff/tif_read.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/libtiff/tif_read.c b/libtiff/tif_read.c
+index 8c5af6a8f5f7..b2edb029a90c 100644
+--- a/libtiff/tif_read.c
++++ b/libtiff/tif_read.c
+@@ -420,7 +420,7 @@ TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
+ return ((tmsize_t)(-1));
+ }
+ } else {
+- tmsize_t ma;
++ 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))
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2650.patch b/media-libs/tiff/files/tiff-4.0.7-bug2650.patch
new file mode 100644
index 000000000000..2aac26987d51
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2650.patch
@@ -0,0 +1,54 @@
+From 5368b55d0f88a34ede3d21782d3142b2e11e6eb9 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 16:33:34 +0000
+Subject: [PATCH] * libtiff/tif_read.c: avoid potential undefined behaviour on
+ signed integer addition in TIFFReadRawStrip1() in isMapped() case. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2650
+
+---
+ ChangeLog | 6 ++++++
+ libtiff/tif_read.c | 29 +++++++++++++++++++----------
+ 2 files changed, 25 insertions(+), 10 deletions(-)
+
+diff --git a/libtiff/tif_read.c b/libtiff/tif_read.c
+index 29a311db0cb7..8c5af6a8f5f7 100644
+--- a/libtiff/tif_read.c
++++ b/libtiff/tif_read.c
+@@ -420,16 +420,25 @@ TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
+ return ((tmsize_t)(-1));
+ }
+ } else {
+- tmsize_t ma,mb;
++ tmsize_t ma;
+ tmsize_t n;
+- ma=(tmsize_t)td->td_stripoffset[strip];
+- mb=ma+size;
+- if ((td->td_stripoffset[strip] > (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 ((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,
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2651.patch b/media-libs/tiff/files/tiff-4.0.7-bug2651.patch
new file mode 100644
index 000000000000..1b800189c594
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2651.patch
@@ -0,0 +1,86 @@
+From 669faf71833c4c2e72774b2e732ca4d28b149c83 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 19:02:49 +0000
+Subject: [PATCH] * libtiff/tiffiop.h, tif_unix.c, tif_win32.c, tif_vms.c: add
+ _TIFFcalloc()
+
+* libtiff/tif_read.c: TIFFReadBufferSetup(): use _TIFFcalloc() to zero
+initialize tif_rawdata.
+Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2651
+---
+ ChangeLog | 8 ++++++++
+ libtiff/tif_read.c | 6 ++++--
+ libtiff/tif_unix.c | 10 +++++++++-
+ libtiff/tif_vms.c | 10 +++++++++-
+ libtiff/tif_win32.c | 10 +++++++++-
+ libtiff/tiffio.h | 3 ++-
+ 6 files changed, 41 insertions(+), 6 deletions(-)
+
+diff --git a/libtiff/tif_read.c b/libtiff/tif_read.c
+index b2edb029a90c..6a8c7daf3dfa 100644
+--- a/libtiff/tif_read.c
++++ b/libtiff/tif_read.c
+@@ -985,7 +985,9 @@ TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
+ "Invalid buffer size");
+ return (0);
+ }
+- tif->tif_rawdata = (uint8*) _TIFFmalloc(tif->tif_rawdatasize);
++ /* 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) {
+diff --git a/libtiff/tif_unix.c b/libtiff/tif_unix.c
+index 81e9d6653c2a..80c437cfa37a 100644
+--- a/libtiff/tif_unix.c
++++ b/libtiff/tif_unix.c
+@@ -316,6 +316,14 @@ _TIFFmalloc(tmsize_t s)
+ 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)
+ {
+diff --git a/libtiff/tif_win32.c b/libtiff/tif_win32.c
+index 24b824f1bd56..090baed87135 100644
+--- a/libtiff/tif_win32.c
++++ b/libtiff/tif_win32.c
+@@ -360,6 +360,14 @@ _TIFFmalloc(tmsize_t s)
+ 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)
+ {
+diff --git a/libtiff/tiffio.h b/libtiff/tiffio.h
+index 6e508181dbce..ef61b5c06a03 100644
+--- a/libtiff/tiffio.h
++++ b/libtiff/tiffio.h
+@@ -293,6 +293,7 @@ extern TIFFCodec* TIFFGetConfiguredCODECs(void);
+ */
+
+ 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);
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2653.patch b/media-libs/tiff/files/tiff-4.0.7-bug2653.patch
new file mode 100644
index 000000000000..b65a94daeac2
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2653.patch
@@ -0,0 +1,33 @@
+From 5083c41f3a6824f392adf3a6dce1548afded4211 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 12:15:01 +0000
+Subject: [PATCH] * libtiff/tif_jpeg.c: avoid integer division by zero in
+ JPEGSetupEncode() when horizontal or vertical sampling is set to 0. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2653
+
+---
+ ChangeLog | 6 ++++++
+ libtiff/tif_jpeg.c | 9 ++++++++-
+ 2 files changed, 14 insertions(+), 1 deletion(-)
+
+diff --git a/libtiff/tif_jpeg.c b/libtiff/tif_jpeg.c
+index dc4364c821a4..09ef4949f9ee 100644
+--- a/libtiff/tif_jpeg.c
++++ b/libtiff/tif_jpeg.c
+@@ -1626,6 +1626,13 @@ JPEGSetupEncode(TIFF* tif)
+ 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);
++ }
++
+ /*
+ * A ReferenceBlackWhite field *must* be present since the
+ * default value is inappropriate for YCbCr. Fill in the
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2658.patch b/media-libs/tiff/files/tiff-4.0.7-bug2658.patch
new file mode 100644
index 000000000000..9f2bb6a50ee2
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2658.patch
@@ -0,0 +1,33 @@
+From 928f0b0b2881ac32b32d9e165e88e3c9aed0fb9c Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Wed, 11 Jan 2017 16:38:26 +0000
+Subject: [PATCH] =?UTF-8?q?*=20libtiff/tif=5Fgetimage.c:=20add=20explicit?=
+ =?UTF-8?q?=20uint32=20cast=20in=20putagreytile=20to=20avoid=20UndefinedBe?=
+ =?UTF-8?q?haviorSanitizer=20warning.=20Patch=20by=20Nicol=C3=A1s=20Pe?=
+ =?UTF-8?q?=C3=B1a.=20Fixes=20http://bugzilla.maptools.org/show=5Fbug.cgi?=
+ =?UTF-8?q?=3Fid=3D2658?=
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+---
+ ChangeLog | 7 +++++++
+ libtiff/tif_getimage.c | 4 ++--
+ 2 files changed, 9 insertions(+), 2 deletions(-)
+
+diff --git a/libtiff/tif_getimage.c b/libtiff/tif_getimage.c
+index 2ea838556732..52a2402f7171 100644
+--- a/libtiff/tif_getimage.c
++++ b/libtiff/tif_getimage.c
+@@ -1302,7 +1302,7 @@ DECLAREContigPutFunc(putagreytile)
+ while (h-- > 0) {
+ for (x = w; x-- > 0;)
+ {
+- *cp++ = BWmap[*pp][0] & (*(pp+1) << 24 | ~A1);
++ *cp++ = BWmap[*pp][0] & ((uint32)*(pp+1) << 24 | ~A1);
+ pp += samplesperpixel;
+ }
+ cp += toskew;
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2659-2.patch b/media-libs/tiff/files/tiff-4.0.7-bug2659-2.patch
new file mode 100644
index 000000000000..539536fe4ffb
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2659-2.patch
@@ -0,0 +1,41 @@
+From 41236c5f744eaa691e23e55f5a5dd556a65e211e Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Thu, 12 Jan 2017 19:23:20 +0000
+Subject: [PATCH] * libtiff/tif_ojpeg.c: fix leak in
+ OJPEGReadHeaderInfoSecTablesQTable, OJPEGReadHeaderInfoSecTablesDcTable and
+ OJPEGReadHeaderInfoSecTablesAcTable
+
+---
+ ChangeLog | 3 ++-
+ libtiff/tif_ojpeg.c | 8 +++++++-
+ 2 files changed, 9 insertions(+), 2 deletions(-)
+
+diff --git a/libtiff/tif_ojpeg.c b/libtiff/tif_ojpeg.c
+index ac70d1b14c4f..bd4cff5d8921 100644
+--- a/libtiff/tif_ojpeg.c
++++ b/libtiff/tif_ojpeg.c
+@@ -1790,7 +1790,10 @@ OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
+ TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
+ p=(uint32)TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
+ if (p!=64)
++ {
++ _TIFFfree(ob);
+ return(0);
++ }
+ sp->qtable[m]=ob;
+ sp->sof_tq[m]=m;
+ }
+@@ -1854,7 +1857,10 @@ OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
+ rb[sizeof(uint32)+5+n]=o[n];
+ p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+ if (p!=q)
++ {
++ _TIFFfree(rb);
+ return(0);
++ }
+ sp->dctable[m]=rb;
+ sp->sos_tda[m]=(m<<4);
+ }
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2659.patch b/media-libs/tiff/files/tiff-4.0.7-bug2659.patch
new file mode 100644
index 000000000000..8afab46b888f
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2659.patch
@@ -0,0 +1,34 @@
+From 7c501dbfb5315f31798f9123026210260cbe7432 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Thu, 12 Jan 2017 17:43:25 +0000
+Subject: [PATCH] =?UTF-8?q?*=20libtiff/tif=5Fojpeg.c:=20fix=20leak=20in=20?=
+ =?UTF-8?q?OJPEGReadHeaderInfoSecTablesAcTable=20when=20read=20fails.=20Pa?=
+ =?UTF-8?q?tch=20by=20Nicol=C3=A1s=20Pe=C3=B1a.=20Fixes=20http://bugzilla.?=
+ =?UTF-8?q?maptools.org/show=5Fbug.cgi=3Fid=3D2659?=
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+---
+ ChangeLog | 7 +++++++
+ libtiff/tif_ojpeg.c | 5 ++++-
+ 2 files changed, 11 insertions(+), 1 deletion(-)
+
+diff --git a/libtiff/tif_ojpeg.c b/libtiff/tif_ojpeg.c
+index 93839d8f3e11..ac70d1b14c4f 100644
+--- a/libtiff/tif_ojpeg.c
++++ b/libtiff/tif_ojpeg.c
+@@ -1918,7 +1918,10 @@ OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
+ rb[sizeof(uint32)+5+n]=o[n];
+ p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+ if (p!=q)
++ {
++ _TIFFfree(rb);
+ return(0);
++ }
+ sp->actable[m]=rb;
+ sp->sos_tda[m]=(sp->sos_tda[m]|m);
+ }
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-bug2665.patch b/media-libs/tiff/files/tiff-4.0.7-bug2665.patch
new file mode 100644
index 000000000000..020adca8e7aa
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-bug2665.patch
@@ -0,0 +1,43 @@
+From e345ce2ad81c85eb8e469b7b959067b2681957ca Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Sat, 18 Feb 2017 20:30:26 +0000
+Subject: [PATCH] =?UTF-8?q?*=20libtiff/tif=5Fpixarlog.c:=20fix=20memory=20?=
+ =?UTF-8?q?leak=20in=20error=20code=20path=20of=20PixarLogSetupDecode().?=
+ =?UTF-8?q?=20Patch=20by=20Nicol=C3=A1s=20Pe=C3=B1a.=20Fixes=20http://bugz?=
+ =?UTF-8?q?illa.maptools.org/show=5Fbug.cgi=3Fid=3D2665?=
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+---
+ ChangeLog | 6 ++++++
+ libtiff/tif_pixarlog.c | 8 +++++++-
+ 2 files changed, 13 insertions(+), 1 deletion(-)
+
+diff --git a/libtiff/tif_pixarlog.c b/libtiff/tif_pixarlog.c
+index 9836dce63450..972ee75e0324 100644
+--- a/libtiff/tif_pixarlog.c
++++ b/libtiff/tif_pixarlog.c
+@@ -699,6 +699,9 @@ PixarLogSetupDecode(TIFF* tif)
+ 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);
+@@ -706,6 +709,9 @@ PixarLogSetupDecode(TIFF* tif)
+ }
+
+ 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 {
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-fax2tiff.patch b/media-libs/tiff/files/tiff-4.0.7-fax2tiff.patch
new file mode 100644
index 000000000000..f3476e98d6ff
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-fax2tiff.patch
@@ -0,0 +1,39 @@
+https://bugs.gentoo.org/598938
+
+From 82c53c6f19d8d7854b9b88aa16802f31b1cc258c Mon Sep 17 00:00:00 2001
+From: Bob Friesenhahn <bfriesen@simple.dallas.tx.us>
+Date: Sun, 20 Nov 2016 18:04:52 +0000
+Subject: [PATCH] =?UTF-8?q?*=20tools/fax2tiff.c=20(main):=20Applied=20patc?=
+ =?UTF-8?q?h=20by=20J=C3=B6rg=20Ahrens=20to=20fix=20passing=20client=20dat?=
+ =?UTF-8?q?a=20for=20Win32=20builds=20using=20tif=5Fwin32.c=20(USE=5FWIN32?=
+ =?UTF-8?q?=5FFILEIO=20defined)=20for=20file=20I/O.=20=20Patch=20was=20pro?=
+ =?UTF-8?q?vided=20via=20email=20on=20November=2020,=202016.?=
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+---
+ ChangeLog | 7 +++++++
+ tools/fax2tiff.c | 5 +++--
+ 2 files changed, 10 insertions(+), 2 deletions(-)
+
+diff --git a/tools/fax2tiff.c b/tools/fax2tiff.c
+index e00de5277bc2..01f85540ac9c 100644
+--- a/tools/fax2tiff.c
++++ b/tools/fax2tiff.c
+@@ -283,10 +283,11 @@ main(int argc, char* argv[])
+ }
+ #if defined(_WIN32) && defined(USE_WIN32_FILEIO)
+ client_data.fh = _get_osfhandle(fileno(in));
++ TIFFSetClientdata(faxTIFF, (thandle_t) client_data.fh);
+ #else
+ client_data.fd = fileno(in);
++ TIFFSetClientdata(faxTIFF, (thandle_t) client_data.fd);
+ #endif
+- TIFFSetClientdata(faxTIFF, (thandle_t) &client_data);
+ TIFFSetFileName(faxTIFF, (const char*)argv[optind]);
+ TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
+ TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-hylafax-hack.patch b/media-libs/tiff/files/tiff-4.0.7-hylafax-hack.patch
new file mode 100644
index 000000000000..69158200ac73
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-hylafax-hack.patch
@@ -0,0 +1,38 @@
+https://bugs.gentoo.org/612172
+
+From 96bb01f5d834e0b01c0231768c43b8d309aede34 Mon Sep 17 00:00:00 2001
+From: Even Rouault <even.rouault@spatialys.com>
+Date: Tue, 13 Dec 2016 18:15:48 +0000
+Subject: [PATCH] * libtiff/tif_fax3.h: revert change done on 2016-01-09 that
+ made Param member of TIFFFaxTabEnt structure a uint16 to reduce size of the
+ binary. It happens that the Hylafax software uses the tables that follow this
+ typedef (TIFFFaxMainTable, TIFFFaxWhiteTable, TIFFFaxBlackTable), also they
+ are not in a public libtiff header. Raised by Lee Howard. Fixes
+ http://bugzilla.maptools.org/show_bug.cgi?id=2636
+
+---
+ ChangeLog | 10 ++++++++++
+ libtiff/tif_fax3.h | 6 ++++--
+ 2 files changed, 14 insertions(+), 2 deletions(-)
+
+diff --git a/libtiff/tif_fax3.h b/libtiff/tif_fax3.h
+index e0b2ca6bfc9d..45ce43f1cf2e 100644
+--- a/libtiff/tif_fax3.h
++++ b/libtiff/tif_fax3.h
+@@ -81,10 +81,12 @@ extern void _TIFFFax3fillruns(unsigned char*, uint32*, uint32*, uint32);
+ #define S_MakeUp 11
+ #define S_EOL 12
+
++/* WARNING: do not change the layout of this structure as the Halyfax 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 */
+- uint16 Param; /* unsigned 16-bit run length 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[];
+--
+2.12.0
+
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0005-Leak-TIFFFetchStripThing.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0005-Leak-TIFFFetchStripThing.patch
new file mode 100644
index 000000000000..afb0151f9632
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0005-Leak-TIFFFetchStripThing.patch
@@ -0,0 +1,20 @@
+https://codereview.chromium.org/2204793002
+https://crbug.com/633387
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: thestig <thestig@chromium.org>
+Date: Mon Aug 1 19:36:27 2016 -0700
+
+Fix a memory leak in libtiff.
+
+--- a/libtiff/tif_dirread.c
++++ b/libtiff/tif_dirread.c
+@@ -5372,6 +5372,8 @@ TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
+ static const char module[] = "TIFFFetchStripThing";
+ enum TIFFReadDirEntryErr err;
+ uint64* data;
++ _TIFFfree(*lpp);
++ *lpp = 0;
+ err=TIFFReadDirEntryLong8Array(tif,dir,&data);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0006-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0006-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
new file mode 100644
index 000000000000..a45ee342f779
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0006-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
@@ -0,0 +1,33 @@
+https://codereview.chromium.org/2284063002
+https://crbug.com/618267
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: tracy_jiang <tracy_jiang@foxitsoftware.com>
+Date: Mon Aug 29 13:42:56 2016 -0700
+
+Fix for #618267. Adding a method to determine if multiplication has
+overflow.
+
+--- a/libtiff/tif_aux.c
++++ b/libtiff/tif_aux.c
+@@ -69,7 +69,7 @@ _TIFFCheckRealloc(TIFF* tif, void* buffer,
+ /*
+ * XXX: Check for integer overflow.
+ */
+- if (nmemb && elem_size && bytes / elem_size == nmemb)
++ if (nmemb && elem_size && !_TIFFIfMultiplicationOverflow(nmemb, elem_size))
+ cp = _TIFFrealloc(buffer, bytes);
+
+ if (cp == NULL) {
+--- a/libtiff/tiffiop.h
++++ b/libtiff/tiffiop.h
+@@ -315,6 +315,9 @@ typedef size_t TIFFIOSize_t;
+ #define _TIFF_off_t off_t
+ #endif
+
++#include <limits.h>
++#define _TIFFIfMultiplicationOverflow(op1, op2) ((op1) > SSIZE_MAX / (op2))
++
+ #if defined(__cplusplus)
+ extern "C" {
+ #endif
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0007-uninitialized-value.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0007-uninitialized-value.patch
new file mode 100644
index 000000000000..ab5627f5c169
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0007-uninitialized-value.patch
@@ -0,0 +1,26 @@
+https://codereview.chromium.org/2389993002
+https://crbug.com/651632
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: dsinclair <dsinclair@chromium.org>
+Date: Mon Oct 3 13:59:57 2016 -0700
+
+Fix potentially uninitialized value.
+
+Depending on what ReadOK does it's possible for |dircount16| to be used without
+being initialized. The read code calls back into PDFium specific code which then
+calls into the stream reading code.
+
+Initialize the value to be sure it is set.
+
+--- a/libtiff/tif_dirread.c
++++ b/libtiff/tif_dirread.c
+@@ -4443,7 +4443,7 @@ TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
+ static const char module[] = "TIFFFetchDirectory";
+
+ void* origdir;
+- uint16 dircount16;
++ uint16 dircount16 = 0;
+ uint32 dirsize;
+ TIFFDirEntry* dir;
+ uint8* ma;
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0008-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0008-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
new file mode 100644
index 000000000000..35f59b9bffd9
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0008-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
@@ -0,0 +1,26 @@
+https://codereview.chromium.org/2405693002
+https://crbug.com/654169
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: stackexploit <stackexploit@gmail.com>
+Date: Mon Oct 10 10:58:25 2016 -0700
+
+libtiff: Prevent a buffer overflow in function ChopUpSingleUncompressedStrip.
+
+The patch (https://codereview.chromium.org/2284063002) for Issue 618267
+was insufficient. The integer overflow still could be triggered and could
+lead to heap buffer overflow.
+
+This CL strengthens integer overflow check in function _TIFFCheckRealloc.
+
+--- a/libtiff/tif_aux.c
++++ b/libtiff/tif_aux.c
+@@ -69,7 +69,7 @@ _TIFFCheckRealloc(TIFF* tif, void* buffer,
+ /*
+ * XXX: Check for integer overflow.
+ */
+- if (nmemb && elem_size && !_TIFFIfMultiplicationOverflow(nmemb, elem_size))
++ if (nmemb > 0 && elem_size > 0 && !_TIFFIfMultiplicationOverflow(nmemb, elem_size))
+ cp = _TIFFrealloc(buffer, bytes);
+
+ if (cp == NULL) {
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0013-validate-refblackwhite.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0013-validate-refblackwhite.patch
new file mode 100644
index 000000000000..d98ff9d0f252
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0013-validate-refblackwhite.patch
@@ -0,0 +1,42 @@
+https://pdfium-review.googlesource.com/2151
+https://crbug.com/632883
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: Dan Sinclair <dsinclair@chromium.org>
+Date: Mon Jan 9 09:50:50 2017 -0500
+
+[libtiff] Validate refblackwhite values
+
+The td_refblackwhite value is currently assigned without validation. This
+may pose an issue as the image can specify the value as nan. This will cause
+problems later when we use the nan in calcluations.
+
+This CL validates each of the float values are not nan and if they are sets
+them to the default provided by the TIFF spec v6.
+
+--- a/libtiff/tif_dir.c
++++ b/libtiff/tif_dir.c
+@@ -31,6 +31,7 @@
+ * (and also some miscellaneous stuff)
+ */
+ #include "tiffiop.h"
++#include <math.h>
+ #include <float.h>
+
+ /*
+@@ -426,6 +426,15 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
+ case TIFFTAG_REFERENCEBLACKWHITE:
+ /* XXX should check for null range */
+ _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
++ int i;
++ for (i = 0; i < 6; i++) {
++ if (isnan(td->td_refblackwhite[i])) {
++ if (i % 2 == 0)
++ td->td_refblackwhite[i] = 0;
++ else
++ td->td_refblackwhite[i] = pow(2, td->td_bitspersample) - 1;
++ }
++ }
+ break;
+ case TIFFTAG_INKNAMES:
+ v = (uint16) va_arg(ap, uint16_vap);
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0017-safe_skews_in_gtTileContig.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0017-safe_skews_in_gtTileContig.patch
new file mode 100644
index 000000000000..7914743393ff
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0017-safe_skews_in_gtTileContig.patch
@@ -0,0 +1,97 @@
+https://pdfium-review.googlesource.com/2355
+https://crbug.com/681300
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: Nicolas Pena <npm@chromium.org>
+Date: Wed Jan 25 10:41:06 2017 -0500
+
+Prevent skew overflows in gtTileContig
+
+Using int64 to check whether uint32 operations have overflowed.
+
+--- a/libtiff/tif_getimage.c
++++ b/libtiff/tif_getimage.c
+@@ -31,6 +31,7 @@
+ */
+ #include "tiffiop.h"
+ #include <stdio.h>
++#include <limits.h>
+
+ static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
+ static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
+@@ -629,6 +628,7 @@ gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+ uint32 tw, th;
+ unsigned char* buf;
+ int32 fromskew, toskew;
++ int64 safeskew;
+ uint32 nrow;
+ int ret = 1, flip;
+ uint32 this_tw, tocol;
+@@ -649,19 +647,37 @@ gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+- toskew = -(int32)(tw + w);
++ safeskew = 0;
++ safeskew -= tw;
++ safeskew -= w;
+ }
+ else {
+ y = 0;
+- toskew = -(int32)(tw - w);
++ safeskew = 0;
++ safeskew -= tw;
++ safeskew +=w;
+ }
+
++ if(safeskew > INT_MAX || safeskew < INT_MIN){
++ _TIFFfree(buf);
++ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "Invalid skew");
++ return (0);
++ }
++ toskew = safeskew;
++
+ /*
+ * 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;
++ safeskew = toskew;
++ safeskew += leftmost_fromskew;
++ if(safeskew > INT_MAX || safeskew < INT_MIN){
++ _TIFFfree(buf);
++ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "Invalid skew");
++ return (0);
++ }
++ leftmost_toskew = safeskew;
+ for (row = 0; row < h; row += nrow)
+ {
+ rowstoread = th - (row + img->row_offset) % th;
+@@ -704,9 +684,24 @@ gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+ /*
+ * Rightmost tile is clipped on right side.
+ */
+- fromskew = tw - (w - tocol);
++ safeskew = tw;
++ safeskew -= w;
++ safeskew += tocol;
++ if(safeskew > INT_MAX || safeskew < INT_MIN){
++ _TIFFfree(buf);
++ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "Invalid skew");
++ return (0);
++ }
++ fromskew = safeskew;
+ this_tw = tw - fromskew;
+- this_toskew = toskew + fromskew;
++ safeskew = toskew;
++ safeskew += fromskew;
++ if(safeskew > INT_MAX || safeskew < INT_MIN){
++ _TIFFfree(buf);
++ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "Invalid skew");
++ return (0);
++ }
++ this_toskew = safeskew;
+ }
+ (*put)(img, raster+y*w+tocol, tocol, y, this_tw, nrow, fromskew, this_toskew, buf + pos);
+ tocol += this_tw;
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0018-fix-leak-in-PredictorSetupDecode.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0018-fix-leak-in-PredictorSetupDecode.patch
new file mode 100644
index 000000000000..b74ddc5c499c
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0018-fix-leak-in-PredictorSetupDecode.patch
@@ -0,0 +1,27 @@
+https://pdfium-review.googlesource.com/2432
+https://crbug.com/683834
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: Nicolas Pena <npm@chromium.org>
+Date: Thu Jan 26 15:45:02 2017 -0500
+
+Fix leak in PredictorSetupDecode by calling tif_cleanup on failure
+
+tif_data and tif_cleanup are both set on the TIFFInit methods, see for
+instance TIFFInitPixarLog. If PredictorSetupDecode fails, whatever was
+filled on tif_data should be cleaned up. The previous leak fix from
+PixarLogSetupDecode is no longer necessary.
+
+--- a/libtiff/tif_predict.c
++++ b/libtiff/tif_predict.c
+@@ -118,7 +118,10 @@ PredictorSetupDecode(TIFF* tif)
+ TIFFDirectory* td = &tif->tif_dir;
+
+ if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
++ {
++ (*tif->tif_cleanup)(tif);
+ return 0;
++ }
+
+ if (sp->predictor == 2) {
+ switch (td->td_bitspersample) {
diff --git a/media-libs/tiff/files/tiff-4.0.7-pdfium-0021-oom-TIFFFillStrip.patch b/media-libs/tiff/files/tiff-4.0.7-pdfium-0021-oom-TIFFFillStrip.patch
new file mode 100644
index 000000000000..11975cf38acf
--- /dev/null
+++ b/media-libs/tiff/files/tiff-4.0.7-pdfium-0021-oom-TIFFFillStrip.patch
@@ -0,0 +1,29 @@
+https://pdfium-review.googlesource.com/3811
+https://crbug.com/707431
+https://pdfium.googlesource.com/pdfium/+/master/libtiff/
+
+Author: Nicolas Pena <npm@chromium.org>
+Date: Wed Apr 5 15:50:53 2017 -0400
+
+Libtiff: Prevent OOM in TIFFFillStrip
+
+In TIFFFillStrip, calls to TIFFReadBufferSetup may allocate large amounts of
+memory. In this CL we do sanity checks on the claimed size of the raw strip
+data before that happens, to prevent out-of-memory.
+
+--- a/libtiff/tif_read.c
++++ b/libtiff/tif_read.c
+@@ -616,6 +616,13 @@ TIFFFillStrip(TIFF* tif, uint32 strip)
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ return(0);
+ }
++ const tmsize_t size=isMapped(tif)? tif->tif_size : (tmsize_t)TIFFGetFileSize(tif);
++ if (bytecountm > size) {
++ TIFFErrorExt(tif->tif_clientdata, module,
++ "Requested read strip size %lu is too large",
++ (unsigned long) strip);
++ return (0);
++ }
+ if (bytecountm > tif->tif_rawdatasize) {
+ tif->tif_curstrip = NOSTRIP;
+ if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
diff --git a/media-libs/tiff/metadata.xml b/media-libs/tiff/metadata.xml
new file mode 100644
index 000000000000..1844a8c19073
--- /dev/null
+++ b/media-libs/tiff/metadata.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">
+<pkgmetadata>
+<maintainer type="project">
+ <email>graphics@gentoo.org</email>
+ <name>Gentoo Graphics Project</name>
+</maintainer>
+<longdescription>TIFF image library and tools, mostly for scanner and fax
+ support, but also used by GIS and other imaging tools.</longdescription>
+<upstream>
+ <remote-id type="cpe">cpe:/a:libtiff:libtiff</remote-id>
+ <remote-id type="cpe">cpe:/a:libtiff_project:libtiff</remote-id>
+</upstream>
+</pkgmetadata>
diff --git a/media-libs/tiff/tiff-3.9.7-r1.ebuild b/media-libs/tiff/tiff-3.9.7-r1.ebuild
new file mode 100644
index 000000000000..66315663f5fe
--- /dev/null
+++ b/media-libs/tiff/tiff-3.9.7-r1.ebuild
@@ -0,0 +1,71 @@
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=5
+
+# this ebuild is only for the libtiff.so.3 (+ 4) and libtiffxx.so.3 (+ 4) SONAME for ABI compat
+
+inherit eutils libtool multilib multilib-minimal
+
+DESCRIPTION="Library for manipulation of TIFF (Tag Image File Format) images"
+HOMEPAGE="http://libtiff.maptools.org"
+SRC_URI="ftp://ftp.remotesensing.org/pub/libtiff/${P}.tar.gz"
+
+LICENSE="libtiff"
+SLOT="3"
+KEYWORDS="~alpha amd64 ~arm arm64 ~hppa ~ia64 m68k ~mips ~ppc ~ppc64 s390 sh ~sparc x86 ~sparc-fbsd ~x86-fbsd ~amd64-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~sparc-solaris ~x64-solaris ~x86-solaris"
+IUSE="+cxx jbig jpeg zlib"
+
+RDEPEND="jpeg? ( >=virtual/jpeg-0-r2[${MULTILIB_USEDEP}] )
+ jbig? ( >=media-libs/jbigkit-2.1[${MULTILIB_USEDEP}] )
+ zlib? ( >=sys-libs/zlib-1.2.8-r1[${MULTILIB_USEDEP}] )
+ !media-libs/tiff-compat
+ !=media-libs/tiff-3*:0
+ abi_x86_32? (
+ !<=app-emulation/emul-linux-x86-baselibs-20130224-r10
+ !app-emulation/emul-linux-x86-baselibs[-abi_x86_32(-)]
+ )"
+DEPEND="${RDEPEND}"
+
+src_prepare() {
+ epatch \
+ "${FILESDIR}"/${P}-CVE-2012-{4447,4564,5581}.patch \
+ "${FILESDIR}"/${P}-tiffinfo-exif.patch \
+ "${FILESDIR}"/${P}-printdir-width.patch
+
+ elibtoolize
+}
+
+multilib_src_configure() {
+ ECONF_SOURCE="${S}" econf \
+ --libdir=/libdir \
+ --disable-static \
+ $(use_enable cxx) \
+ $(use_enable zlib) \
+ $(use_enable jpeg) \
+ $(use_enable jbig) \
+ --without-x
+}
+
+multilib_src_install() {
+ # Let `make install` and libtool handle insecure runpath(s)
+ dodir tmp
+ emake DESTDIR="${ED}/tmp" install
+
+ # .so.3 (upstream) is used by sci-chemistry/icm
+ # .so.4 (Debian) is used by net-im/skype
+ exeinto /usr/$(get_libdir)
+ doexe "${ED}"/tmp/libdir/libtiff$(get_libname 3)
+ dosym libtiff$(get_libname 3) /usr/$(get_libdir)/libtiff$(get_libname 4)
+ if use cxx; then
+ doexe "${ED}"/tmp/libdir/libtiffxx$(get_libname 3)
+ dosym libtiffxx$(get_libname 3) /usr/$(get_libdir)/libtiffxx$(get_libname 4)
+ fi
+
+ rm -rf "${ED}"/tmp
+}
+
+multilib_src_install_all() {
+ # (avoid installing docs)
+ :
+}
diff --git a/media-libs/tiff/tiff-4.0.7-r3.ebuild b/media-libs/tiff/tiff-4.0.7-r3.ebuild
new file mode 100644
index 000000000000..b77d5196171d
--- /dev/null
+++ b/media-libs/tiff/tiff-4.0.7-r3.ebuild
@@ -0,0 +1,121 @@
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI="6"
+inherit autotools eutils libtool multilib-minimal
+
+DESCRIPTION="Tag Image File Format (TIFF) library"
+HOMEPAGE="http://libtiff.maptools.org"
+SRC_URI="http://download.osgeo.org/libtiff/${P}.tar.gz
+ ftp://ftp.remotesensing.org/pub/libtiff/${P}.tar.gz"
+
+LICENSE="libtiff"
+SLOT="0"
+KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~m68k ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~x64-cygwin ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~amd64-linux ~arm-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~m68k-mint ~sparc-solaris ~x64-solaris ~x86-solaris"
+IUSE="+cxx jbig jpeg lzma static-libs test zlib"
+
+RDEPEND="jpeg? ( >=virtual/jpeg-0-r2:0=[${MULTILIB_USEDEP}] )
+ jbig? ( >=media-libs/jbigkit-2.1:=[${MULTILIB_USEDEP}] )
+ lzma? ( >=app-arch/xz-utils-5.0.5-r1:=[${MULTILIB_USEDEP}] )
+ zlib? ( >=sys-libs/zlib-1.2.8-r1:=[${MULTILIB_USEDEP}] )
+ abi_x86_32? (
+ !<=app-emulation/emul-linux-x86-baselibs-20130224-r9
+ !app-emulation/emul-linux-x86-baselibs[-abi_x86_32(-)]
+ )"
+DEPEND="${RDEPEND}"
+
+REQUIRED_USE="test? ( jpeg )" #483132
+
+PATCHES=(
+ "${FILESDIR}"/${P}-fax2tiff.patch #598938
+ "${FILESDIR}"/${P}-CVE-2016-10266.patch
+ "${FILESDIR}"/${P}-bug2598.patch
+ "${FILESDIR}"/${P}-bug2604.patch
+ "${FILESDIR}"/${P}-bug2608.patch
+ "${FILESDIR}"/${P}-CVE-2016-10267.patch
+ "${FILESDIR}"/${P}-bug2620.patch
+ "${FILESDIR}"/${P}-bug2621.patch
+ "${FILESDIR}"/${P}-bug2619.patch
+ "${FILESDIR}"/${P}-bug2594.patch
+ "${FILESDIR}"/${P}-bug2597.patch
+ "${FILESDIR}"/${P}-bug2599.patch
+ "${FILESDIR}"/${P}-bug2607.patch
+ "${FILESDIR}"/${P}-bug2610.patch
+ "${FILESDIR}"/${P}-bug2605.patch
+ "${FILESDIR}"/${P}-hylafax-hack.patch #612172
+ "${FILESDIR}"/${P}-bug2633-bug2634.patch
+ "${FILESDIR}"/${P}-bug2635.patch
+ "${FILESDIR}"/${P}-bug2627.patch
+ "${FILESDIR}"/${P}-bug2638.patch
+ "${FILESDIR}"/${P}-bug2639.patch
+ "${FILESDIR}"/${P}-bug2640.patch
+ "${FILESDIR}"/${P}-bug2653.patch
+ "${FILESDIR}"/${P}-bug2535.patch
+ "${FILESDIR}"/${P}-bug2644.patch
+ "${FILESDIR}"/${P}-bug2642-bug2643-bug2646-bug2647.patch
+ "${FILESDIR}"/${P}-bug2648.patch
+ "${FILESDIR}"/${P}-bug2650.patch
+ "${FILESDIR}"/${P}-bug2658.patch
+ "${FILESDIR}"/${P}-bug2650-2.patch
+ "${FILESDIR}"/${P}-bug2651.patch
+ "${FILESDIR}"/${P}-CVE-2017-5225.patch #610330
+ "${FILESDIR}"/${P}-bug2130.patch
+ "${FILESDIR}"/${P}-bug2659.patch
+ "${FILESDIR}"/${P}-bug2659-2.patch
+ "${FILESDIR}"/${P}-bug2631.patch
+ "${FILESDIR}"/${P}-bug2665.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0005-Leak-TIFFFetchStripThing.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0006-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0007-uninitialized-value.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0008-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0013-validate-refblackwhite.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0017-safe_skews_in_gtTileContig.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0018-fix-leak-in-PredictorSetupDecode.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0021-oom-TIFFFillStrip.patch
+)
+
+MULTILIB_WRAPPED_HEADERS=(
+ /usr/include/tiffconf.h
+)
+
+src_prepare() {
+ default
+
+ # tiffcp-thumbnail.sh fails as thumbnail binary doesn't get built anymore since tiff-4.0.7
+ sed '/tiffcp-thumbnail\.sh/d' -i test/Makefile.am || die
+
+ eautoreconf
+}
+
+multilib_src_configure() {
+ ECONF_SOURCE="${S}" econf \
+ $(use_enable static-libs static) \
+ $(use_enable zlib) \
+ $(use_enable jpeg) \
+ $(use_enable jbig) \
+ $(use_enable lzma) \
+ $(use_enable cxx) \
+ --without-x
+
+ # remove useless subdirs
+ if ! multilib_is_native_abi ; then
+ sed -i \
+ -e 's/ tools//' \
+ -e 's/ contrib//' \
+ -e 's/ man//' \
+ -e 's/ html//' \
+ Makefile || die
+ fi
+}
+
+multilib_src_test() {
+ if ! multilib_is_native_abi ; then
+ emake -C tools
+ fi
+ emake check
+}
+
+multilib_src_install_all() {
+ prune_libtool_files --all
+ rm -f "${ED}"/usr/share/doc/${PF}/{COPYRIGHT,README*,RELEASE-DATE,TODO,VERSION}
+}
diff --git a/media-libs/tiff/tiff-4.0.7.ebuild b/media-libs/tiff/tiff-4.0.7.ebuild
new file mode 100644
index 000000000000..59be2b40a56c
--- /dev/null
+++ b/media-libs/tiff/tiff-4.0.7.ebuild
@@ -0,0 +1,73 @@
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=6
+inherit autotools eutils libtool multilib-minimal
+
+DESCRIPTION="Tag Image File Format (TIFF) library"
+HOMEPAGE="http://libtiff.maptools.org"
+SRC_URI="http://download.osgeo.org/libtiff/${P}.tar.gz
+ ftp://ftp.remotesensing.org/pub/libtiff/${P}.tar.gz"
+
+LICENSE="libtiff"
+SLOT="0"
+KEYWORDS="alpha amd64 arm arm64 hppa ia64 m68k ~mips ppc ppc64 s390 sh sparc x86 ~x64-cygwin ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~amd64-linux ~arm-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~m68k-mint ~sparc-solaris ~x64-solaris ~x86-solaris"
+IUSE="+cxx jbig jpeg lzma static-libs test zlib"
+
+RDEPEND="jpeg? ( >=virtual/jpeg-0-r2:0=[${MULTILIB_USEDEP}] )
+ jbig? ( >=media-libs/jbigkit-2.1:=[${MULTILIB_USEDEP}] )
+ lzma? ( >=app-arch/xz-utils-5.0.5-r1:=[${MULTILIB_USEDEP}] )
+ zlib? ( >=sys-libs/zlib-1.2.8-r1:=[${MULTILIB_USEDEP}] )
+ abi_x86_32? (
+ !<=app-emulation/emul-linux-x86-baselibs-20130224-r9
+ !app-emulation/emul-linux-x86-baselibs[-abi_x86_32(-)]
+ )"
+DEPEND="${RDEPEND}"
+
+REQUIRED_USE="test? ( jpeg )" #483132
+
+MULTILIB_WRAPPED_HEADERS=(
+ /usr/include/tiffconf.h
+)
+
+src_prepare() {
+ default
+
+ # tiffcp-thumbnail.sh fails as thumbnail binary doesn't get built anymore since tiff-4.0.7
+ sed '/tiffcp-thumbnail\.sh/d' -i test/Makefile.am || die
+
+ eautoreconf
+}
+
+multilib_src_configure() {
+ ECONF_SOURCE="${S}" econf \
+ $(use_enable static-libs static) \
+ $(use_enable zlib) \
+ $(use_enable jpeg) \
+ $(use_enable jbig) \
+ $(use_enable lzma) \
+ $(use_enable cxx) \
+ --without-x
+
+ # remove useless subdirs
+ if ! multilib_is_native_abi ; then
+ sed -i \
+ -e 's/ tools//' \
+ -e 's/ contrib//' \
+ -e 's/ man//' \
+ -e 's/ html//' \
+ Makefile || die
+ fi
+}
+
+multilib_src_test() {
+ if ! multilib_is_native_abi ; then
+ emake -C tools
+ fi
+ emake check
+}
+
+multilib_src_install_all() {
+ prune_libtool_files --all
+ rm -f "${ED}"/usr/share/doc/${PF}/{COPYRIGHT,README*,RELEASE-DATE,TODO,VERSION}
+}
diff --git a/media-libs/tiff/tiff-4.0.8.ebuild b/media-libs/tiff/tiff-4.0.8.ebuild
new file mode 100644
index 000000000000..6f8049841afe
--- /dev/null
+++ b/media-libs/tiff/tiff-4.0.8.ebuild
@@ -0,0 +1,84 @@
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI="6"
+inherit autotools eutils libtool multilib-minimal
+
+DESCRIPTION="Tag Image File Format (TIFF) library"
+HOMEPAGE="http://libtiff.maptools.org"
+SRC_URI="http://download.osgeo.org/libtiff/${P}.tar.gz
+ ftp://ftp.remotesensing.org/pub/libtiff/${P}.tar.gz"
+
+LICENSE="libtiff"
+SLOT="0"
+KEYWORDS="alpha amd64 arm ~arm64 hppa ia64 ~m68k ~mips ppc ppc64 ~s390 ~sh sparc x86 ~x64-cygwin ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~amd64-linux ~arm-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~m68k-mint ~sparc-solaris ~x64-solaris ~x86-solaris"
+IUSE="+cxx jbig jpeg lzma static-libs test zlib"
+
+RDEPEND="jpeg? ( >=virtual/jpeg-0-r2:0=[${MULTILIB_USEDEP}] )
+ jbig? ( >=media-libs/jbigkit-2.1:=[${MULTILIB_USEDEP}] )
+ lzma? ( >=app-arch/xz-utils-5.0.5-r1:=[${MULTILIB_USEDEP}] )
+ zlib? ( >=sys-libs/zlib-1.2.8-r1:=[${MULTILIB_USEDEP}] )
+ abi_x86_32? (
+ !<=app-emulation/emul-linux-x86-baselibs-20130224-r9
+ !app-emulation/emul-linux-x86-baselibs[-abi_x86_32(-)]
+ )"
+DEPEND="${RDEPEND}"
+
+REQUIRED_USE="test? ( jpeg )" #483132
+
+PATCHES=(
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0005-Leak-TIFFFetchStripThing.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0006-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0007-uninitialized-value.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0008-HeapBufferOverflow-ChopUpSingleUncompressedStrip.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0013-validate-refblackwhite.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0017-safe_skews_in_gtTileContig.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0018-fix-leak-in-PredictorSetupDecode.patch
+ "${FILESDIR}"/${PN}-4.0.7-pdfium-0021-oom-TIFFFillStrip.patch
+)
+
+MULTILIB_WRAPPED_HEADERS=(
+ /usr/include/tiffconf.h
+)
+
+src_prepare() {
+ default
+
+ # tiffcp-thumbnail.sh fails as thumbnail binary doesn't get built anymore since tiff-4.0.7
+ sed '/tiffcp-thumbnail\.sh/d' -i test/Makefile.am || die
+
+ eautoreconf
+}
+
+multilib_src_configure() {
+ ECONF_SOURCE="${S}" econf \
+ $(use_enable static-libs static) \
+ $(use_enable zlib) \
+ $(use_enable jpeg) \
+ $(use_enable jbig) \
+ $(use_enable lzma) \
+ $(use_enable cxx) \
+ --without-x
+
+ # remove useless subdirs
+ if ! multilib_is_native_abi ; then
+ sed -i \
+ -e 's/ tools//' \
+ -e 's/ contrib//' \
+ -e 's/ man//' \
+ -e 's/ html//' \
+ Makefile || die
+ fi
+}
+
+multilib_src_test() {
+ if ! multilib_is_native_abi ; then
+ emake -C tools
+ fi
+ emake check
+}
+
+multilib_src_install_all() {
+ prune_libtool_files --all
+ rm -f "${ED}"/usr/share/doc/${PF}/{COPYRIGHT,README*,RELEASE-DATE,TODO,VERSION}
+}