~vcs-imports/putty/trunk

« back to all changes in this revision

Viewing changes to misc.c

  • Committer: simon
  • Date: 2014-09-09 11:46:10 UTC
  • Revision ID: svn-v4:cda61777-01e9-0310-a592-d414129be87e:putty:10218
Move base64_decode_atom into misc.c.

I'm about to need to refer to it from a source file that won't
necessarily always be linked against sshpubk.c, so it needs to live
somewhere less specialist. Now it sits alongside base64_encode_atom
(already in misc.c for another reason), which is neater anyway.

Show diffs side-by-side

added added

removed removed

Lines of Context:
473
473
}
474
474
 
475
475
/* ----------------------------------------------------------------------
476
 
 * Base64 encoding routine. This is required in public-key writing
477
 
 * but also in HTTP proxy handling, so it's centralised here.
 
476
 * Core base64 encoding and decoding routines.
478
477
 */
479
478
 
480
479
void base64_encode_atom(unsigned char *data, int n, char *out)
501
500
        out[3] = '=';
502
501
}
503
502
 
 
503
int base64_decode_atom(char *atom, unsigned char *out)
 
504
{
 
505
    int vals[4];
 
506
    int i, v, len;
 
507
    unsigned word;
 
508
    char c;
 
509
 
 
510
    for (i = 0; i < 4; i++) {
 
511
        c = atom[i];
 
512
        if (c >= 'A' && c <= 'Z')
 
513
            v = c - 'A';
 
514
        else if (c >= 'a' && c <= 'z')
 
515
            v = c - 'a' + 26;
 
516
        else if (c >= '0' && c <= '9')
 
517
            v = c - '0' + 52;
 
518
        else if (c == '+')
 
519
            v = 62;
 
520
        else if (c == '/')
 
521
            v = 63;
 
522
        else if (c == '=')
 
523
            v = -1;
 
524
        else
 
525
            return 0;                  /* invalid atom */
 
526
        vals[i] = v;
 
527
    }
 
528
 
 
529
    if (vals[0] == -1 || vals[1] == -1)
 
530
        return 0;
 
531
    if (vals[2] == -1 && vals[3] != -1)
 
532
        return 0;
 
533
 
 
534
    if (vals[3] != -1)
 
535
        len = 3;
 
536
    else if (vals[2] != -1)
 
537
        len = 2;
 
538
    else
 
539
        len = 1;
 
540
 
 
541
    word = ((vals[0] << 18) |
 
542
            (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
 
543
    out[0] = (word >> 16) & 0xFF;
 
544
    if (len > 1)
 
545
        out[1] = (word >> 8) & 0xFF;
 
546
    if (len > 2)
 
547
        out[2] = word & 0xFF;
 
548
    return len;
 
549
}
 
550
 
504
551
/* ----------------------------------------------------------------------
505
552
 * Generic routines to deal with send buffers: a linked list of
506
553
 * smallish blocks, with the operations