| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 1998-2002 Pham Kim Long <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "charset.h" |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#if defined(_WIN32) |
| 13 |
#include <fcntl.h> |
| 14 |
#include <io.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#include "vnconv.h" |
| 18 |
|
| 19 |
int vnFileStreamConvert(int inCharset, int outCharset, FILE *inf, FILE *outf); |
| 20 |
|
| 21 |
DllExport int genConvert(VnCharset &incs, VnCharset &outcs, ByteInStream &input, |
| 22 |
ByteOutStream &output) { |
| 23 |
StdVnChar stdChar; |
| 24 |
int bytesRead, bytesWritten; |
| 25 |
|
| 26 |
incs.startInput(); |
| 27 |
outcs.startOutput(); |
| 28 |
|
| 29 |
int ret = 1; |
| 30 |
while (!input.eos()) { |
| 31 |
stdChar = 0; |
| 32 |
if (incs.nextInput(input, stdChar, bytesRead)) { |
| 33 |
if (stdChar != INVALID_STD_CHAR) { |
| 34 |
if (VnCharsetLibObj.m_options.toLower) |
| 35 |
stdChar = StdVnToLower(stdChar); |
| 36 |
else if (VnCharsetLibObj.m_options.toUpper) |
| 37 |
stdChar = StdVnToUpper(stdChar); |
| 38 |
if (VnCharsetLibObj.m_options.removeTone) |
| 39 |
stdChar = StdVnGetRoot(stdChar); |
| 40 |
ret = outcs.putChar(output, stdChar, bytesWritten); |
| 41 |
} |
| 42 |
} else |
| 43 |
break; |
| 44 |
} |
| 45 |
return (ret ? 0 : VNCONV_OUT_OF_MEMORY); |
| 46 |
} |
| 47 |
|
| 48 |
//---------------------------------------------- |
| 49 |
// Arguments: |
| 50 |
// inCharset: charset of input |
| 51 |
// outCharset: charset of output |
| 52 |
// input: input data |
| 53 |
// output: output data |
| 54 |
// inLen: [in] size of input. if inLen = -1, input data is |
| 55 |
// null-terminated. |
| 56 |
// [out] if input inLen != -1, output iLen is the numbers of byte |
| 57 |
// left in input. |
| 58 |
// maxOutLen: [in] size of output. |
| 59 |
// [out] number of bytes output, if enough memory |
| 60 |
// number of bytes needed for output, if not enough |
| 61 |
// memory |
| 62 |
// Returns: 0 if successful |
| 63 |
// error code: if failed |
| 64 |
//---------------------------------------------- |
| 65 |
// int VnConvert(int inCharset, int outCharset, UKBYTE *input, UKBYTE *output, |
| 66 |
// int & inLen, int & maxOutLen) |
| 67 |
|
| 68 |
DllExport int VnConvert(int inCharset, int outCharset, UKBYTE *input, |
| 69 |
UKBYTE *output, int *pInLen, int *pMaxOutLen) { |
| 70 |
int inLen, maxOutLen; |
| 71 |
int ret = -1; |
| 72 |
|
| 73 |
inLen = *pInLen; |
| 74 |
maxOutLen = *pMaxOutLen; |
| 75 |
|
| 76 |
if (inLen != -1 && inLen < 0) // invalid inLen |
| 77 |
return ret; |
| 78 |
|
| 79 |
VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset); |
| 80 |
VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset); |
| 81 |
|
| 82 |
if (!pInCharset || !pOutCharset) |
| 83 |
return VNCONV_INVALID_CHARSET; |
| 84 |
|
| 85 |
StringBIStream is(input, inLen, pInCharset->elementSize()); |
| 86 |
StringBOStream os(output, maxOutLen); |
| 87 |
|
| 88 |
ret = genConvert(*pInCharset, *pOutCharset, is, os); |
| 89 |
*pMaxOutLen = os.getOutBytes(); |
| 90 |
*pInLen = is.left(); |
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
//--------------------------------------- |
| 95 |
// Arguments: |
| 96 |
// inFile: input file name. NULL if STDIN is used |
| 97 |
// outFile: output file name, NULL if STDOUT is used |
| 98 |
// Returns: |
| 99 |
// 0: successful |
| 100 |
// errCode: if failed |
| 101 |
//--------------------------------------- |
| 102 |
DllExport int VnFileConvert(int inCharset, int outCharset, const char *inFile, |
| 103 |
const char *outFile) { |
| 104 |
FILE *inf = NULL; |
| 105 |
FILE *outf = NULL; |
| 106 |
int ret = 0; |
| 107 |
char tmpName[32]; |
| 108 |
|
| 109 |
if (inFile == NULL) { |
| 110 |
inf = stdin; |
| 111 |
#if defined(_WIN32) |
| 112 |
_setmode(_fileno(stdin), _O_BINARY); |
| 113 |
#endif |
| 114 |
} else { |
| 115 |
inf = fopen(inFile, "rb"); |
| 116 |
if (inf == NULL) { |
| 117 |
ret = VNCONV_ERR_INPUT_FILE; |
| 118 |
goto end; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if (outFile == NULL) |
| 123 |
outf = stdout; |
| 124 |
else { |
| 125 |
// setup temporary output file (because real output file may be the same |
| 126 |
// as input file |
| 127 |
char outDir[256]; |
| 128 |
strcpy(outDir, outFile); |
| 129 |
|
| 130 |
#if defined(_WIN32) |
| 131 |
char *p = strrchr(outDir, '\\'); |
| 132 |
#else |
| 133 |
char *p = strrchr(outDir, '/'); |
| 134 |
#endif |
| 135 |
|
| 136 |
if (p == NULL) |
| 137 |
outDir[0] = 0; |
| 138 |
else |
| 139 |
*p = 0; |
| 140 |
|
| 141 |
strcpy(tmpName, outDir); |
| 142 |
strcat(tmpName, "XXXXXX"); |
| 143 |
|
| 144 |
if (mkstemp(tmpName) == -1) { |
| 145 |
fclose(inf); |
| 146 |
ret = VNCONV_ERR_OUTPUT_FILE; |
| 147 |
goto end; |
| 148 |
} |
| 149 |
outf = fopen(tmpName, "wb"); |
| 150 |
|
| 151 |
if (outf == NULL) { |
| 152 |
fclose(inf); |
| 153 |
ret = VNCONV_ERR_OUTPUT_FILE; |
| 154 |
goto end; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
ret = vnFileStreamConvert(inCharset, outCharset, inf, outf); |
| 159 |
if (inf != stdin) |
| 160 |
fclose(inf); |
| 161 |
if (outf != stdout) { |
| 162 |
fclose(outf); |
| 163 |
|
| 164 |
// delete output file if exisits |
| 165 |
if (ret == 0) { |
| 166 |
remove(outFile); |
| 167 |
#if !defined(_WIN32) |
| 168 |
char cmd[256]; |
| 169 |
sprintf(cmd, "mv %s %s", tmpName, outFile); |
| 170 |
cmd[0] = system(cmd); |
| 171 |
#else |
| 172 |
if (rename(tmpName, outFile) != 0) { |
| 173 |
remove(tmpName); |
| 174 |
ret = VNCONV_ERR_OUTPUT_FILE; |
| 175 |
goto end; |
| 176 |
} |
| 177 |
#endif |
| 178 |
} else |
| 179 |
remove(tmpName); |
| 180 |
} |
| 181 |
|
| 182 |
end: |
| 183 |
#if defined(_WIN32) |
| 184 |
if (inf == stdin) { |
| 185 |
_setmode(_fileno(stdin), _O_BINARY); |
| 186 |
} |
| 187 |
#endif |
| 188 |
return ret; |
| 189 |
} |
| 190 |
|
| 191 |
//------------------------------------------------ |
| 192 |
// Returns: |
| 193 |
// 0: successful |
| 194 |
// errCode: if failed |
| 195 |
//--------------------------------------- |
| 196 |
int vnFileStreamConvert(int inCharset, int outCharset, FILE *inf, FILE *outf) { |
| 197 |
VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset); |
| 198 |
VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset); |
| 199 |
|
| 200 |
if (!pInCharset || !pOutCharset) |
| 201 |
return VNCONV_INVALID_CHARSET; |
| 202 |
|
| 203 |
if (outCharset == CONV_CHARSET_UNICODE) { |
| 204 |
UKWORD sign = 0xFEFF; |
| 205 |
fwrite(&sign, sizeof(UKWORD), 1, outf); |
| 206 |
} |
| 207 |
|
| 208 |
FileBIStream is; |
| 209 |
FileBOStream os; |
| 210 |
|
| 211 |
is.attach(inf); |
| 212 |
os.attach(outf); |
| 213 |
|
| 214 |
return genConvert(*pInCharset, *pOutCharset, is, os); |
| 215 |
} |
| 216 |
|
| 217 |
const char *ErrTable[VNCONV_LAST_ERROR] = { |
| 218 |
"No error", |
| 219 |
"Unknown error", |
| 220 |
"Invalid charset", |
| 221 |
"Error opening input file", |
| 222 |
"Error opening output file", |
| 223 |
"Error writing to output stream", |
| 224 |
"Not enough memory", |
| 225 |
}; |
| 226 |
|
| 227 |
DllExport const char *VnConvErrMsg(int errCode) { |
| 228 |
if (errCode < 0 || errCode >= VNCONV_LAST_ERROR) |
| 229 |
errCode = VNCONV_UNKNOWN_ERROR; |
| 230 |
return ErrTable[errCode]; |
| 231 |
} |