/src/libidn2/unistring/uniconv/u8-strconv-to-enc.c
Line  | Count  | Source  | 
1  |  | /* Conversion from UTF-8 to legacy encodings.  | 
2  |  |    Copyright (C) 2002, 2006-2007, 2009-2025 Free Software Foundation, Inc.  | 
3  |  |  | 
4  |  |    This file is free software.  | 
5  |  |    It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".  | 
6  |  |    You can redistribute it and/or modify it under either  | 
7  |  |      - the terms of the GNU Lesser General Public License as published  | 
8  |  |        by the Free Software Foundation, either version 3, or (at your  | 
9  |  |        option) any later version, or  | 
10  |  |      - the terms of the GNU General Public License as published by the  | 
11  |  |        Free Software Foundation; either version 2, or (at your option)  | 
12  |  |        any later version, or  | 
13  |  |      - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".  | 
14  |  |  | 
15  |  |    This file is distributed in the hope that it will be useful,  | 
16  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
17  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
18  |  |    Lesser General Public License and the GNU General Public License  | 
19  |  |    for more details.  | 
20  |  |  | 
21  |  |    You should have received a copy of the GNU Lesser General Public  | 
22  |  |    License and of the GNU General Public License along with this  | 
23  |  |    program.  If not, see <https://www.gnu.org/licenses/>.  */  | 
24  |  |  | 
25  |  | /* Written by Bruno Haible <bruno@clisp.org>.  */  | 
26  |  |  | 
27  |  | #include <config.h>  | 
28  |  |  | 
29  |  | /* Specification.  */  | 
30  |  | #include "uniconv.h"  | 
31  |  |  | 
32  |  | #include <errno.h>  | 
33  |  | #include <stdlib.h>  | 
34  |  | #include <string.h>  | 
35  |  |  | 
36  |  | #include "c-strcaseeq.h"  | 
37  |  | #include "striconveha.h"  | 
38  |  | #include "unistr.h"  | 
39  |  |  | 
40  |  | char *  | 
41  |  | u8_strconv_to_encoding (const uint8_t *string,  | 
42  |  |                         const char *tocode,  | 
43  |  |                         enum iconv_ilseq_handler handler)  | 
44  | 489  | { | 
45  | 489  |   char *result;  | 
46  | 489  |   size_t length;  | 
47  |  |  | 
48  | 489  |   if (STRCASEEQ (tocode, "UTF-8", 'U','T','F','-','8',0,0,0,0))  | 
49  | 0  |     { | 
50  |  |       /* Conversion from UTF-8 to UTF-8.  No need to go through iconv().  */  | 
51  | 0  |       length = u8_strlen (string) + 1;  | 
52  | 0  |       if (u8_check (string, length))  | 
53  | 0  |         { | 
54  | 0  |           errno = EILSEQ;  | 
55  | 0  |           return NULL;  | 
56  | 0  |         }  | 
57  | 0  |       result = (char *) malloc (length);  | 
58  | 0  |       if (result == NULL)  | 
59  | 0  |         { | 
60  | 0  |           errno = ENOMEM;  | 
61  | 0  |           return NULL;  | 
62  | 0  |         }  | 
63  | 0  |       memcpy (result, (const char *) string, length);  | 
64  | 0  |       return result;  | 
65  | 0  |     }  | 
66  | 489  |   else  | 
67  | 489  |     { | 
68  | 489  |       result = NULL;  | 
69  | 489  |       length = 0;  | 
70  | 489  |       if (mem_iconveha ((const char *) string, u8_strlen (string) + 1,  | 
71  | 489  |                         "UTF-8", tocode,  | 
72  | 489  |                         handler == iconveh_question_mark, handler,  | 
73  | 489  |                         NULL, &result, &length) < 0)  | 
74  | 222  |         return NULL;  | 
75  |  |       /* Verify the result has exactly one NUL byte, at the end.  */  | 
76  | 267  |       if (!(length > 0 && result[length-1] == '\0'  | 
77  | 267  |             && strlen (result) == length-1))  | 
78  | 0  |         { | 
79  | 0  |           free (result);  | 
80  | 0  |           errno = EILSEQ;  | 
81  | 0  |           return NULL;  | 
82  | 0  |         }  | 
83  | 267  |       return result;  | 
84  | 267  |     }  | 
85  | 489  | }  |