Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Michael A. (Mike) Iovine
p1
Commits
93900930
Commit
93900930
authored
5 years ago
by
John M. (Jack) Maxfield
Browse files
Options
Download
Email Patches
Plain Diff
Add huffman and example file
parent
2453f2a8
master
No related merge requests found
Pipeline
#30164
failed with stage
in 0 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+2
-0
.gitignore
Makefile
+5
-2
Makefile
example.bin
+1
-0
example.bin
huffman.c
+115
-0
huffman.c
with
123 additions
and
2 deletions
+123
-2
.gitignore
View file @
93900930
myzip0
myunzip0
inflate
huffman
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Makefile
View file @
93900930
all
:
myzip0 myunzip0
all
:
myzip0 myunzip0
huffman
myzip0
:
myzip0.c
cc myzip0.c
-o
myzip0
myunzip0
:
myunzip0.c
cc myunzip0.c
-o
myunzip0
\ No newline at end of file
cc myunzip0.c
-o
myunzip0
huffman
:
huffman.c
cc huffman.c
-o
huffman
\ No newline at end of file
This diff is collapsed.
Click to expand it.
example.bin
0 → 100644
View file @
93900930
"3DUfw
\ No newline at end of file
This diff is collapsed.
Click to expand it.
huffman.c
0 → 100644
View file @
93900930
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
/* A struct for writing to files as
streams of individual bits */
typedef
struct
__bit_writer
{
FILE
*
file
;
uint8_t
byte
;
int
mod
;
}
bit_writer
;
bit_writer
*
bit_writer_init
(
FILE
*
file
)
{
bit_writer
*
bw
=
malloc
(
sizeof
(
bit_writer
));
assert
(
bw
);
bw
->
file
=
file
;
bw
->
byte
=
0
;
bw
->
mod
=
0
;
return
bw
;
}
void
bit_writer_free
(
bit_writer
*
bw
)
{
free
(
bw
);
}
void
bit_writer_write_bit
(
bit_writer
*
bw
,
bool
bit
)
{
bw
->
byte
|=
(
bit
>
0
)
<<
(
bw
->
mod
++
);
if
(
bw
->
mod
==
8
)
{
fwrite
(
&
bw
->
byte
,
1
,
1
,
bw
->
file
);
bw
->
byte
=
0
;
bw
->
mod
=
0
;
}
}
void
bit_writer_flush
(
bit_writer
*
bw
)
{
while
(
bw
->
mod
!=
0
)
bit_writer_write_bit
(
bw
,
0
);
}
void
write_byte
(
bit_writer
*
bw
,
uint8_t
byte
)
{
if
(
byte
<=
143
)
/* 8 bit encoding */
{
uint32_t
to_encode
=
0x30
+
(
uint32_t
)
byte
;
for
(
int
b
=
8
-
1
;
b
>=
0
;
b
--
)
bit_writer_write_bit
(
bw
,
to_encode
&
(
1
<<
b
));
}
else
/* 9 bit encoding */
{
uint32_t
to_encode
=
0x100
+
(
uint32_t
)
byte
;
for
(
int
b
=
9
-
1
;
b
>=
0
;
b
--
)
{
// bool to_write = () != 0;
// printf("Writing %d\n", to_write);
bit_writer_write_bit
(
bw
,
to_encode
&
(
1
<<
b
));
}
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
)
{
printf
(
"Usage: %s <input file>
\n
"
,
argv
[
0
]);
exit
(
1
);
}
char
*
input_file_name
=
argv
[
1
];
char
*
output_file_name
=
malloc
(
sizeof
(
char
)
*
(
strlen
(
input_file_name
)
+
strlen
(
".deflate"
)
+
1
));
sprintf
(
output_file_name
,
"%s.deflate"
,
input_file_name
);
FILE
*
input_file
=
fopen
(
input_file_name
,
"r"
);
FILE
*
output_file
=
fopen
(
output_file_name
,
"w"
);
free
(
output_file_name
);
bit_writer
*
bw
=
bit_writer_init
(
output_file
);
bit_writer_write_bit
(
bw
,
1
);
/* BFINAL */
bit_writer_write_bit
(
bw
,
1
);
/* BTYPE */
bit_writer_write_bit
(
bw
,
0
);
// write_byte(bw, 144);
char
c
;
while
(
fread
(
&
c
,
1
,
1
,
input_file
))
{
// printf("Writing %d\n", (int) c);
write_byte
(
bw
,
c
);
}
for
(
int
i
=
0
;
i
<
7
;
i
++
)
bit_writer_write_bit
(
bw
,
0
);
bit_writer_flush
(
bw
);
bit_writer_free
(
bw
);
fclose
(
output_file
);
fclose
(
input_file
);
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help