[], 'safe_senders' => [], 'safe_recipients' => [], 'contacts' => [], ]; self::collect($tree, false, false, $lists); foreach ($lists as &$l) { $l = array_values(array_unique($l)); } return $lists; } /** * @param array $entries * * @return array [addresses, domains] split on the leading '@' */ private static function splitList($entries) { $addr = []; $dom = []; foreach ($entries as $e) { $e = trim((string) $e); if ($e === '') { continue; } if ($e[0] === '@') { $dom[] = $e; } else { $addr[] = $e; } } return [$addr, $dom]; } private static function utf16($str) { return iconv('UTF-8', 'UTF-16LE', $str) . "\x00\x00"; } private static function resAnd($subs) { return chr(self::RES_AND) . pack('V', count($subs)) . implode('', $subs); } private static function resOr($subs) { return chr(self::RES_OR) . pack('V', count($subs)) . implode('', $subs); } private static function resNot($sub) { return chr(self::RES_NOT) . $sub; } private static function resSub($sub) { return chr(self::RES_SUB) . pack('V', self::SUBOBJ_RECIPIENTS) . $sub; } private static function resExist($tag) { return chr(self::RES_EXIST) . pack('V', $tag); } private static function resProperty($relop, $tag, $packedValue) { return chr(self::RES_PROPERTY) . chr($relop) . pack('V', $tag) . pack('V', $tag) . $packedValue; } private static function resContent($fuzzy, $tag, $str) { return chr(self::RES_CONTENT) . pack('V', $fuzzy) . pack('V', $tag) . pack('V', $tag) . self::utf16($str); } /** * @param mixed $entries * @param mixed $tag * @param mixed $fuzzy * * @return string a RES_OR of one RES_CONTENT per entry (zero-count when empty) */ private static function contentList($entries, $tag, $fuzzy) { $subs = []; foreach ($entries as $e) { $subs[] = self::resContent($fuzzy, $tag, $e); } return self::resOr($subs); } private static function skipNamedProps($blob, &$pos) { if (strlen($blob) < $pos + 2) { return false; } $count = unpack('v', substr($blob, $pos, 2))[1]; $pos += 2; if ($count === 0) { return true; } $pos += 2 * $count; if (strlen($blob) < $pos + 4) { return false; } $size = unpack('V', substr($blob, $pos, 4))[1]; $pos += 4 + $size; return $pos <= strlen($blob); } /** * Pull one restriction. Returns a node array or false. * Nodes: ['t' => type, 'subs' => [], 'tag' => int, 'fuzzy' => int, 'val' => string]. * A depth bound keeps a hostile deeply-nested blob from exhausting memory. * * @param mixed $blob * @param mixed $pos * @param mixed $depth */ private static function pullRestriction($blob, &$pos, $depth = 0) { if ($pos >= strlen($blob) || $depth > 64) { return false; } $type = ord($blob[$pos++]); switch ($type) { case self::RES_AND: case self::RES_OR: $count = self::pullUint32($blob, $pos); if ($count === false || $count > 0x10000) { return false; } $subs = []; for ($i = 0; $i < $count; ++$i) { $sub = self::pullRestriction($blob, $pos, $depth + 1); if ($sub === false) { return false; } $subs[] = $sub; } return ['t' => $type, 'subs' => $subs]; case self::RES_NOT: $sub = self::pullRestriction($blob, $pos, $depth + 1); return $sub === false ? false : ['t' => $type, 'subs' => [$sub]]; case self::RES_CONTENT: $fuzzy = self::pullUint32($blob, $pos); $tag = self::pullUint32($blob, $pos); $vtag = self::pullUint32($blob, $pos); if ($vtag === false) { return false; } $val = self::pullValue($blob, $pos, $vtag & 0xFFFF); if ($val === false) { return false; } return ['t' => $type, 'fuzzy' => $fuzzy, 'tag' => $tag, 'val' => $val]; case self::RES_PROPERTY: ++$pos; // relop $tag = self::pullUint32($blob, $pos); $vtag = self::pullUint32($blob, $pos); if ($vtag === false || self::pullValue($blob, $pos, $vtag & 0xFFFF) === false) { return false; } return ['t' => $type, 'tag' => $tag]; case self::RES_EXIST: $tag = self::pullUint32($blob, $pos); return $tag === false ? false : ['t' => $type, 'tag' => $tag]; case self::RES_SUB: $subobj = self::pullUint32($blob, $pos); $sub = self::pullRestriction($blob, $pos, $depth + 1); return $sub === false ? false : ['t' => $type, 'tag' => $subobj, 'subs' => [$sub]]; case self::RES_COMMENT: $count = ord($blob[$pos++] ?? "\x00"); for ($i = 0; $i < $count; ++$i) { $vtag = self::pullUint32($blob, $pos); if ($vtag === false || self::pullValue($blob, $pos, $vtag & 0xFFFF) === false) { return false; } } $present = ord($blob[$pos++] ?? "\x00"); if ($present) { $sub = self::pullRestriction($blob, $pos, $depth + 1); if ($sub === false) { return false; } return $sub; } return ['t' => self::RES_AND, 'subs' => []]; default: return false; } } private static function pullUint32($blob, &$pos) { if (strlen($blob) < $pos + 4) { return false; } $v = unpack('V', substr($blob, $pos, 4))[1]; $pos += 4; return $v; } /** * Pull a property value of the given PT type. Strings decode to UTF-8. * * @param mixed $blob * @param mixed $pos * @param mixed $type */ private static function pullValue($blob, &$pos, $type) { switch ($type) { case 0x001F: // PT_UNICODE $end = $pos; $len = strlen($blob); while ($end + 1 < $len && ($blob[$end] !== "\x00" || $blob[$end + 1] !== "\x00")) { $end += 2; } if ($end + 1 >= $len) { return false; } $val = iconv('UTF-16LE', 'UTF-8', substr($blob, $pos, $end - $pos)); $pos = $end + 2; return $val; case 0x001E: // PT_STRING8 $end = strpos($blob, "\x00", $pos); if ($end === false) { return false; } $val = substr($blob, $pos, $end - $pos); $pos = $end + 1; return $val; case 0x0003: // PT_LONG case 0x000A: // PT_ERROR $v = self::pullUint32($blob, $pos); return $v === false ? false : (string) $v; case 0x000B: // PT_BOOLEAN if ($pos >= strlen($blob)) { return false; } return (string) ord($blob[$pos++]); case 0x0002: // PT_SHORT if (strlen($blob) < $pos + 2) { return false; } $v = unpack('v', substr($blob, $pos, 2))[1]; $pos += 2; return (string) $v; case 0x0102: // PT_BINARY $len = self::pullUint32($blob, $pos); if ($len === false || strlen($blob) < $pos + $len) { return false; } $val = substr($blob, $pos, $len); $pos += $len; return $val; default: return false; } } /** * Classify every string comparison in the tree into the four lists. * $negated: inside an odd number of RES_NOT. * $inRecipients: inside a RES_SUB on the recipients table. * * @param mixed $node * @param mixed $negated * @param mixed $inRecipients * @param mixed $lists */ private static function collect($node, $negated, $inRecipients, &$lists) { switch ($node['t']) { case self::RES_AND: case self::RES_OR: foreach ($node['subs'] as $sub) { self::collect($sub, $negated, $inRecipients, $lists); } break; case self::RES_NOT: self::collect($node['subs'][0], !$negated, $inRecipients, $lists); break; case self::RES_SUB: self::collect($node['subs'][0], $negated, true, $lists); break; case self::RES_CONTENT: $val = $node['val']; if (!is_string($val) || $val === '') { break; } $substring = (($node['fuzzy'] ?? 0) & 0x0001) === 0x0001; if ($inRecipients) { if (!$negated) { break; } $lists['safe_recipients'][] = $val; } elseif (!$negated) { $lists['blocked_senders'][] = $val; } elseif ($substring && $val[0] !== '@') { // Outlook's trusted-contacts clause contains full addresses matched // as substrings. It is preserved, and never shown as safe senders. $lists['contacts'][] = $val; } else { $lists['safe_senders'][] = $val; } break; } } }