diff options
Diffstat (limited to 'base64.c')
-rw-r--r-- | base64.c | 105 |
1 files changed, 6 insertions, 99 deletions
diff --git a/base64.c b/base64.c index 8a57246..253bf4e 100644 --- a/base64.c +++ b/base64.c @@ -7,9 +7,6 @@ Thank you for inspiration: http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode */ -#include <u.h> -#include <libc.h> -#include <stdio.h> #include "base64.h" //Base64 char table - used internally for encoding @@ -55,13 +52,14 @@ unsigned int b64d_size(unsigned int in_size) { return ((3*in_size)/4); } -unsigned int b64_encode(const unsigned char* in, unsigned int in_len, unsigned char* out) { +unsigned int +b64_encode(char* in, unsigned int in_len, char* out){ unsigned int i=0, j=0, k=0, s[3]; - for (i=0;i<in_len;i++) { + for (i=0;i<in_len;i++){ s[j++]=*(in+i); - if (j==3) { + if (j==3){ out[k+0] = b64_chr[ (s[0]&255)>>2 ]; out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ]; out[k+2] = b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ]; @@ -88,7 +86,8 @@ unsigned int b64_encode(const unsigned char* in, unsigned int in_len, unsigned c return k; } -unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned char* out) { +unsigned int +b64_decode(char* in, unsigned int in_len, char* out){ unsigned int i=0, j=0, k=0, s[4]; @@ -112,95 +111,3 @@ unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned c return k; } - -unsigned int b64_encodef(char *InFile, char *OutFile) { - - FILE *pInFile = fopen(InFile,"rb"); - FILE *pOutFile = fopen(OutFile,"wb"); - - unsigned int i=0; - unsigned int j=0; - unsigned int c=0; - unsigned int s[4]; - - if ((pInFile==NULL) || (pOutFile==NULL) ) { - if (pInFile!=NULL){fclose(pInFile);} - if (pOutFile!=NULL){fclose(pOutFile);} - return 0; - } - - while(c!=EOF) { - c=fgetc(pInFile); - if (c==EOF) - break; - s[j++]=c; - if (j==3) { - fputc(b64_chr[ (s[0]&255)>>2 ],pOutFile); - fputc(b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ],pOutFile); - fputc(b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ],pOutFile); - fputc(b64_chr[ s[2]&0x3F ],pOutFile); - j=0; i+=4; - } - } - - if (j) { - if (j==1) - s[1] = 0; - fputc(b64_chr[ (s[0]&255)>>2 ],pOutFile); - fputc(b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ],pOutFile); - if (j==2) - fputc(b64_chr[ ((s[1]&0x0F)<<2) ],pOutFile); - else - fputc('=',pOutFile); - fputc('=',pOutFile); - i+=4; - } - - fclose(pInFile); - fclose(pOutFile); - - return i; -} - -unsigned int b64_decodef(char *InFile, char *OutFile) { - - FILE *pInFile = fopen(InFile,"rb"); - FILE *pOutFile = fopen(OutFile,"wb"); - - unsigned int c=0; - unsigned int j=0; - unsigned int k=0; - unsigned int s[4]; - - if ((pInFile==NULL) || (pOutFile==NULL) ) { - if (pInFile!=NULL){fclose(pInFile);} - if (pOutFile!=NULL){fclose(pOutFile);} - return 0; - } - - while(c!=EOF) { - c=fgetc(pInFile); - if (c==EOF) - break; - s[j++]=b64_int(c); - if (j==4) { - fputc(((s[0]&255)<<2)+((s[1]&0x30)>>4),pOutFile); - if (s[2]!=64) { - fputc(((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2),pOutFile); - if ((s[3]!=64)) { - fputc(((s[2]&0x03)<<6)+(s[3]),pOutFile); k+=3; - } else { - k+=2; - } - } else { - k+=1; - } - j=0; - } - } - - fclose(pInFile); - fclose(pOutFile); - - return k; -} |