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FpbnQsICAgIC8qIEJTX0RFRlBVU0hCVVRUT04gKi8KICAgIENCX1BhaW50LCAgICAvKiBCU19DSEVDS0JPWCAqLwogICAgQ0JfUGFpbnQsICAgIC8qIEJTX0FVVE9DSEVDS0JPWCAqLwogICAgQ0JfUGFpbnQsICAgIC8qIEJTX1JBRElPQlVUVE9OICovCiAgICBDQl9QYWludCwgICAgLyogQlNfM1NUQVRFICovCiAgICBDQl9QYWludCwgICAgLyogQlNfQVVUTzNTVEFURSAqLwogICAgR0JfUGFpbnQsICAgIC8qIEJTX0dST1VQQk9YICovCiAgICBVQl9QYWludCwgICAgLyogQlNfVVNFUkJVVFRPTiAqLwogICAgQ0JfUGFpbnQsICAgIC8qIEJTX0FVVE9SQURJT0JVVFRPTiAqLwogICAgTlVMTCwgICAgICAgIC8qIE5vdCBkZWZpbmVkICovCiAgICBPQl9QYWludCAgICAgLyogQlNfT1dORVJEUkFXICovCn07CgojZGVmaW5lIFBBSU5UX0JVVFRPTih3bmRQdHIsc3R5bGUsYWN0aW9uKSBcCiAgICAgaWYgKGJ0blBhaW50RnVuY1tzdHlsZV0pIHsgXAogICAgICAgICBIREMgaGRjID0gR2V0REMoICh3bmRQdHIpLT5od25kU2VsZiApOyBcCiAgICAgICAgIChidG5QYWludEZ1bmNbc3R5bGVdKSh3bmRQdHIsaGRjLGFjdGlvbik7IFwKICAgICAgICAgUmVsZWFzZURDKCAod25kUHRyKS0+aHduZFNlbGYsIGhkYyApOyB9CgojZGVmaW5lIEJVVFRPTl9TRU5EX0NUTENPTE9SKHduZFB0cixoZGMpIFwKICAgIFNlbmRNZXNzYWdlQSggR2V0UGFyZW50KCh3bmRQdHIpLT5od25kU2VsZiksIFdNX0NUTENPTE9SQlROLCBcCiAgICAgICAgICAgICAgICAgICAgKGhkYyksICh3bmRQdHIpLT5od25kU2VsZiApCgpzdGF0aWMgSEJJVE1BUCBoYml0bWFwQ2hlY2tCb3hlcyA9IDA7CnN0YXRpYyBXT1JEIGNoZWNrQm94V2lkdGggPSAwLCBjaGVja0JveEhlaWdodCA9IDA7CgoKLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqICAgICAgICAgICBCdXR0b25XbmRQcm9jX2xvY2tlZAogKiAKICogQ2FsbGVkIHdpdGggd2luZG93IGxvY2sgaGVsZC4KICovCnN0YXRpYyBpbmxpbmUgTFJFU1VMVCBXSU5BUEkgQnV0dG9uV25kUHJvY19sb2NrZWQoV05EKiB3bmRQdHIsIFVJTlQgdU1zZywKCQkJCQkgICBXUEFSQU0gd1BhcmFtLCBMUEFSQU0gbFBhcmFtICkKewogICAgUkVDVCByZWN0OwogICAgSFdORAloV25kID0gd25kUHRyLT5od25kU2VsZjsKICAgIFBPSU5UIHB0OwogICAgQlVUVE9OSU5GTyAqaW5mb1B0ciA9IChCVVRUT05JTkZPICopd25kUHRyLT53RXh0cmE7CiAgICBMT05HIHN0eWxlID0gd25kUHRyLT5kd1N0eWxlICYgMHgwZjsKICAgIEhBTkRMRSBvbGRIYml0bWFwOwoKICAgIHB0LnggPSBMT1dPUkQobFBhcmFtKTsKICAgIHB0LnkgPSBISVdPUkQobFBhcmFtKTsKCiAgICBzd2l0Y2ggKHVNc2cpCiAgICB7CiAgICBjYXNlIFdNX0dFVERMR0NPREU6CiAgICAgICAgc3dpdGNoKHN0eWxlKQogICAgICAgIHsKICAgICAgICBjYXNlIEJTX1BVU0hCVVRUT046ICAgICAgcmV0dXJuIERMR0NfQlVUVE9OIHwgRExHQ19VTkRFRlBVU0hCVVRUT047CiAgICAgICAgY2FzZSBCU19ERUZQVVNIQlVUVE9OOiAgIHJldHVybiBETEdDX0JVVFRPTiB8IERMR0NfREVGUFVTSEJVVFRPTjsKICAgICAgICBjYXNlIEJTX1JBRElPQlVUVE9OOgogICAgICAgIGNhc2UgQlNfQVVUT1JBRElPQlVUVE9OOiByZXR1cm4gRExHQ19CVVRUT04gfCBETEdDX1JBRElPQlVUVE9OOwogICAgICAgIGRlZmF1bHQ6ICAgICAgICAgICAgICAgICByZXR1cm4gRExHQ19CVVRUT047CiAgICAgICAgfQoKICAgIGNhc2UgV01fRU5BQkxFOgogICAgICAgIFBBSU5UX0JVVFRPTiggd25kUHRyLCBzdHlsZSwgT0RBX0RSQVdFTlRJUkUgKTsKICAgICAgICBicmVhazsKCiAgICBjYXNlIFdNX0NSRUFURToKICAgICAgICBpZiAoIWhiaXRtYXBDaGVja0JveGVzKQogICAgICAgIHsKICAgICAgICAgICAgQklUTUFQIGJtcDsKICAgICAgICAgICAgaGJpdG1hcENoZWNrQm94ZXMgPSBMb2FkQml0bWFwQSgwLCBNQUtFSU5UUkVTT1VSQ0VBKE9CTV9DSEVDS0JPWEVTKSk7CiAgICAgICAgICAgIEdldE9iamVjdEEoIGhiaXRtYXBDaGVja0JveGVzLCBzaXplb2YoYm1wKSwgJmJtcCApOwogICAgICAgICAgICBjaGVja0JveFdpZHRoICA9IGJtcC5ibVdpZHRoIC8gNDsKICAgICAgICAgICAgY2hlY2tCb3hIZWlnaHQgPSBibXAuYm1IZWlnaHQgLyAzOwogICAgICAgIH0KICAgICAgICBpZiAoc3R5bGUgPCAwTCB8fCBzdHlsZSA+PSBNQVhfQlROX1RZUEUpCiAgICAgICAgICAgIHJldHVybiAtMTsgLyogYWJvcnQgKi8KICAgICAgICBpbmZvUHRyLT5zdGF0ZSA9IEJVVFRPTl9VTkNIRUNLRUQ7CiAgICAgICAgaW5mb1B0ci0+aEZvbnQgPSAwOwogICAgICAgIGluZm9QdHItPmhJbWFnZSA9IDA7CiAgICAgICAgcmV0dXJuIDA7CgogICAgY2FzZSBXTV9FUkFTRUJLR05EOgogICAgICAgIHJldHVybiAxOwoKICAgIGNhc2UgV01fUEFJTlQ6CiAgICAgICAgaWYgKGJ0blBhaW50RnVuY1tzdHlsZV0pCiAgICAgICAgewogICAgICAgICAgICBQQUlOVFNUUlVDVCBwczsKICAgICAgICAgICAgSERDIGhkYyA9IHdQYXJhbSA/IChIREMpd1BhcmFtIDogQmVnaW5QYWludCggaFduZCwgJnBzICk7CiAgICAgICAgICAgIGludCBuT2xkTW9kZSA9IFNldEJrTW9kZSggaGRjLCBPUEFRVUUgKTsKICAgICAgICAgICAgKGJ0blBhaW50RnVuY1tzdHlsZV0pKCB3bmRQdHIsIGhkYywgT0RBX0RSQVdFTlRJUkUgKTsKICAgICAgICAgICAgU2V0QmtNb2RlKGhkYywgbk9sZE1vZGUpOyAvKiAgcmVzZXQgcGFpbnRpbmcgbW9kZSAqLwogICAgICAgICAgICBpZiggIXdQYXJhbSApIEVuZFBhaW50KCBoV25kLCAmcHMgKTsKICAgICAgICB9CiAgICAgICAgYnJlYWs7CgogICAgY2FzZSBXTV9LRVlET1dOOgoJaWYgKHdQYXJhbSA9PSBWS19TUEFDRSkKCXsKCSAgICBTZW5kTWVzc2FnZUEoIGhXbmQsIEJNX1NFVFNUQVRFLCBUUlVFLCAwICk7CgkgICAgaW5mb1B0ci0+c3RhdGUgfD0gQlVUVE9OX0JUTlBSRVNTRUQ7Cgl9CglicmVhazsKCQogICAgY2FzZSBXTV9MQlVUVE9OREJMQ0xLOgogICAgICAgIGlmKHduZFB0ci0+ZHdTdHlsZSAmIEJTX05PVElGWSB8fCAKICAgICAgICAgICAgICAgIHN0eWxlPT1CU19SQURJT0JVVFRPTiB8fAogICAgICAgICAgICAgICAgc3R5bGU9PUJTX1VTRVJCVVRUT04gfHwKICAgICAgICAgICAgICAgIHN0eWxlPT1CU19PV05FUkRSQVcpIHsKICAgICAgICAgICAgU2VuZE1lc3NhZ2VBKCBHZXRQYXJlbnQoaFduZCksIFdNX0NPTU1BTkQsCiAgICAgICAgICAgICAgICAgICAgTUFLRVdQQVJBTSggd25kUHRyLT53SURtZW51LCBCTl9ET1VCTEVDTElDS0VEICksIGhXbmQpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgLyogZmFsbCB0aHJvdWdoICovCiAgICBjYXNlIFdNX0xCVVRUT05ET1dOOgogICAgICAgIFNldENhcHR1cmUoIGhXbmQgKTsKICAgICAgICBTZXRGb2N1cyggaFduZCApOwogICAgICAgIFNlbmRNZXNzYWdlQSggaFduZCwgQk1fU0VUU1RBVEUsIFRSVUUsIDAgKTsKICAgICAgICBpbmZvUHRyLT5zdGF0ZSB8PSBCVVRUT05fQlROUFJFU1NFRDsKICAgICAgICBicmVhazsKCiAgICBjYXNlIFdNX0tFWVVQOgoJaWYgKHdQYXJhbSAhPSBWS19TUEFDRSkKCSAgICBicmVhazsKCS8qIGZhbGwgdGhyb3VnaCAqLwogICAgY2FzZSBXTV9MQlVUVE9OVVA6CiAgICAgICAgaWYgKCEoaW5mb1B0ci0+c3RhdGUgJiBCVVRUT05fQlROUFJFU1NFRCkpIGJyZWFrOwogICAgICAgIGluZm9QdHItPnN0YXRlICY9IEJVVFRPTl9OU1RBVEVTOwogICAgICAgIGlmICghKGluZm9QdHItPnN0YXRlICYgQlVUVE9OX0hJR0hMSUdIVEVEKSkgewogICAgICAgICAgICBSZWxlYXNlQ2FwdHVyZSgpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgU2VuZE1lc3NhZ2VBKCBoV25kLCBCTV9TRVRTVEFURSwgRkFMU0UsIDAgKTsKICAgICAgICBSZWxlYXNlQ2FwdHVyZSgpOwogICAgICAgIEdldENsaWVudFJlY3QoIGhXbmQsICZyZWN0ICk7CglpZiAodU1zZyA9PSBXTV9LRVlVUCB8fCBQdEluUmVjdCggJnJlY3QsIHB0ICkpCiAgICAgICAgewogICAgICAgICAgICBzd2l0Y2goc3R5bGUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgY2FzZSBCU19BVVRPQ0hFQ0tCT1g6CiAgICAgICAgICAgICAgICBTZW5kTWVzc2FnZUEoIGhXbmQsIEJNX1NFVENIRUNLLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICEoaW5mb1B0ci0+c3RhdGUgJiBCVVRUT05fQ0hFQ0tFRCksIDAgKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIEJTX0FVVE9SQURJT0JVVFRPTjoKICAgICAgICAgICAgICAgIFNlbmRNZXNzYWdlQSggaFduZCwgQk1fU0VUQ0hFQ0ssIFRSVUUsIDAgKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIEJTX0FVVE8zU1RBVEU6CiAgICAgICAgICAgICAgICBTZW5kTWVzc2FnZUEoIGhXbmQsIEJNX1NFVENIRUNLLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIChpbmZvUHRyLT5zdGF0ZSAmIEJVVFRPTl8zU1RBVEUpID8gMCA6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKChpbmZvUHRyLT5zdGF0ZSAmIDMpICsgMSksIDAgKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIFNlbmRNZXNzYWdlQSggR2V0UGFyZW50KGhXbmQpLCBXTV9DT01NQU5ELAogICAgICAgICAgICAgICAgICAgICAgICAgICAgTUFLRVdQQVJBTSggd25kUHRyLT53SURtZW51LCBCTl9DTElDS0VEICksIGhXbmQpOwogICAgICAgIH0KICAgICAgICBicmVhazsKCiAgICBjYXNlIFdNX0NBUFRVUkVDSEFOR0VEOgogICAgICAgIGlmIChpbmZvUHRyLT5zdGF0ZSAmIEJVVFRPTl9CVE5QUkVTU0VEKSB7CiAgICAgICAgICAgIGluZm9QdHItPnN0YXRlICY9IEJVVFRPTl9OU1RBVEVTOwogICAgICAgICAgICBpZiAoaW5mb1B0ci0+c3RhdGUgJiBCVVRUT05fSElHSExJR0hURUQpIAogICAgICAgICAgICAgICAgU2VuZE1lc3NhZ2VBKCBoV25kLCBCTV9TRVRTVEFURSwgRkFMU0UsIDAgKTsKICAgICAgICB9CiAgICAgICAgYnJlYWs7CgogICAgY2FzZSBXTV9NT1VTRU1PVkU6CiAgICAgICAgaWYgKEdldENhcHR1cmUoKSA9PSBoV25kKQogICAgICAgIHsKICAgICAgICAgICAgR2V0Q2xpZW50UmVjdCggaFduZCwgJnJlY3QgKTsKICAgICAgICAgICAgU2VuZE1lc3NhZ2VBKCBoV25kLCBCTV9TRVRTVEFURSwgUHRJblJlY3QoJnJlY3QsIHB0KSwgMCApOwogICAgICAgIH0KICAgICAgICBicmVhazsKCiAgICBjYXNlIFdNX05DSElUVEVTVDoKICAgICAgICBpZihzdHlsZSA9PSBCU19HUk9VUEJPWCkgcmV0dXJuIEhUVFJBTlNQQVJFTlQ7CiAgICAgICAgcmV0dXJuIERlZldpbmRvd1Byb2NBKCBoV25kLCB1TXNnLCB3UGFyYW0sIGxQYXJhbSApOwoKICAgIGNhc2UgV01fU0VUVEVYVDoKICAgICAgICBERUZXTkRfU2V0VGV4dEEoIHduZFB0ciwgKExQQ1NUUilsUGFyYW0gKTsKCWlmKCB3bmRQdHItPmR3U3R5bGUgJiBXU19WSVNJQkxFICkKICAgICAgICAgICAgUEFJTlRfQlVUVE9OKCB3bmRQdHIsIHN0eWxlLCBPREFfRFJBV0VOVElSRSApOwogICAgICAgIHJldHVybiAwOwoKICAgIGNhc2UgV01fU0VURk9OVDoKICAgICAgICBpbmZvUHRyLT5oRm9udCA9IChIRk9OVDE2KXdQYXJhbTsKICAgICAgICBpZiAobFBhcmFtICYmICh3bmRQdHItPmR3U3R5bGUgJiBXU19WSVNJQkxFKSkgCgkgICAgUEFJTlRfQlVUVE9OKCB3bmRQdHIsIHN0eWxlLCBPREFfRFJBV0VOVElSRSApOwogICAgICAgIGJyZWFrOwoKICAgIGNhc2UgV01fR0VURk9OVDoKICAgICAgICByZXR1cm4gaW5mb1B0ci0+aEZvbnQ7CgogICAgY2FzZSBXTV9TRVRGT0NVUzoKICAgICAgICBpZiAoKHN0eWxlID09IEJTX1JBRElPQlVUVE9OIHx8IHN0eWxlID09IEJTX0FVVE9SQURJT0JVVFRPTikgJiYgKEdldENhcHR1cmUoKSAhPSBoV25kK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VRTVEFURToKICAgICAgICByZXR1cm4gaW5mb1B0ci0+c3RhdGU7CgogICAgY2FzZSBCTV9TRVRTVEFURTE2OgogICAgY2FzZSBCTV9TRVRTVEFURToKICAgICAgICBpZiAod1BhcmFtKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGluZm9QdHItPnN0YXRlICYgQlVUVE9OX0hJR0hMSUdIVEVEKSBicmVhazsKICAgICAgICAgICAgaW5mb1B0ci0+c3RhdGUgfD0gQlVUVE9OX0hJR0hMSUdIVEVEOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBpZiAoIShpbmZvUHRyLT5zdGF0ZSAmIEJVVFRPTl9ISUdITElHSFRFRCkpIGJyZWFrOwogICAgICAgICAgICBpbmZvUHRyLT5zdGF0ZSAmPSB+QlVUVE9OX0hJR0hMSUdIVEVEOwogICAgICAgIH0KICAgICAgICBQQUlOVF9CVVRUT04oIHduZFB0ciwgc3R5bGUsIE9EQV9TRUxFQ1QgKTsKICAgICAgICBicmVhazsKCiAgICBkZWZhdWx0OgogICAgICAgIHJldHVybiBEZWZXaW5kb3dQcm9jQShoV25kLCB1TXNnLCB3UGFyYW0sIGxQYXJhbSk7CiAgICB9CiAgICByZXR1cm4gMDsKfQoKLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqICAgICAgICAgICBCdXR0b25XbmRQcm9jCiAqIFRoZSBidXR0b24gd2luZG93IHByb2NlZHVyZS4gVGhpcyBpcyBqdXN0IGEgd3JhcHBlciB3aGljaCBsb2NrcwogKiB0aGUgcGFzc2VkIEhXTkQgYW5kIGNhbGxzIHRoZSByZWFsIHdpbmRvdyBwcm9jZWR1cmUgKHdpdGggYSBXTkQqCiAqIHBvaW50ZXIgcG9pbnRpbmcgdG8gdGhlIGxvY2tlZCB3aW5kb3dzdHJ1Y3R1cmUpLgogKi8KTFJFU1VMVCBXSU5BUEkgQnV0dG9uV25kUHJvYyggSFdORCBoV25kLCBVSU5UIHVNc2csCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFdQQVJBTSB3UGFyYW0sIExQQVJBTSBsUGFyYW0gKQp7CiAgICBMUkVTVUxUIHJlczsKICAgIFdORCAqd25kUHRyID0gV0lOX0ZpbmRXbmRQdHIoaFduZCk7CgogICAgcmVzID0gQnV0dG9uV25kUHJvY19sb2NrZWQod25kUHRyLHVNc2csd1BhcmFtLGxQYXJhbSk7CgogICAgV0lOX1JlbGVhc2VXbmRQdHIod25kUHRyKTsKICAgIHJldHVybiByZXM7Cn0KCi8qKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqICAgICAgIFB1c2ggQnV0dG9uIEZ1bmN0aW9ucwogKi8Kc3RhdGljIHZvaWQgUEJfUGFpbnQoIFdORCAqd25kUHRyLCBIREMgaERDLCBXT1JEIGFjdGlvbiApCnsKICAgIEJVVFRPTklORk8gKmluZm9QdHIgICAgICA9IChCVVRUT05JTkZPICopd25kUHRyLT53RXh0cmE7CiAgICBCT09MICAgICAgICBiSGlnaExpZ2h0ZWQgPSAoaW5mb1B0ci0+c3RhdGUgJiBCVVRUT05fSElHSExJR0hURUQpOwoKICAgIC8qIAogICAgICogRGVsZWdhdGUgdGhpcyB0byB0aGUgbW9yZSBnZW5lcmljIHB1c2hidXR0b24gcGFpbnRpbmcKICAgICAqIG1ldGhvZC4KICAgICAqLwogICAgQlVUVE9OX0RyYXdQdXNoQnV0dG9uKHduZFB0ciwKCQkJICBoREMsCgkJCSAgYWN0aW9uLAoJCQkgIGJIaWdoTGlnaHRlZCk7Cn0KCi8qKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqIFRoaXMgbWV0aG9kIHdpbGwgYWN0dWFsbHkgZG8gdGhlIGRyYXdpbmcgb2YgdGhlIHB1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gICAgICAgICAgICAgICAgICAgICAgICAgR2V0U3lzQ29sb3IoQ09MT1JfQlROVEVYVCkgKTsKICAgICAgICAgICAgRHJhd1RleHRXKCBoREMsIHduZFB0ci0+dGV4dCwgLTEsICZyYywKICAgICAgICAgICAgICAgICAgICAgICAgIERUX1NJTkdMRUxJTkUgfCBEVF9DRU5URVIgfCBEVF9WQ0VOVEVSICk7CiAgICAgICAgICAgIC8qIGRvIHdlIGhhdmUgdGhlIGZvY3VzPwoJICAgICAqIFdpbjl4IGRyYXdzIGZvY3VzIGxhc3Qgd2l0aCBhIHNpemUgcHJvcC4gdG8gdGhlIGJ1dHRvbgoJICAgICAqLwogICAgICAgICAgICBpZiAoVFdFQUtfV2luZUxvb2sgPT0gV0lOMzFfTE9PSwoJCSYmIGluZm9QdHItPnN0YXRlICYgQlVUVE9OX0hBU0ZPQ1VTKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBSRUNUIHIgPSB7IDAsIDAsIDAsIDAgfTsKICAgICAgICAgICAgICAgIElOVCB4ZGVsdGEsIHlkZWx0YTsKCiAgICAgICAgICAgICAgICBEcmF3VGV4dFcoIGhEQywgd25kUHRyLT50ZXh0LCAtMSwgJnIsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgRFRfU0lOR0xFTElORSB8IERUX0NBTENSRUNUICk7CiAgICAgICAgICAgICAgICB4ZGVsdGEgPSAoKHJjLnJpZ2h0IC0gcmMubGVmdCkgLSAoci5yaWdodCAtIHIubGVmdCkgLSAxKSAvIDI7CiAgICAgICAgICAgICAgICB5ZGVsdGEgPSAoKHJjLmJvdHRvbSAtIHJjLnRvcCkgLSAoci5ib3R0b20gLSByLnRvcCkgLSAxKSAvIDI7CiAgICAgICAgICAgICAgICBpZiAoeGRlbHRhIDwgMCkgeGRlbHRhID0gMDsKICAgICAgICAgICAgICAgIGlmICh5ZGVsdGEgPCAwKSB5ZGVsdGEgPSAwOwogICAgICAgICAgICAgICAgSW5mbGF0ZVJlY3QoICZyYyw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MgaXMgd2hhdCBDVEwzRCBleHBlY3RzICovCgogICAgICAgIFNldFJlY3RFbXB0eSgmcmJveCk7CiAgICAgICAgaWYoIHRleHRsZW4gKQogICAgICAgICAgICBEcmF3VGV4dFcoIGhEQywgd25kUHRyLT50ZXh0LCB0ZXh0bGVuLCAmcmJveCwKCQkJIERUX1NJTkdMRUxJTkUgfCBEVF9DQUxDUkVDVCApOwogICAgICAgIHRleHRsZW4gPSByYm94LmJvdHRvbSAtIHJib3gudG9wOwogICAgICAgIGRlbHRhID0gKChydGV4dC5ib3R0b20gLSBydGV4dC50b3ApIC0gdGV4dGxlbikvMjsKICAgICAgICByYm94LmJvdHRvbSA9IChyYm94LnRvcCA9IHJ0ZXh0LnRvcCArIGRlbHRhIC0gMSkgKyB0ZXh0bGVuICsgMjsKICAgICAgICB0ZXh0bGVuID0gcmJveC5yaWdodCAtIHJib3gubGVmdDsKICAgICAgICByYm94LnJpZ2h0ID0gKHJib3gubGVmdCArPSAtLXJ0ZXh0LmxlZnQpICsgdGV4dGxlbiArIDI7CiAgICAgICAgSW50ZXJzZWN0UmVjdCgmcmJveCwgJnJib3gsICZydGV4dCk7CiAgICAgICAgRHJhd0ZvY3VzUmVjdCggaERDLCAmcmJveCApOwogICAgfQp9CgoKLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogICAgICAgQlVUVE9OX0NoZWNrQXV0b1JhZGlvQnV0dG9uCiAqCiAqIHduZFB0ciBpcyBjaGVja2VkLCB1bmNoZWNrIGV2ZXJ5IG90aGVyIGF1dG8gcmFkaW8gYnV0dG9uIGluIGdyb3VwCiAqLwpzdGF0aWMgdm9pZCBCVVRUT05fQ2hlY2tBdXRvUmFkaW9CdXR0b24oIFdORCAqd25kUHRyICkKewogICAgSFdORCBwYXJlbnQsIHNpYmxpbmcsIHN0YXJ0OwogICAgaWYgKCEod25kUHRyLT5kd1N0eWxlICYgV1NfQ0hJTEQpKSByZXR1cm47CiAgICBwYXJlbnQgPSB3bmRQdHItPnBhcmVudC0+aHduZFNlbGY7CiAgICAvKiBhc3N1cmUgdGhhdCBzdGFydGluZyBjb250cm9sIGlzIG5vdCBkaXNhYmxlZCBvciBpbnZpc2libGUgKi8KICAgIHN0YXJ0ID0gc2libGluZyA9IEdldE5leHREbGdHcm91cEl0ZW0oIHBhcmVudCwgd25kUHRyLT5od25kU2VsZiwgVFJVRSApOwogICAgZG8KICAgIHsKICAgICAgICBXTkQgKnRtcFduZDsKICAgICAgICBpZiAoIXNpYmxpbmcpIGJyZWFrOwogICAgICAgIHRtcFduZCA9IFdJTl9GaW5kV25kUHRyKHNpYmxpbmcpOwogICAgICAgIGlmICgod25kUHRyLT5od25kU2VsZiAhPSBzaWJsaW5nKSAmJgogICAgICAgICAgICAoKHRtcFduZC0+ZHdTdHlsZSAmIDB4MGYpID09IEJTX0FVVE9SQURJT0JVVFRPTikpCiAgICAgICAgICAgIFNlbmRNZXNzYWdlQSggc2libGluZywgQk1fU0VUQ0hFQ0ssIEJVVFRPTl9VTkNIRUNLRUQsIDAgKTsKICAgICAgICBzaWJsaW5nID0gR2V0TmV4dERsZ0dyb3VwSXRlbSggcGFyZW50LCBzaWJsaW5nLCBGQUxTRSApOwogICAgICAgIFdJTl9SZWxlYXNlV25kUHRyKHRtcFduZCk7CiAgICB9IHdoaWxlIChzaWJsaW5nICE9IHN0YXJ0KTsKfQoKCi8qKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqICAgICAgIEdyb3VwIEJveCBGdW5jdGlvbnMKICovCgpzdGF0aWMgdm9pZCBHQl9QYWludCggV05EICp3bmRQdHIsIEhEQyBoREMsIFdPUkQgYWN0aW9uICkKewogICAgUkVDVCByYywgcmNGcmFtZTsKICAgIEJVVFRPTklORk8gKmluZm9QdHIgPSAoQlVUVE9OSU5GTyAqKXduZFB0ci0+d0V4dHJhOwo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AgICAgICAgIFNldFRleHRDb2xvciggaERDLCBHZXRTeXNDb2xvcihDT0xPUl9HUkFZVEVYVCkgKTsKICAgICAgICByYy5sZWZ0ICs9IDEwOwogICAgICAgIERyYXdUZXh0VyggaERDLCB3bmRQdHItPnRleHQsIC0xLCAmcmMsIERUX1NJTkdMRUxJTkUgfCBEVF9OT0NMSVAgKTsKICAgIH0KfQoKCi8qKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqICAgICAgIFVzZXIgQnV0dG9uIEZ1bmN0aW9ucwogKi8KCnN0YXRpYyB2b2lkIFVCX1BhaW50KCBXTkQgKnduZFB0ciwgSERDIGhEQywgV09SRCBhY3Rpb24gKQp7CiAgICBSRUNUIHJjOwogICAgSEJSVVNIIGhCcnVzaDsKICAgIEJVVFRPTklORk8gKmluZm9QdHIgPSAoQlVUVE9OSU5GTyAqKXduZFB0ci0+d0V4dHJhOwoKICAgIGlmIChhY3Rpb24gPT0gT0RBX1NFTEVDVCkgcmV0dXJuOwoKICAgIEdldENsaWVudFJlY3QoIHduZFB0ci0+aHduZFNlbGYsICZyYyk7CgogICAgaWYgKGluZm9QdHItPmhGb250KSBTZWxlY3RPYmplY3QoIGhEQywgaW5mb1B0ci0+aEZvbnQgKTsKICAgIGhCcnVzaCA9IEdldENvbnRyb2xCcnVzaDE2KCB3bmRQdHItPmh3bmRTZWxmLCBoREMsIENUTENPTE9SX0JUTiApOwoKICAgIEZpbGxSZWN0KCBoREMsICZyYywgaEJydXNoICk7CiAgICBpZiAoKGFjdGlvbiA9PSBPREFfRk9DVVMpIHx8CiAgICAgICAgKChhY3Rpb24gPT0gT0RBX0RSQVdFTlRJUkUpICYmIChpbmZvUHRyLT5zdGF0ZSAmIEJVVFRPTl9IQVNGT0NVUykpKQogICAgICAgIERyYXdGb2N1c1JlY3QoIGhEQywgJnJjICk7Cn0KCgovKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgogKiAgICAgICBPd25lcmRyYXduIEJ1dHRvbiBGdW5jdGlvbnMKICovCgpzdGF0aWMgdm9pZCBPQl9QYWludCggV05EICp3bmRQdHIsIEhEQyBoREMsIFdPUkQgYWN0aW9uICkKewogICAgQlVUVE9OSU5GTyAqaW5mb1B0ciA9IChCVVRUT05JTkZPICopd25kUHRyLT53RXh0cmE7CiAgICBEUkFXSVRFTVNUUlVDVCBkaXM7CiAgICBIUkdOIGNsaXBSZWdpb247CiAgICBSRUNUIGNsaXBSZWN0OwoKICAgIGRpcy5DdGxUeXBlICAgID0gT0RUX0JVVFRPTjsKICAgIGRpcy5DdGxJRCAgICAgID0gd25kUHRyLT53SURtZW51OwogICAgZGlzLml0ZW1JRCAgICAgPSAwOwogICAgZGlzLml0ZW1BY3Rpb24gPSBhY3Rpb247CiAgICBkaXMuaXRlbVN0YXRlICA9ICgoaW5mb1B0ci0+c3RhdGUgJiBCVVRUT05fSEFTRk9DVVMpID8gT0RTX0ZPQ1VTIDogMCkgfAogICAgICAgICAgICAgICAgICAgICAoKGluZm9QdHItPnN0YXRlICYgQlVUVE9OX0hJR0hMSUdIVEVEKSA/IE9EU19TRUxFQ1RFRCA6IDApIHwKICAgICAgICAgICAgICAgICAgICAgKCh3bmRQdHItPmR3U3R5bGUgJiBXU19ESVNBQkxFRCkgPyBPRFNfRElTQUJMRUQgOiAwKTsKICAgIGRpcy5od25kSXRlbSAgID0gd25kUHRyLT5od25kU2VsZjsKICAgIGRpcy5oREMgICAgICAgID0gaERDOwogICAgZGlzLml0ZW1EYXRhICAgPSAwOwogICAgR2V0Q2xpZW50UmVjdCggd25kUHRyLT5od25kU2VsZiwgJmRpcy5yY0l0ZW0gKTsKCiAgICBjbGlwUmVnaW9uID0gQ3JlYXRlUmVjdFJnbkluZGlyZWN0KCZkaXMucmNJdGVtKTsgICAKICAgIGlmIChHZXRDbGlwUmduKGhEQywgY2xpcFJlZ2lvbikgIT0gMSkKICAgIHsKCURlbGV0ZU9iamVjdChjbGlwUmVnaW9uKTsKCWNsaXBSZWdpb249KEhSR04pTlVMTDsKICAgIH0KICAgIGNsaXBSZWN0ID0gZGlzLnJjSXRlbTsKICAgIERQdG9MUChoREMsIChMUFBPSU5UKSAmY2xpcFJlY3QsIDIpOyAgICAKICAgIEludGVyc2VjdENsaXBSZWN0KGhEQywgY2xpcFJlY3QubGVmdCwgIGNsaXBSZWN0LnRvcCwgY2xpcFJlY3QucmlnaHQsIGNsaXBSZWN0LmJvdHRvbSk7CgogICAgU2V0QmtDb2xvciggaERDLCBHZXRTeXNDb2xvciggQ09MT1JfQlRORkFDRSApICk7CgogICAgU2VuZE1lc3NhZ2VBKCBHZXRQYXJlbnQod25kUHRyLT5od25kU2VsZiksIFdNX0RSQVdJVEVNLAogICAgICAgICAgICAgICAgICAgIHduZFB0ci0+d0lEbWVudSwgKExQQVJBTSkmZGlzICk7CgogICAgU2VsZWN0Q2xpcFJnbihoREMsIGNsaXBSZWdpb24pOwkJCn0KCg==