{-# LINE 1 "Data/Text/ICU/Spoof.hsc" #-}
{-# LANGUAGE BangPatterns, CPP, DeriveDataTypeable, ForeignFunctionInterface,
    OverloadedStrings, RecordWildCards, ScopedTypeVariables #-}
-- |
-- Module      : Data.Text.ICU.Spoof
-- Copyright   : (c) 2015 Ben Hamilton
--
-- License     : BSD-style
-- Maintainer  : bgertzfield@gmail.com
-- Stability   : experimental
-- Portability : GHC
--
-- String spoofing (confusability) checks for Unicode, implemented as
-- bindings to the International Components for Unicode (ICU) uspoof
-- library.
--
-- See <http://unicode.org/reports/tr36/ UTR #36> and
-- <http://unicode.org/reports/tr39/ UTS #39> for detailed information
-- about the underlying algorithms and databases used by this module.

module Data.Text.ICU.Spoof
    (
    -- * Unicode spoof checking API
    -- $api
    -- * Types
      MSpoof
    , OpenFromSourceParseError(..)
    , SpoofCheck(..)
    , SpoofCheckResult(..)
    , RestrictionLevel(..)
    , SkeletonTypeOverride(..)
    -- * Functions
    , open
    , openFromSerialized
    , openFromSource
    , getSkeleton
    , getChecks
    , setChecks
    , getRestrictionLevel
    , setRestrictionLevel
    , getAllowedLocales
    , setAllowedLocales
    , areConfusable
    , spoofCheck
    , serialize
    ) where





import Control.DeepSeq (NFData(..))
import Control.Exception (Exception, throwIO, catchJust)
import Data.Bits ((.&.))
import Data.ByteString (ByteString)
import Data.ByteString.Internal (create, memcpy, toForeignPtr)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.Int (Int32)
import Data.List (intercalate)
import Data.Text (Text, pack, splitOn, strip, unpack)
import Data.Text.Foreign (useAsPtr)
import Data.Text.ICU.BitMask (ToBitMask, fromBitMask, highestValueInBitMask,
                              toBitMask)
import Data.Text.ICU.Spoof.Internal (MSpoof, USpoof, withSpoof, wrap,
                                     wrapWithSerialized)
import Data.Text.ICU.Error (u_PARSE_ERROR)
import Data.Text.ICU.Error.Internal (UErrorCode, UParseError,
                                     ParseError(..), handleError,
                                     handleOverflowError, handleParseError)

{-# LINE 70 "Data/Text/ICU/Spoof.hsc" #-}
import Data.Text.Foreign (fromPtr)

{-# LINE 75 "Data/Text/ICU/Spoof.hsc" #-}
import Data.Typeable (Typeable)
import Data.Word (Word8)
import Foreign.C.String (CString, peekCString, withCString)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr (Ptr, nullPtr, plusPtr)
import Foreign.Storable (peek)
import Foreign.ForeignPtr (withForeignPtr)

-- $api
--
-- The 'spoofCheck', 'areConfusable', and 'getSkeleton' functions analyze
-- Unicode text for visually confusable (or \"spoof\") characters.
--
-- For example, Latin, Cyrillic, and Greek all contain unique Unicode
-- values which appear nearly identical on-screen:
--
-- @
--      A    0041    LATIN CAPITAL LETTER A
--      &#x0391;    0391    GREEK CAPITAL LETTER ALPHA
--      &#x0410;    0410    CYRILLIC CAPITAL LETTER A
--      &#x13AA;    13AA    CHEROKEE LETTER GO
--      &#x1D00;    1D00    LATIN LETTER SMALL CAPITAL A
--      &#x15C5;    15C5    CANADIAN SYLLABICS CARRIER GHO
--      &#xFF21;    FF21    FULLWIDTH LATIN CAPITAL LETTER A
--      &#x102A0;    102A0   CARIAN LETTER A
--      &#x1D400;    1D400   MATHEMATICAL BOLD CAPITAL A
-- @
--
-- and so on. To check a string for visually confusable characters:
--
--   1. 'open' an 'MSpoof'
--   2. optionally configure it with 'setChecks', 'setRestrictionLevel',
-- and/or 'setAllowedLocales', then
--   3. 'spoofCheck' a single string, use 'areConfusable' to check if two
-- strings could be confused for each other, or use 'getSkeleton' to precompute
-- a \"skeleton\" string (similar to a hash code) which can be cached
-- and re-used to quickly check (using Unicode string comparison) if
-- two strings are confusable.
--
-- By default, these methods will use ICU's bundled copy of
-- <http://unicode.org/Public/security/latest/confusables.txt confusables.txt>
-- and <http://unicode.org/Public/security/latest/confusablesWholeScript.txt confusablesWholeScript.txt>,
-- which could be out of date. To provide your own confusables databases, use
-- 'openFromSource'. (To avoid repeatedly parsing these databases, you
-- can then 'serialize' your configured 'MSpoof' and later
-- 'openFromSerialized' to load the pre-parsed databases.)

data SpoofCheck
  -- | Makes 'areConfusable' report if both identifiers are both from the
  -- same script and are visually confusable. Does not affect 'spoofCheck'.
  = SingleScriptConfusable

  -- | Makes 'areConfusable' report if both identifiers are visually
  -- confusable and at least one identifier contains characters from more
  -- than one script.
  --
  -- Makes 'spoofCheck' report if the identifier contains multiple scripts,
  -- and is confusable with some other identifier in a single script.
  | MixedScriptConfusable

  -- | Makes 'areConfusable' report if each identifier is of a different
  -- single script, and the identifiers are visually confusable.
  | WholeScriptConfusable

  -- | By default, spoof checks assume the strings have been processed
  -- through 'toCaseFold' and only check lower-case identifiers. If
  -- this is set, spoof checks will check both upper and lower case
  -- identifiers.
  | AnyCase

  -- | Checks that identifiers are no looser than the specified
  -- level passed to 'setRestrictionLevel'.
  | RestrictionLevel

  -- | Checks the identifier for the presence of invisible characters,
  -- such as zero-width spaces, or character sequences that are likely
  -- not to display, such as multiple occurrences of the same
  -- non-spacing mark.
  | Invisible

  -- | Checks whether the identifier contains only characters from a
  -- specified set (for example, via 'setAllowedLocales').
  | CharLimit

  -- | Checks that the identifier contains numbers from only a
  -- single script.
  | MixedNumbers

  -- | Enables all checks.
  | AllChecks

  -- | Enables returning a 'RestrictionLevel' in the 'SpoofCheckResult'.
  | AuxInfo
  deriving (SpoofCheck
SpoofCheck -> SpoofCheck -> Bounded SpoofCheck
forall a. a -> a -> Bounded a
maxBound :: SpoofCheck
$cmaxBound :: SpoofCheck
minBound :: SpoofCheck
$cminBound :: SpoofCheck
Bounded, Int -> SpoofCheck
SpoofCheck -> Int
SpoofCheck -> [SpoofCheck]
SpoofCheck -> SpoofCheck
SpoofCheck -> SpoofCheck -> [SpoofCheck]
SpoofCheck -> SpoofCheck -> SpoofCheck -> [SpoofCheck]
(SpoofCheck -> SpoofCheck)
-> (SpoofCheck -> SpoofCheck)
-> (Int -> SpoofCheck)
-> (SpoofCheck -> Int)
-> (SpoofCheck -> [SpoofCheck])
-> (SpoofCheck -> SpoofCheck -> [SpoofCheck])
-> (SpoofCheck -> SpoofCheck -> [SpoofCheck])
-> (SpoofCheck -> SpoofCheck -> SpoofCheck -> [SpoofCheck])
-> Enum SpoofCheck
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: SpoofCheck -> SpoofCheck -> SpoofCheck -> [SpoofCheck]
$cenumFromThenTo :: SpoofCheck -> SpoofCheck -> SpoofCheck -> [SpoofCheck]
enumFromTo :: SpoofCheck -> SpoofCheck -> [SpoofCheck]
$cenumFromTo :: SpoofCheck -> SpoofCheck -> [SpoofCheck]
enumFromThen :: SpoofCheck -> SpoofCheck -> [SpoofCheck]
$cenumFromThen :: SpoofCheck -> SpoofCheck -> [SpoofCheck]
enumFrom :: SpoofCheck -> [SpoofCheck]
$cenumFrom :: SpoofCheck -> [SpoofCheck]
fromEnum :: SpoofCheck -> Int
$cfromEnum :: SpoofCheck -> Int
toEnum :: Int -> SpoofCheck
$ctoEnum :: Int -> SpoofCheck
pred :: SpoofCheck -> SpoofCheck
$cpred :: SpoofCheck -> SpoofCheck
succ :: SpoofCheck -> SpoofCheck
$csucc :: SpoofCheck -> SpoofCheck
Enum, SpoofCheck -> SpoofCheck -> Bool
(SpoofCheck -> SpoofCheck -> Bool)
-> (SpoofCheck -> SpoofCheck -> Bool) -> Eq SpoofCheck
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SpoofCheck -> SpoofCheck -> Bool
$c/= :: SpoofCheck -> SpoofCheck -> Bool
== :: SpoofCheck -> SpoofCheck -> Bool
$c== :: SpoofCheck -> SpoofCheck -> Bool
Eq, Int -> SpoofCheck -> ShowS
[SpoofCheck] -> ShowS
SpoofCheck -> [Char]
(Int -> SpoofCheck -> ShowS)
-> (SpoofCheck -> [Char])
-> ([SpoofCheck] -> ShowS)
-> Show SpoofCheck
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [SpoofCheck] -> ShowS
$cshowList :: [SpoofCheck] -> ShowS
show :: SpoofCheck -> [Char]
$cshow :: SpoofCheck -> [Char]
showsPrec :: Int -> SpoofCheck -> ShowS
$cshowsPrec :: Int -> SpoofCheck -> ShowS
Show)

instance ToBitMask SpoofCheck where
  toBitMask :: SpoofCheck -> Int
toBitMask SpoofCheck
SingleScriptConfusable = Int
1
{-# LINE 172 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask MixedScriptConfusable  = 2
{-# LINE 173 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask WholeScriptConfusable  = 4
{-# LINE 174 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask AnyCase                = 8
{-# LINE 175 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask RestrictionLevel       = 16
{-# LINE 176 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask Invisible              = 32
{-# LINE 177 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask CharLimit              = 64
{-# LINE 178 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask MixedNumbers           = 128
{-# LINE 179 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask AllChecks              = 65535
{-# LINE 180 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask AuxInfo                = 1073741824
{-# LINE 181 "Data/Text/ICU/Spoof.hsc" #-}

type USpoofCheck = Int32

data RestrictionLevel
  -- | Checks that the string contains only Unicode values in the range
  -- #0000#&#2013;#007F# inclusive.
  = ASCII
  -- | Checks that the string contains only characters from a single script.
  | SingleScriptRestrictive
  -- | Checks that the string contains only characters from a single script,
  -- or from the combinations (Latin + Han + Hiragana + Katakana),
  -- (Latin + Han + Bopomofo), or (Latin + Han + Hangul).
  | HighlyRestrictive
  -- | Checks that the string contains only characters from the combinations
  -- (Latin + Cyrillic + Greek + Cherokee), (Latin + Han + Hiragana + Katakana),
  -- (Latin + Han + Bopomofo), or (Latin + Han + Hangul).
  | ModeratelyRestrictive
  -- | Allows arbitrary mixtures of scripts.
  | MinimallyRestrictive
  -- | Allows any valid identifiers, including characters outside of the
  -- Identifier Profile.
  | Unrestrictive
  deriving (RestrictionLevel
RestrictionLevel -> RestrictionLevel -> Bounded RestrictionLevel
forall a. a -> a -> Bounded a
maxBound :: RestrictionLevel
$cmaxBound :: RestrictionLevel
minBound :: RestrictionLevel
$cminBound :: RestrictionLevel
Bounded, Int -> RestrictionLevel
RestrictionLevel -> Int
RestrictionLevel -> [RestrictionLevel]
RestrictionLevel -> RestrictionLevel
RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
RestrictionLevel
-> RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
(RestrictionLevel -> RestrictionLevel)
-> (RestrictionLevel -> RestrictionLevel)
-> (Int -> RestrictionLevel)
-> (RestrictionLevel -> Int)
-> (RestrictionLevel -> [RestrictionLevel])
-> (RestrictionLevel -> RestrictionLevel -> [RestrictionLevel])
-> (RestrictionLevel -> RestrictionLevel -> [RestrictionLevel])
-> (RestrictionLevel
    -> RestrictionLevel -> RestrictionLevel -> [RestrictionLevel])
-> Enum RestrictionLevel
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: RestrictionLevel
-> RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
$cenumFromThenTo :: RestrictionLevel
-> RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
enumFromTo :: RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
$cenumFromTo :: RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
enumFromThen :: RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
$cenumFromThen :: RestrictionLevel -> RestrictionLevel -> [RestrictionLevel]
enumFrom :: RestrictionLevel -> [RestrictionLevel]
$cenumFrom :: RestrictionLevel -> [RestrictionLevel]
fromEnum :: RestrictionLevel -> Int
$cfromEnum :: RestrictionLevel -> Int
toEnum :: Int -> RestrictionLevel
$ctoEnum :: Int -> RestrictionLevel
pred :: RestrictionLevel -> RestrictionLevel
$cpred :: RestrictionLevel -> RestrictionLevel
succ :: RestrictionLevel -> RestrictionLevel
$csucc :: RestrictionLevel -> RestrictionLevel
Enum, RestrictionLevel -> RestrictionLevel -> Bool
(RestrictionLevel -> RestrictionLevel -> Bool)
-> (RestrictionLevel -> RestrictionLevel -> Bool)
-> Eq RestrictionLevel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RestrictionLevel -> RestrictionLevel -> Bool
$c/= :: RestrictionLevel -> RestrictionLevel -> Bool
== :: RestrictionLevel -> RestrictionLevel -> Bool
$c== :: RestrictionLevel -> RestrictionLevel -> Bool
Eq, Int -> RestrictionLevel -> ShowS
[RestrictionLevel] -> ShowS
RestrictionLevel -> [Char]
(Int -> RestrictionLevel -> ShowS)
-> (RestrictionLevel -> [Char])
-> ([RestrictionLevel] -> ShowS)
-> Show RestrictionLevel
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [RestrictionLevel] -> ShowS
$cshowList :: [RestrictionLevel] -> ShowS
show :: RestrictionLevel -> [Char]
$cshow :: RestrictionLevel -> [Char]
showsPrec :: Int -> RestrictionLevel -> ShowS
$cshowsPrec :: Int -> RestrictionLevel -> ShowS
Show)

instance ToBitMask RestrictionLevel where
  toBitMask :: RestrictionLevel -> Int
toBitMask RestrictionLevel
ASCII                   = Int
268435456
{-# LINE 207 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask SingleScriptRestrictive = 536870912
{-# LINE 208 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask HighlyRestrictive       = 805306368
{-# LINE 209 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask ModeratelyRestrictive   = 1073741824
{-# LINE 210 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask MinimallyRestrictive    = 1342177280
{-# LINE 211 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask Unrestrictive           = 1610612736
{-# LINE 212 "Data/Text/ICU/Spoof.hsc" #-}

type URestrictionLevel = Int32

data SpoofCheckResult
  -- | The string passed all configured spoof checks.
  = CheckOK
  -- | The string failed one or more spoof checks.
  | CheckFailed [SpoofCheck]
  -- | The string failed one or more spoof checks, and
  -- failed to pass the configured restriction level.
  | CheckFailedWithRestrictionLevel {
    -- | The spoof checks which the string failed.
    SpoofCheckResult -> [SpoofCheck]
failedChecks :: [SpoofCheck]
    -- | The restriction level which the string failed to pass.
    , SpoofCheckResult -> RestrictionLevel
failedLevel :: RestrictionLevel
    }
  deriving (SpoofCheckResult -> SpoofCheckResult -> Bool
(SpoofCheckResult -> SpoofCheckResult -> Bool)
-> (SpoofCheckResult -> SpoofCheckResult -> Bool)
-> Eq SpoofCheckResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SpoofCheckResult -> SpoofCheckResult -> Bool
$c/= :: SpoofCheckResult -> SpoofCheckResult -> Bool
== :: SpoofCheckResult -> SpoofCheckResult -> Bool
$c== :: SpoofCheckResult -> SpoofCheckResult -> Bool
Eq, Int -> SpoofCheckResult -> ShowS
[SpoofCheckResult] -> ShowS
SpoofCheckResult -> [Char]
(Int -> SpoofCheckResult -> ShowS)
-> (SpoofCheckResult -> [Char])
-> ([SpoofCheckResult] -> ShowS)
-> Show SpoofCheckResult
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [SpoofCheckResult] -> ShowS
$cshowList :: [SpoofCheckResult] -> ShowS
show :: SpoofCheckResult -> [Char]
$cshow :: SpoofCheckResult -> [Char]
showsPrec :: Int -> SpoofCheckResult -> ShowS
$cshowsPrec :: Int -> SpoofCheckResult -> ShowS
Show)

data SkeletonTypeOverride
  -- | By default, 'getSkeleton' builds skeletons which catch
  -- visually confusable characters across multiple scripts.
  -- Pass this flag to override that behavior and build skeletons
  -- which catch visually confusable characters across single scripts.
  = SkeletonSingleScript
  -- | By default, 'getSkeleton' assumes the input string has already
  -- been passed through 'toCaseFold' and is lower-case. Pass this
  -- flag to override that behavior and allow upper and lower-case strings.
  | SkeletonAnyCase
  deriving (SkeletonTypeOverride
SkeletonTypeOverride
-> SkeletonTypeOverride -> Bounded SkeletonTypeOverride
forall a. a -> a -> Bounded a
maxBound :: SkeletonTypeOverride
$cmaxBound :: SkeletonTypeOverride
minBound :: SkeletonTypeOverride
$cminBound :: SkeletonTypeOverride
Bounded, Int -> SkeletonTypeOverride
SkeletonTypeOverride -> Int
SkeletonTypeOverride -> [SkeletonTypeOverride]
SkeletonTypeOverride -> SkeletonTypeOverride
SkeletonTypeOverride
-> SkeletonTypeOverride -> [SkeletonTypeOverride]
SkeletonTypeOverride
-> SkeletonTypeOverride
-> SkeletonTypeOverride
-> [SkeletonTypeOverride]
(SkeletonTypeOverride -> SkeletonTypeOverride)
-> (SkeletonTypeOverride -> SkeletonTypeOverride)
-> (Int -> SkeletonTypeOverride)
-> (SkeletonTypeOverride -> Int)
-> (SkeletonTypeOverride -> [SkeletonTypeOverride])
-> (SkeletonTypeOverride
    -> SkeletonTypeOverride -> [SkeletonTypeOverride])
-> (SkeletonTypeOverride
    -> SkeletonTypeOverride -> [SkeletonTypeOverride])
-> (SkeletonTypeOverride
    -> SkeletonTypeOverride
    -> SkeletonTypeOverride
    -> [SkeletonTypeOverride])
-> Enum SkeletonTypeOverride
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: SkeletonTypeOverride
-> SkeletonTypeOverride
-> SkeletonTypeOverride
-> [SkeletonTypeOverride]
$cenumFromThenTo :: SkeletonTypeOverride
-> SkeletonTypeOverride
-> SkeletonTypeOverride
-> [SkeletonTypeOverride]
enumFromTo :: SkeletonTypeOverride
-> SkeletonTypeOverride -> [SkeletonTypeOverride]
$cenumFromTo :: SkeletonTypeOverride
-> SkeletonTypeOverride -> [SkeletonTypeOverride]
enumFromThen :: SkeletonTypeOverride
-> SkeletonTypeOverride -> [SkeletonTypeOverride]
$cenumFromThen :: SkeletonTypeOverride
-> SkeletonTypeOverride -> [SkeletonTypeOverride]
enumFrom :: SkeletonTypeOverride -> [SkeletonTypeOverride]
$cenumFrom :: SkeletonTypeOverride -> [SkeletonTypeOverride]
fromEnum :: SkeletonTypeOverride -> Int
$cfromEnum :: SkeletonTypeOverride -> Int
toEnum :: Int -> SkeletonTypeOverride
$ctoEnum :: Int -> SkeletonTypeOverride
pred :: SkeletonTypeOverride -> SkeletonTypeOverride
$cpred :: SkeletonTypeOverride -> SkeletonTypeOverride
succ :: SkeletonTypeOverride -> SkeletonTypeOverride
$csucc :: SkeletonTypeOverride -> SkeletonTypeOverride
Enum, SkeletonTypeOverride -> SkeletonTypeOverride -> Bool
(SkeletonTypeOverride -> SkeletonTypeOverride -> Bool)
-> (SkeletonTypeOverride -> SkeletonTypeOverride -> Bool)
-> Eq SkeletonTypeOverride
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SkeletonTypeOverride -> SkeletonTypeOverride -> Bool
$c/= :: SkeletonTypeOverride -> SkeletonTypeOverride -> Bool
== :: SkeletonTypeOverride -> SkeletonTypeOverride -> Bool
$c== :: SkeletonTypeOverride -> SkeletonTypeOverride -> Bool
Eq, Int -> SkeletonTypeOverride -> ShowS
[SkeletonTypeOverride] -> ShowS
SkeletonTypeOverride -> [Char]
(Int -> SkeletonTypeOverride -> ShowS)
-> (SkeletonTypeOverride -> [Char])
-> ([SkeletonTypeOverride] -> ShowS)
-> Show SkeletonTypeOverride
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [SkeletonTypeOverride] -> ShowS
$cshowList :: [SkeletonTypeOverride] -> ShowS
show :: SkeletonTypeOverride -> [Char]
$cshow :: SkeletonTypeOverride -> [Char]
showsPrec :: Int -> SkeletonTypeOverride -> ShowS
$cshowsPrec :: Int -> SkeletonTypeOverride -> ShowS
Show)

instance ToBitMask SkeletonTypeOverride where
  toBitMask :: SkeletonTypeOverride -> Int
toBitMask SkeletonTypeOverride
SkeletonSingleScript = Int
1
{-# LINE 244 "Data/Text/ICU/Spoof.hsc" #-}
  toBitMask SkeletonAnyCase      = 8
{-# LINE 245 "Data/Text/ICU/Spoof.hsc" #-}

type USkeletonTypeOverride = Int32

makeSpoofCheckResult :: USpoofCheck -> SpoofCheckResult
makeSpoofCheckResult :: Int32 -> SpoofCheckResult
makeSpoofCheckResult Int32
c =
  case Int32
c of
    Int32
0 -> SpoofCheckResult
CheckOK
    Int32
_ ->
      case Maybe RestrictionLevel
restrictionLevel of
        Maybe RestrictionLevel
Nothing -> [SpoofCheck] -> SpoofCheckResult
CheckFailed [SpoofCheck]
spoofChecks
        Just RestrictionLevel
l -> [SpoofCheck] -> RestrictionLevel -> SpoofCheckResult
CheckFailedWithRestrictionLevel [SpoofCheck]
spoofChecks RestrictionLevel
l
      where spoofChecks :: [SpoofCheck]
spoofChecks = Int -> [SpoofCheck]
forall a. (Enum a, Bounded a, ToBitMask a) => Int -> [a]
fromBitMask (Int -> [SpoofCheck]) -> Int -> [SpoofCheck]
forall a b. (a -> b) -> a -> b
$ Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$
                          Int32
c Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.&. Int32
65535
{-# LINE 258 "Data/Text/ICU/Spoof.hsc" #-}
            restrictionValue :: Int32
restrictionValue = Int32
c Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.&. Int32
2130706432
{-# LINE 259 "Data/Text/ICU/Spoof.hsc" #-}
            restrictionLevel :: Maybe RestrictionLevel
restrictionLevel = Int -> Maybe RestrictionLevel
forall a. (Enum a, Bounded a, ToBitMask a) => Int -> Maybe a
highestValueInBitMask (Int -> Maybe RestrictionLevel) -> Int -> Maybe RestrictionLevel
forall a b. (a -> b) -> a -> b
$ Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$
                               Int32
restrictionValue

-- | Indicates which input file to 'openFromSource' failed to parse upon error.
data OpenFromSourceParseErrorFile =
  ConfusablesTxtError | ConfusablesWholeScriptTxtError
  deriving (OpenFromSourceParseErrorFile
-> OpenFromSourceParseErrorFile -> Bool
(OpenFromSourceParseErrorFile
 -> OpenFromSourceParseErrorFile -> Bool)
-> (OpenFromSourceParseErrorFile
    -> OpenFromSourceParseErrorFile -> Bool)
-> Eq OpenFromSourceParseErrorFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OpenFromSourceParseErrorFile
-> OpenFromSourceParseErrorFile -> Bool
$c/= :: OpenFromSourceParseErrorFile
-> OpenFromSourceParseErrorFile -> Bool
== :: OpenFromSourceParseErrorFile
-> OpenFromSourceParseErrorFile -> Bool
$c== :: OpenFromSourceParseErrorFile
-> OpenFromSourceParseErrorFile -> Bool
Eq, Int -> OpenFromSourceParseErrorFile -> ShowS
[OpenFromSourceParseErrorFile] -> ShowS
OpenFromSourceParseErrorFile -> [Char]
(Int -> OpenFromSourceParseErrorFile -> ShowS)
-> (OpenFromSourceParseErrorFile -> [Char])
-> ([OpenFromSourceParseErrorFile] -> ShowS)
-> Show OpenFromSourceParseErrorFile
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [OpenFromSourceParseErrorFile] -> ShowS
$cshowList :: [OpenFromSourceParseErrorFile] -> ShowS
show :: OpenFromSourceParseErrorFile -> [Char]
$cshow :: OpenFromSourceParseErrorFile -> [Char]
showsPrec :: Int -> OpenFromSourceParseErrorFile -> ShowS
$cshowsPrec :: Int -> OpenFromSourceParseErrorFile -> ShowS
Show)

instance NFData OpenFromSourceParseErrorFile where
  rnf :: OpenFromSourceParseErrorFile -> ()
rnf !OpenFromSourceParseErrorFile
_ = ()

-- | Exception thrown with 'openFromSource' fails to parse one of the input files.
data OpenFromSourceParseError = OpenFromSourceParseError {
    -- | The file which could not be parsed.
      OpenFromSourceParseError -> OpenFromSourceParseErrorFile
errFile :: OpenFromSourceParseErrorFile
    -- | Parse error encountered opening a spoof checker from source.
    , OpenFromSourceParseError -> ParseError
parseError :: ParseError
    } deriving (Int -> OpenFromSourceParseError -> ShowS
[OpenFromSourceParseError] -> ShowS
OpenFromSourceParseError -> [Char]
(Int -> OpenFromSourceParseError -> ShowS)
-> (OpenFromSourceParseError -> [Char])
-> ([OpenFromSourceParseError] -> ShowS)
-> Show OpenFromSourceParseError
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [OpenFromSourceParseError] -> ShowS
$cshowList :: [OpenFromSourceParseError] -> ShowS
show :: OpenFromSourceParseError -> [Char]
$cshow :: OpenFromSourceParseError -> [Char]
showsPrec :: Int -> OpenFromSourceParseError -> ShowS
$cshowsPrec :: Int -> OpenFromSourceParseError -> ShowS
Show, Typeable)

instance NFData OpenFromSourceParseError where
    rnf :: OpenFromSourceParseError -> ()
rnf OpenFromSourceParseError{ParseError
OpenFromSourceParseErrorFile
parseError :: ParseError
errFile :: OpenFromSourceParseErrorFile
parseError :: OpenFromSourceParseError -> ParseError
errFile :: OpenFromSourceParseError -> OpenFromSourceParseErrorFile
..} = ParseError -> ()
forall a. NFData a => a -> ()
rnf ParseError
parseError () -> () -> ()
`seq` OpenFromSourceParseErrorFile -> ()
forall a. NFData a => a -> ()
rnf OpenFromSourceParseErrorFile
errFile

instance Exception OpenFromSourceParseError

-- | Open a spoof checker for checking Unicode strings for lookalike
-- security issues with default options (all 'SpoofCheck's except
-- 'CharLimit').
open :: IO MSpoof
open :: IO MSpoof
open = IO (Ptr USpoof) -> IO MSpoof
wrap (IO (Ptr USpoof) -> IO MSpoof) -> IO (Ptr USpoof) -> IO MSpoof
forall a b. (a -> b) -> a -> b
$ (Ptr UErrorCode -> IO (Ptr USpoof)) -> IO (Ptr USpoof)
forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError Ptr UErrorCode -> IO (Ptr USpoof)
uspoof_open

isParseError :: ParseError -> Maybe ParseError
isParseError :: ParseError -> Maybe ParseError
isParseError = ParseError -> Maybe ParseError
forall a. a -> Maybe a
Just

-- | Open a spoof checker with custom rules given the UTF-8 encoded
-- contents of the @confusables.txt@ and @confusablesWholeScript.txt@
-- files as described in <http://unicode.org/reports/tr39/ Unicode UAX #39>.
openFromSource :: (ByteString, ByteString) -> IO MSpoof
openFromSource :: (ByteString, ByteString) -> IO MSpoof
openFromSource (ByteString
confusables, ByteString
confusablesWholeScript) =
  ByteString -> (CStringLen -> IO MSpoof) -> IO MSpoof
forall a. ByteString -> (CStringLen -> IO a) -> IO a
unsafeUseAsCStringLen ByteString
confusables ((CStringLen -> IO MSpoof) -> IO MSpoof)
-> (CStringLen -> IO MSpoof) -> IO MSpoof
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
cptr, Int
clen) ->
    ByteString -> (CStringLen -> IO MSpoof) -> IO MSpoof
forall a. ByteString -> (CStringLen -> IO a) -> IO a
unsafeUseAsCStringLen ByteString
confusablesWholeScript ((CStringLen -> IO MSpoof) -> IO MSpoof)
-> (CStringLen -> IO MSpoof) -> IO MSpoof
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
wptr, Int
wlen) ->
      Int32 -> (Ptr Int32 -> IO MSpoof) -> IO MSpoof
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Int32
0 ((Ptr Int32 -> IO MSpoof) -> IO MSpoof)
-> (Ptr Int32 -> IO MSpoof) -> IO MSpoof
forall a b. (a -> b) -> a -> b
$ \Ptr Int32
errTypePtr ->
        (ParseError -> Maybe ParseError)
-> IO MSpoof -> (ParseError -> IO MSpoof) -> IO MSpoof
forall e b a.
Exception e =>
(e -> Maybe b) -> IO a -> (b -> IO a) -> IO a
catchJust
          ParseError -> Maybe ParseError
isParseError
          (IO (Ptr USpoof) -> IO MSpoof
wrap (IO (Ptr USpoof) -> IO MSpoof) -> IO (Ptr USpoof) -> IO MSpoof
forall a b. (a -> b) -> a -> b
$ (ICUError -> Bool)
-> (Ptr ParseError -> Ptr UErrorCode -> IO (Ptr USpoof))
-> IO (Ptr USpoof)
forall a.
(ICUError -> Bool)
-> (Ptr ParseError -> Ptr UErrorCode -> IO a) -> IO a
handleParseError
            (ICUError -> ICUError -> Bool
forall a. Eq a => a -> a -> Bool
== ICUError
u_PARSE_ERROR)
            (Ptr CChar
-> Int32
-> Ptr CChar
-> Int32
-> Ptr Int32
-> Ptr ParseError
-> Ptr UErrorCode
-> IO (Ptr USpoof)
uspoof_openFromSource Ptr CChar
cptr (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
clen) Ptr CChar
wptr
              (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
wlen) Ptr Int32
errTypePtr))
          (Ptr Int32 -> ParseError -> IO MSpoof
forall a. Ptr Int32 -> ParseError -> IO a
throwOpenFromSourceParseError Ptr Int32
errTypePtr)

throwOpenFromSourceParseError :: Ptr Int32 -> ParseError -> IO a
throwOpenFromSourceParseError :: forall a. Ptr Int32 -> ParseError -> IO a
throwOpenFromSourceParseError Ptr Int32
errTypePtr ParseError
parseErr = do
  Int32
errType <- Ptr Int32 -> IO Int32
forall a. Storable a => Ptr a -> IO a
peek Ptr Int32
errTypePtr
  let errFile :: OpenFromSourceParseErrorFile
errFile =
        if Int32
errType Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== Int32
1
{-# LINE 313 "Data/Text/ICU/Spoof.hsc" #-}
        then OpenFromSourceParseErrorFile
ConfusablesTxtError
        -- N.B.: ICU as of 55.1 actually leaves errFile set to 0 in this case.
        else OpenFromSourceParseErrorFile
ConfusablesWholeScriptTxtError
  OpenFromSourceParseError -> IO a
forall e a. Exception e => e -> IO a
throwIO (OpenFromSourceParseError -> IO a)
-> OpenFromSourceParseError -> IO a
forall a b. (a -> b) -> a -> b
$! OpenFromSourceParseErrorFile
-> ParseError -> OpenFromSourceParseError
OpenFromSourceParseError OpenFromSourceParseErrorFile
errFile ParseError
parseErr

-- | Open a spoof checker previously serialized to bytes using 'serialize'.
-- The returned 'MSpoof' will retain a reference to the 'ForeignPtr' inside
-- the ByteString, so ensure its contents do not change for the lifetime
-- of the lifetime of the returned value.
openFromSerialized :: ByteString -> IO MSpoof
openFromSerialized :: ByteString -> IO MSpoof
openFromSerialized ByteString
b =
  case ByteString -> (ForeignPtr Word8, Int, Int)
toForeignPtr ByteString
b of
    (ForeignPtr Word8
ptr, Int
off, Int
len) -> ForeignPtr Word8 -> (Ptr Word8 -> IO MSpoof) -> IO MSpoof
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
ptr ((Ptr Word8 -> IO MSpoof) -> IO MSpoof)
-> (Ptr Word8 -> IO MSpoof) -> IO MSpoof
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p ->
      ForeignPtr Word8 -> IO (Ptr USpoof) -> IO MSpoof
wrapWithSerialized ForeignPtr Word8
ptr (IO (Ptr USpoof) -> IO MSpoof) -> IO (Ptr USpoof) -> IO MSpoof
forall a b. (a -> b) -> a -> b
$ (Ptr UErrorCode -> IO (Ptr USpoof)) -> IO (Ptr USpoof)
forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError
      (Ptr Word8
-> Int32 -> Ptr Int32 -> Ptr UErrorCode -> IO (Ptr USpoof)
uspoof_openFromSerialized (Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
off) (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len) Ptr Int32
forall a. Ptr a
nullPtr)

-- | Get the checks performed by a spoof checker.
getChecks :: MSpoof -> IO [SpoofCheck]
getChecks :: MSpoof -> IO [SpoofCheck]
getChecks MSpoof
s = MSpoof -> (Ptr USpoof -> IO [SpoofCheck]) -> IO [SpoofCheck]
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO [SpoofCheck]) -> IO [SpoofCheck])
-> (Ptr USpoof -> IO [SpoofCheck]) -> IO [SpoofCheck]
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  (Int -> [SpoofCheck]
forall a. (Enum a, Bounded a, ToBitMask a) => Int -> [a]
fromBitMask (Int -> [SpoofCheck]) -> (Int32 -> Int) -> Int32 -> [SpoofCheck]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> (Int32 -> Int32) -> Int32 -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
(.&.) Int32
65535) (Int32 -> [SpoofCheck]) -> IO Int32 -> IO [SpoofCheck]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
{-# LINE 333 "Data/Text/ICU/Spoof.hsc" #-}
  handleError (uspoof_getChecks sptr)

-- | Configure the checks performed by a spoof checker.
setChecks :: MSpoof -> [SpoofCheck] -> IO ()
setChecks :: MSpoof -> [SpoofCheck] -> IO ()
setChecks MSpoof
s [SpoofCheck]
c = MSpoof -> (Ptr USpoof -> IO ()) -> IO ()
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO ()) -> IO ()) -> (Ptr USpoof -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  (Ptr UErrorCode -> IO ()) -> IO ()
forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError ((Ptr UErrorCode -> IO ()) -> IO ())
-> (Ptr UErrorCode -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr USpoof -> Int32 -> Ptr UErrorCode -> IO ()
uspoof_setChecks Ptr USpoof
sptr (Int32 -> Ptr UErrorCode -> IO ())
-> (Int -> Int32) -> Int -> Ptr UErrorCode -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Ptr UErrorCode -> IO ()) -> Int -> Ptr UErrorCode -> IO ()
forall a b. (a -> b) -> a -> b
$ [SpoofCheck] -> Int
forall a. ToBitMask a => a -> Int
toBitMask [SpoofCheck]
c

-- | Get the restriction level of a spoof checker.
getRestrictionLevel :: MSpoof -> IO (Maybe RestrictionLevel)
getRestrictionLevel :: MSpoof -> IO (Maybe RestrictionLevel)
getRestrictionLevel MSpoof
s = MSpoof
-> (Ptr USpoof -> IO (Maybe RestrictionLevel))
-> IO (Maybe RestrictionLevel)
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO (Maybe RestrictionLevel))
 -> IO (Maybe RestrictionLevel))
-> (Ptr USpoof -> IO (Maybe RestrictionLevel))
-> IO (Maybe RestrictionLevel)
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  (Int -> Maybe RestrictionLevel
forall a. (Enum a, Bounded a, ToBitMask a) => Int -> Maybe a
highestValueInBitMask (Int -> Maybe RestrictionLevel)
-> (Int32 -> Int) -> Int32 -> Maybe RestrictionLevel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (Int32 -> Maybe RestrictionLevel)
-> IO Int32 -> IO (Maybe RestrictionLevel)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr USpoof -> IO Int32
uspoof_getRestrictionLevel Ptr USpoof
sptr

-- | Configure the restriction level of a spoof checker.
setRestrictionLevel :: MSpoof -> RestrictionLevel -> IO ()
setRestrictionLevel :: MSpoof -> RestrictionLevel -> IO ()
setRestrictionLevel MSpoof
s RestrictionLevel
l = MSpoof -> (Ptr USpoof -> IO ()) -> IO ()
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO ()) -> IO ()) -> (Ptr USpoof -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
    Ptr USpoof -> Int32 -> IO ()
uspoof_setRestrictionLevel Ptr USpoof
sptr (Int32 -> IO ()) -> (Int -> Int32) -> Int -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ RestrictionLevel -> Int
forall a. ToBitMask a => a -> Int
toBitMask RestrictionLevel
l

-- | Get the list of locale names allowed to be used with a spoof checker.
-- (We don't use 'LocaleName' since the root and default locales have no
-- meaning here.)
getAllowedLocales :: MSpoof -> IO [String]
getAllowedLocales :: MSpoof -> IO [[Char]]
getAllowedLocales MSpoof
s = MSpoof -> (Ptr USpoof -> IO [[Char]]) -> IO [[Char]]
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO [[Char]]) -> IO [[Char]])
-> (Ptr USpoof -> IO [[Char]]) -> IO [[Char]]
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  [Char] -> [[Char]]
splitLocales ([Char] -> [[Char]]) -> IO [Char] -> IO [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Ptr CChar -> IO [Char]
peekCString (Ptr CChar -> IO [Char]) -> IO (Ptr CChar) -> IO [Char]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (Ptr UErrorCode -> IO (Ptr CChar)) -> IO (Ptr CChar)
forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError (Ptr USpoof -> Ptr UErrorCode -> IO (Ptr CChar)
uspoof_getAllowedLocales Ptr USpoof
sptr))
  where splitLocales :: [Char] -> [[Char]]
splitLocales = (Text -> [Char]) -> [Text] -> [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> [Char]
unpack (Text -> [Char]) -> (Text -> Text) -> Text -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
strip) ([Text] -> [[Char]]) -> ([Char] -> [Text]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
splitOn Text
"," (Text -> [Text]) -> ([Char] -> Text) -> [Char] -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
pack

-- | Get the list of locale names allowed to be used with a spoof checker.
-- (We don't use 'LocaleName' since the root and default locales have no
-- meaning here.)
setAllowedLocales :: MSpoof -> [String] -> IO ()
setAllowedLocales :: MSpoof -> [[Char]] -> IO ()
setAllowedLocales MSpoof
s [[Char]]
locs = MSpoof -> (Ptr USpoof -> IO ()) -> IO ()
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO ()) -> IO ()) -> (Ptr USpoof -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  [Char] -> (Ptr CChar -> IO ()) -> IO ()
forall a. [Char] -> (Ptr CChar -> IO a) -> IO a
withCString ([Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"," [[Char]]
locs) ((Ptr CChar -> IO ()) -> IO ()) -> (Ptr CChar -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
lptr ->
    (Ptr UErrorCode -> IO ()) -> IO ()
forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError (Ptr USpoof -> Ptr CChar -> Ptr UErrorCode -> IO ()
uspoof_setAllowedLocales Ptr USpoof
sptr Ptr CChar
lptr)

-- | Check if two strings could be confused with each other.
areConfusable :: MSpoof -> Text -> Text -> IO SpoofCheckResult
areConfusable :: MSpoof -> Text -> Text -> IO SpoofCheckResult
areConfusable MSpoof
s Text
t1 Text
t2 = MSpoof
-> (Ptr USpoof -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO SpoofCheckResult) -> IO SpoofCheckResult)
-> (Ptr USpoof -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  Text
-> (Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a. Text -> (Ptr Word8 -> I8 -> IO a) -> IO a
useAsPtr Text
t1 ((Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult)
-> (Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
t1ptr I8
t1len ->
    Text
-> (Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a. Text -> (Ptr Word8 -> I8 -> IO a) -> IO a
useAsPtr Text
t2 ((Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult)
-> (Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
t2ptr I8
t2len ->
      Int32 -> SpoofCheckResult
makeSpoofCheckResult (Int32 -> SpoofCheckResult) -> IO Int32 -> IO SpoofCheckResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
      (Ptr UErrorCode -> IO Int32) -> IO Int32
forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError (

{-# LINE 374 "Data/Text/ICU/Spoof.hsc" #-}
          Ptr USpoof
-> Ptr Word8
-> Int32
-> Ptr Word8
-> Int32
-> Ptr UErrorCode
-> IO Int32
uspoof_areConfusableUTF8

{-# LINE 378 "Data/Text/ICU/Spoof.hsc" #-}
          Ptr USpoof
sptr Ptr Word8
t1ptr (I8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I8
t1len) Ptr Word8
t2ptr (I8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I8
t2len))

-- | Generates re-usable "skeleton" strings which can be used (via
-- Unicode equality) to check if an identifier is confusable
-- with some large set of existing identifiers.
--
-- If you cache the returned strings in storage, you /must/ invalidate
-- your cache any time the underlying confusables database changes
-- (i.e., on ICU upgrade).
--
-- By default, assumes all input strings have been passed through
-- 'toCaseFold' and are lower-case. To change this, pass
-- 'SkeletonAnyCase'.
--
-- By default, builds skeletons which catch visually confusable
-- characters across multiple scripts.  Pass 'SkeletonSingleScript' to
-- override that behavior and build skeletons which catch visually
-- confusable characters across single scripts.
getSkeleton :: MSpoof -> Maybe SkeletonTypeOverride -> Text -> IO Text
getSkeleton :: MSpoof -> Maybe SkeletonTypeOverride -> Text -> IO Text
getSkeleton MSpoof
s Maybe SkeletonTypeOverride
o Text
t = MSpoof -> (Ptr USpoof -> IO Text) -> IO Text
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO Text) -> IO Text)
-> (Ptr USpoof -> IO Text) -> IO Text
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  Text -> (Ptr Word8 -> I8 -> IO Text) -> IO Text
forall a. Text -> (Ptr Word8 -> I8 -> IO a) -> IO a
useAsPtr Text
t ((Ptr Word8 -> I8 -> IO Text) -> IO Text)
-> (Ptr Word8 -> I8 -> IO Text) -> IO Text
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
tptr I8
tlen ->
    Int
-> (Ptr Word8 -> Int32 -> Ptr UErrorCode -> IO Int32)
-> (Ptr Word8 -> Int -> IO Text)
-> IO Text
forall a b.
Storable a =>
Int
-> (Ptr a -> Int32 -> Ptr UErrorCode -> IO Int32)
-> (Ptr a -> Int -> IO b)
-> IO b
handleOverflowError (I8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral I8
tlen)
      (\Ptr Word8
dptr Int32
dlen ->
          Ptr USpoof
-> Int32
-> Ptr Word8
-> Int32
-> Ptr Word8
-> Int32
-> Ptr UErrorCode
-> IO Int32
getS Ptr USpoof
sptr Int32
oflags Ptr Word8
tptr (I8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I8
tlen) Ptr Word8
dptr (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
dlen))
      (\Ptr Word8
dptr Int
dlen -> Ptr Word8 -> I8 -> IO Text
from Ptr Word8
dptr (Int -> I8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
dlen))
    where oflags :: Int32
oflags = Int32
-> (SkeletonTypeOverride -> Int32)
-> Maybe SkeletonTypeOverride
-> Int32
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int32
0 (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int32)
-> (SkeletonTypeOverride -> Int) -> SkeletonTypeOverride -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SkeletonTypeOverride -> Int
forall a. ToBitMask a => a -> Int
toBitMask) Maybe SkeletonTypeOverride
o
          (Ptr USpoof
-> Int32
-> Ptr Word8
-> Int32
-> Ptr Word8
-> Int32
-> Ptr UErrorCode
-> IO Int32
getS, Ptr Word8 -> I8 -> IO Text
from) =

{-# LINE 406 "Data/Text/ICU/Spoof.hsc" #-}
            (Ptr USpoof
-> Int32
-> Ptr Word8
-> Int32
-> Ptr Word8
-> Int32
-> Ptr UErrorCode
-> IO Int32
uspoof_getSkeletonUTF8, Ptr Word8 -> I8 -> IO Text
fromPtr)

{-# LINE 410 "Data/Text/ICU/Spoof.hsc" #-}
-- | Checks if a string could be confused with any other.
spoofCheck :: MSpoof -> Text -> IO SpoofCheckResult
spoofCheck :: MSpoof -> Text -> IO SpoofCheckResult
spoofCheck MSpoof
s Text
t = MSpoof
-> (Ptr USpoof -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO SpoofCheckResult) -> IO SpoofCheckResult)
-> (Ptr USpoof -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  Text
-> (Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a. Text -> (Ptr Word8 -> I8 -> IO a) -> IO a
useAsPtr Text
t ((Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult)
-> (Ptr Word8 -> I8 -> IO SpoofCheckResult) -> IO SpoofCheckResult
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
tptr I8
tlen ->
    Int32 -> SpoofCheckResult
makeSpoofCheckResult (Int32 -> SpoofCheckResult) -> IO Int32 -> IO SpoofCheckResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Ptr UErrorCode -> IO Int32) -> IO Int32
forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError
      (

{-# LINE 417 "Data/Text/ICU/Spoof.hsc" #-}
       uspoof_checkUTF8

{-# LINE 421 "Data/Text/ICU/Spoof.hsc" #-}
       sptr tptr (fromIntegral tlen) nullPtr)

-- | Serializes the rules in this spoof checker to a byte array,
-- suitable for re-use by 'openFromSerialized'.
--
-- Only includes any data provided to 'openFromSource'. Does not
-- include any other state or configuration.
serialize :: MSpoof -> IO ByteString
serialize :: MSpoof -> IO ByteString
serialize MSpoof
s = MSpoof -> (Ptr USpoof -> IO ByteString) -> IO ByteString
forall a. MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof MSpoof
s ((Ptr USpoof -> IO ByteString) -> IO ByteString)
-> (Ptr USpoof -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr USpoof
sptr ->
  Int
-> (Ptr Word8 -> Int32 -> Ptr UErrorCode -> IO Int32)
-> (Ptr Word8 -> Int -> IO ByteString)
-> IO ByteString
forall a b.
Storable a =>
Int
-> (Ptr a -> Int32 -> Ptr UErrorCode -> IO Int32)
-> (Ptr a -> Int -> IO b)
-> IO b
handleOverflowError Int
0
    (\Ptr Word8
dptr Int32
dlen -> (Ptr USpoof -> Ptr Word8 -> Int32 -> Ptr UErrorCode -> IO Int32
uspoof_serialize Ptr USpoof
sptr Ptr Word8
dptr (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
dlen)))
    (\Ptr Word8
dptr Int
dlen -> Int -> (Ptr Word8 -> IO ()) -> IO ByteString
create (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
dlen) ((Ptr Word8 -> IO ()) -> IO ByteString)
-> (Ptr Word8 -> IO ()) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
bptr ->
      Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy Ptr Word8
dptr Ptr Word8
bptr (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
dlen))

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_open" uspoof_open
    :: Ptr UErrorCode -> IO (Ptr USpoof)

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_openFromSerialized"
  uspoof_openFromSerialized
    :: Ptr Word8 -> Int32 -> Ptr Int32 -> Ptr UErrorCode -> IO (Ptr USpoof)

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_openFromSource"
  uspoof_openFromSource
    :: CString -> Int32 -> CString -> Int32 -> Ptr Int32 -> Ptr UParseError ->
       Ptr UErrorCode -> IO (Ptr USpoof)

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_getChecks"
  uspoof_getChecks
    :: Ptr USpoof -> Ptr UErrorCode -> IO USpoofCheck

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_setChecks"
  uspoof_setChecks
    :: Ptr USpoof -> USpoofCheck -> Ptr UErrorCode -> IO ()

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_getRestrictionLevel"
  uspoof_getRestrictionLevel
    :: Ptr USpoof -> IO URestrictionLevel

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_setRestrictionLevel"
  uspoof_setRestrictionLevel
    :: Ptr USpoof -> URestrictionLevel -> IO ()

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_getAllowedLocales"
  uspoof_getAllowedLocales
    :: Ptr USpoof -> Ptr UErrorCode -> IO CString

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_setAllowedLocales"
  uspoof_setAllowedLocales
    :: Ptr USpoof -> CString -> Ptr UErrorCode -> IO ()


{-# LINE 472 "Data/Text/ICU/Spoof.hsc" #-}

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_areConfusableUTF8"
  uspoof_areConfusableUTF8
    :: Ptr USpoof -> Ptr Word8 -> Int32 -> Ptr Word8 -> Int32 -> Ptr UErrorCode
       -> IO USpoofCheck

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_checkUTF8" uspoof_checkUTF8
    :: Ptr USpoof -> Ptr Word8 -> Int32 -> Ptr Int32 -> Ptr UErrorCode
       -> IO USpoofCheck

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_getSkeletonUTF8"
  uspoof_getSkeletonUTF8
    :: Ptr USpoof -> USkeletonTypeOverride -> Ptr Word8 -> Int32 -> Ptr Word8 ->
       Int32 -> Ptr UErrorCode -> IO Int32


{-# LINE 504 "Data/Text/ICU/Spoof.hsc" #-}

foreign import ccall unsafe "hs_text_icu.h __hs_uspoof_serialize"
  uspoof_serialize
    :: Ptr USpoof -> Ptr Word8 -> Int32 -> Ptr UErrorCode -> IO Int32