Skip to content

Commit

Permalink
openFromSource
Browse files Browse the repository at this point in the history
  • Loading branch information
bhamiltoncx committed Aug 21, 2015
1 parent ffa027c commit 9e290c9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Data/Text/ICU/Spoof.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Data.Text.ICU.Spoof
, SkeletonTypeOverride(..)
-- * Functions
, open
, openFromSource
, getSkeleton
, getChecks
, setChecks
Expand All @@ -37,6 +38,8 @@ module Data.Text.ICU.Spoof

import Control.Applicative
import Data.Bits ((.&.))
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.Int (Int32)
import Data.Maybe (listToMaybe)
import Data.Text (Text)
Expand All @@ -45,6 +48,7 @@ import Data.Text.ICU.BitMask (ToBitMask, fromBitMask, toBitMask)
import Data.Text.ICU.Spoof.Internal (MSpoof, USpoof, Spoof, withSpoof, wrap)
import Data.Text.ICU.Error.Internal (UErrorCode, handleError, handleOverflowError)
import Data.Text.ICU.Internal (UChar)
import Foreign.C.String (CString)
import Foreign.Ptr (Ptr, castPtr, nullPtr)

-- $api
Expand Down Expand Up @@ -124,7 +128,15 @@ makeSpoofCheckResult c =

-- | Open a spoof checker for checking Unicode strings for lookalike security issues.
open :: IO MSpoof
open = wrap =<< (handleError uspoof_open)
open = wrap =<< handleError uspoof_open

-- | Open a spoof checker given the contents of the "confusables.txt" and "confusablesWholeScript.txt"
-- files as described in Unicode UAX #39.
openFromSource :: ByteString -> ByteString -> IO MSpoof
openFromSource confusables confusablesWholeScript =
unsafeUseAsCStringLen confusables $ \(cptr, clen) ->
unsafeUseAsCStringLen confusablesWholeScript $ \(wptr, wlen) ->
wrap =<< handleError (uspoof_openFromSource cptr (fromIntegral clen) wptr (fromIntegral wlen) nullPtr nullPtr)

-- | Get the checks performed by a spoof checker.
getChecks :: MSpoof -> IO SpoofCheckResult
Expand Down Expand Up @@ -182,6 +194,9 @@ spoofCheck s t = do
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_openFromSource" uspoof_openFromSource
:: CString -> Int32 -> CString -> Int32 -> Ptr Int32 -> Ptr Int32 -> Ptr UErrorCode -> IO (Ptr USpoof)

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

Expand Down
10 changes: 10 additions & 0 deletions cbits/text_icu.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ USpoofChecker *__hs_uspoof_open(UErrorCode *status)
return uspoof_open(status);
}

USpoofChecker *__hs_uspoof_openFromSource(const char *confusables, int32_t confusablesLen,
const char *confusablesWholeScript, int32_t confusablesWholeScriptLen,
int32_t *errType, int32_t *unused, /* really UParseError */
UErrorCode *status)
{
return uspoof_openFromSource(confusables, confusablesLen,
confusablesWholeScript, confusablesWholeScriptLen,
errType, NULL, status);
}

void __hs_uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status)
{
uspoof_setChecks(sc, checks, status);
Expand Down
4 changes: 4 additions & 0 deletions include/hs_text_icu.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ int32_t __hs_u_strCompareIter(UCharIterator *iter1, UCharIterator *iter2);
/* uspoof.h */

USpoofChecker *__hs_uspoof_open(UErrorCode *status);
USpoofChecker *__hs_uspoof_openFromSource(const char *confusables, int32_t confusablesLen,
const char *confusablesWholeScript, int32_t confusablesWholeScriptLen,
int32_t *errType, int32_t *unused, /* really UParseError */
UErrorCode *status);
void __hs_uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status);
int32_t __hs_uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status);

Expand Down

0 comments on commit 9e290c9

Please sign in to comment.