regedit: Re-implement 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 2017-03-16 12:05:00 +00:00 committed by Alexandre Julliard
parent ae5684479e
commit 6b6e2cb924

View File

@ -793,120 +793,79 @@ static void processRegLinesA(FILE *in, char* first_chars)
HeapFree(GetProcessHeap(), 0, buf); HeapFree(GetProcessHeap(), 0, buf);
} }
static void processRegLinesW(FILE *in) static WCHAR *get_lineW(FILE *fp)
{ {
WCHAR* buf = NULL; /* line read from input stream */ static size_t size;
ULONG lineSize = REG_VAL_BUF_SIZE; static WCHAR *buf, *next;
size_t CharsInBuf = -1; WCHAR *line;
WCHAR* s; /* The pointer into buf for where the current fgets should read */ if (!size)
WCHAR* line; /* The start of the current line */ {
size = REG_VAL_BUF_SIZE;
buf = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(WCHAR)); buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
CHECK_ENOUGH_MEMORY(buf); CHECK_ENOUGH_MEMORY(buf);
*buf = 0;
next = buf;
}
line = next;
s = buf; while (next)
line = buf;
while(!feof(in)) {
size_t size_remaining;
int size_to_get;
WCHAR *s_eol = NULL; /* various local uses */
/* Do we need to expand the buffer ? */
assert (s >= buf && s <= buf + lineSize);
size_remaining = lineSize - (s-buf);
if (size_remaining < 2) /* room for 1 character and the \0 */
{ {
WCHAR *new_buffer; static const WCHAR line_endings[] = {'\r','\n',0};
size_t new_size = lineSize + (REG_VAL_BUF_SIZE / sizeof(WCHAR)); WCHAR *p = strpbrkW(line, line_endings);
if (new_size > lineSize) /* no arithmetic overflow */ if (!p)
new_buffer = HeapReAlloc (GetProcessHeap(), 0, buf, new_size * sizeof(WCHAR));
else
new_buffer = NULL;
CHECK_ENOUGH_MEMORY(new_buffer);
buf = new_buffer;
line = buf;
s = buf + lineSize - size_remaining;
lineSize = new_size;
size_remaining = lineSize - (s-buf);
}
/* Get as much as possible into the buffer, terminated either by
* eof, error or getting the maximum amount. Abort on error.
*/
size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
CharsInBuf = fread(s, sizeof(WCHAR), size_to_get - 1, in);
s[CharsInBuf] = 0;
if (CharsInBuf == 0) {
if (ferror(in)) {
perror ("While reading input");
exit (IO_ERROR);
} else {
assert (feof(in));
*s = '\0';
/* It is not clear to me from the definition that the
* contents of the buffer are well defined on detecting
* an eof without managing to read anything.
*/
}
}
/* If we didn't read the eol nor the eof go around for the rest */
while(1)
{ {
const WCHAR line_endings[] = {'\r','\n',0}; size_t len, count;
s_eol = strpbrkW(line, line_endings); len = strlenW(next);
memmove(buf, next, (len + 1) * sizeof(WCHAR));
if(!s_eol) { if (size - len < 3)
/* 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 WCHAR *new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, (size * 2) * sizeof(WCHAR));
* have to recalculate 'line' if the buffer expands */ CHECK_ENOUGH_MEMORY(new_buf);
MoveMemory(buf, line, (strlenW(line)+1) * sizeof(WCHAR)); buf = new_buf;
line = buf; size *= 2;
s = strchrW(line, '\0');
break;
} }
if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
/* If it is a comment line then discard it and go around again */ {
if (*line == '#' || *line == ';') { next = NULL;
if (*s_eol == '\r' && *(s_eol+1) == '\n') return buf;
line = s_eol + 2; }
else buf[len + count] = 0;
line = s_eol + 1; next = buf;
line = buf;
continue; continue;
} }
next = p + 1;
/* If there is a concatenating \\ then go around again */ if (*p == '\r' && *(p + 1) == '\n') next++;
if (*(s_eol-1) == '\\') { *p = 0;
WCHAR* NextLine = s_eol + 1; if (*(p - 1) == '\\')
{
if(*s_eol == '\r' && *(s_eol+1) == '\n') *(--p) = 0;
NextLine++; while (*next && (*next == ' ' || *next == '\t')) next++;
memmove(p, next, (strlenW(next) + 1) * sizeof(WCHAR));
while(*(NextLine+1) == ' ' || *(NextLine+1) == '\t') next = line;
NextLine++;
MoveMemory(s_eol - 1, NextLine, (CharsInBuf - (NextLine - s) + 1)*sizeof(WCHAR));
CharsInBuf -= NextLine - s_eol + 1;
continue; continue;
} }
if (*line == ';' || *line == '#')
{
line = next;
continue;
}
return line;
}
HeapFree( GetProcessHeap(), 0, buf );
size = 0;
return NULL;
}
/* Remove any line feed. Leave s_eol on the last \0 */ static void processRegLinesW(FILE *fp)
if (*s_eol == '\r' && *(s_eol + 1) == '\n') {
*s_eol++ = '\0'; WCHAR *line;
*s_eol = '\0';
while ((line = get_lineW(fp)))
processRegEntry(line, TRUE); processRegEntry(line, TRUE);
line = s_eol + 1;
}
}
closeKey(); closeKey();
HeapFree(GetProcessHeap(), 0, buf);
} }
/****************************************************************************** /******************************************************************************