2017-08-12 14:56:14 -04:00
|
|
|
/*
|
|
|
|
__ __ _
|
|
|
|
___\ \/ /_ __ __ _| |_
|
|
|
|
/ _ \\ /| '_ \ / _` | __|
|
|
|
|
| __// \| |_) | (_| | |_
|
|
|
|
\___/_/\_\ .__/ \__,_|\__|
|
|
|
|
|_| XML parser
|
|
|
|
|
|
|
|
Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
|
|
|
|
Copyright (c) 2000-2017 Expat development team
|
|
|
|
Licensed under the MIT license:
|
|
|
|
|
|
|
|
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.
|
2000-04-21 00:20:31 -04:00
|
|
|
*/
|
1998-04-05 11:11:29 -04:00
|
|
|
|
1998-03-28 13:04:56 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2001-07-25 13:13:34 -04:00
|
|
|
#include <unistd.h>
|
1998-03-28 13:04:56 -05:00
|
|
|
|
|
|
|
#ifndef MAP_FILE
|
2019-08-03 14:34:54 -04:00
|
|
|
# define MAP_FILE 0
|
1998-03-28 13:04:56 -05:00
|
|
|
#endif
|
|
|
|
|
2017-08-29 12:59:04 -04:00
|
|
|
#include "xmltchar.h"
|
1998-03-28 13:04:56 -05:00
|
|
|
#include "filemap.h"
|
|
|
|
|
2017-08-29 12:59:04 -04:00
|
|
|
#ifdef XML_UNICODE_WCHAR_T
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_STR "ls"
|
2017-08-29 12:59:04 -04:00
|
|
|
#else
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_STR "s"
|
2017-08-29 12:59:04 -04:00
|
|
|
#endif
|
|
|
|
|
2002-07-01 11:13:02 -04:00
|
|
|
int
|
2017-08-29 12:59:04 -04:00
|
|
|
filemap(const tchar *name,
|
|
|
|
void (*processor)(const void *, size_t, const tchar *, void *arg),
|
2019-08-03 14:34:54 -04:00
|
|
|
void *arg) {
|
1998-03-28 13:04:56 -05:00
|
|
|
int fd;
|
|
|
|
size_t nbytes;
|
|
|
|
struct stat sb;
|
|
|
|
void *p;
|
|
|
|
|
2017-08-29 12:59:04 -04:00
|
|
|
fd = topen(name, O_RDONLY);
|
1998-03-28 13:04:56 -05:00
|
|
|
if (fd < 0) {
|
2017-08-29 12:59:04 -04:00
|
|
|
tperror(name);
|
1998-03-28 13:04:56 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &sb) < 0) {
|
2017-08-29 12:59:04 -04:00
|
|
|
tperror(name);
|
1998-03-28 13:04:56 -05:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! S_ISREG(sb.st_mode)) {
|
1998-03-28 13:04:56 -05:00
|
|
|
close(fd);
|
2017-08-29 12:59:04 -04:00
|
|
|
fprintf(stderr, "%" XML_FMT_STR ": not a regular file\n", name);
|
1998-03-28 13:04:56 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-22 19:48:52 -04:00
|
|
|
if (sb.st_size > XML_MAX_CHUNK_LEN) {
|
|
|
|
close(fd);
|
2019-08-03 14:34:54 -04:00
|
|
|
return 2; /* Cannot be passed to XML_Parse in one go */
|
2017-05-22 19:48:52 -04:00
|
|
|
}
|
2002-07-01 11:13:02 -04:00
|
|
|
|
1998-03-28 13:04:56 -05:00
|
|
|
nbytes = sb.st_size;
|
2006-06-27 22:55:51 -04:00
|
|
|
/* mmap fails for zero length files */
|
|
|
|
if (nbytes == 0) {
|
|
|
|
static const char c = '\0';
|
|
|
|
processor(&c, 0, name, arg);
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
p = (void *)mmap((void *)0, (size_t)nbytes, PROT_READ, MAP_FILE | MAP_PRIVATE,
|
|
|
|
fd, (off_t)0);
|
1998-03-28 13:04:56 -05:00
|
|
|
if (p == (void *)-1) {
|
2017-08-29 12:59:04 -04:00
|
|
|
tperror(name);
|
1998-03-28 13:04:56 -05:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
processor(p, nbytes, name, arg);
|
2016-03-11 20:41:00 -05:00
|
|
|
munmap((void *)p, nbytes);
|
1998-03-28 13:04:56 -05:00
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|