2 *******************************************************************************
4 * Copyright (C) 2003-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2003jul11
14 * created by: Ram Viswanadha
17 #include "unicode/utypes.h"
25 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
26 #define NFS4_MAX_BUFFER_SIZE 1000
27 #define PREFIX_SUFFIX_SEPARATOR 0x0040 /* '@' */
30 const char* NFS4DataFileNames
[5] ={
40 nfs4_prepare( const char* src
, int32_t srcLength
,
41 char* dest
, int32_t destCapacity
,
42 NFS4ProfileState state
,
43 UParseError
* parseError
,
46 UChar b1Stack
[NFS4_MAX_BUFFER_SIZE
],
47 b2Stack
[NFS4_MAX_BUFFER_SIZE
];
48 char b3Stack
[NFS4_MAX_BUFFER_SIZE
];
50 /* initialize pointers to stack buffers */
51 UChar
*b1
= b1Stack
, *b2
= b2Stack
;
53 int32_t b1Len
=0, b2Len
=0, b3Len
=0,
54 b1Capacity
= NFS4_MAX_BUFFER_SIZE
,
55 b2Capacity
= NFS4_MAX_BUFFER_SIZE
,
56 b3Capacity
= NFS4_MAX_BUFFER_SIZE
,
59 UStringPrepProfile
* profile
= NULL
;
60 /* get the test data path */
61 const char *testdatapath
= NULL
;
63 if(status
==NULL
|| U_FAILURE(*status
)){
66 if((src
==NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
67 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
70 testdatapath
= loadTestData(status
);
72 /* convert the string from UTF-8 to UTF-16 */
73 u_strFromUTF8(b1
,b1Capacity
,&b1Len
,src
,srcLength
,status
);
74 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
76 /* reset the status */
77 *status
= U_ZERO_ERROR
;
79 b1
= (UChar
*) malloc(b1Len
* U_SIZEOF_UCHAR
);
81 *status
= U_MEMORY_ALLOCATION_ERROR
;
86 u_strFromUTF8(b1
, b1Capacity
, &b1Len
, src
, srcLength
, status
);
89 /* open the profile */
90 profile
= usprep_open(testdatapath
, NFS4DataFileNames
[state
], status
);
91 /* prepare the string */
92 b2Len
= usprep_prepare(profile
, b1
, b1Len
, b2
, b2Capacity
, USPREP_DEFAULT
, parseError
, status
);
93 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
94 *status
= U_ZERO_ERROR
;
95 b2
= (UChar
*) malloc(b2Len
* U_SIZEOF_UCHAR
);
97 *status
= U_MEMORY_ALLOCATION_ERROR
;
100 b2Len
= usprep_prepare(profile
, b1
, b1Len
, b2
, b2Len
, USPREP_DEFAULT
, parseError
, status
);
103 /* convert the string back to UTF-8 */
104 u_strToUTF8(b3
,b3Capacity
, &b3Len
, b2
, b2Len
, status
);
105 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
106 *status
= U_ZERO_ERROR
;
107 b3
= (char*) malloc(b3Len
);
109 *status
= U_MEMORY_ALLOCATION_ERROR
;
113 u_strToUTF8(b3
,b3Capacity
, &b3Len
, b2
, b2Len
, status
);
117 if(dest
!=NULL
&& reqLength
<= destCapacity
){
118 memmove(dest
, b3
, reqLength
);
132 return u_terminateChars(dest
, destCapacity
, reqLength
, status
);
135 /* sorted array for binary search*/
136 static const char* special_prefixes
[]={
137 "\x0041\x004e\x004f\x004e\x0059\x004d\x004f\x0055\x0053",
138 "\x0041\x0055\x0054\x0048\x0045\x004e\x0054\x0049\x0043\x0041\x0054\x0045\x0044",
139 "\x0042\x0041\x0054\x0043\x0048",
140 "\x0044\x0049\x0041\x004c\x0055\x0050",
141 "\x0045\x0056\x0045\x0052\x0059\x004f\x004e\x0045",
142 "\x0047\x0052\x004f\x0055\x0050",
143 "\x0049\x004e\x0054\x0045\x0052\x0041\x0043\x0054\x0049\x0056\x0045",
144 "\x004e\x0045\x0054\x0057\x004f\x0052\x004b",
145 "\x004f\x0057\x004e\x0045\x0052",
149 /* binary search the sorted array */
151 findStringIndex(const char* const *sortedArr
, int32_t sortedArrLen
, const char* target
, int32_t targetLen
){
153 int left
, middle
, right
,rc
;
156 right
= sortedArrLen
-1;
158 while(left
<= right
){
159 middle
= (left
+right
)/2;
160 rc
=strncmp(sortedArr
[middle
],target
, targetLen
);
174 getPrefixSuffix(const char *src
, int32_t srcLength
,
175 const char **prefix
, int32_t *prefixLen
,
176 const char **suffix
, int32_t *suffixLen
,
182 if(src
[i
] == PREFIX_SUFFIX_SEPARATOR
){
183 if((i
+1) == srcLength
){
184 /* we reached the end of the string */
189 i
++;/* the prefix contains the separator */
196 *suffixLen
= srcLength
- i
;
197 /* special prefixes must not be followed by suffixes! */
198 if((findStringIndex(special_prefixes
,LENGTHOF(special_prefixes
), *prefix
, *prefixLen
-1) != -1) && (*suffix
!= NULL
)){
199 *status
= U_PARSE_ERROR
;
206 nfs4_mixed_prepare( const char* src
, int32_t srcLength
,
207 char* dest
, int32_t destCapacity
,
208 UParseError
* parseError
,
211 const char *prefix
= NULL
, *suffix
= NULL
;
212 int32_t prefixLen
=0, suffixLen
=0;
213 char pStack
[NFS4_MAX_BUFFER_SIZE
],
214 sStack
[NFS4_MAX_BUFFER_SIZE
];
215 char *p
=pStack
, *s
=sStack
;
216 int32_t pLen
=0, sLen
=0, reqLen
=0,
217 pCapacity
= NFS4_MAX_BUFFER_SIZE
,
218 sCapacity
= NFS4_MAX_BUFFER_SIZE
;
221 if(status
==NULL
|| U_FAILURE(*status
)){
224 if((src
==NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
225 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
229 srcLength
= (int32_t)strlen(src
);
231 getPrefixSuffix(src
, srcLength
, &prefix
, &prefixLen
, &suffix
, &suffixLen
, status
);
233 /* prepare the prefix */
234 pLen
= nfs4_prepare(prefix
, prefixLen
, p
, pCapacity
, NFS4_MIXED_PREP_PREFIX
, parseError
, status
);
235 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
236 *status
= U_ZERO_ERROR
;
237 p
= (char*) malloc(pLen
);
239 *status
= U_MEMORY_ALLOCATION_ERROR
;
242 pLen
= nfs4_prepare(prefix
, prefixLen
, p
, pLen
, NFS4_MIXED_PREP_PREFIX
, parseError
, status
);
245 /* prepare the suffix */
247 sLen
= nfs4_prepare(suffix
, suffixLen
, s
, sCapacity
, NFS4_MIXED_PREP_SUFFIX
, parseError
, status
);
248 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
249 *status
= U_ZERO_ERROR
;
250 s
= (char*) malloc(pLen
);
252 *status
= U_MEMORY_ALLOCATION_ERROR
;
255 sLen
= nfs4_prepare(suffix
, suffixLen
, s
, sLen
, NFS4_MIXED_PREP_SUFFIX
, parseError
, status
);
258 reqLen
= pLen
+sLen
+1 /* for the delimiter */;
259 if(dest
!= NULL
&& reqLen
<= destCapacity
){
260 memmove(dest
, p
, pLen
);
263 dest
[pLen
++] = PREFIX_SUFFIX_SEPARATOR
;
264 memmove(dest
+pLen
, s
, sLen
);
276 return u_terminateChars(dest
, destCapacity
, reqLen
, status
);
280 nfs4_cis_prepare( const char* src
, int32_t srcLength
,
281 char* dest
, int32_t destCapacity
,
282 UParseError
* parseError
,
284 return nfs4_prepare(src
, srcLength
, dest
, destCapacity
, NFS4_CIS_PREP
, parseError
, status
);
289 nfs4_cs_prepare( const char* src
, int32_t srcLength
,
290 char* dest
, int32_t destCapacity
,
291 UBool isCaseSensitive
,
292 UParseError
* parseError
,
295 return nfs4_prepare(src
, srcLength
, dest
, destCapacity
, NFS4_CS_PREP_CS
, parseError
, status
);
297 return nfs4_prepare(src
, srcLength
, dest
, destCapacity
, NFS4_CS_PREP_CI
, parseError
, status
);
303 * Hey, Emacs, please set the following:
306 * indent-tabs-mode: nil