You are on page 1of 8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

/ * E s t ep r o g r a m ai m p l e m e n t au ns e n c i l l oc o m p r e s o r d eH u f f m a n ,q u ep o d r i au s a r s ep a r au np r o g r a m a d em a y o re n v e r g a d u r a . c o m p r i m i r :h u fCe n t r a d as a l i d a d e s c o m p r . :h u fDe n t r a d as a l i d a i n f o : h u fIe n t r a d a * / # i n c l u d e< s t d i o . h > # i n c l u d e< m a l l o c . h > s t r u c tn o d o { s t r u c tn o d o* d e r , * i z q , * a r r ; / *f o r m ae ln o d o* / i n tc u e n t a ; / *a p a r i c i o n e sd e lc a r c t e r* / c h a rb i t ; / *0o1* / u n s i g n e dc h a rk a r a c t e r ; / *e lc a r c t e r( p a r al ad e s c o m p r e s i n* / c h a r* c o d i g o ; / *c a d e n ad ec e r o syu n o sc o nl ac o d i f i c a c i n* / c h a rn b i t s ; / *m ea p u n t oe ln u m e r od eb i t sq u ec o d i f i c a ne l c a r c t e r* / } H O J A S [ 2 5 6 ] , * T E L A R [ 2 5 6 ] , * M E N O R , * S E G U N D O ; i n tN S I M B = 0 , n s i m b ; F I L E* f , * g ; i n tN B Y T E S = 0 ; / * p r e p a r a rl a sh o j a s * / i n tp r e p a r a r _ h o j a s ( c h a r* a r c h i v o ) { i n tj ; f o r ( j = 0 ; j < 2 5 6 ; + + j ) { H O J A S [ j ] . d e r = H O J A S [ j ] . i z q = H O J A S [ j ] . a r r = N U L L ; H O J A S [ j ] . c u e n t a = 0 ; H O J A S [ j ] . k a r a c t e r = j ; H O J A S [ j ] . c o d i g o = N U L L ; } i f( ( f = f o p e n ( a r c h i v o , " r b " ) ) ! = N U L L ) { w h i l e( ( j = f g e t c ( f ) ) ! = E O F ) { + + H O J A S [ j ] . c u e n t a ; + + N B Y T E S ; } f c l o s e ( f ) ; } e l s e { r e t u r n ( 1 ) ; } f o r ( j = 0 ; j < 2 5 6 ; + + j ) { i f( H O J A S [ j ] . c u e n t a ! = 0 ) + + N S I M B ; } n s i m b = N S I M B ;
http://www.disc.ua.es/~gil/huffman.c 1/8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

r e t u r n ( 0 ) ; } / * p r e p a r a rt e l a r * / v o i dp r e p a r a r _ t e l a r ( ) { i n tj ; f o r ( j = 0 ; j < 2 5 6 ; + + j ) { T E L A R [ j ] = & ( H O J A S [ j ] ) ; } r e t u r n ; } / * t e j e re l r b o l * / v o i dt e j e r ( ) { i n tm e n o r = 1 ; / *g u a r d ai n d i c e* / i n ts e g u n d o = 1 ; / *g u a r d ai n d i c e* / i n tt e m p o r a l ; / *g u a r d ac u e n t a* / i n tj ; s t r u c tn o d o* P ; / *n u e v on o d o* / i f( n s i m b = = 1 )r e t u r n ; / *b u s c a rm e n o rv a l o r* / f o r ( j = 0 ; j < 2 5 6 ; + + j ) { i f( T E L A R [ j ] = = N U L L )c o n t i n u e ; i f( T E L A R [ j ] > c u e n t a = = 0 )c o n t i n u e ; i f( m e n o r = = 1 ) { m e n o r = j ; t e m p o r a l = T E L A R [ j ] > c u e n t a ; }e l s e { i f( T E L A R [ j ] > c u e n t a < t e m p o r a l ) { m e n o r = j ; t e m p o r a l = T E L A R [ j ] > c u e n t a ; } } } / *b u s c a rs e g u n d om e n o r* / f o r ( j = 0 ; j < 2 5 6 ; + + j ) { i f( T E L A R [ j ] = = N U L L )c o n t i n u e ; i f( T E L A R [ j ] > c u e n t a = = 0 )c o n t i n u e ; i f( j = = m e n o r )c o n t i n u e ; i f( s e g u n d o = = 1 ) { s e g u n d o = j ; t e m p o r a l = T E L A R [ j ] > c u e n t a ; }e l s e { i f( T E L A R [ j ] > c u e n t a < t e m p o r a l ) { s e g u n d o = j ; t e m p o r a l = T E L A R [ j ] > c u e n t a ;
http://www.disc.ua.es/~gil/huffman.c 2/8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

} } } / *t e j e ru nn u e v on o d o* / P = ( s t r u c tn o d o* ) m a l l o c ( s i z e o f ( s t r u c tn o d o ) ) ; T E L A R [ m e n o r ] > a r r = P ; T E L A R [ s e g u n d o ] > a r r = P ; P > i z q = T E L A R [ m e n o r ] ; P > d e r = T E L A R [ s e g u n d o ] ; P > a r r = N U L L ; T E L A R [ m e n o r ] > b i t = 0 ; T E L A R [ s e g u n d o ] > b i t = 1 ; P > c u e n t a = T E L A R [ m e n o r ] > c u e n t a + T E L A R [ s e g u n d o ] > c u e n t a ; T E L A R [ m e n o r ] = N U L L ; T E L A R [ s e g u n d o ] = P ; n s i m b ; / *s i g u et e j i e n d oh a s t aq u es l oq u e d eu nn o d o* / t e j e r ( ) ; } / * U n av e zc o n s t r u i d oe l r b o l ,p u e d oc o d i f i c a r c a d ac a r c t e r .P a r ae s or e c o r r od e s d el ah o j a al ar a z ,a p u n t o0o1e nu n ap i l ayl u e g o p a s ol ap i l aau n ac a d e n a .U n2d e t e r m i n ae l f i nd el ac a d e n a . * / v o i dc o d i f i c a r ( ) { c h a rp i l a [ 6 4 ] ; c h a rt o p e ; i n tj ; c h a r* w ; s t r u c tn o d o* P ; f o r ( j = 0 ; j < 2 5 6 ; + + j ) { i f( H O J A S [ j ] . c u e n t a = = 0 )c o n t i n u e ; P = ( s t r u c tn o d o* ) ( & ( H O J A S [ j ] ) ) ; t o p e = 0 ; w h i l e( P > a r r ! = N U L L ) { p i l a [ t o p e ] = P > b i t ; + + t o p e ; P = P > a r r ; } H O J A S [ j ] . n b i t s = t o p e ; H O J A S [ j ] . c o d i g o = ( c h a r* ) m a l l o c ( ( t o p e + 1 ) * s i z e o f ( c h a r ) ) ; w = H O J A S [ j ] . c o d i g o ; t o p e ; w h i l e( t o p e > 1 ) { * w = p i l a [ t o p e ] ; t o p e ; + + w ; } * w = 2 ; } r e t u r n ;
http://www.disc.ua.es/~gil/huffman.c 3/8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

/ * d e b u g .I m p r i m el ai n f os o b r ec a d a c a r c t e r ,c o m on m e r od ea p a r i c i o n e s yc a d e n ac o nq u es ec o d i f i c a * / v o i dd e b u g ( ) { i n tj , k ; c h a r* w ; i n tt a m _ c o m p r i m i d o = 0 ; f o r ( j = 0 ; j < 2 5 6 ; + + j ) { i f( H O J A S [ j ] . c u e n t a = = 0 )c o n t i n u e ; t a m _ c o m p r i m i d o + = ( H O J A S [ j ] . c u e n t a * H O J A S [ j ] . n b i t s ) ; p r i n t f ( " % 3 d% 6 d" , j , H O J A S [ j ] . c u e n t a ) ; w = H O J A S [ j ] . c o d i g o ; w h i l e( * w ! = 2 ) { p r i n t f ( " % c " , 4 8 + ( * w ) ) ; + + w ; } p r i n t f ( " \ n " ) ; } p r i n t f ( " N S I M B :% d \ n " , N S I M B ) ; p r i n t f ( " N B Y T E S :% d \ n " , N B Y T E S ) ; p r i n t f ( " T A M A OC O M P R I M I D O :% d \ n " , t a m _ c o m p r i m i d o / 8 + 1 ) ; r e t u r n ; } / * E s c r i b el ac a b e c e r ad e la r c h i v od e d e s t i n o .L ac a b e c e r ac o n t i e n e :e l n m e r od eb y t e sd e la r c h i v oo r i g e n , e ln m e r od ec a r a c t e r e sd i s t i n t o s e ne s ea r c h i v oyu n al i s t ad ep a r e j a s n m e r od ec a r c t e r c u e n t ad ee s e c a r c t e r .E s oe ss u f i c i e n t ep a r al a d e s c o m p r e s i n * / i n te s c r i b e _ c a b e c e r a ( c h a r* d e s t i n o ) { i n tj , k ; F I L E* g ; c h a r* p = ( c h a r* ) ( & N B Y T E S ) ; i f( ( g = f o p e n ( d e s t i n o , " w b " ) ) = = N U L L )r e t u r n ( 1 ) ; f o r ( j = 0 ; j < 4 ; + + j ) { f p u t c ( * p , g ) ; + + p ; } p = ( c h a r* ) ( & N S I M B ) ; f p u t c ( * p , g ) ; f o r ( j = 0 ; j < 2 5 6 ; + + j ) { i f( H O J A S [ j ] . c u e n t a = = 0 )c o n t i n u e ;
http://www.disc.ua.es/~gil/huffman.c 4/8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

f p u t c ( j , g ) ; p = ( c h a r* ) ( & ( H O J A S [ j ] . c u e n t a ) ) ; f o r ( k = 0 ; k < 4 ; + + k ) { f p u t c ( * p , g ) ; + + p ; } } f c l o s e ( g ) ; r e t u r n ( 0 ) ; } / * U n av e zc o n s t r u i d oe l r b o lyc o d i f i c a d o c a d ac a r c t e rs ep u e d ep r o c e d e ral a c o m p r e s i n :s et o m a r c a r c t e rac a r c t e r d e la r c h i v oo r i g e nys eu s a r l ac a d e n a d ec o d i f i c a c i np a r ai re s c r i b i e n d o b i t se nu nb u f f e rd eu nc a r c t e r ,q u e c a d av e zq u eq u e d el l e n os ep a s a r a l a r c h i v od ed e s t i n o * / i n tc o m p r i m i r ( c h a r* o r i g e n ,c h a r* d e s t i n o ) { u n s i g n e dc h a rd = 0 ; i n tx ; c h a rn b i t = 0 ; c h a r* p ; i f( ( f = f o p e n ( o r i g e n , " r b " ) ) = = N U L L )r e t u r n ( 1 ) ; i f( ( g = f o p e n ( d e s t i n o , " a b " ) ) = = N U L L )r e t u r n ( 2 ) ;/ *y ae s t al ac a b e c e r a* / w h i l e( ( x = f g e t c ( f ) ) ! = E O F ) { p = H O J A S [ x ] . c o d i g o ; w h i l e( * p ! = 2 ) { i f( n b i t = = 8 ) { n b i t = 0 ; f p u t c ( d , g ) ; d = 0 ; }e l s e { i f( * p = = 1 ) { d | = ( 1 < < n b i t ) ; } + + n b i t ; + + p ; } } } f p u t c ( d , g ) ; f c l o s e ( f ) ; f c l o s e ( g ) ; r e t u r n ( 0 ) ; } / * D e s c o m p r i m ee la r c h i v o .E lp r i m e rp a s o e sl e e rl ac a b e c e r a ,p a s op r e v i oal a
http://www.disc.ua.es/~gil/huffman.c 5/8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

d e s c o m p r e s i n .R e c u e r d of o r m a t od e l ac a b e c e r a : N B Y T E S | N S I M B | ( c h a r , c u e n t a ) * * / i n td e s c o m p r i m i r ( c h a r* o r i g e n ,c h a r* d e s t i n o ) { c h a r* p ; i n tj , k , n , m ; u n s i g n e dc h a rx , n b i t ; s t r u c tn o d o* P , * Q ; i f( ( g = f o p e n ( o r i g e n , " r b " ) ) = = N U L L )r e t u r n ( 1 ) ; i f( ( f = f o p e n ( d e s t i n o , " w b " ) ) = = N U L L )r e t u r n ( 2 ) ; / *l e e rN B Y T E S* / p = ( c h a r* ) ( & n ) ; f o r ( j = 0 ; j < 4 ; + + j ) { * p = ( u n s i g n e dc h a r ) f g e t c ( g ) ; + + p ; } N B Y T E S = n ; / *l e e rN S I M B* / N S I M B = n s i m b = f g e t c ( g ) ; / *p r e p a r a rl a sh o j a s* / f o r ( j = 0 ; j < 2 5 6 ; + + j ) { H O J A S [ j ] . c u e n t a = 0 ; H O J A S [ j ] . i z q = H O J A S [ j ] . d e r = H O J A S [ j ] . a r r = N U L L ; H O J A S [ j ] . k a r a c t e r = j ; } f o r ( j = 0 ; j < N S I M B ; + + j ) { n = f g e t c ( g ) ; p = ( c h a r* ) ( & m ) ; f o r ( k = 0 ; k < 4 ; + + k ) { * p = ( u n s i g n e dc h a r ) f g e t c ( g ) ; + + p ; } H O J A S [ n ] . c u e n t a = m ; } / *c o n s t r u y oe l r b o l* / p r e p a r a r _ t e l a r ( ) ; t e j e r ( ) ; / *a p u n t oal ar a zd e l r b o l* / j = 0 ; w h i l e( H O J A S [ j ] . c u e n t a = = 0 )+ + j ; P = ( s t r u c tn o d o* ) ( & ( H O J A S [ j ] ) ) ; w h i l e( P > a r r ! = N U L L )P = P > a r r ; / *a h o r ay as ep u e d ed e s c o m p r i m i r* / j = 0 ; x = f g e t c ( g ) ; n b i t = 0 ; Q = P ; w h i l e ( j < N B Y T E S ) {
http://www.disc.ua.es/~gil/huffman.c 6/8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

i f( Q > i z q = = N U L L ) { f p u t c ( Q > k a r a c t e r , f ) ; Q = P ; + + j ; }e l s e i f( n b i t = = 8 ) { x = f g e t c ( g ) ; n b i t = 0 ; }e l s e { i f( x & ( 1 < < n b i t ) ) { Q = Q > d e r ; } e l s e { Q = Q > i z q ; } + + n b i t ; } } f c l o s e ( f ) ; f c l o s e ( g ) ; r e t u r n ( 0 ) ; } m a i n ( i n ta r g c ,c h a r* a r g v [ ] ) { i n tj ; i f( a r g c < 2 )r e t u r n ; i f( * ( a r g v [ 1 ] ) = = ' C ' ) {/ *c o m p r i m i r* / i f( a r g c ! = 4 )r e t u r n ; i f( p r e p a r a r _ h o j a s ( a r g v [ 2 ] ) ) { p r i n t f ( " e r r o ra b r i e n d oa r c h i v o \ n " ) ; r e t u r n ; } p r e p a r a r _ t e l a r ( ) ; t e j e r ( ) ; c o d i f i c a r ( ) ; i f( e s c r i b e _ c a b e c e r a ( a r g v [ 3 ] ) ) { p r i n t f ( " e r r o ra b r i e n d oa r c h i v o \ n " ) ; r e t u r n ; } i f( c o m p r i m i r ( a r g v [ 2 ] , a r g v [ 3 ] ) ) { p r i n t f ( " e r r o ra b r i e n d oa r c h i v o \ n " ) ; r e t u r n ; } } e l s e i f( * ( a r g v [ 1 ] ) = = ' D ' ) {/ *d e s c o m p r i m i r* / i f( a r g c ! = 4 )r e t u r n ; i f( d e s c o m p r i m i r ( a r g v [ 2 ] , a r g v [ 3 ] ) ) { p r i n t f ( " e r r o ra b r i e n d oa r c h i v o \ n " ) ; r e t u r n ; } } e l s e
http://www.disc.ua.es/~gil/huffman.c 7/8

9/2/2014

https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDoQFjAD&url=http%3A%2F%2Fwww.disc.ua.es%2F~gil%2Fhuffma

i f( * ( a r g v [ 1 ] ) = = ' I ' ) {/ *i n f o* / i f( a r g c ! = 3 )r e t u r n ; i f( p r e p a r a r _ h o j a s ( a r g v [ 2 ] ) ) { p r i n t f ( " e r r o ra b r i e n d oa r c h i v o \ n " ) ; r e t u r n ; } p r e p a r a r _ t e l a r ( ) ; t e j e r ( ) ; c o d i f i c a r ( ) ; d e b u g ( ) ; } r e t u r n ; }

http://www.disc.ua.es/~gil/huffman.c

8/8