File: | jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c |
Warning: | line 112, column 5 Value stored to 'outputLen' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Oracle designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Oracle in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 | * or visit www.oracle.com if you need additional information or have any |
23 | * questions. |
24 | */ |
25 | #include <stdio.h> |
26 | #include <stddef.h> |
27 | #include <stdlib.h> |
28 | #include <string.h> |
29 | #include <ctype.h> |
30 | #include <locale.h> |
31 | #include <langinfo.h> |
32 | #include <iconv.h> |
33 | |
34 | /* Routines to convert back and forth between Platform Encoding and UTF-8 */ |
35 | |
36 | /* Error and assert macros */ |
37 | #define UTF_ERROR(m)utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 37, m) utfError(__FILE__"/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c", __LINE__37, m) |
38 | #define UTF_ASSERT(x)( (x)==0 ? utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 38, "ASSERT ERROR " "x") : (void)0 ) ( (x)==0 ? UTF_ERROR("ASSERT ERROR " #x)utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 38, "ASSERT ERROR " #x) : (void)0 ) |
39 | #define UTF_DEBUG(x) |
40 | |
41 | /* Global variables */ |
42 | static iconv_t iconvToPlatform = (iconv_t)-1; |
43 | static iconv_t iconvFromPlatform = (iconv_t)-1; |
44 | |
45 | /* |
46 | * Error handler |
47 | */ |
48 | static void |
49 | utfError(char *file, int line, char *message) |
50 | { |
51 | (void)fprintf(stderr, "UTF ERROR [\"%s\":%d]: %s\n", file, line, message)__fprintf_chk (stderr, 2 - 1, "UTF ERROR [\"%s\":%d]: %s\n", file , line, message); |
52 | abort(); |
53 | } |
54 | |
55 | /* |
56 | * Initialize all utf processing. |
57 | */ |
58 | static void |
59 | utfInitialize(void) |
60 | { |
61 | const char* codeset; |
62 | |
63 | #ifndef MACOSX |
64 | /* Set the locale from the environment */ |
65 | (void)setlocale(LC_ALL6, ""); |
66 | |
67 | /* Get the codeset name */ |
68 | codeset = (char*)nl_langinfo(CODESETCODESET); |
69 | if ( codeset == NULL((void*)0) || codeset[0] == 0 ) { |
70 | UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n")); |
71 | return; |
72 | } |
73 | #else /* MACOSX */ |
74 | /* On Mac, platform string (i.e., sun.jnu.encoding value) is always UTF-8 */ |
75 | codeset = "UTF-8"; |
76 | #endif |
77 | |
78 | UTF_DEBUG(("Codeset = %s\n", codeset)); |
79 | |
80 | /* If we don't need this, skip it */ |
81 | if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) { |
82 | UTF_DEBUG(("NO iconv() being used because it is not needed\n")); |
83 | return; |
84 | } |
85 | |
86 | /* Open conversion descriptors */ |
87 | iconvToPlatform = iconv_open(codeset, "UTF-8"); |
88 | if ( iconvToPlatform == (iconv_t)-1 ) { |
89 | UTF_ERROR("Failed to complete iconv_open() setup")utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 89, "Failed to complete iconv_open() setup"); |
90 | } |
91 | iconvFromPlatform = iconv_open("UTF-8", codeset); |
92 | if ( iconvFromPlatform == (iconv_t)-1 ) { |
93 | UTF_ERROR("Failed to complete iconv_open() setup")utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 93, "Failed to complete iconv_open() setup"); |
94 | } |
95 | } |
96 | |
97 | /* |
98 | * Do iconv() conversion. |
99 | * Returns length or -1 if output overflows. |
100 | */ |
101 | static int |
102 | iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen) |
103 | { |
104 | int outputLen = 0; |
105 | |
106 | UTF_ASSERT(bytes)( (bytes)==0 ? utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 106, "ASSERT ERROR " "bytes") : (void)0 ); |
107 | UTF_ASSERT(len>=0)( (len>=0)==0 ? utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 107, "ASSERT ERROR " "len>=0") : (void)0 ); |
108 | UTF_ASSERT(output)( (output)==0 ? utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 108, "ASSERT ERROR " "output") : (void)0 ); |
109 | UTF_ASSERT(outputMaxLen>len)( (outputMaxLen>len)==0 ? utfError("/home/daniel/Projects/java/jdk/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c" , 109, "ASSERT ERROR " "outputMaxLen>len") : (void)0 ); |
110 | |
111 | output[0] = 0; |
112 | outputLen = 0; |
Value stored to 'outputLen' is never read | |
113 | |
114 | if ( ic != (iconv_t)-1 ) { |
115 | int returnValue; |
116 | size_t inLeft; |
117 | size_t outLeft; |
118 | char *inbuf; |
119 | char *outbuf; |
120 | |
121 | inbuf = bytes; |
122 | outbuf = output; |
123 | inLeft = len; |
124 | outLeft = outputMaxLen; |
125 | returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft); |
126 | if ( returnValue >= 0 && inLeft==0 ) { |
127 | outputLen = outputMaxLen-outLeft; |
128 | output[outputLen] = 0; |
129 | return outputLen; |
130 | } |
131 | |
132 | /* Failed to do the conversion */ |
133 | UTF_DEBUG(("iconv() failed to do the conversion\n")); |
134 | return -1; |
135 | } |
136 | |
137 | /* Just copy bytes */ |
138 | outputLen = len; |
139 | (void)memcpy(output, bytes, len); |
140 | output[len] = 0; |
141 | return outputLen; |
142 | } |
143 | |
144 | /* |
145 | * Convert UTF-8 to Platform Encoding. |
146 | * Returns length or -1 if output overflows. |
147 | */ |
148 | static int |
149 | utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen) |
150 | { |
151 | return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen); |
152 | } |
153 | |
154 | int |
155 | convertUtf8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) { |
156 | if (iconvToPlatform == (iconv_t)-1) { |
157 | utfInitialize(); |
158 | } |
159 | return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len); |
160 | } |