regedit: Rewrite processRegLinesA into an ANSI version of processRegLinesW.

Signed-off-by: Hugh McMaster <hugh.mcmaster@outlook.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Hugh McMaster 2016-07-21 05:48:30 +00:00 committed by Alexandre Julliard
parent 3056608fb8
commit 3763c7a610
1 changed files with 94 additions and 118 deletions

View File

@ -596,144 +596,120 @@ static void processRegEntry(WCHAR* stdInput, BOOL isUnicode)
*/ */
static void processRegLinesA(FILE *in, char* first_chars) static void processRegLinesA(FILE *in, char* first_chars)
{ {
LPSTR line = NULL; /* line read from input stream */ char *buf = NULL; /* the line read from the input stream */
ULONG lineSize = REG_VAL_BUF_SIZE; unsigned long line_size = REG_VAL_BUF_SIZE;
size_t chars_in_buf = -1;
char *s; /* A pointer to buf for fread */
char *line; /* The start of the current line */
WCHAR *lineW;
buf = HeapAlloc(GetProcessHeap(), 0, line_size);
CHECK_ENOUGH_MEMORY(buf);
s = buf;
line = buf;
line = HeapAlloc(GetProcessHeap(), 0, lineSize);
CHECK_ENOUGH_MEMORY(line);
memcpy(line, first_chars, 2); memcpy(line, first_chars, 2);
while (!feof(in)) {
LPSTR s; /* The pointer into line for where the current fgets should read */
WCHAR* lineW;
s = line;
if (first_chars) if (first_chars)
{
s += 2; s += 2;
first_chars = NULL;
}
for (;;) { while (!feof(in)) {
size_t size_remaining; size_t size_remaining;
int size_to_get, i; int size_to_get;
char *s_eol; /* various local uses */ char *s_eol = NULL; /* various local uses */
/* Do we need to expand the buffer? */ /* Do we need to expand the buffer? */
assert (s >= line && s <= line + lineSize); assert(s >= buf && s <= buf + line_size);
size_remaining = lineSize - (s-line); size_remaining = line_size - (s - buf);
if (size_remaining < 3) /* need at least 3 bytes of room for \r\n\0 */ if (size_remaining < 3) /* we need at least 3 bytes of room for \r\n\0 */
{ {
char *new_buffer; char *new_buffer;
size_t new_size = lineSize + REG_VAL_BUF_SIZE; size_t new_size = line_size + REG_VAL_BUF_SIZE;
if (new_size > lineSize) /* no arithmetic overflow */ if (new_size > line_size) /* no arithmetic overflow */
new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size); new_buffer = HeapReAlloc(GetProcessHeap(), 0, buf, new_size);
else else
new_buffer = NULL; new_buffer = NULL;
CHECK_ENOUGH_MEMORY(new_buffer); CHECK_ENOUGH_MEMORY(new_buffer);
line = new_buffer; buf = new_buffer;
s = line + lineSize - size_remaining; line = buf;
lineSize = new_size; s = buf + line_size - size_remaining;
size_remaining = lineSize - (s-line); line_size = new_size;
size_remaining = line_size - (s - buf);
} }
/* Get as much as possible into the buffer, terminated either by /* Get as much as possible into the buffer, terminating on EOF,
* eof, error, eol or getting the maximum amount. Abort on error. * error or once we have read the maximum amount. Abort on error.
*/ */
size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining); size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
/* get a single line. note that `i' must be one past the last chars_in_buf = fread(s, 1, size_to_get - 1, in);
* meaningful character in `s' when this loop exits */ s[chars_in_buf] = 0;
for(i = 0; i < size_to_get-1; ++i){
int xchar;
xchar = fgetc(in); if (chars_in_buf == 0) {
s[i] = xchar;
if(xchar == EOF){
if (ferror(in)) { if (ferror(in)) {
perror("While reading input"); perror("While reading input");
exit(IO_ERROR); exit(IO_ERROR);
}else } else {
assert(feof(in)); assert(feof(in));
break; *s = '\0';
} }
if(s[i] == '\r'){
/* read the next character iff it's \n */
if(i+2 >= size_to_get){
/* buffer too short, so put back the EOL char to
* read next cycle */
ungetc('\r', in);
break;
}
s[i+1] = fgetc(in);
if(s[i+1] != '\n'){
ungetc(s[i+1], in);
i = i+1;
}else
i = i+2;
break;
}
if(s[i] == '\n'){
i = i+1;
break;
}
}
s[i] = '\0';
/* If we didn't read the eol nor the eof go around for the rest */
s_eol = strpbrk (s, "\r\n");
if (!feof (in) && !s_eol) {
s = strchr (s, '\0');
continue;
} }
/* If it is a comment line then discard it and go around again */ /* If we didn't read the end-of-line sequence or EOF, go around again */
while (1)
{
s_eol = strpbrk(line, "\r\n");
if (!s_eol) {
/* Move the stub of the line to the start of the buffer so
* we get the maximum space to read into, and so we don't
* have to recalculate 'line' if the buffer expands */
MoveMemory(buf, line, strlen(line) + 1);
line = buf;
s = strchr(line, '\0');
break;
}
/* If we find a comment line, discard it and go around again */
if (line [0] == '#' || line [0] == ';') { if (line [0] == '#' || line [0] == ';') {
s = line;
continue;
}
/* Remove any line feed. Leave s_eol on the first \0 */
if (s_eol) {
if (*s_eol == '\r' && *(s_eol + 1) == '\n') if (*s_eol == '\r' && *(s_eol + 1) == '\n')
*(s_eol+1) = '\0'; line = s_eol + 2;
*s_eol = '\0';
} else
s_eol = strchr (s, '\0');
/* If there is a concatenating \\ then go around again */
if (s_eol > line && *(s_eol-1) == '\\') {
int c;
s = s_eol-1;
do
{
c = fgetc(in);
} while(c == ' ' || c == '\t');
if(c == EOF)
{
fprintf(stderr, "regedit: ERROR - invalid continuation.\n");
}
else else
{ line = s_eol + 1;
*s = c;
s++;
}
continue; continue;
} }
/* If there is a concatenating '\\', go around again */
if (*(s_eol - 1) == '\\') {
char *next_line = s_eol + 1;
if (*s_eol == '\r' && *(s_eol + 1) == '\n')
next_line++;
while (*(next_line + 1) == ' ' || *(next_line + 1) == '\t')
next_line++;
MoveMemory(s_eol - 1, next_line, chars_in_buf - (next_line - s) + 1);
chars_in_buf -= next_line - s_eol + 1;
s_eol = 0;
continue;
}
/* Remove any line feed. Leave s_eol on the last \0 */
if (*s_eol == '\r' && *(s_eol + 1) == '\n')
*s_eol++ = '\0';
*s_eol = '\0';
lineW = GetWideString(line); lineW = GetWideString(line);
break; /* That is the full virtual line */
}
processRegEntry(lineW, FALSE); processRegEntry(lineW, FALSE);
HeapFree(GetProcessHeap(), 0, lineW); HeapFree(GetProcessHeap(), 0, lineW);
line = s_eol + 1;
s_eol = 0;
continue; /* That is the full virtual line */
}
} }
processRegEntry(NULL, FALSE); processRegEntry(NULL, FALSE);
HeapFree(GetProcessHeap(), 0, line); HeapFree(GetProcessHeap(), 0, buf);
} }
static void processRegLinesW(FILE *in) static void processRegLinesW(FILE *in)