Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nghttp2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
nghttp2
Commits
7a9eca1f
Commit
7a9eca1f
authored
Dec 19, 2013
by
Tatsuhiro Tsujikawa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
nghttp2_hd: Simplify huffman encode
parent
0af0bd43
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
560 additions
and
579 deletions
+560
-579
lib/nghttp2_hd_huffman.c
lib/nghttp2_hd_huffman.c
+35
-55
lib/nghttp2_hd_huffman.h
lib/nghttp2_hd_huffman.h
+3
-3
lib/nghttp2_hd_huffman_data.c
lib/nghttp2_hd_huffman_data.c
+514
-514
mkhufftbl.py
mkhufftbl.py
+8
-7
No files found.
lib/nghttp2_hd_huffman.c
View file @
7a9eca1f
...
...
@@ -26,6 +26,7 @@
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "nghttp2_hd.h"
...
...
@@ -44,14 +45,12 @@ extern const int16_t res_huff_decode_table[][256];
static
uint8_t
get_prefix_byte
(
const
uint8_t
*
in
,
size_t
len
,
size_t
bitoff
)
{
uint8_t
b
;
size_t
bitleft
;
if
(
bitoff
==
0
)
{
return
*
in
;
}
bitleft
=
8
-
bitoff
;
b
=
(
*
in
&
((
1
<<
bitleft
)
-
1
))
<<
bitoff
;
b
=
*
in
<<
bitoff
;
if
(
len
>
1
)
{
b
|=
*
(
in
+
1
)
>>
bitleft
;
b
|=
*
(
in
+
1
)
>>
(
8
-
bitoff
)
;
}
return
b
;
}
...
...
@@ -90,57 +89,33 @@ static int huff_decode(const uint8_t *in, size_t len, size_t bitoff,
}
return
rv
;
}
/*
* Returns next LSB aligned |nbits| bits from huffman symbol |sym|,
* starting |codebitoff| bit offset (from beginning of code sequence,
* so it could be more than 8).
* Encodes huffman code |sym| into |*dest_ptr|, whose least |rembits|
* bits are not filled yet. The |rembits| must be in range [1, 8],
* inclusive. At the end of the process, the |*dest_ptr| is updated
* and points where next output should be placed. The number of
* unfilled bits in the pointed location is returned.
*/
static
uint8_t
huff_get_lsb_aligned
(
const
nghttp2_huff_sym
*
sym
,
size_t
codebitoff
,
size_t
nbits
)
{
size_t
codeidx
=
codebitoff
/
8
;
uint8_t
a
=
sym
->
code
[
codeidx
];
size_t
localbitoff
=
codebitoff
&
0x7
;
size_t
bitleft
=
8
-
localbitoff
;
if
(
bitleft
>=
nbits
)
{
return
(
a
>>
(
bitleft
-
nbits
))
&
((
1
<<
nbits
)
-
1
);
}
else
{
size_t
right
=
nbits
-
bitleft
;
a
&=
((
1
<<
bitleft
)
-
1
);
a
<<=
right
;
if
((
sym
->
nbits
+
7
)
/
8
>
codeidx
+
1
)
{
a
|=
sym
->
code
[
codeidx
+
1
]
>>
(
8
-
right
);
}
return
a
;
}
}
/*
* Encodes huffman code |sym| into |*dest_ptr|,starting |bitoff|
* offset. The |bitoff| must be strictly less than 8. At the end of
* the process, the |*dest_ptr| is updated and points where next
* output should be placed. The bit offset of the pointed location is
* returned.
*/
static
size_t
huff_encode_sym
(
uint8_t
**
dest_ptr
,
size_t
bitoff
,
static
size_t
huff_encode_sym
(
uint8_t
**
dest_ptr
,
size_t
rembits
,
const
nghttp2_huff_sym
*
sym
)
{
size_t
b
=
0
;
if
(
bitoff
==
0
)
**
dest_ptr
=
0
;
**
dest_ptr
|=
huff_get_lsb_aligned
(
sym
,
b
,
8
-
bitoff
);
b
+=
8
-
bitoff
;
++*
dest_ptr
;
for
(;
b
<
sym
->
nbits
;
b
+=
8
,
++*
dest_ptr
)
{
**
dest_ptr
=
huff_get_lsb_aligned
(
sym
,
b
,
8
);
}
bitoff
=
8
-
(
b
-
sym
->
nbits
);
if
(
bitoff
>
0
)
{
--*
dest_ptr
;
size_t
nbits
=
sym
->
nbits
;
for
(;;)
{
if
(
rembits
>
nbits
)
{
**
dest_ptr
|=
sym
->
code
<<
(
rembits
-
nbits
);
rembits
-=
nbits
;
break
;
}
**
dest_ptr
|=
sym
->
code
>>
(
nbits
-
rembits
);
++*
dest_ptr
;
nbits
-=
rembits
;
rembits
=
8
;
if
(
nbits
==
0
)
{
break
;
}
**
dest_ptr
=
0
;
}
return
bitoff
;
return
rembits
;
}
size_t
nghttp2_hd_huff_encode_count
(
const
uint8_t
*
src
,
size_t
len
,
...
...
@@ -166,7 +141,7 @@ ssize_t nghttp2_hd_huff_encode(uint8_t *dest, size_t destlen,
const
uint8_t
*
src
,
size_t
srclen
,
nghttp2_hd_side
side
)
{
int
bitoff
=
0
;
int
rembits
=
8
;
uint8_t
*
dest_first
=
dest
;
size_t
i
;
const
nghttp2_huff_sym
*
huff_sym_table
;
...
...
@@ -178,13 +153,18 @@ ssize_t nghttp2_hd_huff_encode(uint8_t *dest, size_t destlen,
}
for
(
i
=
0
;
i
<
srclen
;
++
i
)
{
const
nghttp2_huff_sym
*
sym
=
&
huff_sym_table
[
src
[
i
]];
bitoff
=
huff_encode_sym
(
&
dest
,
bitoff
,
sym
);
if
(
rembits
==
8
)
{
*
dest
=
0
;
}
rembits
=
huff_encode_sym
(
&
dest
,
rembits
,
sym
);
}
/* 256 is special terminal symbol, pad with its prefix */
if
(
bitoff
>
0
)
{
*
dest
|=
huff_sym_table
[
256
].
code
[
0
]
>>
bitoff
;
if
(
rembits
<
8
)
{
const
nghttp2_huff_sym
*
sym
=
&
huff_sym_table
[
256
];
*
dest
|=
sym
->
code
>>
(
sym
->
nbits
-
rembits
);
++
dest
;
}
return
dest
-
dest_first
+
(
bitoff
>
0
)
;
return
dest
-
dest_first
;
}
static
int
check_last_byte
(
const
uint8_t
*
src
,
size_t
srclen
,
size_t
idx
,
...
...
lib/nghttp2_hd_huffman.h
View file @
7a9eca1f
...
...
@@ -35,9 +35,9 @@ typedef int16_t huff_decode_table_type[256];
typedef
struct
{
/* The number of bits in this code */
size
_t
nbits
;
/*
Code sequence padded with 0
*/
uint
8_t
code
[
4
]
;
uint32
_t
nbits
;
/*
Huffman code aligned to LSB
*/
uint
32_t
code
;
}
nghttp2_huff_sym
;
#endif
/* NGHTTP2_HD_HUFFMAN_H */
lib/nghttp2_hd_huffman_data.c
View file @
7a9eca1f
This diff is collapsed.
Click to expand it.
mkhufftbl.py
View file @
7a9eca1f
...
...
@@ -44,23 +44,25 @@ root = Node(0)
nodes
.
append
(
root
)
for
line
in
sys
.
stdin
:
m
=
re
.
match
(
r'.*\(\s*(\d+)\) ([|01]+) \[(\d+)\]
.*'
,
line
)
m
=
re
.
match
(
r'.*\(\s*(\d+)\) ([|01]+) \[(\d+)\]
\s+(\S+)
.*'
,
line
)
if
m
:
#print m.group(1), m.group(2), m.group(3)
if
len
(
m
.
group
(
4
))
>
8
:
raise
Error
(
'Code is more than 4 bytes long'
)
sym
=
int
(
m
.
group
(
1
))
pat
=
re
.
sub
(
r'\|'
,
''
,
m
.
group
(
2
))
nbits
=
int
(
m
.
group
(
3
))
assert
(
len
(
pat
)
==
nbits
)
binpat
=
to_bin
(
pat
)
assert
(
len
(
binpat
)
==
(
nbits
+
7
)
/
8
)
symbol_tbl
[
sym
]
=
(
binpat
,
nbits
)
symbol_tbl
[
sym
]
=
(
binpat
,
nbits
,
m
.
group
(
4
)
)
#print "Inserting", sym
insert
(
root
,
sym
,
binpat
,
nbits
,
0
)
print
'''
\
typedef struct {
size
_t nbits;
uint
8_t code[4]
;
uint32
_t nbits;
uint
32_t code
;
} nghttp2_huff_sym;
'''
...
...
@@ -70,9 +72,8 @@ for i in range(257):
pat
=
list
(
symbol_tbl
[
i
][
0
])
pat
+=
[
0
]
*
(
4
-
len
(
pat
))
print
'''
\
{{ {}, {{ {} }} }}{}
\
'''
.
format
(
symbol_tbl
[
i
][
1
],
', '
.
join
([
str
(
k
)
for
k
in
pat
]),
','
if
i
<
256
else
''
)
{{ {}, 0x{}u }}{}
\
'''
.
format
(
symbol_tbl
[
i
][
1
],
symbol_tbl
[
i
][
2
],
','
if
i
<
256
else
''
)
print
'};'
print
''
...
...
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