comparison lzw.c @ 4728:5db8e9e8f71d libavcodec

move eob_reached logic into ff_lzw_decode_tail() which simplifies the code, avoids some checks in the innermost loop and also gets rid of the controversal break while hopefully retaining the last byte in a valid bytestream, invalid bytestreams still can have very significant overread
author michael
date Sun, 25 Mar 2007 15:43:46 +0000
parents 23f8f6efc870
children 8583aa3c21bc
comparison
equal deleted inserted replaced
4727:23f8f6efc870 4728:5db8e9e8f71d
40 0x00FF, 0x01FF, 0x03FF, 0x07FF, 40 0x00FF, 0x01FF, 0x03FF, 0x07FF,
41 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF 41 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
42 }; 42 };
43 43
44 struct LZWState { 44 struct LZWState {
45 int eob_reached;
46 uint8_t *pbuf, *ebuf; 45 uint8_t *pbuf, *ebuf;
47 int bbits; 46 int bbits;
48 unsigned int bbuf; 47 unsigned int bbuf;
49 48
50 int mode; ///< Decoder mode 49 int mode; ///< Decoder mode
72 71
73 if(s->mode == FF_LZW_GIF) { 72 if(s->mode == FF_LZW_GIF) {
74 while (s->bbits < s->cursize) { 73 while (s->bbits < s->cursize) {
75 if (!s->bs) { 74 if (!s->bs) {
76 s->bs = *s->pbuf++; 75 s->bs = *s->pbuf++;
77 if(!s->bs) {
78 s->eob_reached = 1;
79 break;
80 }
81 } 76 }
82 s->bbuf |= (*s->pbuf++) << s->bbits; 77 s->bbuf |= (*s->pbuf++) << s->bbits;
83 s->bbits += 8; 78 s->bbits += 8;
84 s->bs--; 79 s->bs--;
85 } 80 }
86 c = s->bbuf & s->curmask; 81 c = s->bbuf & s->curmask;
87 s->bbuf >>= s->cursize; 82 s->bbuf >>= s->cursize;
88 } else { // TIFF 83 } else { // TIFF
89 while (s->bbits < s->cursize) { 84 while (s->bbits < s->cursize) {
90 if (s->pbuf >= s->ebuf) {
91 s->eob_reached = 1;
92 }
93 s->bbuf = (s->bbuf << 8) | (*s->pbuf++); 85 s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
94 s->bbits += 8; 86 s->bbits += 8;
95 } 87 }
96 c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask; 88 c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask;
97 } 89 }
105 } 97 }
106 98
107 void ff_lzw_decode_tail(LZWState *p) 99 void ff_lzw_decode_tail(LZWState *p)
108 { 100 {
109 struct LZWState *s = (struct LZWState *)p; 101 struct LZWState *s = (struct LZWState *)p;
110 while(!s->eob_reached) 102
111 lzw_get_code(s); 103 if(s->mode == FF_LZW_GIF) {
104 while(s->pbuf < s->ebuf && s->bs>0){
105 s->pbuf += s->bs;
106 s->bs = *s->pbuf++;
107 }
108 }else
109 s->pbuf= s->ebuf;
112 } 110 }
113 111
114 void ff_lzw_decode_open(LZWState **p) 112 void ff_lzw_decode_open(LZWState **p)
115 { 113 {
116 *p = av_mallocz(sizeof(struct LZWState)); 114 *p = av_mallocz(sizeof(struct LZWState));
134 struct LZWState *s = (struct LZWState *)p; 132 struct LZWState *s = (struct LZWState *)p;
135 133
136 if(csize < 1 || csize > LZW_MAXBITS) 134 if(csize < 1 || csize > LZW_MAXBITS)
137 return -1; 135 return -1;
138 /* read buffer */ 136 /* read buffer */
139 s->eob_reached = 0;
140 s->pbuf = buf; 137 s->pbuf = buf;
141 s->ebuf = s->pbuf + buf_size; 138 s->ebuf = s->pbuf + buf_size;
142 s->bbuf = 0; 139 s->bbuf = 0;
143 s->bbits = 0; 140 s->bbits = 0;
144 s->bs = 0; 141 s->bs = 0;