1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) Nintendo.  All rights reserved.
4 
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law.  They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10 
11  *---------------------------------------------------------------------------*/
12 
13 #ifndef NN_NGC_CAFE_PROFANITY_FILTER_BASE_H_
14 #define NN_NGC_CAFE_PROFANITY_FILTER_BASE_H_
15 
16 #include <nn/types.h>
17 #include <nn/Result.h>
18 #include <nn/ngc/CAFE/ngc_ProfanityFilterPatternList.h>
19 
20 namespace nn
21 {
22 namespace ngc
23 {
24 namespace Cafe
25 {
26 
27 /*!
28 @addtogroup  nn_ngc_api
29   @{
30 */
31 
32 /*!
33 @brief  Base class for profanity filtering.
34 
35 Due to the trade-off between memory usage and execution speed, different versions of the profanity filter may be released in the future. Any future variations will be derived from this base class.
36 
37 */
38 class ProfanityFilterBase
39 {
40 public:
41     /*!
42 @brief  Destroys the object.
43 */
~ProfanityFilterBase()44     virtual ~ProfanityFilterBase(){}
45 
46     /*!
47 @brief  Gets the version number of the profanity pattern file currently installed on the system.
48 @return  The version number. Versions begin from 1, with higher numbers indicating newer versions. Returns <tt>0</tt> if the attempt to get the value fails.
49 */
50     virtual u32 GetContentVersion() = 0;
51 
52     /*!
53 @brief  Checks a number of specified words against the specified pattern lists to determine whether strings contain profanity and should not be displayed.
54 
55 This function checks single words, such as screen names.
56 It cannot be used to process text added by the user.
57 To check text input, use the <tt>ProfanityFilterBase::MaskProfanityWordsInText</tt> function instead.
58 
59 This operation may take a long time. (It blocks.) Processing time can vary depending on such factors as the version of the system and the length of the specified string.
60 We recommend running this function in a separate thread. Running it in the main thread can cause the frame rate to decrease.
61 You can use this function to check a maximum of <tt>@ref nn::ngc::MAX_WORD_NUM</tt> strings at one time.
62 Checking multiple strings at the same time requires less time than checking each string separately.
63 
64 The results of the check on each string are stored in the <var>pCheckResults</var> array.
65 Check by taking the bitwise AND of this array and the result of left-shifting the value <tt>1</tt> by the value specified in <var>nPatterns</var>.
66 The bit field has a value of <tt>0</tt> if there are no profanity patterns specified in any pattern lists.
67 
68 For more information about the pattern lists that text must be checked against, see the most recent UGC guidelines.
69 
70 The at symbol ("@") could denote an email address. The function always detects strings containing this character as possible disallowed words. However, it does not detect strings containing lots of numbers as problematic, although such strings could be used to display telephone numbers or other sensitive information.
71 Use the <tt>@ref nn::ngc::Cafe::CountNumbers</tt> function to check for this.
72 
73 @param[out] pCheckResults  Specifies the buffer that stores the results of the check. This parameter requires an array with <var>nWordCount</var> elements.
74 @param[in] nPatterns  Specifies which pattern list to check against.
75 @param[in] ppWords  Specifies a <tt>NULL</tt>-terminated string array containing the words to check. Use UTF16-BE (big-endian) encoding.
76 @param[in] nWordCount  Specifies the number of strings to check. The maximum number is <tt>@ref nn::ngc::MAX_WORD_NUM</tt>.
77 
78 @return  Returns the result of execution. The following values are returned.
79 @retval nn::ResultSuccess  Indicates that the check succeeded.
80 @retval ResultNotInitialized  Indicates the object was not initialized.
81 @retval ResultInvalidPointer  Indicates an invalid pointer.
82 @retval ResultInvalidSize  Indicates that the specified number of strings is invalid.
83 @retval ResultReadContentsFailed  Indicates a failure to load the profanity filter. This error is not returned on a normally functioning system.
84 
85 
86 
87 */
88     virtual nn::Result CheckProfanityWords( bit32* pCheckResults, bit32 nPatterns, const wchar_t** ppWords, size_t nWordCount ) = 0;
89 
90     /*!
91 @brief  Checks the specified text against the specified pattern list to determine whether strings contain profanity and should not be displayed.
92 
93 This function processes text strings equal to or longer than a particular length.
94 It cannot process short single words, such as screen names.
95 To check words, use the <tt>ProfanityFilterBase::CheckProfanityWords</tt> function instead.
96 
97 When this function is called, it checks specified strings to determine whether they contain profanity and masks those parts with asterisks (*).
98 
99 This operation may take a long time. (It blocks.) Processing time may vary depending on such factors as the version of the system and the length of the specified string.
100 We recommend running this function in a separate thread. Running it in the main thread can cause the frame rate to decrease.
101 
102 For more information about the pattern lists that text must be checked against, see the most recent UGC guidelines.
103 
104 @param[out] pProfanityWordCount  Specifies a pointer to the number of times profanity appeared in a text. If this information is not required, you can specify <tt>NULL</tt>.
105 @param[in] nPatterns  Specifies the profanity pattern list.
106 @param[in,out] pText  Specifies the pointer to the text that is the subject of the <var>pText</var> check. Places where profanity appears in the text are overwritten by an asterisk.
107 For this reason, you must specify a buffer that the application can write to.
108 A maximum of <tt>@ref nn::ngc::MAX_TEXT_LENGTH</tt> characters, including the terminating null character, can be checked. If a string is longer than this, the part that exceeds this maximum length is not checked.
109 Use UTF16-BE (big-endian) encoding.
110 
111 @return  Returns the result of execution. The following values are returned.
112 @retval nn::ResultSuccess  Indicates that the check succeeded.
113 @retval ResultNotInitialized  Indicates the object was not initialized.
114 @retval ResultInvalidPointer  Indicates an invalid pointer.
115 @retval ResultReadContentsFailed  Indicates a failure to load the profanity filter. This error is not returned on a normally functioning system.
116 
117 
118 
119 
120 */
121     virtual nn::Result MaskProfanityWordsInText( int* pProfanityWordCount, bit32 nPatterns, wchar_t* pText ) = 0;
122 
123 protected:
124     bit32 GetPatternBitsFromRegion( bool bCommunicateWithOtherRegions );
125     static void ConvertUserInputForWord( wchar_t* pConvertedWord, int nLength, const wchar_t* pWord );
126     static void ConvertUserInputForText( wchar_t* pConvertedWord, u8* pSourceLengthMapping, int nLength, const wchar_t* pWord );
127     bool IsIncludesAtSign( const wchar_t* pWord, int nLength );
128     static bool ConvertRegexPatternForText( wchar_t* pConvertedPattern, const wchar_t* pPattern, size_t nLength );
129 
130 private:
131     /*!
132 @brief  Mapping between small and large katakana variants (for example, "ア" versus "ァ").
133 */
134     typedef struct SmallMap
135     {
136         //! Small character (for example, "ァ").
137         wchar_t small;
138 
139         //! Large character (for example, "ア").
140         wchar_t big;
141     } SmallMap;
142 
143     /*!
144 @brief  Mapping between half width and full width katakana characters.
145 */
146     typedef struct HalfwidthMap
147     {
148         //! Single-byte characters.
149         wchar_t half;
150 
151         //! Double-byte characters.
152         wchar_t full;
153     } HalfwidthMap;
154 
155     static void ConvertKatakanaToBig( wchar_t *pKana );
156     static bool ConvertHalfwidthKatakanaToFullwidth( wchar_t *pBuffer, wchar_t half, const HalfwidthMap* pMapping, int mapNum );
157 
158 private:
159     static const SmallMap s_SmallMapping[];
160     static const HalfwidthMap s_HalfMapping[];
161     static const HalfwidthMap s_VoicedMapping[];
162     static const HalfwidthMap s_SemiVoicedMapping[];
163 };
164 
165 //! @}
166 
167 }   // namespace Cafe
168 }   // namespace ngc
169 }   // namespace nn
170 
171 #endif // NN_NGC_CAFE_PROFANITY_FILTER_BASE_H_
172