65,187
社区成员




#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BASE64_VALUE_SZ 256
int base64_value[BASE64_VALUE_SZ];
const unsigned char alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
class Base64Utility {
public:
Base64Utility();
int base64_encode(char *src, int srclen, char *dst, int tail);
int base64_decode(char *src, int srclen, char *dst);
private:
void base_64_init(void);
};
Base64Utility::Base64Utility() {
base_64_init();
}
void Base64Utility::base_64_init(void) {
int i;
for (i = 0; i < BASE64_VALUE_SZ; i++) base64_value[i] = -1;
for (i = 0; i < 64; i++) base64_value[(int) alphabet[i]] = i;
base64_value['='] = 0;
}
int Base64Utility::base64_encode(char *src, int srclen, char *dst, int tail) {
int bits, char_count, len;
char *o_char, *lim, *o_lim;
unsigned char c;
if ( !src || !dst) return 0;
len = srclen;
lim = src + len;
o_char = dst;
o_lim = dst + (len*4)/3 + 1;
char_count = 0;
bits = 0;
while ( (src < lim) && (o_char < o_lim)) {
c = *(src++);
bits += c;
char_count++;
if (char_count == 3) {
*(o_char++) = alphabet[bits >> 18];
*(o_char++) = alphabet[(bits >> 12) & 0x3f];
*(o_char++) = alphabet[(bits >> 6) & 0x3f];
*(o_char++) = alphabet[bits & 0x3f];
bits = 0;
char_count = 0;
} else {
bits <<= 8;
}
}
if (char_count != 0) {
bits <<= 16 - (8 * char_count);
*(o_char++) = alphabet[bits >> 18];
*(o_char++) = alphabet[(bits >> 12) & 0x3f];
if (char_count == 1) {
if (tail) {
*(o_char++) = '=';
*(o_char++) = '=';
}
} else {
*(o_char++) = alphabet[(bits >> 6) & 0x3f];
if (tail) {
*(o_char++) = '=';
}
}
}
*(o_char) = 0;
return strlen(dst);
}
int Base64Utility::base64_decode(char *src, int srclen, char *dst) {
int j;
unsigned int k;
int c, base_result_sz;
long val;
if (!src || !dst) return 0;
base_result_sz = srclen;
val = c = 0;
for (j = 0; *src; src++) {
k = (int) *src % BASE64_VALUE_SZ;
if (base64_value[k] < 0) continue;
val <<= 6;
val += base64_value[k];
if (++c < 4) continue;
dst[j++] = (char) (val >> 16);
dst[j++] = (val >> 8) & 0xff;
dst[j++] = val & 0xff;
val = c = 0;
}
switch (c) {
case 2://xxxxxx xx0000
dst[j++] = (val >> 4) & 0xff;
break;
case 3://XXXXXX XXxxxx xxxx00
dst[j++] = (char) (val >> 10);
dst[j++] = (val >> 2) & 0xff;
break;
}
return j;
}
Base64Utility b64u;
#define MAXLENS 1024768
#define MAXLEND 1366360
char bufd[MAXLEND];
char bufs[MAXLENS];
FILE *fs,*fd;
int fsize;
int main(int argc,char *argv[]) {
if (argc<4) {
USE:
printf("%s <-e|-E|-d> srcfile desfile\n",argv[0]);
return 1;
}
if (stricmp(argv[1],"-e") && stricmp(argv[1],"-d")) goto USE;
if (0==stricmp(argv[1],"-e")) {
fs=fopen(argv[2],"rb");
if (NULL==fs) {
printf("Can not open file %s!\n",argv[2]);
return 2;
}
fsize=fread(bufs,1,MAXLENS,fs);
if (fsize<=0) {
fclose(fs);
printf("Can not read file %s!\n",argv[2]);
return 3;
}
if (MAXLENS==fsize) printf("Warning: Up to %d bytes.\n",MAXLENS);
fclose(fs);
b64u.base64_encode(bufs,fsize,bufd,('E'==argv[2][1]));
fd=fopen(argv[3],"w");
if (NULL==fd) {
printf("Can not create file %s!\n",argv[3]);
return 4;
}
fprintf(fd,"%s",bufd);
fclose(fd);
} else {//0==stricmp(argv[1],"-d")
fd=fopen(argv[2],"rb");
if (NULL==fd) {
printf("Can not open file %s!\n",argv[2]);
return 2;
}
fsize=fread(bufd,1,MAXLEND,fd);
if (fsize<=0) {
fclose(fd);
printf("Can not read file %s!\n",argv[2]);
return 3;
}
if (MAXLEND==fsize) printf("Warning: Up to %d bytes.\n",MAXLEND);
fclose(fd);
fsize=b64u.base64_decode(bufd,fsize,bufs);
fs=fopen(argv[3],"wb");
if (NULL==fs) {
printf("Can not create file %s!\n",argv[3]);
return 4;
}
if (fsize!=(int)fwrite(bufs,1,fsize,fs)) {
printf("Write %s error!\n",argv[3]);
fclose(fs);
return 5;
}
fclose(fs);
}
return 0;
}
//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
struct FB {
char fn[256];
size_t fl;
char *b;
struct FB *next;
struct FB *prev;
} *fh,*fb,*ft;
char ln[256];
char fpn[256];
FILE *af;
FILE *f;
int L,n;
int main() {
system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");
af=fopen("c:\\allfn.txt","r");
if (NULL==af) {
printf("Can not open file c:\\allfn.txt!\n");
return 1;
}
fh=NULL;
fb=NULL;
n=0;
while (1) {
if (NULL==fgets(ln,256,af)) break;
L=strlen(ln);
if ('\n'==ln[L-1]) ln[L-1]=0;
printf("read %s\n",ln);
strcpy(fpn,"c:\\tmp\\");
strcat(fpn,ln);
ft=(struct FB *)malloc(sizeof(struct FB));
if (NULL==ft) {
printf("Can not malloc ft!\n");
fclose(af);
return 2;//之前的malloc在main退出后由操作系统自动free
}
printf("ft[%d]==%p\n",n,ft);
strcpy(ft->fn,fpn);
f=fopen(fpn,"rb");
if (NULL==f) {
printf("Can not open file %s!\n",fpn);
fclose(af);
return 3;//之前的malloc在main退出后由操作系统自动free
}
ft->fl=_filelength(fileno(f));
ft->b=malloc(ft->fl);
if (NULL==ft->b) {
printf("Can not malloc ft->b!\n");
fclose(f);
fclose(af);
return 4;//之前的malloc在main退出后由操作系统自动free
}
printf("ft[%d]->b==%p\n",n,ft->b);
if (ft->fl!=fread(ft->b,1,ft->fl,f)) {
printf("fread error!\n");
fclose(f);
fclose(af);
return 5;//之前的malloc在main退出后由操作系统自动free
}
fclose(f);
ft->next=NULL;
if (NULL==fh) {
ft->prev=NULL;
fh=ft;
} else {
fb->next=ft;
ft->prev=fb;
}
fb=ft;
n++;
}
fclose(af);
printf("-----list-----\n");
for (ft=fh;NULL!=ft;ft=ft->next) {
printf("%8d %s\n",ft->fl,ft->fn);
if (NULL!=ft) fb=ft;
}
printf("-----free-----\n");
n--;
if (NULL!=fh) {
for (ft=fb->prev;NULL!=ft;ft=ft->prev) {
if (NULL!=ft->next->b) {
printf("ft[%d]->b==%p\n",n,ft->next->b);
free(ft->next->b);
}
if (NULL!=ft->next) {
printf("ft[%d]==%p\n",n,ft->next);
free(ft->next);
}
n--;
}
if (NULL!=fh->b) {
printf("ft[0]->b==%p\n",fh->b);
free(fh->b);
}
printf("ft[0]==%p\n",fh);
free(fh);
}
return 0;
}
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//找不到文件
//
//C:\tmp\tmp\Debug>tmp
//找不到文件
//-----list-----
//-----free-----
//
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//2011-06-30 18:04 44,840 my_c.rar
//2011-06-30 17:18 1,036 err.frm
//2011-06-30 14:32 14,243 出租.txt
//2011-06-28 12:08 23,681 MSDN98书签.txt
// 4 个文件 83,800 字节
// 0 个目录 17,041,870,848 可用字节
//
//C:\tmp\tmp\Debug>tmp
//read my_c.rar
//ft[0]==00421800
//ft[0]->b==00520068
//read err.frm
//ft[1]==00421670
//ft[1]->b==0052AFC0
//read 出租.txt
//ft[2]==00421530
//ft[2]->b==00378F28
//read MSDN98书签.txt
//ft[3]==004213F0
//ft[3]->b==0052B3F8
//-----list-----
// 44840 c:\tmp\my_c.rar
// 1036 c:\tmp\err.frm
// 14243 c:\tmp\出租.txt
// 23681 c:\tmp\MSDN98书签.txt
//-----free-----
//ft[3]->b==0052B3F8
//ft[3]==004213F0
//ft[2]->b==00378F28
//ft[2]==00421530
//ft[1]->b==0052AFC0
//ft[1]==00421670
//ft[0]->b==00520068
//ft[0]==00421800
//
//C:\tmp\tmp\Debug>