Skip to content

Commit 831532a

Browse files
bradhfarindk
authored andcommitted
Windows alternatives for unistd and friends.
1 parent 83c0954 commit 831532a

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

libheif/box.cc

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@
5050
#define M_PI 3.14159265358979323846
5151
#endif
5252

53-
#include <unistd.h> // TODO: Windows
53+
#if !defined(_WIN32) && !defined(_WIN64)
54+
#include <unistd.h>
55+
#else
56+
#include <fcntl.h>
57+
#include <io.h>
58+
#endif
59+
5460

5561
Fraction::Fraction(int32_t num, int32_t den)
5662
{
@@ -155,7 +161,7 @@ bool Fraction::is_valid() const
155161
return denominator != 0;
156162
}
157163

158-
uint32_t from_fourcc(const char* string)
164+
static uint32_t from_fourcc(const char* string)
159165
{
160166
return ((string[0] << 24) |
161167
(string[1] << 16) |
@@ -1035,7 +1041,7 @@ Error Box_ftyp::parse(BitstreamRange& range)
10351041
m_major_brand = range.read32();
10361042
m_minor_version = range.read32();
10371043

1038-
if (get_box_size() <= get_header_size() + 8) {
1044+
if (get_box_size() - 8 <= get_header_size()) {
10391045
// Sanity check.
10401046
return Error(heif_error_Invalid_input,
10411047
heif_suberror_Invalid_box_size,
@@ -1406,8 +1412,15 @@ void Box_iloc::set_use_tmp_file(bool flag)
14061412
{
14071413
m_use_tmpfile = flag;
14081414
if (flag) {
1415+
#if !defined(_WIN32) && !defined(_WIN64)
14091416
strcpy(m_tmp_filename, "/tmp/libheif-XXXXXX");
14101417
m_tmpfile_fd = mkstemp(m_tmp_filename);
1418+
#else
1419+
char tmpname[L_tmpnam_s];
1420+
// TODO: check return value (errno_t)
1421+
tmpnam_s(tmpname, L_tmpnam_s);
1422+
_sopen_s(&m_tmpfile_fd, tmpname, _O_CREAT | _O_TEMPORARY | _O_TRUNC | _O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE);
1423+
#endif
14111424
}
14121425
}
14131426

@@ -1629,7 +1642,11 @@ Error Box_iloc::append_data(heif_item_id item_ID,
16291642
extent.length = data.size();
16301643

16311644
if (m_use_tmpfile && construction_method==0) {
1645+
#if !defined(_WIN32) && !defined(_WIN64)
16321646
ssize_t cnt = ::write(m_tmpfile_fd, data.data(), data.size());
1647+
#else
1648+
int cnt = _write(m_tmpfile_fd, data.data(), data.size());
1649+
#endif
16331650
if (cnt < 0) {
16341651
std::stringstream sstr;
16351652
sstr << "Could not write to tmp file: error " << errno;
@@ -1883,7 +1900,11 @@ Error Box_iloc::write_mdat_after_iloc(StreamWriter& writer)
18831900

18841901
if (m_use_tmpfile) {
18851902
std::vector<uint8_t> data(extent.length);
1903+
#if !defined(_WIN32) && !defined(_WIN64)
18861904
ssize_t cnt = ::read(m_tmpfile_fd, data.data(), extent.length);
1905+
#else
1906+
int cnt = _read(m_tmpfile_fd, data.data(), extent.length);
1907+
#endif
18871908
if (cnt<0) {
18881909
std::stringstream sstr;
18891910
sstr << "Cannot read tmp data file, error " << errno;
@@ -2632,7 +2653,7 @@ Error Box_ipma::parse(BitstreamRange& range)
26322653

26332654
int assoc_cnt = range.read8();
26342655
for (int k = 0; k < assoc_cnt; k++) {
2635-
PropertyAssociation association;
2656+
PropertyAssociation association{};
26362657

26372658
uint16_t index;
26382659
if (get_flags() & 1) {
@@ -3902,9 +3923,9 @@ Error Box_cmin::write(StreamWriter& writer) const
39023923
}
39033924

39043925

3905-
std::array<double,9> mul(const std::array<double,9>& a, const std::array<double,9>& b)
3926+
static std::array<double,9> mul(const std::array<double,9>& a, const std::array<double,9>& b)
39063927
{
3907-
std::array<double,9> m;
3928+
std::array<double, 9> m{};
39083929

39093930
m[0] = a[0]*b[0] + a[1]*b[3] + a[2]*b[6];
39103931
m[1] = a[0]*b[1] + a[1]*b[4] + a[2]*b[7];

0 commit comments

Comments
 (0)