Fossil SCM

Improved rendering of ascii-art hexadecimal for the captcha.

drh 2013-08-23 20:58 trunk
Commit d3940100474699517a0051485e6b97ca024f92d9
1 file changed +56 -6
+56 -6
--- src/captcha.c
+++ src/captcha.c
@@ -69,16 +69,16 @@
6969
** Render an 8-character hexadecimal string as ascii art.
7070
** Space to hold the result is obtained from malloc() and should be freed
7171
** by the caller.
7272
*/
7373
char *captcha_render(const char *zPw){
74
- char *z = fossil_malloc( 500 );
74
+ char *z = fossil_malloc( 9*6*strlen(zPw) + 7 );
7575
int i, j, k, m;
7676
7777
k = 0;
7878
for(i=0; i<6; i++){
79
- for(j=0; j<8; j++){
79
+ for(j=0; zPw[j]; j++){
8080
unsigned char v = hexValue(zPw[j]);
8181
v = (aFont1[v] >> ((5-i)*4)) & 0xf;
8282
for(m=8; m>=1; m = m>>1){
8383
if( v & m ){
8484
z[k++] = 'X';
@@ -202,17 +202,17 @@
202202
** Render an 8-digit hexadecimal string as ascii arg.
203203
** Space to hold the result is obtained from malloc() and should be freed
204204
** by the caller.
205205
*/
206206
char *captcha_render(const char *zPw){
207
- char *z = fossil_malloc( 300 );
207
+ char *z = fossil_malloc( 7*4*strlen(zPw) + 5 );
208208
int i, j, k, m;
209209
const char *zChar;
210210
211211
k = 0;
212212
for(i=0; i<4; i++){
213
- for(j=0; j<8; j++){
213
+ for(j=0; zPw[j]; j++){
214214
unsigned char v = hexValue(zPw[j]);
215215
zChar = azFont2[4*v + i];
216216
for(m=0; zChar[m]; m++){
217217
z[k++] = zChar[m];
218218
}
@@ -359,19 +359,52 @@
359359
** Render an 8-digit hexadecimal string as ascii arg.
360360
** Space to hold the result is obtained from malloc() and should be freed
361361
** by the caller.
362362
*/
363363
char *captcha_render(const char *zPw){
364
- char *z = fossil_malloc( 600 );
364
+ char *z = fossil_malloc( 10*6*strlen(zPw) + 7 );
365365
int i, j, k, m;
366366
const char *zChar;
367
+ unsigned char x;
368
+ int y;
367369
368370
k = 0;
369371
for(i=0; i<6; i++){
370
- for(j=0; j<8; j++){
372
+ x = 0;
373
+ for(j=0; zPw[j]; j++){
371374
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
+ }
372403
zChar = azFont3[6*v + i];
404
+ while( y && zChar[0]==' ' ){ y--; zChar++; }
405
+ while( y && z[k-1]==' ' ){ y--; k--; }
373406
for(m=0; zChar[m]; m++){
374407
z[k++] = zChar[m];
375408
}
376409
}
377410
z[k++] = '\n';
@@ -513,5 +546,22 @@
513546
@ Enter security code shown above:
514547
@ <input type="hidden" name="captchaseed" value="%u(uSeed)" />
515548
@ <input type="text" name="captcha" size=8 />
516549
@ </td></tr></table></div>
517550
}
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
+}
518568
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button