2018-11-02 22:10:07 +00: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.
|
2002-10-02 07:16:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-06-29 06:24:30 +00:00
|
|
|
/* Functions close(2) and read(2) */
|
2019-11-24 23:46:29 +00:00
|
|
|
#if ! defined(_WIN32) && ! defined(_WIN64)
|
|
|
|
# include <unistd.h>
|
2008-05-08 13:51:16 +00:00
|
|
|
#endif
|
2018-11-02 22:10:07 +00:00
|
|
|
|
|
|
|
/* Function "read": */
|
|
|
|
#if defined(_MSC_VER)
|
2019-11-24 23:46:29 +00:00
|
|
|
# include <io.h>
|
|
|
|
/* https://msdn.microsoft.com/en-us/library/wyssk1bs(v=vs.100).aspx */
|
|
|
|
# define _EXPAT_read _read
|
|
|
|
# define _EXPAT_read_count_t int
|
|
|
|
# define _EXPAT_read_req_t unsigned int
|
|
|
|
#else /* POSIX */
|
|
|
|
/* http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html */
|
|
|
|
# define _EXPAT_read read
|
|
|
|
# define _EXPAT_read_count_t ssize_t
|
|
|
|
# define _EXPAT_read_req_t size_t
|
2008-05-08 13:51:16 +00:00
|
|
|
#endif
|
|
|
|
|
2002-10-02 07:16:04 +00:00
|
|
|
#ifndef S_ISREG
|
2019-11-24 23:46:29 +00:00
|
|
|
# ifndef S_IFREG
|
|
|
|
# define S_IFREG _S_IFREG
|
|
|
|
# endif
|
|
|
|
# ifndef S_IFMT
|
|
|
|
# define S_IFMT _S_IFMT
|
|
|
|
# endif
|
|
|
|
# define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
|
2002-10-02 07:16:04 +00:00
|
|
|
#endif /* not S_ISREG */
|
|
|
|
|
|
|
|
#ifndef O_BINARY
|
2019-11-24 23:46:29 +00:00
|
|
|
# ifdef _O_BINARY
|
|
|
|
# define O_BINARY _O_BINARY
|
|
|
|
# else
|
|
|
|
# define O_BINARY 0
|
|
|
|
# endif
|
2002-10-02 07:16:04 +00:00
|
|
|
#endif
|
|
|
|
|
2018-11-02 22:10:07 +00:00
|
|
|
#include "xmltchar.h"
|
2002-10-02 07:16:04 +00:00
|
|
|
#include "filemap.h"
|
|
|
|
|
|
|
|
int
|
2018-11-02 22:10:07 +00:00
|
|
|
filemap(const tchar *name,
|
|
|
|
void (*processor)(const void *, size_t, const tchar *, void *arg),
|
2019-11-24 23:46:29 +00:00
|
|
|
void *arg) {
|
2002-10-02 07:16:04 +00:00
|
|
|
size_t nbytes;
|
|
|
|
int fd;
|
2018-11-02 22:10:07 +00:00
|
|
|
_EXPAT_read_count_t n;
|
2002-10-02 07:16:04 +00:00
|
|
|
struct stat sb;
|
|
|
|
void *p;
|
|
|
|
|
2019-11-24 23:46:29 +00:00
|
|
|
fd = topen(name, O_RDONLY | O_BINARY);
|
2002-10-02 07:16:04 +00:00
|
|
|
if (fd < 0) {
|
2018-11-02 22:10:07 +00:00
|
|
|
tperror(name);
|
2002-10-02 07:16:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &sb) < 0) {
|
2018-11-02 22:10:07 +00:00
|
|
|
tperror(name);
|
2013-02-26 00:43:01 +00:00
|
|
|
close(fd);
|
2002-10-02 07:16:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-11-24 23:46:29 +00:00
|
|
|
if (! S_ISREG(sb.st_mode)) {
|
2018-11-02 22:10:07 +00:00
|
|
|
ftprintf(stderr, T("%s: not a regular file\n"), name);
|
2013-02-26 00:43:01 +00:00
|
|
|
close(fd);
|
2002-10-02 07:16:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-02 22:10:07 +00:00
|
|
|
if (sb.st_size > XML_MAX_CHUNK_LEN) {
|
|
|
|
close(fd);
|
2019-11-24 23:46:29 +00:00
|
|
|
return 2; /* Cannot be passed to XML_Parse in one go */
|
2018-11-02 22:10:07 +00:00
|
|
|
}
|
|
|
|
|
2002-10-02 07:16:04 +00:00
|
|
|
nbytes = sb.st_size;
|
2008-05-08 13:51:16 +00:00
|
|
|
/* malloc will return NULL with nbytes == 0, handle files with size 0 */
|
|
|
|
if (nbytes == 0) {
|
|
|
|
static const char c = '\0';
|
|
|
|
processor(&c, 0, name, arg);
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-10-02 07:16:04 +00:00
|
|
|
p = malloc(nbytes);
|
2019-11-24 23:46:29 +00:00
|
|
|
if (! p) {
|
2018-11-02 22:10:07 +00:00
|
|
|
ftprintf(stderr, T("%s: out of memory\n"), name);
|
2008-05-08 13:51:16 +00:00
|
|
|
close(fd);
|
2002-10-02 07:16:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-02 22:10:07 +00:00
|
|
|
n = _EXPAT_read(fd, p, (_EXPAT_read_req_t)nbytes);
|
2002-10-02 07:16:04 +00:00
|
|
|
if (n < 0) {
|
2018-11-02 22:10:07 +00:00
|
|
|
tperror(name);
|
2008-05-08 13:51:16 +00:00
|
|
|
free(p);
|
2002-10-02 07:16:04 +00:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-02 22:10:07 +00:00
|
|
|
if (n != (_EXPAT_read_count_t)nbytes) {
|
|
|
|
ftprintf(stderr, T("%s: read unexpected number of bytes\n"), name);
|
2008-05-08 13:51:16 +00:00
|
|
|
free(p);
|
2002-10-02 07:16:04 +00:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
processor(p, nbytes, name, arg);
|
|
|
|
free(p);
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|