1 /* Locale-specific memory transformation
3 Copyright (C) 2006 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert <eggert@cs.ucla.edu>. */
28 /* Store into DEST (of size DESTSIZE) the text in SRC (of size SRCSIZE)
29 transformed so that the result of memcmp on two transformed texts
30 (with ties going to the longer text) is the same as the result of
31 memcoll on the two texts before their transformation. Perhaps
32 temporarily modify the byte after SRC, but restore its original
33 contents before returning.
35 Return the size of the resulting text, or an indeterminate value if
36 there is an error. Set errno to an error number if there is an
37 error, and to zero otherwise. DEST contains an indeterminate value
38 if there is an error or if the resulting size is greater than
42 memxfrm (char *restrict dest
, size_t destsize
,
43 char *restrict src
, size_t srcsize
)
51 char ch
= src
[srcsize
];
56 size_t slen
= strlen (src
+ si
);
58 size_t result0
= result
;
60 result
+= strxfrm (dest
+ di
, src
+ si
, destsize
- di
) + 1;
63 if (result
<= result0
)
69 if (result
== destsize
+ 1 && si
+ slen
== srcsize
)
71 /* The destination is exactly the right size, but strxfrm wants
72 room for a trailing null. Work around the problem with a
74 size_t bufsize
= destsize
- di
+ 1;
77 if (sizeof stackbuf
< bufsize
)
79 buf
= malloc (bufsize
);
83 strxfrm (buf
, src
+ si
, bufsize
);
84 memcpy (dest
+ di
, buf
, destsize
- di
);
85 if (sizeof stackbuf
< bufsize
)
90 di
= (result
< destsize
? result
: destsize
);
95 return result
- (si
!= srcsize
);
99 if (srcsize
< destsize
)
100 memcpy (dest
, src
, srcsize
);