Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Rudolf
/
huffman
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
94e5b5c8
authored
Nov 22, 2016
by
Rudolf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor refactoring. Update throwaway size info and make it possible to change
parent
d976ef40
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
38 deletions
huffman.c
huffman.c
View file @
94e5b5c8
...
@@ -7,36 +7,9 @@
...
@@ -7,36 +7,9 @@
#include "header.h"
#include "header.h"
#include "print_helper.h"
#include "print_helper.h"
#define HUFFMAN_START 3
/* The first bits that are reserved for throwaways. The throwaways indicates the
* number of bits to throw away at the end when decoding the file. */
void
free_tree
(
struct
tree
*
parent
)
#define THROWAWAY_SIZE 3
{
if
(
parent
==
NULL
)
return
;
free_tree
(
parent
->
left
);
free_tree
(
parent
->
right
);
free
(
parent
);
}
long
get_file_size
(
FILE
*
file
)
{
long
pos
=
ftell
(
file
);
fseek
(
file
,
0
,
SEEK_END
);
long
size
=
ftell
(
file
);
fseek
(
file
,
pos
,
SEEK_SET
);
return
size
;
}
/* free it yourself faget. */
char
*
read_text
(
FILE
*
file
,
long
size
)
{
char
*
buf
=
malloc
(
size
+
1
);
fread
(
buf
,
size
,
1
,
file
);
buf
[
size
]
=
'\0'
;
return
buf
;
}
int
insert_throwaways
(
FILE
*
file
,
unsigned
char
throwaways
)
int
insert_throwaways
(
FILE
*
file
,
unsigned
char
throwaways
)
{
{
...
@@ -47,9 +20,9 @@ int insert_throwaways(FILE *file, unsigned char throwaways)
...
@@ -47,9 +20,9 @@ int insert_throwaways(FILE *file, unsigned char throwaways)
fseek
(
file
,
0
,
SEEK_SET
);
fseek
(
file
,
0
,
SEEK_SET
);
fread
(
&
firstbyte
,
sizeof
(
firstbyte
),
1
,
file
);
fread
(
&
firstbyte
,
sizeof
(
firstbyte
),
1
,
file
);
throwaways
&=
0x
7
;
throwaways
&=
0x
ff
>>
(
8
-
THROWAWAY_SIZE
)
;
firstbyte
|=
(
throwaways
<<
5
);
firstbyte
|=
(
throwaways
<<
(
8
-
THROWAWAY_SIZE
)
);
fseek
(
file
,
0
,
SEEK_SET
);
fseek
(
file
,
0
,
SEEK_SET
);
fwrite
(
&
firstbyte
,
sizeof
(
firstbyte
),
1
,
file
);
fwrite
(
&
firstbyte
,
sizeof
(
firstbyte
),
1
,
file
);
...
@@ -68,22 +41,51 @@ unsigned char read_throwaways(FILE *file)
...
@@ -68,22 +41,51 @@ unsigned char read_throwaways(FILE *file)
fseek
(
file
,
0
,
SEEK_SET
);
fseek
(
file
,
0
,
SEEK_SET
);
fread
(
&
firstbyte
,
sizeof
(
firstbyte
),
1
,
file
);
fread
(
&
firstbyte
,
sizeof
(
firstbyte
),
1
,
file
);
firstbyte
>>=
5
;
firstbyte
>>=
8
-
THROWAWAY_SIZE
;
firstbyte
&=
0x
7
;
firstbyte
&=
0x
ff
>>
(
8
-
THROWAWAY_SIZE
)
;
fseek
(
file
,
filepos
,
SEEK_SET
);
fseek
(
file
,
filepos
,
SEEK_SET
);
return
firstbyte
;
return
firstbyte
;
}
}
int
encode_file
(
FILE
*
in
,
FILE
*
out
)
long
get_file_size
(
FILE
*
file
)
{
long
pos
=
ftell
(
file
);
fseek
(
file
,
0
,
SEEK_END
);
long
size
=
ftell
(
file
);
fseek
(
file
,
pos
,
SEEK_SET
);
return
size
;
}
static
void
free_tree
(
struct
tree
*
parent
)
{
if
(
parent
==
NULL
)
return
;
free_tree
(
parent
->
left
);
free_tree
(
parent
->
right
);
free
(
parent
);
}
/* free it yourself faget. */
static
char
*
read_text
(
FILE
*
file
,
long
size
)
{
char
*
buf
=
malloc
(
size
+
1
);
fread
(
buf
,
size
,
1
,
file
);
buf
[
size
]
=
'\0'
;
return
buf
;
}
static
int
encode_file
(
FILE
*
in
,
FILE
*
out
)
{
{
struct
tree
*
tree
;
struct
tree
*
tree
;
struct
BIT_BUFFER
bitbuf
;
struct
BIT_BUFFER
bitbuf
;
bb_init
(
&
bitbuf
,
out
);
bb_init
(
&
bitbuf
,
out
);
/* Make room for throwaways right now. */
/* Make room for throwaways right now. */
bitbuf
.
pos
+=
HUFFMAN_START
;
bitbuf
.
pos
+=
THROWAWAY_SIZE
;
long
size
=
get_file_size
(
in
);
long
size
=
get_file_size
(
in
);
...
@@ -106,13 +108,13 @@ int encode_file(FILE *in, FILE *out)
...
@@ -106,13 +108,13 @@ int encode_file(FILE *in, FILE *out)
bb_flush
(
&
bitbuf
);
bb_flush
(
&
bitbuf
);
}
}
int
decode_file
(
FILE
*
in
,
FILE
*
out
)
static
int
decode_file
(
FILE
*
in
,
FILE
*
out
)
{
{
struct
tree
*
tree
=
malloc
(
sizeof
(
*
tree
));
struct
tree
*
tree
=
malloc
(
sizeof
(
*
tree
));
struct
BIT_BUFFER
bitbuf
;
struct
BIT_BUFFER
bitbuf
;
bb_init
(
&
bitbuf
,
in
);
bb_init
(
&
bitbuf
,
in
);
bitbuf
.
pos
+=
HUFFMAN_START
;
bitbuf
.
pos
+=
THROWAWAY_SIZE
;
fseek
(
in
,
0
,
SEEK_SET
);
fseek
(
in
,
0
,
SEEK_SET
);
decode_header
(
&
bitbuf
,
tree
);
decode_header
(
&
bitbuf
,
tree
);
...
...
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