Fossil SCM
Improved rendering of ascii-art hexadecimal for the captcha.
Commit
d3940100474699517a0051485e6b97ca024f92d9
Parent
73fdb8a73d784f5…
1 file changed
+56
-6
+56
-6
| --- src/captcha.c | ||
| +++ src/captcha.c | ||
| @@ -69,16 +69,16 @@ | ||
| 69 | 69 | ** Render an 8-character hexadecimal string as ascii art. |
| 70 | 70 | ** Space to hold the result is obtained from malloc() and should be freed |
| 71 | 71 | ** by the caller. |
| 72 | 72 | */ |
| 73 | 73 | char *captcha_render(const char *zPw){ |
| 74 | - char *z = fossil_malloc( 500 ); | |
| 74 | + char *z = fossil_malloc( 9*6*strlen(zPw) + 7 ); | |
| 75 | 75 | int i, j, k, m; |
| 76 | 76 | |
| 77 | 77 | k = 0; |
| 78 | 78 | for(i=0; i<6; i++){ |
| 79 | - for(j=0; j<8; j++){ | |
| 79 | + for(j=0; zPw[j]; j++){ | |
| 80 | 80 | unsigned char v = hexValue(zPw[j]); |
| 81 | 81 | v = (aFont1[v] >> ((5-i)*4)) & 0xf; |
| 82 | 82 | for(m=8; m>=1; m = m>>1){ |
| 83 | 83 | if( v & m ){ |
| 84 | 84 | z[k++] = 'X'; |
| @@ -202,17 +202,17 @@ | ||
| 202 | 202 | ** Render an 8-digit hexadecimal string as ascii arg. |
| 203 | 203 | ** Space to hold the result is obtained from malloc() and should be freed |
| 204 | 204 | ** by the caller. |
| 205 | 205 | */ |
| 206 | 206 | char *captcha_render(const char *zPw){ |
| 207 | - char *z = fossil_malloc( 300 ); | |
| 207 | + char *z = fossil_malloc( 7*4*strlen(zPw) + 5 ); | |
| 208 | 208 | int i, j, k, m; |
| 209 | 209 | const char *zChar; |
| 210 | 210 | |
| 211 | 211 | k = 0; |
| 212 | 212 | for(i=0; i<4; i++){ |
| 213 | - for(j=0; j<8; j++){ | |
| 213 | + for(j=0; zPw[j]; j++){ | |
| 214 | 214 | unsigned char v = hexValue(zPw[j]); |
| 215 | 215 | zChar = azFont2[4*v + i]; |
| 216 | 216 | for(m=0; zChar[m]; m++){ |
| 217 | 217 | z[k++] = zChar[m]; |
| 218 | 218 | } |
| @@ -359,19 +359,52 @@ | ||
| 359 | 359 | ** Render an 8-digit hexadecimal string as ascii arg. |
| 360 | 360 | ** Space to hold the result is obtained from malloc() and should be freed |
| 361 | 361 | ** by the caller. |
| 362 | 362 | */ |
| 363 | 363 | char *captcha_render(const char *zPw){ |
| 364 | - char *z = fossil_malloc( 600 ); | |
| 364 | + char *z = fossil_malloc( 10*6*strlen(zPw) + 7 ); | |
| 365 | 365 | int i, j, k, m; |
| 366 | 366 | const char *zChar; |
| 367 | + unsigned char x; | |
| 368 | + int y; | |
| 367 | 369 | |
| 368 | 370 | k = 0; |
| 369 | 371 | for(i=0; i<6; i++){ |
| 370 | - for(j=0; j<8; j++){ | |
| 372 | + x = 0; | |
| 373 | + for(j=0; zPw[j]; j++){ | |
| 371 | 374 | unsigned char v = hexValue(zPw[j]); |
| 375 | + x = (x<<4) + v; | |
| 376 | + switch( x ){ | |
| 377 | + case 0x7a: | |
| 378 | + case 0xfa: | |
| 379 | + y = 3; | |
| 380 | + break; | |
| 381 | + case 0x47: | |
| 382 | + y = 2; | |
| 383 | + break; | |
| 384 | + case 0xf6: | |
| 385 | + case 0xa9: | |
| 386 | + case 0xa4: | |
| 387 | + case 0xa1: | |
| 388 | + case 0x9a: | |
| 389 | + case 0x76: | |
| 390 | + case 0x61: | |
| 391 | + case 0x67: | |
| 392 | + case 0x69: | |
| 393 | + case 0x41: | |
| 394 | + case 0x42: | |
| 395 | + case 0x43: | |
| 396 | + case 0x4a: | |
| 397 | + y = 1; | |
| 398 | + break; | |
| 399 | + default: | |
| 400 | + y = 0; | |
| 401 | + break; | |
| 402 | + } | |
| 372 | 403 | zChar = azFont3[6*v + i]; |
| 404 | + while( y && zChar[0]==' ' ){ y--; zChar++; } | |
| 405 | + while( y && z[k-1]==' ' ){ y--; k--; } | |
| 373 | 406 | for(m=0; zChar[m]; m++){ |
| 374 | 407 | z[k++] = zChar[m]; |
| 375 | 408 | } |
| 376 | 409 | } |
| 377 | 410 | z[k++] = '\n'; |
| @@ -513,5 +546,22 @@ | ||
| 513 | 546 | @ Enter security code shown above: |
| 514 | 547 | @ <input type="hidden" name="captchaseed" value="%u(uSeed)" /> |
| 515 | 548 | @ <input type="text" name="captcha" size=8 /> |
| 516 | 549 | @ </td></tr></table></div> |
| 517 | 550 | } |
| 551 | + | |
| 552 | +/* | |
| 553 | +** WEBPAGE: test-captcha | |
| 554 | +*/ | |
| 555 | +void captcha_test(void){ | |
| 556 | + const char *zPw = P("name"); | |
| 557 | + if( zPw==0 || zPw[0]==0 ){ | |
| 558 | + u64 x; | |
| 559 | + sqlite3_randomness(sizeof(x), &x); | |
| 560 | + zPw = mprintf("%016llx", x); | |
| 561 | + } | |
| 562 | + style_header("Captcha Test"); | |
| 563 | + @ <pre> | |
| 564 | + @ %s(captcha_render(zPw)) | |
| 565 | + @ </pre> | |
| 566 | + style_footer(); | |
| 567 | +} | |
| 518 | 568 |
| --- src/captcha.c | |
| +++ src/captcha.c | |
| @@ -69,16 +69,16 @@ | |
| 69 | ** Render an 8-character hexadecimal string as ascii art. |
| 70 | ** Space to hold the result is obtained from malloc() and should be freed |
| 71 | ** by the caller. |
| 72 | */ |
| 73 | char *captcha_render(const char *zPw){ |
| 74 | char *z = fossil_malloc( 500 ); |
| 75 | int i, j, k, m; |
| 76 | |
| 77 | k = 0; |
| 78 | for(i=0; i<6; i++){ |
| 79 | for(j=0; j<8; j++){ |
| 80 | unsigned char v = hexValue(zPw[j]); |
| 81 | v = (aFont1[v] >> ((5-i)*4)) & 0xf; |
| 82 | for(m=8; m>=1; m = m>>1){ |
| 83 | if( v & m ){ |
| 84 | z[k++] = 'X'; |
| @@ -202,17 +202,17 @@ | |
| 202 | ** Render an 8-digit hexadecimal string as ascii arg. |
| 203 | ** Space to hold the result is obtained from malloc() and should be freed |
| 204 | ** by the caller. |
| 205 | */ |
| 206 | char *captcha_render(const char *zPw){ |
| 207 | char *z = fossil_malloc( 300 ); |
| 208 | int i, j, k, m; |
| 209 | const char *zChar; |
| 210 | |
| 211 | k = 0; |
| 212 | for(i=0; i<4; i++){ |
| 213 | for(j=0; j<8; j++){ |
| 214 | unsigned char v = hexValue(zPw[j]); |
| 215 | zChar = azFont2[4*v + i]; |
| 216 | for(m=0; zChar[m]; m++){ |
| 217 | z[k++] = zChar[m]; |
| 218 | } |
| @@ -359,19 +359,52 @@ | |
| 359 | ** Render an 8-digit hexadecimal string as ascii arg. |
| 360 | ** Space to hold the result is obtained from malloc() and should be freed |
| 361 | ** by the caller. |
| 362 | */ |
| 363 | char *captcha_render(const char *zPw){ |
| 364 | char *z = fossil_malloc( 600 ); |
| 365 | int i, j, k, m; |
| 366 | const char *zChar; |
| 367 | |
| 368 | k = 0; |
| 369 | for(i=0; i<6; i++){ |
| 370 | for(j=0; j<8; j++){ |
| 371 | unsigned char v = hexValue(zPw[j]); |
| 372 | zChar = azFont3[6*v + i]; |
| 373 | for(m=0; zChar[m]; m++){ |
| 374 | z[k++] = zChar[m]; |
| 375 | } |
| 376 | } |
| 377 | z[k++] = '\n'; |
| @@ -513,5 +546,22 @@ | |
| 513 | @ Enter security code shown above: |
| 514 | @ <input type="hidden" name="captchaseed" value="%u(uSeed)" /> |
| 515 | @ <input type="text" name="captcha" size=8 /> |
| 516 | @ </td></tr></table></div> |
| 517 | } |
| 518 |
| --- src/captcha.c | |
| +++ src/captcha.c | |
| @@ -69,16 +69,16 @@ | |
| 69 | ** Render an 8-character hexadecimal string as ascii art. |
| 70 | ** Space to hold the result is obtained from malloc() and should be freed |
| 71 | ** by the caller. |
| 72 | */ |
| 73 | char *captcha_render(const char *zPw){ |
| 74 | char *z = fossil_malloc( 9*6*strlen(zPw) + 7 ); |
| 75 | int i, j, k, m; |
| 76 | |
| 77 | k = 0; |
| 78 | for(i=0; i<6; i++){ |
| 79 | for(j=0; zPw[j]; j++){ |
| 80 | unsigned char v = hexValue(zPw[j]); |
| 81 | v = (aFont1[v] >> ((5-i)*4)) & 0xf; |
| 82 | for(m=8; m>=1; m = m>>1){ |
| 83 | if( v & m ){ |
| 84 | z[k++] = 'X'; |
| @@ -202,17 +202,17 @@ | |
| 202 | ** Render an 8-digit hexadecimal string as ascii arg. |
| 203 | ** Space to hold the result is obtained from malloc() and should be freed |
| 204 | ** by the caller. |
| 205 | */ |
| 206 | char *captcha_render(const char *zPw){ |
| 207 | char *z = fossil_malloc( 7*4*strlen(zPw) + 5 ); |
| 208 | int i, j, k, m; |
| 209 | const char *zChar; |
| 210 | |
| 211 | k = 0; |
| 212 | for(i=0; i<4; i++){ |
| 213 | for(j=0; zPw[j]; j++){ |
| 214 | unsigned char v = hexValue(zPw[j]); |
| 215 | zChar = azFont2[4*v + i]; |
| 216 | for(m=0; zChar[m]; m++){ |
| 217 | z[k++] = zChar[m]; |
| 218 | } |
| @@ -359,19 +359,52 @@ | |
| 359 | ** Render an 8-digit hexadecimal string as ascii arg. |
| 360 | ** Space to hold the result is obtained from malloc() and should be freed |
| 361 | ** by the caller. |
| 362 | */ |
| 363 | char *captcha_render(const char *zPw){ |
| 364 | char *z = fossil_malloc( 10*6*strlen(zPw) + 7 ); |
| 365 | int i, j, k, m; |
| 366 | const char *zChar; |
| 367 | unsigned char x; |
| 368 | int y; |
| 369 | |
| 370 | k = 0; |
| 371 | for(i=0; i<6; i++){ |
| 372 | x = 0; |
| 373 | for(j=0; zPw[j]; j++){ |
| 374 | unsigned char v = hexValue(zPw[j]); |
| 375 | x = (x<<4) + v; |
| 376 | switch( x ){ |
| 377 | case 0x7a: |
| 378 | case 0xfa: |
| 379 | y = 3; |
| 380 | break; |
| 381 | case 0x47: |
| 382 | y = 2; |
| 383 | break; |
| 384 | case 0xf6: |
| 385 | case 0xa9: |
| 386 | case 0xa4: |
| 387 | case 0xa1: |
| 388 | case 0x9a: |
| 389 | case 0x76: |
| 390 | case 0x61: |
| 391 | case 0x67: |
| 392 | case 0x69: |
| 393 | case 0x41: |
| 394 | case 0x42: |
| 395 | case 0x43: |
| 396 | case 0x4a: |
| 397 | y = 1; |
| 398 | break; |
| 399 | default: |
| 400 | y = 0; |
| 401 | break; |
| 402 | } |
| 403 | zChar = azFont3[6*v + i]; |
| 404 | while( y && zChar[0]==' ' ){ y--; zChar++; } |
| 405 | while( y && z[k-1]==' ' ){ y--; k--; } |
| 406 | for(m=0; zChar[m]; m++){ |
| 407 | z[k++] = zChar[m]; |
| 408 | } |
| 409 | } |
| 410 | z[k++] = '\n'; |
| @@ -513,5 +546,22 @@ | |
| 546 | @ Enter security code shown above: |
| 547 | @ <input type="hidden" name="captchaseed" value="%u(uSeed)" /> |
| 548 | @ <input type="text" name="captcha" size=8 /> |
| 549 | @ </td></tr></table></div> |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | ** WEBPAGE: test-captcha |
| 554 | */ |
| 555 | void captcha_test(void){ |
| 556 | const char *zPw = P("name"); |
| 557 | if( zPw==0 || zPw[0]==0 ){ |
| 558 | u64 x; |
| 559 | sqlite3_randomness(sizeof(x), &x); |
| 560 | zPw = mprintf("%016llx", x); |
| 561 | } |
| 562 | style_header("Captcha Test"); |
| 563 | @ <pre> |
| 564 | @ %s(captcha_render(zPw)) |
| 565 | @ </pre> |
| 566 | style_footer(); |
| 567 | } |
| 568 |