1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/* $Id: dos2unix.c 3114 2017-10-29 18:02:04Z bird $ */
/** @file
* dos2unix - Line ending conversion routines.
*/
/*
* Copyright (c) 2017 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Alternatively, the content of this file may be used under the terms of the
* GPL version 2 or later, or LGPL version 2.1 or later.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include "dos2unix.h"
#include <k/kDefs.h>
#include <errno.h>
#include <fcntl.h>
#if K_OS == K_OS_WINDOWS
# include <io.h>
#else
# include <unistd.h>
#endif
#include <assert.h>
#ifndef O_BINARY
# ifdef _O_BINARY
# define O_BINARY _O_BINARY
# else
# define O_BINARY 0
# endif
#endif
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
#define STACK_BUF_SIZE 0x20000
#define DOS2UNIX_LF 0x0a
#define DOS2UNIX_CR 0x0d
/**
* Does a line ending analysis of the given file.
*
* @returns 0 on success, errno value on open or read error.
* @param pszFilename The path to the file
* @param pfStyle Where to return the DOS2UNIX_STYLE_XXX and
* DOS2UNIX_F_XXX flags.
* @param pcDosEols Where to return the number of DOS end-of-line
* sequences found. Optional.
* @param pcUnixEols Where to return the number of UNIX end-of-line
* sequences found.
*/
int dos2unix_analyze_file(const char *pszFilename, KU32 *pfStyle, KSIZE *pcDosEols, KSIZE *pcUnixEols)
{
int iRet = 0;
int fd = open(pszFilename, O_RDONLY | O_BINARY);
if (fd >= 0)
{
iRet = dos2unix_analyze_fd(fd, pfStyle, pcDosEols, pcUnixEols);
close(fd);
}
else
{
iRet = errno;
*pfStyle = DOS2UNIX_STYLE_NONE;
if (pcUnixEols)
*pcUnixEols = 0;
if (pcDosEols)
*pcDosEols = 0;
}
return iRet;
}
/**
* Does a line ending analysis of the given file descriptor.
*
* @returns 0 on success, errno value on open or read error.
* @param fd The file descriptor to analyze. Caller must
* place this as the desired position.
* @param pfStyle Where to return the DOS2UNIX_STYLE_XXX and
* DOS2UNIX_F_XXX flags.
* @param pcDosEols Where to return the number of DOS end-of-line
* sequences found. Optional.
* @param pcUnixEols Where to return the number of UNIX end-of-line
* sequences found.
*/
int dos2unix_analyze_fd(int fd, KU32 *pfStyle, KSIZE *pcDosEols, KSIZE *pcUnixEols)
{
KSIZE cUnixEols = 0;
KSIZE cDosEols = 0;
KSIZE cLoneCrs = 0;
KBOOL fPendingCr = K_FALSE;
int iRet = 0;
/*
* Do the analysis.
*/
*pfStyle = DOS2UNIX_STYLE_NONE;
for (;;)
{
char achBuf[STACK_BUF_SIZE];
int cchRead = read(fd, achBuf, sizeof(achBuf));
if (cchRead > 0)
{
int off = 0;
if (fPendingCr)
{
if (achBuf[0] == DOS2UNIX_LF)
{
off++;
cDosEols++;
}
else
cLoneCrs++;
fPendingCr = K_FALSE;
}
while (off < cchRead)
{
char ch = achBuf[off++];
if ((unsigned char)ch > (unsigned char)DOS2UNIX_CR)
{ /* likely */ }
else if (ch == DOS2UNIX_CR)
{
if (off < cchRead && achBuf[off] == DOS2UNIX_CR)
cDosEols++;
else
{
fPendingCr = K_TRUE;
while (off < cchRead)
{
ch = achBuf[off++];
if (ch != DOS2UNIX_CR)
{
if (ch == DOS2UNIX_LF)
cDosEols++;
else
cLoneCrs++;
fPendingCr = K_FALSE;
break;
}
cLoneCrs++;
}
}
}
else if (ch == DOS2UNIX_LF)
cUnixEols++;
else if (ch == '\0')
*pfStyle |= DOS2UNIX_F_BINARY;
}
}
else
{
if (cchRead < 0)
iRet = errno;
if (fPendingCr)
cLoneCrs++;
break;
}
}
/*
* Set return values.
*/
if (cUnixEols > 0 && cDosEols == 0)
*pfStyle |= DOS2UNIX_STYLE_UNIX;
else if (cDosEols > 0 && cUnixEols == 0)
*pfStyle |= DOS2UNIX_STYLE_DOS;
else if (cDosEols != 0 && cUnixEols != 0)
*pfStyle |= DOS2UNIX_STYLE_MIXED;
if (pcUnixEols)
*pcUnixEols = cUnixEols;
if (pcDosEols)
*pcDosEols = cDosEols;
return iRet;
}
/**
* Converts a buffer to unix line (LF) endings.
*
* @retval K_TRUE if pending CR. The caller must handle this case.
* @retval K_FALSE if no pending CR.
*
* @param pchSrc The input buffer.
* @param cchSrc Number of characters to convert from the input
* buffer.
* @param pchDst The output buffer. This must be at least as big as
* the input. It is okay if this overlaps with the
* source buffer, as long as this is at the same or a
* lower address.
* @param pcchDst Where to return the number of characters in the
* output buffer.
*/
KBOOL dos2unix_convert_to_unix(const char *pchSrc, KSIZE cchSrc, char *pchDst, KSIZE *pcchDst)
{
KSIZE offDst = 0;
while (cchSrc-- > 0)
{
char ch = *pchSrc++;
if ((unsigned char)ch != (unsigned char)DOS2UNIX_CR)
pchDst[offDst++] = ch;
else if (cchSrc > 0 && *pchSrc == DOS2UNIX_LF)
{
pchDst[offDst++] = DOS2UNIX_LF;
cchSrc--;
pchSrc++;
}
else if (cchSrc == 0)
{
*pcchDst = offDst;
return K_TRUE;
}
else
pchDst[offDst++] = ch;
}
*pcchDst = offDst;
return K_FALSE;
}
/**
* Converts a buffer to DOS (CRLF) endings.
*
* @retval K_TRUE if pending CR. The caller must handle this case.
* @retval K_FALSE if no pending CR.
*
* @param pchSrc The input buffer.
* @param cchSrc Number of characters to convert from the input
* buffer.
* @param pchDst The output buffer. This must be at least _twice_ as
* big as the input. It is okay if the top half of the
* buffer overlaps with the source buffer.
* @param pcchDst Where to return the number of characters in the
* output buffer.
*/
KBOOL dos2unix_convert_to_dos(const char *pchSrc, KSIZE cchSrc, char *pchDst, KSIZE *pcchDst)
{
KSIZE offDst = 0;
while (cchSrc-- > 0)
{
char ch = *pchSrc++;
if ((unsigned char)ch > (unsigned char)DOS2UNIX_CR)
pchDst[offDst++] = ch;
else if (ch == DOS2UNIX_CR)
{
/* We treat CR kind of like an escape character. */
do
{
if (cchSrc > 0)
{
pchDst[offDst++] = ch;
cchSrc--;
ch = *pchSrc++;
}
else
{
*pcchDst = offDst;
return K_TRUE;
}
} while (ch == DOS2UNIX_CR);
pchDst[offDst++] = ch;
}
else if (ch == DOS2UNIX_LF)
{
pchDst[offDst++] = DOS2UNIX_CR;
pchDst[offDst++] = DOS2UNIX_LF;
}
else
pchDst[offDst++] = ch;
}
*pcchDst = offDst;
return K_FALSE;
}
|