Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
Heatshrink
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Grant Kim
Heatshrink
Commits
70e80a9a
Commit
70e80a9a
authored
10 years ago
by
Scott Vokes
Browse files
Options
Downloads
Plain Diff
Merge pull request #10 from philpem/fix_windows_binary_mode
Fix a number of Windows-specific issues
parents
555f7cf0
565e5639
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
heatshrink.c
+42
-14
42 additions, 14 deletions
heatshrink.c
with
43 additions
and
14 deletions
.gitignore
+
1
−
0
View file @
70e80a9a
...
...
@@ -4,3 +4,4 @@ test_heatshrink_static
*.o
*.core
*.dSYM
*.exe
This diff is collapsed.
Click to expand it.
heatshrink.c
+
42
−
14
View file @
70e80a9a
...
...
@@ -4,7 +4,6 @@
#include
<stdint.h>
#include
<assert.h>
#include
<string.h>
#include
<err.h>
#include
<fcntl.h>
#include
"heatshrink_encoder.h"
...
...
@@ -21,6 +20,26 @@
#define LOG(...)
/* NO-OP */
#endif
#if _WIN32
#include
<errno.h>
#define HEATSHRINK_ERR(retval, ...) do { \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "Undefined error: %d\n", errno); \
exit(retval); \
} while(0)
#else
#include
<err.h>
#define HEATSHRINK_ERR(...) err(__VA_ARGS__)
#endif
/*
* We have to open binary files with the O_BINARY flag on Windows. Most other
* platforms don't differentiate between binary and non-binary files.
*/
#ifndef O_BINARY
#define O_BINARY 0
#endif
static
const
int
version_major
=
HEATSHRINK_VERSION_MAJOR
;
static
const
int
version_minor
=
HEATSHRINK_VERSION_MINOR
;
static
const
int
version_patch
=
HEATSHRINK_VERSION_PATCH
;
...
...
@@ -110,19 +129,19 @@ static io_handle *handle_open(char *fname, IO_mode m, size_t buf_sz) {
if
(
0
==
strcmp
(
"-"
,
fname
))
{
io
->
fd
=
STDIN_FILENO
;
}
else
{
io
->
fd
=
open
(
fname
,
O_RDONLY
);
io
->
fd
=
open
(
fname
,
O_RDONLY
|
O_BINARY
);
}
}
else
if
(
m
==
IO_WRITE
)
{
if
(
0
==
strcmp
(
"-"
,
fname
))
{
io
->
fd
=
STDOUT_FILENO
;
}
else
{
io
->
fd
=
open
(
fname
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
/*| O_EXCL*/
,
0644
);
io
->
fd
=
open
(
fname
,
O_WRONLY
|
O_BINARY
|
O_CREAT
|
O_TRUNC
/*| O_EXCL*/
,
0644
);
}
}
if
(
io
->
fd
==
-
1
)
{
/* failed to open */
free
(
io
);
err
(
1
,
"open"
);
HEATSHRINK_ERR
(
1
,
"open"
);
return
NULL
;
}
...
...
@@ -135,7 +154,7 @@ static ssize_t handle_read(io_handle *io, size_t size, uint8_t **buf) {
LOG
(
"@ read %zd
\n
"
,
size
);
if
(
buf
==
NULL
)
{
return
-
1
;
}
if
(
size
>
io
->
size
)
{
printf
(
"size %zd, io->size %zd
\n
"
,
size
,
io
->
size
);
f
printf
(
stderr
,
"size %zd, io->size %zd
\n
"
,
size
,
io
->
size
);
return
-
1
;
}
if
(
io
->
mode
!=
IO_READ
)
{
return
-
1
;
}
...
...
@@ -154,10 +173,10 @@ static ssize_t handle_read(io_handle *io, size_t size, uint8_t **buf) {
io
->
fill
-=
io
->
read
;
io
->
read
=
0
;
ssize_t
read_sz
=
read
(
io
->
fd
,
&
io
->
buf
[
io
->
fill
],
io
->
size
-
io
->
fill
);
if
(
read_sz
<
0
)
{
err
(
1
,
"read"
);
}
if
(
read_sz
<
0
)
{
HEATSHRINK_ERR
(
1
,
"read"
);
}
io
->
total
+=
read_sz
;
if
(
read_sz
==
0
)
{
/* EOF */
if
(
close
(
io
->
fd
)
<
0
)
{
err
(
1
,
"close"
);
}
if
(
close
(
io
->
fd
)
<
0
)
{
HEATSHRINK_ERR
(
1
,
"close"
);
}
io
->
fd
=
-
1
;
}
io
->
fill
+=
read_sz
;
...
...
@@ -192,7 +211,7 @@ static ssize_t handle_sink(io_handle *io, size_t size, uint8_t *input) {
ssize_t
written
=
write
(
io
->
fd
,
io
->
buf
,
io
->
fill
);
LOG
(
"@ flushing %zd, wrote %zd
\n
"
,
io
->
fill
,
written
);
io
->
total
+=
written
;
if
(
written
==
-
1
)
{
err
(
1
,
"write"
);
}
if
(
written
==
-
1
)
{
HEATSHRINK_ERR
(
1
,
"write"
);
}
memmove
(
io
->
buf
,
&
io
->
buf
[
written
],
io
->
fill
-
written
);
io
->
fill
-=
written
;
}
...
...
@@ -207,7 +226,7 @@ static void handle_close(io_handle *io) {
ssize_t
written
=
write
(
io
->
fd
,
io
->
buf
,
io
->
fill
);
io
->
total
+=
written
;
LOG
(
"@ close: flushing %zd, wrote %zd
\n
"
,
io
->
fill
,
written
);
if
(
written
==
-
1
)
{
err
(
1
,
"write"
);
}
if
(
written
==
-
1
)
{
HEATSHRINK_ERR
(
1
,
"write"
);
}
}
close
(
io
->
fd
);
io
->
fd
=
-
1
;
...
...
@@ -270,7 +289,7 @@ static int encode(config *cfg) {
uint8_t
*
input
=
NULL
;
read_sz
=
handle_read
(
in
,
window_sz
,
&
input
);
if
(
input
==
NULL
)
{
printf
(
"handle read failure
\n
"
);
f
printf
(
stderr
,
"handle read failure
\n
"
);
die
(
"read"
);
}
if
(
read_sz
<
0
)
{
die
(
"read"
);
}
...
...
@@ -281,7 +300,7 @@ static int encode(config *cfg) {
if
(
handle_drop
(
in
,
read_sz
)
<
0
)
{
die
(
"drop"
);
}
};
if
(
read_sz
==
-
1
)
{
err
(
1
,
"read"
);
}
if
(
read_sz
==
-
1
)
{
HEATSHRINK_ERR
(
1
,
"read"
);
}
heatshrink_encoder_free
(
hse
);
close_and_report
(
cfg
);
...
...
@@ -344,7 +363,7 @@ static int decode(config *cfg) {
uint8_t
*
input
=
NULL
;
read_sz
=
handle_read
(
in
,
window_sz
,
&
input
);
if
(
input
==
NULL
)
{
printf
(
"handle read failure
\n
"
);
f
printf
(
stderr
,
"handle read failure
\n
"
);
die
(
"read"
);
}
if
(
read_sz
==
0
)
{
...
...
@@ -358,7 +377,7 @@ static int decode(config *cfg) {
if
(
handle_drop
(
in
,
read_sz
)
<
0
)
{
die
(
"drop"
);
}
}
}
if
(
read_sz
==
-
1
)
{
err
(
1
,
"read"
);
}
if
(
read_sz
==
-
1
)
{
HEATSHRINK_ERR
(
1
,
"read"
);
}
heatshrink_decoder_free
(
hsd
);
close_and_report
(
cfg
);
...
...
@@ -427,7 +446,7 @@ int main(int argc, char **argv) {
if
(
0
==
strcmp
(
cfg
.
in_fname
,
cfg
.
out_fname
)
&&
(
0
!=
strcmp
(
"-"
,
cfg
.
in_fname
)))
{
printf
(
"Refusing to overwrite file '%s' with itself.
\n
"
,
cfg
.
in_fname
);
f
printf
(
stderr
,
"Refusing to overwrite file '%s' with itself.
\n
"
,
cfg
.
in_fname
);
exit
(
1
);
}
...
...
@@ -436,6 +455,15 @@ int main(int argc, char **argv) {
cfg
.
out
=
handle_open
(
cfg
.
out_fname
,
IO_WRITE
,
cfg
.
buffer_size
);
if
(
cfg
.
out
==
NULL
)
{
die
(
"Failed to open output file for write"
);
}
#if _WIN32
/*
* On Windows, stdin and stdout default to text mode. Switch them to
* binary mode before sending data through them.
*/
_setmode
(
STDOUT_FILENO
,
O_BINARY
);
_setmode
(
STDIN_FILENO
,
O_BINARY
);
#endif
if
(
cfg
.
cmd
==
OP_ENC
)
{
return
encode
(
&
cfg
);
}
else
if
(
cfg
.
cmd
==
OP_DEC
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment