Fossil SCM
Unroll loop for even greater speed
Commit
cc09e00358fd274a2d1167104e4539a35c852477
Parent
63313a5f168933d…
1 file changed
+19
-9
+19
-9
| --- src/lookslike.c | ||
| +++ src/lookslike.c | ||
| @@ -150,23 +150,23 @@ | ||
| 150 | 150 | }; |
| 151 | 151 | static const unsigned char us2b[] = { /* for lead bytes 0xC2-0xDF */ |
| 152 | 152 | 2, 0x80, 0xBF |
| 153 | 153 | }; |
| 154 | 154 | static const unsigned char us3a[] = { /* for lead byte 0xE0 */ |
| 155 | - 3, 0xA0, 0xBF, 0x80, 0xBF | |
| 155 | + 3, 0xA0, 0xBF | |
| 156 | 156 | }; |
| 157 | 157 | static const unsigned char us3b[] = { /* for lead bytes 0xE1-0xEF */ |
| 158 | - 3, 0x80, 0xBF, 0x80, 0xBF | |
| 158 | + 3, 0x80, 0xBF | |
| 159 | 159 | }; |
| 160 | 160 | static const unsigned char us4a[] = { /* for lead byte 0xF0 */ |
| 161 | - 4, 0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF | |
| 161 | + 4, 0x90, 0xBF | |
| 162 | 162 | }; |
| 163 | 163 | static const unsigned char us4b[] = { /* for lead bytes 0xF1-0xF3 */ |
| 164 | - 4, 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF | |
| 164 | + 4, 0x80, 0xBF | |
| 165 | 165 | }; |
| 166 | 166 | static const unsigned char us4c[] = { /* for lead byte 0xF4 */ |
| 167 | - 4, 0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF | |
| 167 | + 4, 0x80, 0x8F | |
| 168 | 168 | }; |
| 169 | 169 | |
| 170 | 170 | /* a table used for quick lookup of the definition that goes with a |
| 171 | 171 | * particular lead byte */ |
| 172 | 172 | static const unsigned char* const lb_tab[] = { |
| @@ -202,24 +202,34 @@ | ||
| 202 | 202 | ++z; |
| 203 | 203 | --n; |
| 204 | 204 | }else{ |
| 205 | 205 | /* get the definition for this lead byte */ |
| 206 | 206 | const unsigned char* def = lb_tab[(*z++)-0x80]; |
| 207 | - unsigned char i, len; | |
| 207 | + unsigned char len; | |
| 208 | 208 | |
| 209 | 209 | /* if the definition doesn't exist, return invalid */ |
| 210 | 210 | if( !def ) return LOOK_INVALID; |
| 211 | 211 | /* get the expected sequence length */ |
| 212 | 212 | len = *def++; |
| 213 | 213 | /* if there aren't enough bytes left, return invalid */ |
| 214 | - if( n<len ) return LOOK_INVALID; | |
| 214 | + if( n<len ) { | |
| 215 | + return LOOK_INVALID; | |
| 216 | + } | |
| 215 | 217 | /* we already know byte #0 is good, so check the remaining bytes */ |
| 216 | - for(i=1; i<len; ++i){ | |
| 218 | + if( (*z<*def++) || (*z++>*def++) ){ | |
| 217 | 219 | /* if the byte is outside the allowed range for this definition, |
| 218 | 220 | * return invalid */ |
| 219 | - if( (*z<*def++) || (*z++>*def++) ){ | |
| 221 | + return LOOK_INVALID; | |
| 222 | + } | |
| 223 | + if( len > 2 ){ | |
| 224 | + if( (*z<0x80) || (*z++>0xBF) ){ | |
| 220 | 225 | return LOOK_INVALID; |
| 226 | + } | |
| 227 | + if( len > 3 ){ | |
| 228 | + if( (*z<0x80) || (*z++>0xBF) ){ | |
| 229 | + return LOOK_INVALID; | |
| 230 | + } | |
| 221 | 231 | } |
| 222 | 232 | } |
| 223 | 233 | /* advance to the next sequence */ |
| 224 | 234 | n -= len; |
| 225 | 235 | } |
| 226 | 236 |
| --- src/lookslike.c | |
| +++ src/lookslike.c | |
| @@ -150,23 +150,23 @@ | |
| 150 | }; |
| 151 | static const unsigned char us2b[] = { /* for lead bytes 0xC2-0xDF */ |
| 152 | 2, 0x80, 0xBF |
| 153 | }; |
| 154 | static const unsigned char us3a[] = { /* for lead byte 0xE0 */ |
| 155 | 3, 0xA0, 0xBF, 0x80, 0xBF |
| 156 | }; |
| 157 | static const unsigned char us3b[] = { /* for lead bytes 0xE1-0xEF */ |
| 158 | 3, 0x80, 0xBF, 0x80, 0xBF |
| 159 | }; |
| 160 | static const unsigned char us4a[] = { /* for lead byte 0xF0 */ |
| 161 | 4, 0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF |
| 162 | }; |
| 163 | static const unsigned char us4b[] = { /* for lead bytes 0xF1-0xF3 */ |
| 164 | 4, 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF |
| 165 | }; |
| 166 | static const unsigned char us4c[] = { /* for lead byte 0xF4 */ |
| 167 | 4, 0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF |
| 168 | }; |
| 169 | |
| 170 | /* a table used for quick lookup of the definition that goes with a |
| 171 | * particular lead byte */ |
| 172 | static const unsigned char* const lb_tab[] = { |
| @@ -202,24 +202,34 @@ | |
| 202 | ++z; |
| 203 | --n; |
| 204 | }else{ |
| 205 | /* get the definition for this lead byte */ |
| 206 | const unsigned char* def = lb_tab[(*z++)-0x80]; |
| 207 | unsigned char i, len; |
| 208 | |
| 209 | /* if the definition doesn't exist, return invalid */ |
| 210 | if( !def ) return LOOK_INVALID; |
| 211 | /* get the expected sequence length */ |
| 212 | len = *def++; |
| 213 | /* if there aren't enough bytes left, return invalid */ |
| 214 | if( n<len ) return LOOK_INVALID; |
| 215 | /* we already know byte #0 is good, so check the remaining bytes */ |
| 216 | for(i=1; i<len; ++i){ |
| 217 | /* if the byte is outside the allowed range for this definition, |
| 218 | * return invalid */ |
| 219 | if( (*z<*def++) || (*z++>*def++) ){ |
| 220 | return LOOK_INVALID; |
| 221 | } |
| 222 | } |
| 223 | /* advance to the next sequence */ |
| 224 | n -= len; |
| 225 | } |
| 226 |
| --- src/lookslike.c | |
| +++ src/lookslike.c | |
| @@ -150,23 +150,23 @@ | |
| 150 | }; |
| 151 | static const unsigned char us2b[] = { /* for lead bytes 0xC2-0xDF */ |
| 152 | 2, 0x80, 0xBF |
| 153 | }; |
| 154 | static const unsigned char us3a[] = { /* for lead byte 0xE0 */ |
| 155 | 3, 0xA0, 0xBF |
| 156 | }; |
| 157 | static const unsigned char us3b[] = { /* for lead bytes 0xE1-0xEF */ |
| 158 | 3, 0x80, 0xBF |
| 159 | }; |
| 160 | static const unsigned char us4a[] = { /* for lead byte 0xF0 */ |
| 161 | 4, 0x90, 0xBF |
| 162 | }; |
| 163 | static const unsigned char us4b[] = { /* for lead bytes 0xF1-0xF3 */ |
| 164 | 4, 0x80, 0xBF |
| 165 | }; |
| 166 | static const unsigned char us4c[] = { /* for lead byte 0xF4 */ |
| 167 | 4, 0x80, 0x8F |
| 168 | }; |
| 169 | |
| 170 | /* a table used for quick lookup of the definition that goes with a |
| 171 | * particular lead byte */ |
| 172 | static const unsigned char* const lb_tab[] = { |
| @@ -202,24 +202,34 @@ | |
| 202 | ++z; |
| 203 | --n; |
| 204 | }else{ |
| 205 | /* get the definition for this lead byte */ |
| 206 | const unsigned char* def = lb_tab[(*z++)-0x80]; |
| 207 | unsigned char len; |
| 208 | |
| 209 | /* if the definition doesn't exist, return invalid */ |
| 210 | if( !def ) return LOOK_INVALID; |
| 211 | /* get the expected sequence length */ |
| 212 | len = *def++; |
| 213 | /* if there aren't enough bytes left, return invalid */ |
| 214 | if( n<len ) { |
| 215 | return LOOK_INVALID; |
| 216 | } |
| 217 | /* we already know byte #0 is good, so check the remaining bytes */ |
| 218 | if( (*z<*def++) || (*z++>*def++) ){ |
| 219 | /* if the byte is outside the allowed range for this definition, |
| 220 | * return invalid */ |
| 221 | return LOOK_INVALID; |
| 222 | } |
| 223 | if( len > 2 ){ |
| 224 | if( (*z<0x80) || (*z++>0xBF) ){ |
| 225 | return LOOK_INVALID; |
| 226 | } |
| 227 | if( len > 3 ){ |
| 228 | if( (*z<0x80) || (*z++>0xBF) ){ |
| 229 | return LOOK_INVALID; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | /* advance to the next sequence */ |
| 234 | n -= len; |
| 235 | } |
| 236 |