OSDN Git Service

Enlarge buffers for replies on transferring files.
[ffftp/ffftp.git] / utf8msg / utf8msg.cpp
1 // utf8msg.cpp : \83R\83\93\83\\81[\83\8b \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82Ì\83G\83\93\83g\83\8a \83|\83C\83\93\83g\82ð\92è\8b`\82µ\82Ü\82·\81B\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 \r
6 \r
7 int EncodeText(char* pOut, const char* pIn, int Count)\r
8 {\r
9         const char* pInEnd;\r
10         char* pOutBegin;\r
11         bool bEncoded;\r
12         pInEnd = pIn + Count;\r
13         pOutBegin = pOut;\r
14         bEncoded = false;\r
15         while(pIn < pInEnd)\r
16         {\r
17                 switch(*pIn)\r
18                 {\r
19                 case '0':\r
20                 case '1':\r
21                 case '2':\r
22                 case '3':\r
23                 case '4':\r
24                 case '5':\r
25                 case '6':\r
26                 case '7':\r
27                 case '8':\r
28                 case '9':\r
29                 case 'A':\r
30                 case 'B':\r
31                 case 'C':\r
32                 case 'D':\r
33                 case 'E':\r
34                 case 'F':\r
35                 case 'a':\r
36                 case 'b':\r
37                 case 'c':\r
38                 case 'd':\r
39                 case 'e':\r
40                 case 'f':\r
41                         if(bEncoded)\r
42                         {\r
43                                 sprintf(pOut, "\\x%02X", (unsigned char)*pIn);\r
44                                 pOut += 4;\r
45                         }\r
46                         else\r
47                         {\r
48                                 *pOut = *pIn;\r
49                                 pOut++;\r
50                         }\r
51                         break;\r
52                 default:\r
53                         if(*pIn & 0x80)\r
54                         {\r
55                                 bEncoded = true;\r
56                                 sprintf(pOut, "\\x%02X", (unsigned char)*pIn);\r
57                                 pOut += 4;\r
58                         }\r
59                         else\r
60                         {\r
61                                 bEncoded = false;\r
62                                 *pOut = *pIn;\r
63                                 pOut++;\r
64                         }\r
65                         break;\r
66                 }\r
67                 pIn++;\r
68         }\r
69         return (int)((size_t)pOut - (size_t)pOutBegin);\r
70 }\r
71 \r
72 char* FindQuote(const char* pIn, int Count)\r
73 {\r
74         const char* pInEnd;\r
75         bool bEscape;\r
76         pInEnd = pIn + Count;\r
77         bEscape = false;\r
78         while(pIn < pInEnd)\r
79         {\r
80                 switch(*pIn)\r
81                 {\r
82                 case '\\':\r
83                         bEscape = !bEscape;\r
84                         break;\r
85                 case '\"':\r
86                         if(!bEscape)\r
87                                 return (char*)pIn;\r
88                         bEscape = false;\r
89                         break;\r
90                 default:\r
91                         bEscape = false;\r
92                         break;\r
93                 }\r
94                 pIn++;\r
95         }\r
96         return NULL;\r
97 }\r
98 \r
99 char* FindEndOfLine(const char* pIn, int Count)\r
100 {\r
101         const char* pInEnd;\r
102         pInEnd = pIn + Count;\r
103         while(pIn < pInEnd)\r
104         {\r
105                 switch(*pIn)\r
106                 {\r
107                 case '\r':\r
108                 case '\n':\r
109                         if(pIn < pInEnd && *pIn == '\r')\r
110                                 pIn++;\r
111                         if(pIn < pInEnd && *pIn == '\n')\r
112                                 pIn++;\r
113                         return (char*)pIn;\r
114                         break;\r
115                 }\r
116                 pIn++;\r
117         }\r
118         return (char*)pIn;\r
119 }\r
120 \r
121 int _tmain(int argc, _TCHAR* argv[])\r
122 {\r
123         FILE* fpIn;\r
124         FILE* fpOut;\r
125         long InLength;\r
126         long OutLength;\r
127         char* pInBuffer;\r
128         char* pOutBuffer;\r
129         char* pIn;\r
130         char* pOut;\r
131         char* pInEnd;\r
132         char* pOutEnd;\r
133         int Count;\r
134         bool bString;\r
135         char* pInNext;\r
136         char* pInQuote;\r
137         char* pSrc;\r
138         int SrcCount;\r
139         _tsetlocale(LC_ALL, _T(""));\r
140         if(argc != 3)\r
141         {\r
142                 _tprintf(_T("UTF-8\82Å\8f\91\82©\82ê\82½C\8c¾\8cê\83\\81[\83X\83t\83@\83C\83\8b\93à\82Ì\95\8e\9a\97ñ\83\8a\83e\83\89\83\8b\82ð\83G\83\93\83R\81[\83h\82µ\82Ü\82·\81B\n"));\r
143                 _tprintf(_T("\8ds\92\86\82Å\8dÅ\8f\89\82É\8fo\8c»\82·\82é\95\8e\9a\97ñ\83\8a\83e\83\89\83\8b\82Í\82»\82Ì\82Ü\82Ü\95Û\8e\9d\82³\82ê\82Ü\82·\81B\n"));\r
144                 _tprintf(_T("\8ds\92\86\82Å2\94Ô\96Ú\88È\8d~\82É\8fo\8c»\82·\82é\95\8e\9a\97ñ\83\8a\83e\83\89\83\8b\82Í\8dÅ\8f\89\82Ì\82à\82Ì\82Å\83G\83\93\83R\81[\83h\82³\82ê\82Ü\82·\81B\n"));\r
145                 _tprintf(_T("\83R\83}\83\93\83h\83\89\83C\83\93\n"));\r
146                 _tprintf(_T("mbtoutf8 [in] [out]\n"));\r
147                 _tprintf(_T("[in]    \8c³\82Ì\83\\81[\83X\83t\83@\83C\83\8b\82Ì\83t\83@\83C\83\8b\96¼\n"));\r
148                 _tprintf(_T("[out]   \95Û\91\90æ\82Ì\83t\83@\83C\83\8b\96¼\n"));\r
149                 return 0;\r
150         }\r
151         fpIn = _tfopen(argv[1], _T("rb"));\r
152         if(!fpIn)\r
153         {\r
154                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);\r
155                 return 0;\r
156         }\r
157         if(fseek(fpIn, 0, SEEK_END) != 0)\r
158         {\r
159                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);\r
160                 return 0;\r
161         }\r
162         InLength = ftell(fpIn);\r
163         if(fseek(fpIn, 0, SEEK_SET) != 0)\r
164         {\r
165                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);\r
166                 return 0;\r
167         }\r
168         pInBuffer = (char*)malloc(InLength);\r
169         if(!pInBuffer)\r
170         {\r
171                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);\r
172                 return 0;\r
173         }\r
174         if((long)fread(pInBuffer, 1, InLength, fpIn) != InLength)\r
175         {\r
176                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);\r
177                 return 0;\r
178         }\r
179         OutLength = InLength * 4;\r
180         pOutBuffer = (char*)malloc(OutLength);\r
181         if(!pOutBuffer)\r
182         {\r
183                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);\r
184                 return 0;\r
185         }\r
186         pIn = pInBuffer;\r
187         pOut = pOutBuffer;\r
188         pInEnd = pInBuffer + InLength;\r
189         pOutEnd = pOutBuffer + OutLength;\r
190         while(pIn < pInEnd)\r
191         {\r
192                 bString = false;\r
193                 pSrc = NULL;\r
194                 Count = (int)((size_t)pInEnd - (size_t)pIn);\r
195                 pInNext = FindEndOfLine(pIn, Count);\r
196                 while(pInQuote = FindQuote(pIn, (int)((size_t)pInNext - (size_t)pIn)))\r
197                 {\r
198                         if(bString)\r
199                         {\r
200                                 bString = false;\r
201                                 if(pSrc)\r
202                                 {\r
203                                         pIn = pInQuote + 1;\r
204                                         pOut += EncodeText(pOut, pSrc, SrcCount);\r
205                                 }\r
206                                 else\r
207                                 {\r
208                                         pSrc = pIn;\r
209                                         SrcCount = (int)((size_t)pInQuote - (size_t)pIn) + 1;\r
210                                         Count = (int)((size_t)pInQuote - (size_t)pIn) + 1;\r
211                                         memcpy(pOut, pIn, Count);\r
212                                         pIn += Count;\r
213                                         pOut += Count;\r
214                                 }\r
215                         }\r
216                         else\r
217                         {\r
218                                 bString = true;\r
219                                 Count = (int)((size_t)pInQuote - (size_t)pIn) + 1;\r
220                                 memcpy(pOut, pIn, Count);\r
221                                 pIn += Count;\r
222                                 pOut += Count;\r
223                         }\r
224                 }\r
225                 Count = (int)((size_t)pInNext - (size_t)pIn);\r
226                 memcpy(pOut, pIn, Count);\r
227                 pIn += Count;\r
228                 pOut += Count;\r
229         }\r
230         OutLength = (long)((size_t)pOut - (size_t)pOutBuffer);\r
231         fpIn = _tfopen(argv[2], _T("rb"));\r
232         if(fpIn)\r
233         {\r
234                 if(fseek(fpIn, 0, SEEK_END) != 0)\r
235                 {\r
236                         _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
237                         return 0;\r
238                 }\r
239                 InLength = ftell(fpIn);\r
240                 if(fseek(fpIn, 0, SEEK_SET) != 0)\r
241                 {\r
242                         _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
243                         return 0;\r
244                 }\r
245                 pInBuffer = (char*)malloc(InLength);\r
246                 if(!pInBuffer)\r
247                 {\r
248                         _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
249                         return 0;\r
250                 }\r
251                 if((long)fread(pInBuffer, 1, InLength, fpIn) != InLength)\r
252                 {\r
253                         _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
254                         return 0;\r
255                 }\r
256                 if(InLength == OutLength && memcmp(pInBuffer, pOutBuffer, InLength) == 0)\r
257                 {\r
258                         _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82Í\95Ï\8a·\82Ì\95K\97v\82ª\82 \82è\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
259                         return 0;\r
260                 }\r
261         }\r
262         fpOut = _tfopen(argv[2], _T("wb"));\r
263         if(!fpOut)\r
264         {\r
265                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8dì\90¬\82Å\82«\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
266                 return 0;\r
267         }\r
268         if((long)fwrite(pOutBuffer, 1, OutLength, fpOut) != OutLength)\r
269         {\r
270                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8dì\90¬\82Å\82«\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
271                 return 0;\r
272         }\r
273         _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82Í\90³\8fí\82É\95Ï\8a·\82³\82ê\82Ü\82µ\82½\81B\n"), argv[2]);\r
274         return 0;\r
275 }\r
276 \r