Skip to content

Commit

Permalink
Windows alternatives for unistd and friends.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh authored and farindk committed Sep 10, 2024
1 parent 83c0954 commit 831532a
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@
#define M_PI 3.14159265358979323846
#endif

#include <unistd.h> // TODO: Windows
#if !defined(_WIN32) && !defined(_WIN64)
#include <unistd.h>
#else
#include <fcntl.h>
#include <io.h>
#endif


Fraction::Fraction(int32_t num, int32_t den)
{
Expand Down Expand Up @@ -155,7 +161,7 @@ bool Fraction::is_valid() const
return denominator != 0;
}

uint32_t from_fourcc(const char* string)
static uint32_t from_fourcc(const char* string)
{
return ((string[0] << 24) |
(string[1] << 16) |
Expand Down Expand Up @@ -1035,7 +1041,7 @@ Error Box_ftyp::parse(BitstreamRange& range)
m_major_brand = range.read32();
m_minor_version = range.read32();

if (get_box_size() <= get_header_size() + 8) {
if (get_box_size() - 8 <= get_header_size()) {
// Sanity check.
return Error(heif_error_Invalid_input,
heif_suberror_Invalid_box_size,
Expand Down Expand Up @@ -1406,8 +1412,15 @@ void Box_iloc::set_use_tmp_file(bool flag)
{
m_use_tmpfile = flag;
if (flag) {
#if !defined(_WIN32) && !defined(_WIN64)
strcpy(m_tmp_filename, "/tmp/libheif-XXXXXX");
m_tmpfile_fd = mkstemp(m_tmp_filename);
#else
char tmpname[L_tmpnam_s];
// TODO: check return value (errno_t)
tmpnam_s(tmpname, L_tmpnam_s);
_sopen_s(&m_tmpfile_fd, tmpname, _O_CREAT | _O_TEMPORARY | _O_TRUNC | _O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE);
#endif
}
}

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

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

if (m_use_tmpfile) {
std::vector<uint8_t> data(extent.length);
#if !defined(_WIN32) && !defined(_WIN64)
ssize_t cnt = ::read(m_tmpfile_fd, data.data(), extent.length);
#else
int cnt = _read(m_tmpfile_fd, data.data(), extent.length);
#endif
if (cnt<0) {
std::stringstream sstr;
sstr << "Cannot read tmp data file, error " << errno;
Expand Down Expand Up @@ -2632,7 +2653,7 @@ Error Box_ipma::parse(BitstreamRange& range)

int assoc_cnt = range.read8();
for (int k = 0; k < assoc_cnt; k++) {
PropertyAssociation association;
PropertyAssociation association{};

uint16_t index;
if (get_flags() & 1) {
Expand Down Expand Up @@ -3902,9 +3923,9 @@ Error Box_cmin::write(StreamWriter& writer) const
}


std::array<double,9> mul(const std::array<double,9>& a, const std::array<double,9>& b)
static std::array<double,9> mul(const std::array<double,9>& a, const std::array<double,9>& b)
{
std::array<double,9> m;
std::array<double, 9> m{};

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

5 comments on commit 831532a

@gitoss
Copy link

@gitoss gitoss commented on 831532a Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes compilation under my msys2 environment (clang, set up with autobuild media suite) fail:


[  1%] Building CXX object libheif/CMakeFiles/heif.dir/box.cc.obj
C:/media-autobuild/build/libheif-GIT/libheif/box.cc:1423:84: error: use of undeclared identifier '_SH_DENYRW'
 1423 |     _sopen_s(&m_tmpfile_fd, tmpname, _O_CREAT | _O_TEMPORARY | _O_TRUNC | _O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE);
      |                                                                                    ^
C:/media-autobuild/build/libheif-GIT/libheif/box.cc:1423:96: error: use of undeclared identifier '_S_IREAD'
 1423 |     _sopen_s(&m_tmpfile_fd, tmpname, _O_CREAT | _O_TEMPORARY | _O_TRUNC | _O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE);
      |                                                                                                ^
C:/media-autobuild/build/libheif-GIT/libheif/box.cc:1423:107: error: use of undeclared identifier '_S_IWRITE'
 1423 |     _sopen_s(&m_tmpfile_fd, tmpname, _O_CREAT | _O_TEMPORARY | _O_TRUNC | _O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE);
      |                                                                                                           ^

I hope someone sees this in the commit log, so I won't open an issue for the moment.

@farindk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've commented out the code in 252ed4a as it is currently not used.

@gitoss
Copy link

@gitoss gitoss commented on 831532a Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've commented out the code in 252ed4a as it is currently not used.

As far as I can see, these are defined in sys/stat.h - full msys2 path /clang64/include/sys/stat.h


#define _S_IFMT 0xF000
#define _S_IFDIR 0x4000
#define _S_IFCHR 0x2000
#define _S_IFIFO 0x1000
#define _S_IFREG 0x8000
#define _S_IREAD 0x0100
#define _S_IWRITE 0x0080
#define _S_IEXEC 0x0040

@farindk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, these are defined in sys/stat.h - full msys2 path /clang64/include/sys/stat.h

Also this one: _SH_DENYRW ?

@gitoss
Copy link

@gitoss gitoss commented on 831532a Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_SH_DENYRW

That one is in share.h - full msys2 path /clang64/include/ (I guess it's found whatever the root dir is by scanning all include paths, but I'm not much of a C wiz anymoe).

#define _SH_DENYRW 0x10
#define SH_DENYRW _SH_DENYRW 

Please sign in to comment.