Mercurial > gnulib
annotate lib/uniconv/u8-conv-to-enc.c @ 40057:b06060465f09
maint: Run 'make update-copyright'
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 01 Jan 2019 00:25:11 +0100 |
parents | 10eb9086bea0 |
children | 8da9577294da |
rev | line source |
---|---|
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Conversion from UTF-8 to legacy encodings. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2002, 2006-2007, 2009-2019 Free Software Foundation, Inc. |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
9307
ad8a75a45dc9
Change copyright notice from LGPLv2.0+ to LGPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8952
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify it |
ad8a75a45dc9
Change copyright notice from LGPLv2.0+ to LGPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8952
diff
changeset
|
5 under the terms of the GNU Lesser General Public License as published |
ad8a75a45dc9
Change copyright notice from LGPLv2.0+ to LGPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8952
diff
changeset
|
6 by the Free Software Foundation; either version 3 of the License, or |
ad8a75a45dc9
Change copyright notice from LGPLv2.0+ to LGPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8952
diff
changeset
|
7 (at your option) any later version. |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
9307
ad8a75a45dc9
Change copyright notice from LGPLv2.0+ to LGPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8952
diff
changeset
|
12 Lesser General Public License for more details. |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
9307
ad8a75a45dc9
Change copyright notice from LGPLv2.0+ to LGPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8952
diff
changeset
|
14 You should have received a copy of the GNU Lesser General Public License |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 /* Written by Bruno Haible <bruno@clisp.org>. */ |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 #include <config.h> |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 /* Specification. */ |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 #include "uniconv.h" |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 #include <errno.h> |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 #include <stdlib.h> |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 #include <string.h> |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 #include "c-strcaseeq.h" |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 #include "striconveha.h" |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 #include "unistr.h" |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
31 |
11509
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
32 char * |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 u8_conv_to_encoding (const char *tocode, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
34 enum iconv_ilseq_handler handler, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
35 const uint8_t *src, size_t srclen, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
36 size_t *offsets, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
37 char *resultbuf, size_t *lengthp) |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 { |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 if (STRCASEEQ (tocode, "UTF-8", 'U','T','F','-','8',0,0,0,0)) |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 { |
11509
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
41 char *result; |
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
42 |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 /* Conversion from UTF-8 to UTF-8. No need to go through iconv(). */ |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 if (u8_check (src, srclen)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
45 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
46 errno = EILSEQ; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
47 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
48 } |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 /* Memory allocation. */ |
11509
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
51 if (resultbuf != NULL && *lengthp >= srclen) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
52 result = resultbuf; |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
54 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
55 result = (char *) malloc (srclen > 0 ? srclen : 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
56 if (result == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
57 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
58 errno = ENOMEM; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
59 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
60 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
61 } |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 memcpy (result, (const char *) src, srclen); |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 *lengthp = srclen; |
11509
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
65 return result; |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 } |
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 else |
11509
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
68 { |
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
69 char *result = resultbuf; |
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
70 size_t length = *lengthp; |
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
71 |
11510
192bb1b6ecf2
Trivial simplification of last commit.
Bruno Haible <bruno@clisp.org>
parents:
11509
diff
changeset
|
72 if (mem_iconveha ((const char *) src, srclen, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
73 "UTF-8", tocode, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
74 handler == iconveh_question_mark, handler, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
75 offsets, &result, &length) < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
76 return NULL; |
11509
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
77 |
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
78 if (result == NULL) /* when (resultbuf == NULL && length == 0) */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
79 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
80 result = (char *) malloc (1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
81 if (result == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
82 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
83 errno = ENOMEM; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
84 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
85 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11510
diff
changeset
|
86 } |
11509
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
87 *lengthp = length; |
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
88 return result; |
ef4e896a3b4f
Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents:
11508
diff
changeset
|
89 } |
8952
b98315eaaa5f
New module 'uniconv/u8-conv-to-enc'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 } |