C to VB (for BASE 64 routines) 
Author Message
 C to VB (for BASE 64 routines)


Quote:

> Would some kind C guru be able to translate the following C code to VB
> for me? Basically, I need 2 routines (to64 and from64) that convert
> text to and from Base64 format.

> Thanks in advance
> Steve
> ----------------------------------- cut
> ------------------------------------
> /*
> Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)

> Permission to use, copy, modify, and distribute this material

> [... very long C program cut ...]

Not everybody agrees with even the snippets of VB code being posted in this group.

And you posted nearly 8K of C code, was it your very first visit to this forum, by any
chance?

Why on earth didn't you just post the question and ask people to email you to ask to be
sent the code?

----------------------------------------------

Axiomatic Software   CIS: 100273,154
"i forgive you molesworth for those uncouth words"



Thu, 21 Aug 1997 00:22:44 GMT  
 C to VB (for BASE 64 routines)
Would some kind C guru be able to translate the following C code to VB
for me? Basically, I need 2 routines (to64 and from64) that convert
text to and from Base64 format.

Thanks in advance
Steve
----------------------------------- cut
------------------------------------
/*
Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)

Permission to use, copy, modify, and distribute this material
for any purpose and without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies, and that the name of Bellcore not be
used in advertising or publicity pertaining to this
material without the specific, prior written permission
of an authorized representative of Bellcore.  BELLCORE
MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
*/
#include <stdio.h>
#include <ctype.h>

extern char *index();
static char basis_64[] =
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static char index_64[128] = {
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1

Quote:
};

#define char64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])

/* simple main routine to demonstrate the use of the subroutines */
main(int argc, char *argv[]){

        int i, encode=-1;

        for (i=1; i<argc; ++i) {
                if (argv[i][0] == '-') {
                        switch (argv[i][1]) {
                                case 'd':
                                        encode=0;
                                        break;
                                case 'e':
                                        encode=1;
                                        break;
                                default:
                                        fprintf(stderr,
                                                "Usage: base64 [-e]
[-d]\n");
                                        exit(1);
                        }
                }
        }
        if (encode<0) {
                fprintf(stderr, "Usage: base64 [-e] [-d]\n");
                exit(1);
        }
        if (encode)
                to64(stdin, stdout, 0);
        else
                from64(stdin, stdout, (char **) NULL, (int *) 0, 0);

Quote:
}

/*
char64(c)
char c;
{
    char *s = (char *) index(basis_64, c);
    if (s) return(s-basis_64);
    return(-1);
Quote:
}

*/

/* the following gets a character, but fakes it properly into two chars
if
there's a newline character */
static int InNewline=0;

int nextcharin(infile, PortableNewlines)
FILE *infile;
int PortableNewlines;
{
    int c;

#ifndef NEWLINE_CHAR
    return(getc(infile));
#else
    if (!PortableNewlines) return(getc(infile));
    if (InNewline) {
        InNewline = 0;
        return(10); /* LF */
    }
    c = getc(infile);
    if (c == NEWLINE_CHAR) {
        InNewline = 1;
        return(13); /* CR */
    }
    return(c);
#endif

Quote:
}

to64(infile, outfile, PortableNewlines)
FILE *infile, *outfile;
int PortableNewlines;
{
    int c1, c2, c3, ct=0;
    InNewline = 0; /* always reset it */
    while ((c1 = nextcharin(infile, PortableNewlines)) != EOF) {
        c2 = nextcharin(infile, PortableNewlines);
        if (c2 == EOF) {
            output64chunk(c1, 0, 0, 2, outfile);
        } else {
            c3 = nextcharin(infile, PortableNewlines);
            if (c3 == EOF) {
                output64chunk(c1, c2, 0, 1, outfile);
            } else {
                output64chunk(c1, c2, c3, 0, outfile);
            }
        }
        ct += 4;
        if (ct > 71) {
            putc('\n', outfile);
            ct = 0;
        }
    }
    if (ct) putc('\n', outfile);
    fflush(outfile);

Quote:
}

output64chunk(c1, c2, c3, pads, outfile)
FILE *outfile;
{
    putc(basis_64[c1>>2], outfile);
    putc(basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
    if (pads == 2) {
        putc('=', outfile);
        putc('=', outfile);
    } else if (pads) {
        putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
        putc('=', outfile);
    } else {
        putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
        putc(basis_64[c3 & 0x3F], outfile);
    }

Quote:
}

PendingBoundary(s, Boundaries, BoundaryCt)
char *s;
char **Boundaries;
int *BoundaryCt;
{
    int i, len;

    if (s[0] != '-' || s[1] != '-') return(0);

    for (i=0; i < *BoundaryCt; ++i) {
        len = strlen(Boundaries[i]);
        if (!strncmp(s, Boundaries[i], len)) {
            if (s[len] == '-' && s[len+1] == '-') *BoundaryCt = i;
            return(1);
        }
    }
    return(0);

Quote:
}

/* If we're in portable newline mode, we have to convert CRLF to the
    local newline convention on output */

static int CRpending = 0;

#ifdef NEWLINE_CHAR
almostputc(c, outfile, PortableNewlines)
int c;
FILE *outfile;
int PortableNewlines;
{
    if (CRpending) {
        if (c == 10) {
            putc(NEWLINE_CHAR, outfile);
            CRpending = 0;
        } else {
            putc(13, outfile);
            if (c != 13) {
                putc(c, outfile);
                CRpending = 0;
            }
        }
    } else {
        if (PortableNewlines && c == 13) {
            CRpending = 1;
        } else {
            putc(c, outfile);
        }
    }

Quote:
}

#else
almostputc(c, outfile, PortableNewlines)
int c;
FILE *outfile;
int PortableNewlines;
{
    putc(c, outfile);
Quote:
}

#endif

from64(infile, outfile, boundaries, boundaryct, PortableNewlines)
FILE *infile, *outfile;
char **boundaries;
int *boundaryct;
int PortableNewlines;
{
    int c1, c2, c3, c4;
    int newline = 1, DataDone = 0;

    /* always reinitialize */
    CRpending = 0;
    while ((c1 = getc(infile)) != EOF) {
        if (isspace(c1)) {
            if (c1 == '\n') {
                newline = 1;
            } else {
                newline = 0;
            }
            continue;
        }
        if (newline && boundaries && c1 == '-') {
            char Buf[200];
            /* a dash is NOT base 64, so all bets are off if NOT a
boundary
*/
            ungetc(c1, infile);
            fgets(Buf, sizeof(Buf), infile);
            if (boundaries
                 && (Buf[0] == '-')
                 && (Buf[1] == '-')
                 && PendingBoundary(Buf, boundaries, boundaryct)) {
                return;
            }
            fprintf(stderr, "Ignoring unrecognized boundary line: %s\n",
Buf);
            continue;
        }
        if (DataDone) continue;
        newline = 0;
        do {
            c2 = getc(infile);
        } while (c2 != EOF && isspace(c2));
        do {
            c3 = getc(infile);
        } while (c3 != EOF && isspace(c3));
        do {
            c4 = getc(infile);
        } while (c4 != EOF && isspace(c4));
        if (c2 == EOF || c3 == EOF || c4 == EOF) {
            fprintf(stderr, "Warning: base64 decoder saw premature
EOF!\n");
            return;
        }
        if (c1 == '=' || c2 == '=') {
            DataDone=1;
            continue;
        }
        c1 = char64(c1);
        c2 = char64(c2);
        almostputc(((c1<<2) | ((c2&0x30)>>4)), outfile,
PortableNewlines);
        if (c3 == '=') {
            DataDone = 1;
        } else {
            c3 = char64(c3);
            almostputc((((c2&0XF) << 4) | ((c3&0x3C) >> 2)), outfile,
PortableNewlines);
            if (c4 == '=') {
                DataDone = 1;
            } else {
                c4 = char64(c4);
                almostputc((((c3&0x03) <<6) | c4), outfile,
PortableNewlines);
            }
        }
    }
    if (CRpending) putc(13, outfile); /* Don't drop a lone trailing char
13
*/

Quote:
}

----------------------------------- cut
------------------------------------


Tue, 19 Aug 1997 16:48:08 GMT  
 C to VB (for BASE 64 routines)

Quote:


> > Would some kind C guru be able to translate the following C code to VB
> > for me? Basically, I need 2 routines (to64 and from64) that convert
> > text to and from Base64 format.

> > Thanks in advance
> > Steve
> > ----------------------------------- cut
> > ------------------------------------
> > /*
> > Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)

> > Permission to use, copy, modify, and distribute this material

> > [... very long C program cut ...]

> Not everybody agrees with even the snippets of VB code being posted in this grou
> p.

> And you posted nearly 8K of C code, was it your very first visit to this forum,
> by any
> chance?

> Why on earth didn't you just post the question and ask people to email you to as
> k to be
> sent the code?

> ----------------------------------------------

> Axiomatic Software   CIS: 100273,154
> "i forgive you molesworth for those uncouth words"

I looked at that code. If somebody out there has loads of time, doing the
conversion
*might* be a distraction. On the other hand, my experience with this kind of
thing is to stipulate the hourly rate up front before you start to work.

The simple mechanics of rewriting such code takes time. A VB program that uses
mostly all API function code is taking me hours to convert to "C".

I think the newbie thinks he has a unlimited resource pool. Guru or not ;-),
we are still working stiffs.

Regards,
Charly



Fri, 22 Aug 1997 11:53:21 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Needed BASE 64 code for VB 3.

2. (Base 64)Octet-stream attachment to hex

3. encryption base:64

4. Base 64 encoding

5. how to decode base 64 code in e-mail

6. how to decode base 64 code in e-mail

7. RFC/MIME and Base 64

8. RFC/MIME and Base 64

9. Crystal Report .Net Web Service ( Invalid length for a Base-64 string )

10. RFC/MIME and Base 64

11. 32/64-Bit Compiler beats VB and C!

12. VB 6 IDE on 64-bit Windows

 

 
Powered by phpBB® Forum Software