\doxysection{huff\+\_\+code.\+c}
\label{huff__code_8c_source}\index{src/libsphinxbase/util/huff\_code.c@{src/libsphinxbase/util/huff\_code.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 2009\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00039\ }
\DoxyCodeLine{00040\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/huff\_code.h"{}}}
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/hash\_table.h"{}}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/byteorder.h"{}}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/heap.h"{}}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/pio.h"{}}}
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00047\ }
\DoxyCodeLine{00048\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }huff\_node\_s\ \{}
\DoxyCodeLine{00049\ \ \ \ \ \textcolor{keywordtype}{int}\ nbits;}
\DoxyCodeLine{00050\ \ \ \ \ \textcolor{keyword}{struct\ }huff\_node\_s\ *l;}
\DoxyCodeLine{00051\ \ \ \ \ \textcolor{keyword}{union\ }\{}
\DoxyCodeLine{00052\ \ \ \ \ \ \ \ \ int32\ ival;}
\DoxyCodeLine{00053\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *sval;}
\DoxyCodeLine{00054\ \ \ \ \ \ \ \ \ \textcolor{keyword}{struct\ }huff\_node\_s\ *r;}
\DoxyCodeLine{00055\ \ \ \ \ \}\ r;}
\DoxyCodeLine{00056\ \}\ huff\_node\_t;}
\DoxyCodeLine{00057\ }
\DoxyCodeLine{00058\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }huff\_codeword\_s\ \{}
\DoxyCodeLine{00059\ \ \ \ \ \textcolor{keyword}{union\ }\{}
\DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ int32\ ival;}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *sval;}
\DoxyCodeLine{00062\ \ \ \ \ \}\ r;}
\DoxyCodeLine{00063\ \ \ \ \ uint32\ nbits,\ codeword;}
\DoxyCodeLine{00064\ \}\ huff\_codeword\_t;}
\DoxyCodeLine{00065\ }
\DoxyCodeLine{00066\ \textcolor{keyword}{enum}\ \{}
\DoxyCodeLine{00067\ \ \ \ \ HUFF\_CODE\_INT,}
\DoxyCodeLine{00068\ \ \ \ \ HUFF\_CODE\_STR}
\DoxyCodeLine{00069\ \};}
\DoxyCodeLine{00070\ }
\DoxyCodeLine{00071\ \textcolor{keyword}{struct\ }huff\_code\_s\ \{}
\DoxyCodeLine{00072\ \ \ \ \ int16\ refcount;}
\DoxyCodeLine{00073\ \ \ \ \ uint8\ maxbits;}
\DoxyCodeLine{00074\ \ \ \ \ uint8\ type;}
\DoxyCodeLine{00075\ \ \ \ \ uint32\ *firstcode;}
\DoxyCodeLine{00076\ \ \ \ \ uint32\ *numl;}
\DoxyCodeLine{00077\ \ \ \ \ huff\_codeword\_t\ **syms;}
\DoxyCodeLine{00078\ \ \ \ \ hash\_table\_t\ *codewords;}
\DoxyCodeLine{00079\ \ \ \ \ FILE\ *fh;}
\DoxyCodeLine{00080\ \ \ \ \ bit\_encode\_t\ *be;}
\DoxyCodeLine{00081\ \ \ \ \ \textcolor{keywordtype}{int}\ boff;}
\DoxyCodeLine{00082\ \};}
\DoxyCodeLine{00083\ }
\DoxyCodeLine{00084\ \textcolor{keyword}{static}\ huff\_node\_t\ *}
\DoxyCodeLine{00085\ huff\_node\_new\_int(int32\ val)}
\DoxyCodeLine{00086\ \{}
\DoxyCodeLine{00087\ \ \ \ \ huff\_node\_t\ *hn\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*hn));}
\DoxyCodeLine{00088\ \ \ \ \ hn-\/>r.ival\ =\ val;}
\DoxyCodeLine{00089\ \ \ \ \ \textcolor{keywordflow}{return}\ hn;}
\DoxyCodeLine{00090\ \}}
\DoxyCodeLine{00091\ }
\DoxyCodeLine{00092\ \textcolor{keyword}{static}\ huff\_node\_t\ *}
\DoxyCodeLine{00093\ huff\_node\_new\_str(\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *val)}
\DoxyCodeLine{00094\ \{}
\DoxyCodeLine{00095\ \ \ \ \ huff\_node\_t\ *hn\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*hn));}
\DoxyCodeLine{00096\ \ \ \ \ hn-\/>r.sval\ =\ ckd\_salloc(val);}
\DoxyCodeLine{00097\ \ \ \ \ \textcolor{keywordflow}{return}\ hn;}
\DoxyCodeLine{00098\ \}}
\DoxyCodeLine{00099\ }
\DoxyCodeLine{00100\ \textcolor{keyword}{static}\ huff\_node\_t\ *}
\DoxyCodeLine{00101\ huff\_node\_new\_parent(huff\_node\_t\ *l,\ huff\_node\_t\ *r)}
\DoxyCodeLine{00102\ \{}
\DoxyCodeLine{00103\ \ \ \ \ huff\_node\_t\ *hn\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*hn));}
\DoxyCodeLine{00104\ \ \ \ \ hn-\/>l\ =\ l;}
\DoxyCodeLine{00105\ \ \ \ \ hn-\/>r.r\ =\ r;}
\DoxyCodeLine{00106\ \ \ \ \ \textcolor{comment}{/*\ Propagate\ maximum\ bit\ length.\ */}}
\DoxyCodeLine{00107\ \ \ \ \ \textcolor{keywordflow}{if}\ (r-\/>nbits\ >\ l-\/>nbits)}
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ hn-\/>nbits\ =\ r-\/>nbits\ +\ 1;}
\DoxyCodeLine{00109\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ hn-\/>nbits\ =\ l-\/>nbits\ +\ 1;}
\DoxyCodeLine{00111\ \ \ \ \ \textcolor{keywordflow}{return}\ hn;}
\DoxyCodeLine{00112\ \}}
\DoxyCodeLine{00113\ }
\DoxyCodeLine{00114\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00115\ huff\_node\_free\_int(huff\_node\_t\ *root)}
\DoxyCodeLine{00116\ \{}
\DoxyCodeLine{00117\ \ \ \ \ \textcolor{keywordflow}{if}\ (root-\/>l)\ \{}
\DoxyCodeLine{00118\ \ \ \ \ \ \ \ \ huff\_node\_free\_int(root-\/>l);}
\DoxyCodeLine{00119\ \ \ \ \ \ \ \ \ huff\_node\_free\_int(root-\/>r.r);}
\DoxyCodeLine{00120\ \ \ \ \ \}}
\DoxyCodeLine{00121\ \ \ \ \ ckd\_free(root);}
\DoxyCodeLine{00122\ \}}
\DoxyCodeLine{00123\ }
\DoxyCodeLine{00124\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00125\ huff\_node\_free\_str(huff\_node\_t\ *root,\ \textcolor{keywordtype}{int}\ freestr)}
\DoxyCodeLine{00126\ \{}
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{keywordflow}{if}\ (root-\/>l)\ \{}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ huff\_node\_free\_str(root-\/>l,\ freestr);}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ huff\_node\_free\_str(root-\/>r.r,\ freestr);}
\DoxyCodeLine{00130\ \ \ \ \ \}}
\DoxyCodeLine{00131\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (freestr)}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(root-\/>r.sval);}
\DoxyCodeLine{00134\ \ \ \ \ \}}
\DoxyCodeLine{00135\ \ \ \ \ ckd\_free(root);}
\DoxyCodeLine{00136\ \}}
\DoxyCodeLine{00137\ }
\DoxyCodeLine{00138\ \textcolor{keyword}{static}\ huff\_node\_t\ *}
\DoxyCodeLine{00139\ huff\_code\_build\_tree(heap\_t\ *q)}
\DoxyCodeLine{00140\ \{}
\DoxyCodeLine{00141\ \ \ \ \ huff\_node\_t\ *root\ =\ NULL;}
\DoxyCodeLine{00142\ \ \ \ \ int32\ rf;}
\DoxyCodeLine{00143\ }
\DoxyCodeLine{00144\ \ \ \ \ \textcolor{keywordflow}{while}\ (heap\_size(q)\ >\ 1)\ \{}
\DoxyCodeLine{00145\ \ \ \ \ \ \ \ \ huff\_node\_t\ *l,\ *r,\ *p;}
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ int32\ lf,\ rf;}
\DoxyCodeLine{00147\ }
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ heap\_pop(q,\ (\textcolor{keywordtype}{void}\ *)\&l,\ \&lf);}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ heap\_pop(q,\ (\textcolor{keywordtype}{void}\ *)\&r,\ \&rf);}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ p\ =\ huff\_node\_new\_parent(l,\ r);}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ heap\_insert(q,\ p,\ lf\ +\ rf);}
\DoxyCodeLine{00152\ \ \ \ \ \}}
\DoxyCodeLine{00153\ \ \ \ \ heap\_pop(q,\ (\textcolor{keywordtype}{void}\ **)\&root,\ \&rf);}
\DoxyCodeLine{00154\ \ \ \ \ \textcolor{keywordflow}{return}\ root;}
\DoxyCodeLine{00155\ \}}
\DoxyCodeLine{00156\ }
\DoxyCodeLine{00157\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00158\ huff\_code\_canonicalize(huff\_code\_t\ *hc,\ huff\_node\_t\ *root)}
\DoxyCodeLine{00159\ \{}
\DoxyCodeLine{00160\ \ \ \ \ glist\_t\ agenda;}
\DoxyCodeLine{00161\ \ \ \ \ uint32\ *nextcode;}
\DoxyCodeLine{00162\ \ \ \ \ \textcolor{keywordtype}{int}\ i,\ ncw;}
\DoxyCodeLine{00163\ }
\DoxyCodeLine{00164\ \ \ \ \ hc-\/>firstcode\ =\ ckd\_calloc(hc-\/>maxbits+1,\ \textcolor{keyword}{sizeof}(*hc-\/>firstcode));}
\DoxyCodeLine{00165\ \ \ \ \ hc-\/>syms\ =\ ckd\_calloc(hc-\/>maxbits+1,\ \textcolor{keyword}{sizeof}(*hc-\/>syms));}
\DoxyCodeLine{00166\ \ \ \ \ hc-\/>numl\ =\ ckd\_calloc(hc-\/>maxbits+1,\ \textcolor{keyword}{sizeof}(*nextcode));}
\DoxyCodeLine{00167\ \ \ \ \ nextcode\ =\ ckd\_calloc(hc-\/>maxbits+1,\ \textcolor{keyword}{sizeof}(*nextcode));}
\DoxyCodeLine{00168\ }
\DoxyCodeLine{00169\ \ \ \ \ \textcolor{comment}{/*\ Traverse\ the\ tree,\ annotating\ it\ with\ the\ actual\ bit}}
\DoxyCodeLine{00170\ \textcolor{comment}{\ \ \ \ \ *\ lengths,\ and\ histogramming\ them\ in\ numl.\ */}}
\DoxyCodeLine{00171\ \ \ \ \ root-\/>nbits\ =\ 0;}
\DoxyCodeLine{00172\ \ \ \ \ ncw\ =\ 0;}
\DoxyCodeLine{00173\ \ \ \ \ agenda\ =\ glist\_add\_ptr(NULL,\ root);}
\DoxyCodeLine{00174\ \ \ \ \ \textcolor{keywordflow}{while}\ (agenda)\ \{}
\DoxyCodeLine{00175\ \ \ \ \ \ \ \ \ huff\_node\_t\ *node\ =\ gnode\_ptr(agenda);}
\DoxyCodeLine{00176\ \ \ \ \ \ \ \ \ agenda\ =\ gnode\_free(agenda,\ NULL);}
\DoxyCodeLine{00177\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (node-\/>l)\ \{}
\DoxyCodeLine{00178\ \ \ \ \ \ \ \ \ \ \ \ \ node-\/>l-\/>nbits\ =\ node-\/>nbits\ +\ 1;}
\DoxyCodeLine{00179\ \ \ \ \ \ \ \ \ \ \ \ \ agenda\ =\ glist\_add\_ptr(agenda,\ node-\/>l);}
\DoxyCodeLine{00180\ \ \ \ \ \ \ \ \ \ \ \ \ node-\/>r.r-\/>nbits\ =\ node-\/>nbits\ +\ 1;}
\DoxyCodeLine{00181\ \ \ \ \ \ \ \ \ \ \ \ \ agenda\ =\ glist\_add\_ptr(agenda,\ node-\/>r.r);}
\DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ \ \ \ \ hc-\/>numl[node-\/>nbits]++;}
\DoxyCodeLine{00185\ \ \ \ \ \ \ \ \ \ \ \ \ ncw++;}
\DoxyCodeLine{00186\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00187\ \ \ \ \ \}}
\DoxyCodeLine{00188\ \ \ \ \ \textcolor{comment}{/*\ Create\ starting\ codes\ and\ symbol\ tables\ for\ each\ bit\ length.\ */}}
\DoxyCodeLine{00189\ \ \ \ \ hc-\/>syms[hc-\/>maxbits]\ =\ ckd\_calloc(hc-\/>numl[hc-\/>maxbits],\ \textcolor{keyword}{sizeof}(**hc-\/>syms));}
\DoxyCodeLine{00190\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ hc-\/>maxbits\ -\/\ 1;\ i\ >\ 0;\ -\/-\/i)\ \{}
\DoxyCodeLine{00191\ \ \ \ \ \ \ \ \ hc-\/>firstcode[i]\ =\ (hc-\/>firstcode[i+1]\ +\ hc-\/>numl[i+1])\ /\ 2;}
\DoxyCodeLine{00192\ \ \ \ \ \ \ \ \ hc-\/>syms[i]\ =\ ckd\_calloc(hc-\/>numl[i],\ \textcolor{keyword}{sizeof}(**hc-\/>syms));}
\DoxyCodeLine{00193\ \ \ \ \ \}}
\DoxyCodeLine{00194\ \ \ \ \ memcpy(nextcode,\ hc-\/>firstcode,\ (hc-\/>maxbits\ +\ 1)\ *\ \textcolor{keyword}{sizeof}(*nextcode));}
\DoxyCodeLine{00195\ \ \ \ \ \textcolor{comment}{/*\ Traverse\ the\ tree\ again\ to\ produce\ the\ codebook\ itself.\ */}}
\DoxyCodeLine{00196\ \ \ \ \ hc-\/>codewords\ =\ hash\_table\_new(ncw,\ HASH\_CASE\_YES);}
\DoxyCodeLine{00197\ \ \ \ \ agenda\ =\ glist\_add\_ptr(NULL,\ root);}
\DoxyCodeLine{00198\ \ \ \ \ \textcolor{keywordflow}{while}\ (agenda)\ \{}
\DoxyCodeLine{00199\ \ \ \ \ \ \ \ \ huff\_node\_t\ *node\ =\ gnode\_ptr(agenda);}
\DoxyCodeLine{00200\ \ \ \ \ \ \ \ \ agenda\ =\ gnode\_free(agenda,\ NULL);}
\DoxyCodeLine{00201\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (node-\/>l)\ \{}
\DoxyCodeLine{00202\ \ \ \ \ \ \ \ \ \ \ \ \ agenda\ =\ glist\_add\_ptr(agenda,\ node-\/>l);}
\DoxyCodeLine{00203\ \ \ \ \ \ \ \ \ \ \ \ \ agenda\ =\ glist\_add\_ptr(agenda,\ node-\/>r.r);}
\DoxyCodeLine{00204\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00205\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00206\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Initialize\ codebook\ entry,\ which\ also\ retains\ symbol\ pointer.\ */}}
\DoxyCodeLine{00207\ \ \ \ \ \ \ \ \ \ \ \ \ huff\_codeword\_t\ *cw;}
\DoxyCodeLine{00208\ \ \ \ \ \ \ \ \ \ \ \ \ uint32\ codeword\ =\ nextcode[node-\/>nbits]\ \&\ ((1\ <<\ node-\/>nbits)\ -\/\ 1);}
\DoxyCodeLine{00209\ \ \ \ \ \ \ \ \ \ \ \ \ cw\ =\ hc-\/>syms[node-\/>nbits]\ +\ (codeword\ -\/\ hc-\/>firstcode[node-\/>nbits]);}
\DoxyCodeLine{00210\ \ \ \ \ \ \ \ \ \ \ \ \ cw-\/>nbits\ =\ node-\/>nbits;}
\DoxyCodeLine{00211\ \ \ \ \ \ \ \ \ \ \ \ \ cw-\/>r.sval\ =\ node-\/>r.sval;\ \textcolor{comment}{/*\ Will\ copy\ ints\ too...\ */}}
\DoxyCodeLine{00212\ \ \ \ \ \ \ \ \ \ \ \ \ cw-\/>codeword\ =\ codeword;}
\DoxyCodeLine{00213\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>type\ ==\ HUFF\_CODE\_INT)\ \{}
\DoxyCodeLine{00214\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ hash\_table\_enter\_bkey(hc-\/>codewords,}
\DoxyCodeLine{00215\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)\&cw-\/>r.ival,}
\DoxyCodeLine{00216\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(cw-\/>r.ival),}
\DoxyCodeLine{00217\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{void}\ *)cw);}
\DoxyCodeLine{00218\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00219\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00220\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ hash\_table\_enter(hc-\/>codewords,\ cw-\/>r.sval,\ (\textcolor{keywordtype}{void}\ *)cw);}
\DoxyCodeLine{00221\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00222\ \ \ \ \ \ \ \ \ \ \ \ \ ++nextcode[node-\/>nbits];}
\DoxyCodeLine{00223\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00224\ \ \ \ \ \}}
\DoxyCodeLine{00225\ \ \ \ \ ckd\_free(nextcode);}
\DoxyCodeLine{00226\ \}}
\DoxyCodeLine{00227\ }
\DoxyCodeLine{00228\ huff\_code\_t\ *}
\DoxyCodeLine{00229\ huff\_code\_build\_int(int32\ \textcolor{keyword}{const}\ *values,\ int32\ \textcolor{keyword}{const}\ *frequencies,\ \textcolor{keywordtype}{int}\ nvals)}
\DoxyCodeLine{00230\ \{}
\DoxyCodeLine{00231\ \ \ \ \ huff\_code\_t\ *hc;}
\DoxyCodeLine{00232\ \ \ \ \ huff\_node\_t\ *root;}
\DoxyCodeLine{00233\ \ \ \ \ heap\_t\ *q;}
\DoxyCodeLine{00234\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00235\ }
\DoxyCodeLine{00236\ \ \ \ \ hc\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*hc));}
\DoxyCodeLine{00237\ \ \ \ \ hc-\/>refcount\ =\ 1;}
\DoxyCodeLine{00238\ \ \ \ \ hc-\/>type\ =\ HUFF\_CODE\_INT;}
\DoxyCodeLine{00239\ }
\DoxyCodeLine{00240\ \ \ \ \ \textcolor{comment}{/*\ Initialize\ the\ heap\ with\ nodes\ for\ each\ symbol.\ */}}
\DoxyCodeLine{00241\ \ \ \ \ q\ =\ heap\_new();}
\DoxyCodeLine{00242\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ nvals;\ ++i)\ \{}
\DoxyCodeLine{00243\ \ \ \ \ \ \ \ \ heap\_insert(q,}
\DoxyCodeLine{00244\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ huff\_node\_new\_int(values[i]),}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ frequencies[i]);}
\DoxyCodeLine{00246\ \ \ \ \ \}}
\DoxyCodeLine{00247\ }
\DoxyCodeLine{00248\ \ \ \ \ \textcolor{comment}{/*\ Now\ build\ the\ tree,\ which\ gives\ us\ codeword\ lengths.\ */}}
\DoxyCodeLine{00249\ \ \ \ \ root\ =\ huff\_code\_build\_tree(q);}
\DoxyCodeLine{00250\ \ \ \ \ heap\_destroy(q);}
\DoxyCodeLine{00251\ \ \ \ \ \textcolor{keywordflow}{if}\ (root\ ==\ NULL\ ||\ root-\/>nbits\ >\ 32)\ \{}
\DoxyCodeLine{00252\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Huffman\ trees\ currently\ limited\ to\ 32\ bits\(\backslash\)n"{}});}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ huff\_node\_free\_int(root);}
\DoxyCodeLine{00254\ \ \ \ \ \ \ \ \ huff\_code\_free(hc);}
\DoxyCodeLine{00255\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00256\ \ \ \ \ \}}
\DoxyCodeLine{00257\ }
\DoxyCodeLine{00258\ \ \ \ \ \textcolor{comment}{/*\ Build\ a\ canonical\ codebook.\ */}}
\DoxyCodeLine{00259\ \ \ \ \ hc-\/>maxbits\ =\ root-\/>nbits;}
\DoxyCodeLine{00260\ \ \ \ \ huff\_code\_canonicalize(hc,\ root);}
\DoxyCodeLine{00261\ }
\DoxyCodeLine{00262\ \ \ \ \ \textcolor{comment}{/*\ Tree\ no\ longer\ needed.\ */}}
\DoxyCodeLine{00263\ \ \ \ \ huff\_node\_free\_int(root);}
\DoxyCodeLine{00264\ }
\DoxyCodeLine{00265\ \ \ \ \ \textcolor{keywordflow}{return}\ hc;}
\DoxyCodeLine{00266\ \}}
\DoxyCodeLine{00267\ }
\DoxyCodeLine{00268\ huff\_code\_t\ *}
\DoxyCodeLine{00269\ huff\_code\_build\_str(\textcolor{keywordtype}{char}\ *\ \textcolor{keyword}{const}\ *values,\ int32\ \textcolor{keyword}{const}\ *frequencies,\ \textcolor{keywordtype}{int}\ nvals)}
\DoxyCodeLine{00270\ \{}
\DoxyCodeLine{00271\ \ \ \ \ huff\_code\_t\ *hc;}
\DoxyCodeLine{00272\ \ \ \ \ huff\_node\_t\ *root;}
\DoxyCodeLine{00273\ \ \ \ \ heap\_t\ *q;}
\DoxyCodeLine{00274\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00275\ }
\DoxyCodeLine{00276\ \ \ \ \ hc\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*hc));}
\DoxyCodeLine{00277\ \ \ \ \ hc-\/>refcount\ =\ 1;}
\DoxyCodeLine{00278\ \ \ \ \ hc-\/>type\ =\ HUFF\_CODE\_STR;}
\DoxyCodeLine{00279\ }
\DoxyCodeLine{00280\ \ \ \ \ \textcolor{comment}{/*\ Initialize\ the\ heap\ with\ nodes\ for\ each\ symbol.\ */}}
\DoxyCodeLine{00281\ \ \ \ \ q\ =\ heap\_new();}
\DoxyCodeLine{00282\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ nvals;\ ++i)\ \{}
\DoxyCodeLine{00283\ \ \ \ \ \ \ \ \ heap\_insert(q,}
\DoxyCodeLine{00284\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ huff\_node\_new\_str(values[i]),}
\DoxyCodeLine{00285\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ frequencies[i]);}
\DoxyCodeLine{00286\ \ \ \ \ \}}
\DoxyCodeLine{00287\ }
\DoxyCodeLine{00288\ \ \ \ \ \textcolor{comment}{/*\ Now\ build\ the\ tree,\ which\ gives\ us\ codeword\ lengths.\ */}}
\DoxyCodeLine{00289\ \ \ \ \ root\ =\ huff\_code\_build\_tree(q);}
\DoxyCodeLine{00290\ \ \ \ \ heap\_destroy(q);}
\DoxyCodeLine{00291\ \ \ \ \ \textcolor{keywordflow}{if}\ (root\ ==\ NULL\ ||\ root-\/>nbits\ >\ 32)\ \{}
\DoxyCodeLine{00292\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Huffman\ trees\ currently\ limited\ to\ 32\ bits\(\backslash\)n"{}});}
\DoxyCodeLine{00293\ \ \ \ \ \ \ \ \ huff\_node\_free\_str(root,\ TRUE);}
\DoxyCodeLine{00294\ \ \ \ \ \ \ \ \ huff\_code\_free(hc);}
\DoxyCodeLine{00295\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00296\ \ \ \ \ \}}
\DoxyCodeLine{00297\ }
\DoxyCodeLine{00298\ \ \ \ \ \textcolor{comment}{/*\ Build\ a\ canonical\ codebook.\ */}}
\DoxyCodeLine{00299\ \ \ \ \ hc-\/>maxbits\ =\ root-\/>nbits;}
\DoxyCodeLine{00300\ \ \ \ \ huff\_code\_canonicalize(hc,\ root);}
\DoxyCodeLine{00301\ }
\DoxyCodeLine{00302\ \ \ \ \ \textcolor{comment}{/*\ Tree\ no\ longer\ needed\ (note\ we\ retain\ pointers\ to\ its\ strings).\ */}}
\DoxyCodeLine{00303\ \ \ \ \ huff\_node\_free\_str(root,\ FALSE);}
\DoxyCodeLine{00304\ }
\DoxyCodeLine{00305\ \ \ \ \ \textcolor{keywordflow}{return}\ hc;}
\DoxyCodeLine{00306\ \}}
\DoxyCodeLine{00307\ }
\DoxyCodeLine{00308\ huff\_code\_t\ *}
\DoxyCodeLine{00309\ huff\_code\_read(FILE\ *infh)}
\DoxyCodeLine{00310\ \{}
\DoxyCodeLine{00311\ \ \ \ \ huff\_code\_t\ *hc;}
\DoxyCodeLine{00312\ \ \ \ \ \textcolor{keywordtype}{int}\ i,\ j;}
\DoxyCodeLine{00313\ }
\DoxyCodeLine{00314\ \ \ \ \ hc\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*hc));}
\DoxyCodeLine{00315\ \ \ \ \ hc-\/>refcount\ =\ 1;}
\DoxyCodeLine{00316\ }
\DoxyCodeLine{00317\ \ \ \ \ hc-\/>maxbits\ =\ fgetc(infh);}
\DoxyCodeLine{00318\ \ \ \ \ hc-\/>type\ =\ fgetc(infh);}
\DoxyCodeLine{00319\ }
\DoxyCodeLine{00320\ \ \ \ \ \textcolor{comment}{/*\ Two\ bytes\ of\ padding.\ */}}
\DoxyCodeLine{00321\ \ \ \ \ fgetc(infh);}
\DoxyCodeLine{00322\ \ \ \ \ fgetc(infh);}
\DoxyCodeLine{00323\ }
\DoxyCodeLine{00324\ \ \ \ \ \textcolor{comment}{/*\ Allocate\ stuff.\ */}}
\DoxyCodeLine{00325\ \ \ \ \ hc-\/>firstcode\ =\ ckd\_calloc(hc-\/>maxbits\ +\ 1,\ \textcolor{keyword}{sizeof}(*hc-\/>firstcode));}
\DoxyCodeLine{00326\ \ \ \ \ hc-\/>numl\ =\ ckd\_calloc(hc-\/>maxbits\ +\ 1,\ \textcolor{keyword}{sizeof}(*hc-\/>numl));}
\DoxyCodeLine{00327\ \ \ \ \ hc-\/>syms\ =\ ckd\_calloc(hc-\/>maxbits\ +\ 1,\ \textcolor{keyword}{sizeof}(*hc-\/>syms));}
\DoxyCodeLine{00328\ }
\DoxyCodeLine{00329\ \ \ \ \ \textcolor{comment}{/*\ Read\ the\ symbol\ tables.\ */}}
\DoxyCodeLine{00330\ \ \ \ \ hc-\/>codewords\ =\ hash\_table\_new(hc-\/>maxbits,\ HASH\_CASE\_YES);}
\DoxyCodeLine{00331\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 1;\ i\ <=\ hc-\/>maxbits;\ ++i)\ \{}
\DoxyCodeLine{00332\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fread(\&hc-\/>firstcode[i],\ 4,\ 1,\ infh)\ !=\ 1)}
\DoxyCodeLine{00333\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00334\ \ \ \ \ \ \ \ \ SWAP\_BE\_32(\&hc-\/>firstcode[i]);}
\DoxyCodeLine{00335\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fread(\&hc-\/>numl[i],\ 4,\ 1,\ infh)\ !=\ 1)}
\DoxyCodeLine{00336\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00337\ \ \ \ \ \ \ \ \ SWAP\_BE\_32(\&hc-\/>numl[i]);}
\DoxyCodeLine{00338\ \ \ \ \ \ \ \ \ hc-\/>syms[i]\ =\ ckd\_calloc(hc-\/>numl[i],\ \textcolor{keyword}{sizeof}(**hc-\/>syms));}
\DoxyCodeLine{00339\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ hc-\/>numl[i];\ ++j)\ \{}
\DoxyCodeLine{00340\ \ \ \ \ \ \ \ \ \ \ \ \ huff\_codeword\_t\ *cw\ =\ \&hc-\/>syms[i][j];}
\DoxyCodeLine{00341\ \ \ \ \ \ \ \ \ \ \ \ \ cw-\/>nbits\ =\ i;}
\DoxyCodeLine{00342\ \ \ \ \ \ \ \ \ \ \ \ \ cw-\/>codeword\ =\ hc-\/>firstcode[i]\ +\ j;}
\DoxyCodeLine{00343\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>type\ ==\ HUFF\_CODE\_INT)\ \{}
\DoxyCodeLine{00344\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fread(\&cw-\/>r.ival,\ 4,\ 1,\ infh)\ !=\ 1)}
\DoxyCodeLine{00345\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00346\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ SWAP\_BE\_32(\&cw-\/>r.ival);}
\DoxyCodeLine{00347\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ hash\_table\_enter\_bkey(hc-\/>codewords,}
\DoxyCodeLine{00348\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)\&cw-\/>r.ival,}
\DoxyCodeLine{00349\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(cw-\/>r.ival),}
\DoxyCodeLine{00350\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{void}\ *)cw);}
\DoxyCodeLine{00351\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00352\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00353\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len;}
\DoxyCodeLine{00354\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cw-\/>r.sval\ =\ fread\_line(infh,\ \&len);}
\DoxyCodeLine{00355\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cw-\/>r.sval[len-\/1]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00356\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ hash\_table\_enter(hc-\/>codewords,\ cw-\/>r.sval,\ (\textcolor{keywordtype}{void}\ *)cw);}
\DoxyCodeLine{00357\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00358\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00359\ \ \ \ \ \}}
\DoxyCodeLine{00360\ }
\DoxyCodeLine{00361\ \ \ \ \ \textcolor{keywordflow}{return}\ hc;}
\DoxyCodeLine{00362\ error\_out:}
\DoxyCodeLine{00363\ \ \ \ \ huff\_code\_free(hc);}
\DoxyCodeLine{00364\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00365\ \}}
\DoxyCodeLine{00366\ }
\DoxyCodeLine{00367\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00368\ huff\_code\_write(huff\_code\_t\ *hc,\ FILE\ *outfh)}
\DoxyCodeLine{00369\ \{}
\DoxyCodeLine{00370\ \ \ \ \ \textcolor{keywordtype}{int}\ i,\ j;}
\DoxyCodeLine{00371\ }
\DoxyCodeLine{00372\ \ \ \ \ \textcolor{comment}{/*\ Maximum\ codeword\ length\ */}}
\DoxyCodeLine{00373\ \ \ \ \ fputc(hc-\/>maxbits,\ outfh);}
\DoxyCodeLine{00374\ \ \ \ \ \textcolor{comment}{/*\ Symbol\ type\ */}}
\DoxyCodeLine{00375\ \ \ \ \ fputc(hc-\/>type,\ outfh);}
\DoxyCodeLine{00376\ \ \ \ \ \textcolor{comment}{/*\ Two\ extra\ bytes\ (for\ future\ use\ and\ alignment)\ */}}
\DoxyCodeLine{00377\ \ \ \ \ fputc(0,\ outfh);}
\DoxyCodeLine{00378\ \ \ \ \ fputc(0,\ outfh);}
\DoxyCodeLine{00379\ \ \ \ \ \textcolor{comment}{/*\ For\ each\ codeword\ length:\ */}}
\DoxyCodeLine{00380\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 1;\ i\ <=\ hc-\/>maxbits;\ ++i)\ \{}
\DoxyCodeLine{00381\ \ \ \ \ \ \ \ \ uint32\ val;}
\DoxyCodeLine{00382\ }
\DoxyCodeLine{00383\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Starting\ code,\ number\ of\ codes.\ */}}
\DoxyCodeLine{00384\ \ \ \ \ \ \ \ \ val\ =\ hc-\/>firstcode[i];}
\DoxyCodeLine{00385\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Canonically\ big-\/endian\ (like\ the\ data\ itself)\ */}}
\DoxyCodeLine{00386\ \ \ \ \ \ \ \ \ SWAP\_BE\_32(\&val);}
\DoxyCodeLine{00387\ \ \ \ \ \ \ \ \ fwrite(\&val,\ 4,\ 1,\ outfh);}
\DoxyCodeLine{00388\ \ \ \ \ \ \ \ \ val\ =\ hc-\/>numl[i];}
\DoxyCodeLine{00389\ \ \ \ \ \ \ \ \ SWAP\_BE\_32(\&val);}
\DoxyCodeLine{00390\ \ \ \ \ \ \ \ \ fwrite(\&val,\ 4,\ 1,\ outfh);}
\DoxyCodeLine{00391\ }
\DoxyCodeLine{00392\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Symbols\ for\ each\ code\ (FIXME:\ Should\ compress\ these\ too)\ */}}
\DoxyCodeLine{00393\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ hc-\/>numl[i];\ ++j)\ \{}
\DoxyCodeLine{00394\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>type\ ==\ HUFF\_CODE\_INT)\ \{}
\DoxyCodeLine{00395\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ val\ =\ hc-\/>syms[i][j].r.ival;}
\DoxyCodeLine{00396\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ SWAP\_BE\_32(\&val);}
\DoxyCodeLine{00397\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fwrite(\&val,\ 4,\ 1,\ outfh);}
\DoxyCodeLine{00398\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00399\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00400\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Write\ them\ all\ separated\ by\ newlines,\ so\ that}}
\DoxyCodeLine{00401\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ *\ fgets()\ will\ read\ them\ for\ us.\ */}}
\DoxyCodeLine{00402\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fprintf(outfh,\ \textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ hc-\/>syms[i][j].r.sval);}
\DoxyCodeLine{00403\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00404\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00405\ \ \ \ \ \}}
\DoxyCodeLine{00406\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00407\ \}}
\DoxyCodeLine{00408\ }
\DoxyCodeLine{00409\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00410\ huff\_code\_dump\_codebits(FILE\ *dumpfh,\ uint32\ nbits,\ uint32\ codeword)}
\DoxyCodeLine{00411\ \{}
\DoxyCodeLine{00412\ \ \ \ \ uint32\ i;}
\DoxyCodeLine{00413\ }
\DoxyCodeLine{00414\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ nbits;\ ++i)}
\DoxyCodeLine{00415\ \ \ \ \ \ \ \ \ fputc((codeword\ \&\ (1<<(nbits-\/i-\/1)))\ ?\ \textcolor{charliteral}{'1'}\ :\ \textcolor{stringliteral}{'0'},\ dumpfh);}
\DoxyCodeLine{00416\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00417\ \}}
\DoxyCodeLine{00418\ }
\DoxyCodeLine{00419\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00420\ huff\_code\_dump(huff\_code\_t\ *hc,\ FILE\ *dumpfh)}
\DoxyCodeLine{00421\ \{}
\DoxyCodeLine{00422\ \ \ \ \ \textcolor{keywordtype}{int}\ i,\ j;}
\DoxyCodeLine{00423\ }
\DoxyCodeLine{00424\ \ \ \ \ \textcolor{comment}{/*\ Print\ out\ all\ codewords.\ */}}
\DoxyCodeLine{00425\ \ \ \ \ fprintf(dumpfh,\ \textcolor{stringliteral}{"{}Maximum\ codeword\ length:\ \%d\(\backslash\)n"{}},\ hc-\/>maxbits);}
\DoxyCodeLine{00426\ \ \ \ \ fprintf(dumpfh,\ \textcolor{stringliteral}{"{}Symbols\ are\ \%s\(\backslash\)n"{}},\ (hc-\/>type\ ==\ HUFF\_CODE\_STR)\ ?\ \textcolor{stringliteral}{"{}strings"{}}\ :\ \textcolor{stringliteral}{"{}ints"{}});}
\DoxyCodeLine{00427\ \ \ \ \ fprintf(dumpfh,\ \textcolor{stringliteral}{"{}Codewords:\(\backslash\)n"{}});}
\DoxyCodeLine{00428\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 1;\ i\ <=\ hc-\/>maxbits;\ ++i)\ \{}
\DoxyCodeLine{00429\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ hc-\/>numl[i];\ ++j)\ \{}
\DoxyCodeLine{00430\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>type\ ==\ HUFF\_CODE\_STR)}
\DoxyCodeLine{00431\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fprintf(dumpfh,\ \textcolor{stringliteral}{"{}\%-\/30s"{}},\ hc-\/>syms[i][j].r.sval);}
\DoxyCodeLine{00432\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00433\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fprintf(dumpfh,\ \textcolor{stringliteral}{"{}\%-\/30d"{}},\ hc-\/>syms[i][j].r.ival);}
\DoxyCodeLine{00434\ \ \ \ \ \ \ \ \ \ \ \ \ huff\_code\_dump\_codebits(dumpfh,\ hc-\/>syms[i][j].nbits,}
\DoxyCodeLine{00435\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ hc-\/>syms[i][j].codeword);}
\DoxyCodeLine{00436\ \ \ \ \ \ \ \ \ \ \ \ \ fprintf(dumpfh,\ \textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{00437\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00438\ \ \ \ \ \}}
\DoxyCodeLine{00439\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00440\ \}}
\DoxyCodeLine{00441\ }
\DoxyCodeLine{00442\ huff\_code\_t\ *}
\DoxyCodeLine{00443\ huff\_code\_retain(huff\_code\_t\ *hc)}
\DoxyCodeLine{00444\ \{}
\DoxyCodeLine{00445\ \ \ \ \ ++hc-\/>refcount;}
\DoxyCodeLine{00446\ \ \ \ \ \textcolor{keywordflow}{return}\ hc;}
\DoxyCodeLine{00447\ \}}
\DoxyCodeLine{00448\ }
\DoxyCodeLine{00449\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00450\ huff\_code\_free(huff\_code\_t\ *hc)}
\DoxyCodeLine{00451\ \{}
\DoxyCodeLine{00452\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00453\ }
\DoxyCodeLine{00454\ \ \ \ \ \textcolor{keywordflow}{if}\ (hc\ ==\ NULL)}
\DoxyCodeLine{00455\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00456\ \ \ \ \ \textcolor{keywordflow}{if}\ (-\/-\/hc-\/>refcount\ >\ 0)}
\DoxyCodeLine{00457\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ hc-\/>refcount;}
\DoxyCodeLine{00458\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <=\ hc-\/>maxbits;\ ++i)\ \{}
\DoxyCodeLine{00459\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ j;}
\DoxyCodeLine{00460\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ hc-\/>numl[i];\ ++j)\ \{}
\DoxyCodeLine{00461\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>type\ ==\ HUFF\_CODE\_STR)}
\DoxyCodeLine{00462\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(hc-\/>syms[i][j].r.sval);}
\DoxyCodeLine{00463\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00464\ \ \ \ \ \ \ \ \ ckd\_free(hc-\/>syms[i]);}
\DoxyCodeLine{00465\ \ \ \ \ \}}
\DoxyCodeLine{00466\ \ \ \ \ ckd\_free(hc-\/>firstcode);}
\DoxyCodeLine{00467\ \ \ \ \ ckd\_free(hc-\/>numl);}
\DoxyCodeLine{00468\ \ \ \ \ ckd\_free(hc-\/>syms);}
\DoxyCodeLine{00469\ \ \ \ \ hash\_table\_free(hc-\/>codewords);}
\DoxyCodeLine{00470\ \ \ \ \ ckd\_free(hc);}
\DoxyCodeLine{00471\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00472\ \}}
\DoxyCodeLine{00473\ }
\DoxyCodeLine{00474\ FILE\ *}
\DoxyCodeLine{00475\ huff\_code\_attach(huff\_code\_t\ *hc,\ FILE\ *fh,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *mode)}
\DoxyCodeLine{00476\ \{}
\DoxyCodeLine{00477\ \ \ \ \ FILE\ *oldfh\ =\ huff\_code\_detach(hc);}
\DoxyCodeLine{00478\ }
\DoxyCodeLine{00479\ \ \ \ \ hc-\/>fh\ =\ fh;}
\DoxyCodeLine{00480\ \ \ \ \ \textcolor{keywordflow}{if}\ (mode[0]\ ==\ \textcolor{charliteral}{'w'})}
\DoxyCodeLine{00481\ \ \ \ \ \ \ \ \ hc-\/>be\ =\ bit\_encode\_attach(hc-\/>fh);}
\DoxyCodeLine{00482\ \ \ \ \ \textcolor{keywordflow}{return}\ oldfh;}
\DoxyCodeLine{00483\ \}}
\DoxyCodeLine{00484\ }
\DoxyCodeLine{00485\ FILE\ *}
\DoxyCodeLine{00486\ huff\_code\_detach(huff\_code\_t\ *hc)}
\DoxyCodeLine{00487\ \{}
\DoxyCodeLine{00488\ \ \ \ \ FILE\ *oldfh\ =\ hc-\/>fh;}
\DoxyCodeLine{00489\ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00490\ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>be)\ \{}
\DoxyCodeLine{00491\ \ \ \ \ \ \ \ \ bit\_encode\_flush(hc-\/>be);}
\DoxyCodeLine{00492\ \ \ \ \ \ \ \ \ bit\_encode\_free(hc-\/>be);}
\DoxyCodeLine{00493\ \ \ \ \ \ \ \ \ hc-\/>be\ =\ NULL;}
\DoxyCodeLine{00494\ \ \ \ \ \}}
\DoxyCodeLine{00495\ \ \ \ \ hc-\/>fh\ =\ NULL;}
\DoxyCodeLine{00496\ \ \ \ \ \textcolor{keywordflow}{return}\ oldfh;}
\DoxyCodeLine{00497\ \}}
\DoxyCodeLine{00498\ }
\DoxyCodeLine{00499\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00500\ huff\_code\_encode\_int(huff\_code\_t\ *hc,\ int32\ sym,\ uint32\ *outcw)}
\DoxyCodeLine{00501\ \{}
\DoxyCodeLine{00502\ \ \ \ \ huff\_codeword\_t\ *cw;}
\DoxyCodeLine{00503\ }
\DoxyCodeLine{00504\ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup\_bkey(hc-\/>codewords,}
\DoxyCodeLine{00505\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)\&sym,}
\DoxyCodeLine{00506\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(sym),}
\DoxyCodeLine{00507\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{void}\ **)\&cw)\ <\ 0)}
\DoxyCodeLine{00508\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00509\ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>be)}
\DoxyCodeLine{00510\ \ \ \ \ \ \ \ \ bit\_encode\_write\_cw(hc-\/>be,\ cw-\/>codeword,\ cw-\/>nbits);}
\DoxyCodeLine{00511\ \ \ \ \ \textcolor{keywordflow}{if}\ (outcw)\ *outcw\ =\ cw-\/>codeword;}
\DoxyCodeLine{00512\ \ \ \ \ \textcolor{keywordflow}{return}\ cw-\/>nbits;}
\DoxyCodeLine{00513\ \}}
\DoxyCodeLine{00514\ }
\DoxyCodeLine{00515\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00516\ huff\_code\_encode\_str(huff\_code\_t\ *hc,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *sym,\ uint32\ *outcw)}
\DoxyCodeLine{00517\ \{}
\DoxyCodeLine{00518\ \ \ \ \ huff\_codeword\_t\ *cw;}
\DoxyCodeLine{00519\ }
\DoxyCodeLine{00520\ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup(hc-\/>codewords,}
\DoxyCodeLine{00521\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sym,}
\DoxyCodeLine{00522\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{void}\ **)\&cw)\ <\ 0)}
\DoxyCodeLine{00523\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00524\ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>be)}
\DoxyCodeLine{00525\ \ \ \ \ \ \ \ \ bit\_encode\_write\_cw(hc-\/>be,\ cw-\/>codeword,\ cw-\/>nbits);}
\DoxyCodeLine{00526\ \ \ \ \ \textcolor{keywordflow}{if}\ (outcw)\ *outcw\ =\ cw-\/>codeword;}
\DoxyCodeLine{00527\ \ \ \ \ \textcolor{keywordflow}{return}\ cw-\/>nbits;}
\DoxyCodeLine{00528\ \}}
\DoxyCodeLine{00529\ }
\DoxyCodeLine{00530\ \textcolor{keyword}{static}\ huff\_codeword\_t\ *}
\DoxyCodeLine{00531\ huff\_code\_decode\_data(huff\_code\_t\ *hc,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ **inout\_data,}
\DoxyCodeLine{00532\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ *inout\_data\_len,\ \textcolor{keywordtype}{int}\ *inout\_offset)}
\DoxyCodeLine{00533\ \{}
\DoxyCodeLine{00534\ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *data\ =\ *inout\_data;}
\DoxyCodeLine{00535\ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *end\ =\ data\ +\ *inout\_data\_len;}
\DoxyCodeLine{00536\ \ \ \ \ \textcolor{keywordtype}{int}\ offset\ =\ *inout\_offset;}
\DoxyCodeLine{00537\ \ \ \ \ uint32\ cw;}
\DoxyCodeLine{00538\ \ \ \ \ \textcolor{keywordtype}{int}\ cwlen;}
\DoxyCodeLine{00539\ \ \ \ \ \textcolor{keywordtype}{int}\ byte;}
\DoxyCodeLine{00540\ }
\DoxyCodeLine{00541\ \ \ \ \ \textcolor{keywordflow}{if}\ (data\ ==\ end)}
\DoxyCodeLine{00542\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00543\ \ \ \ \ \textcolor{keywordtype}{byte}\ =\ *data++;}
\DoxyCodeLine{00544\ \ \ \ \ cw\ =\ !!(\textcolor{keywordtype}{byte}\ \&\ (1\ <<\ (7-\/offset++)));}
\DoxyCodeLine{00545\ \ \ \ \ cwlen\ =\ 1;}
\DoxyCodeLine{00546\ \ \ \ \ \textcolor{comment}{/*\ printf("{}\%.*x\ "{},\ cwlen,\ cw);\ */}}
\DoxyCodeLine{00547\ \ \ \ \ \textcolor{keywordflow}{while}\ (cwlen\ <=\ hc-\/>maxbits\ \&\&\ cw\ <\ hc-\/>firstcode[cwlen])\ \{}
\DoxyCodeLine{00548\ \ \ \ \ \ \ \ \ ++cwlen;}
\DoxyCodeLine{00549\ \ \ \ \ \ \ \ \ cw\ <<=\ 1;}
\DoxyCodeLine{00550\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (offset\ >\ 7)\ \{}
\DoxyCodeLine{00551\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (data\ ==\ end)}
\DoxyCodeLine{00552\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00553\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{byte}\ =\ *data++;}
\DoxyCodeLine{00554\ \ \ \ \ \ \ \ \ \ \ \ \ offset\ =\ 0;}
\DoxyCodeLine{00555\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00556\ \ \ \ \ \ \ \ \ cw\ |=\ !!(\textcolor{keywordtype}{byte}\ \&\ (1\ <<\ (7-\/offset++)));}
\DoxyCodeLine{00557\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ printf("{}\%.*x\ "{},\ cwlen,\ cw);\ */}}
\DoxyCodeLine{00558\ \ \ \ \ \}}
\DoxyCodeLine{00559\ \ \ \ \ \textcolor{keywordflow}{if}\ (cwlen\ >\ hc-\/>maxbits)\ \textcolor{comment}{/*\ FAIL:\ invalid\ data\ */}}
\DoxyCodeLine{00560\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00561\ }
\DoxyCodeLine{00562\ \ \ \ \ \textcolor{comment}{/*\ Put\ the\ last\ byte\ back\ if\ there\ are\ bits\ left\ over.\ */}}
\DoxyCodeLine{00563\ \ \ \ \ \textcolor{keywordflow}{if}\ (offset\ <\ 8)}
\DoxyCodeLine{00564\ \ \ \ \ \ \ \ \ -\/-\/data;}
\DoxyCodeLine{00565\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00566\ \ \ \ \ \ \ \ \ offset\ =\ 0;}
\DoxyCodeLine{00567\ }
\DoxyCodeLine{00568\ \ \ \ \ \textcolor{comment}{/*\ printf("{}\%.*x\(\backslash\)n"{},\ cwlen,\ cw);\ */}}
\DoxyCodeLine{00569\ \ \ \ \ *inout\_data\_len\ =\ end\ -\/\ data;}
\DoxyCodeLine{00570\ \ \ \ \ *inout\_data\ =\ data;}
\DoxyCodeLine{00571\ \ \ \ \ *inout\_offset\ =\ offset;}
\DoxyCodeLine{00572\ \ \ \ \ \textcolor{keywordflow}{return}\ hc-\/>syms[cwlen]\ +\ (cw\ -\/\ hc-\/>firstcode[cwlen]);}
\DoxyCodeLine{00573\ \}}
\DoxyCodeLine{00574\ }
\DoxyCodeLine{00575\ \textcolor{keyword}{static}\ huff\_codeword\_t\ *}
\DoxyCodeLine{00576\ huff\_code\_decode\_fh(huff\_code\_t\ *hc)}
\DoxyCodeLine{00577\ \{}
\DoxyCodeLine{00578\ \ \ \ \ uint32\ cw;}
\DoxyCodeLine{00579\ \ \ \ \ \textcolor{keywordtype}{int}\ cwlen;}
\DoxyCodeLine{00580\ \ \ \ \ \textcolor{keywordtype}{int}\ byte;}
\DoxyCodeLine{00581\ }
\DoxyCodeLine{00582\ \ \ \ \ \textcolor{keywordflow}{if}\ ((\textcolor{keywordtype}{byte}\ =\ fgetc(hc-\/>fh))\ ==\ EOF)}
\DoxyCodeLine{00583\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00584\ \ \ \ \ cw\ =\ !!(\textcolor{keywordtype}{byte}\ \&\ (1\ <<\ (7-\/hc-\/>boff++)));}
\DoxyCodeLine{00585\ \ \ \ \ cwlen\ =\ 1;}
\DoxyCodeLine{00586\ \ \ \ \ \textcolor{comment}{/*\ printf("{}\%.*x\ "{},\ cwlen,\ cw);\ */}}
\DoxyCodeLine{00587\ \ \ \ \ \textcolor{keywordflow}{while}\ (cwlen\ <=\ hc-\/>maxbits\ \&\&\ cw\ <\ hc-\/>firstcode[cwlen])\ \{}
\DoxyCodeLine{00588\ \ \ \ \ \ \ \ \ ++cwlen;}
\DoxyCodeLine{00589\ \ \ \ \ \ \ \ \ cw\ <<=\ 1;}
\DoxyCodeLine{00590\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>boff\ >\ 7)\ \{}
\DoxyCodeLine{00591\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((\textcolor{keywordtype}{byte}\ =\ fgetc(hc-\/>fh))\ ==\ EOF)}
\DoxyCodeLine{00592\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00593\ \ \ \ \ \ \ \ \ \ \ \ \ hc-\/>boff\ =\ 0;}
\DoxyCodeLine{00594\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00595\ \ \ \ \ \ \ \ \ cw\ |=\ !!(\textcolor{keywordtype}{byte}\ \&\ (1\ <<\ (7-\/hc-\/>boff++)));}
\DoxyCodeLine{00596\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ printf("{}\%.*x\ "{},\ cwlen,\ cw);\ */}}
\DoxyCodeLine{00597\ \ \ \ \ \}}
\DoxyCodeLine{00598\ \ \ \ \ \textcolor{keywordflow}{if}\ (cwlen\ >\ hc-\/>maxbits)\ \textcolor{comment}{/*\ FAIL:\ invalid\ data\ */}}
\DoxyCodeLine{00599\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00600\ }
\DoxyCodeLine{00601\ \ \ \ \ \textcolor{comment}{/*\ Put\ the\ last\ byte\ back\ if\ there\ are\ bits\ left\ over.\ */}}
\DoxyCodeLine{00602\ \ \ \ \ \textcolor{keywordflow}{if}\ (hc-\/>boff\ <\ 8)}
\DoxyCodeLine{00603\ \ \ \ \ \ \ \ \ ungetc(\textcolor{keywordtype}{byte},\ hc-\/>fh);}
\DoxyCodeLine{00604\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00605\ \ \ \ \ \ \ \ \ hc-\/>boff\ =\ 0;}
\DoxyCodeLine{00606\ }
\DoxyCodeLine{00607\ \ \ \ \ \textcolor{comment}{/*\ printf("{}\%.*x\(\backslash\)n"{},\ cwlen,\ cw);\ */}}
\DoxyCodeLine{00608\ \ \ \ \ \textcolor{keywordflow}{return}\ hc-\/>syms[cwlen]\ +\ (cw\ -\/\ hc-\/>firstcode[cwlen]);}
\DoxyCodeLine{00609\ \}}
\DoxyCodeLine{00610\ }
\DoxyCodeLine{00611\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00612\ huff\_code\_decode\_int(huff\_code\_t\ *hc,\ \textcolor{keywordtype}{int}\ *outval,}
\DoxyCodeLine{00613\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ **inout\_data,}
\DoxyCodeLine{00614\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ *inout\_data\_len,\ \textcolor{keywordtype}{int}\ *inout\_offset)}
\DoxyCodeLine{00615\ \{}
\DoxyCodeLine{00616\ \ \ \ \ huff\_codeword\_t\ *cw;}
\DoxyCodeLine{00617\ }
\DoxyCodeLine{00618\ \ \ \ \ \textcolor{keywordflow}{if}\ (inout\_data)}
\DoxyCodeLine{00619\ \ \ \ \ \ \ \ \ cw\ =\ huff\_code\_decode\_data(hc,\ inout\_data,\ inout\_data\_len,\ inout\_offset);}
\DoxyCodeLine{00620\ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (hc-\/>fh)}
\DoxyCodeLine{00621\ \ \ \ \ \ \ \ \ cw\ =\ huff\_code\_decode\_fh(hc);}
\DoxyCodeLine{00622\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00623\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00624\ }
\DoxyCodeLine{00625\ \ \ \ \ \textcolor{keywordflow}{if}\ (cw\ ==\ NULL)}
\DoxyCodeLine{00626\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00627\ \ \ \ \ \textcolor{keywordflow}{if}\ (outval)}
\DoxyCodeLine{00628\ \ \ \ \ \ \ \ \ *outval\ =\ cw-\/>r.ival;}
\DoxyCodeLine{00629\ }
\DoxyCodeLine{00630\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00631\ \}}
\DoxyCodeLine{00632\ }
\DoxyCodeLine{00633\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *}
\DoxyCodeLine{00634\ huff\_code\_decode\_str(huff\_code\_t\ *hc,}
\DoxyCodeLine{00635\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ **inout\_data,}
\DoxyCodeLine{00636\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ *inout\_data\_len,\ \textcolor{keywordtype}{int}\ *inout\_offset)}
\DoxyCodeLine{00637\ \{}
\DoxyCodeLine{00638\ \ \ \ \ huff\_codeword\_t\ *cw;}
\DoxyCodeLine{00639\ }
\DoxyCodeLine{00640\ \ \ \ \ \textcolor{keywordflow}{if}\ (inout\_data)}
\DoxyCodeLine{00641\ \ \ \ \ \ \ \ \ cw\ =\ huff\_code\_decode\_data(hc,\ inout\_data,\ inout\_data\_len,\ inout\_offset);}
\DoxyCodeLine{00642\ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (hc-\/>fh)}
\DoxyCodeLine{00643\ \ \ \ \ \ \ \ \ cw\ =\ huff\_code\_decode\_fh(hc);}
\DoxyCodeLine{00644\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00645\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00646\ }
\DoxyCodeLine{00647\ \ \ \ \ \textcolor{keywordflow}{if}\ (cw\ ==\ NULL)}
\DoxyCodeLine{00648\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00649\ }
\DoxyCodeLine{00650\ \ \ \ \ \textcolor{keywordflow}{return}\ cw-\/>r.sval;}
\DoxyCodeLine{00651\ \}}

\end{DoxyCode}
