大家好,我写了个程序,作用是把一些文件进行base64编码并跟相关的字符封装起来返回。
主程序如下。但老是运行的时候出现段错误,我知道肯定是操作内存出了问题。可总不知道在什么地方,翻阅了一些基本知识,malloc后一定要进行free。
我想请教下面的问题:(不谈代码写的如何)
1、请各位指点下我这里内存操作是否正确,并指出原因;
2、(b_str=ss();的时候,看起来没有问题,事实上我也不清楚)我把b_str = ss(); 换成 b_str = base64_encode(str,len,&b_len); 运行就出现 段错误,
我发现在换成 b_str = base64_encode(str,len,&b_len); 的时候,去掉free(str);便可以执行,不出现段错误,但结果似乎有点问题,即 puts(b_str);b_str输出后面似乎多了点字符。肯定是操作内存什么地方出错了
恳请各位帮忙!
附件是我的全部程序,解决了再送8分
int main(){
char *p[];
p[0] = "/www/b.c";
p[1] = "/www/c.c";
p[2] = "/www/encode.h";
attachment(p);
}
static char* attachment(char**f){
char **p,*str,filename[100],*ret;
long i = 1024,len=0;
int b_len;
char str1[SHORT_SIZE] = "Content-Type: application/octet-stream; name=\"";
char str2[SHORT_SIZE] = "\"\nContent-Transfer-Encoding: BASE64\nContent-Disposition: attachment; filename=\"";
FILE *fp;
if(f==NULL) return NULL;
p = f;
ret = (char*)malloc(1);
while(*p!=NULL){
unsigned char *b_str;
int j=0,k=0;
str = (char*)malloc(sizeof(char)*i+1);
bzero(str,i+1);
fp = fopen(*p,"rb");
if(!fp){
printf("can not open file %s\n",*p);
exit(0);
}
len += fread(str,sizeof(char),i,fp);
while(!feof(fp)){ // 没有读完
j++;
str = (char*)realloc(str,i*j+1);
len+=fread(str+i*j,sizeof(char),i,fp);
}
*(str+i*j) = '\0';
bzero(filename,100);
if(strrchr(*p,'/')!=0)
strcpy(filename,strrchr(*p,'/')+1);
else
strcpy(filename,*p);
// b_str = base64_encode(str,len,&b_len);
b_str = ss();// 这行换成上面一句就出现 段错误
k = strlen(ret)+b_len+strlen(filename)*2+5+strlen(str1)+strlen(str2);
ret = (char*)realloc(ret,k+1);
puts(b_str); // 问题2说的这里
strcat(ret,str1);strcat(ret,filename);
strcat(ret,str2);strcat(ret,filename);
strcat(ret,"\"\n\n");strcat(ret,b_str);
strcat(ret,"\n\n");*(ret+k) = '\0';
free(b_str);
free(str);
len = 0;
p++;
}
return ret;
}
char * ss(){
char *p;
p = (char*)malloc(101);
bzero(p,101);
strcpy(p,"okbt");
return p;
}
|
下面是base64_encode.c ,我从php原码里弄出来的。
#include "base64_encode.h"
static const char base64_table[] =
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
};
static const char base64_pad = '=';
static const short base64_reverse_table[256] = {
-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,
-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, -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, -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
};
unsigned char *base64_encode(const unsigned char *str, int length, int *ret_length)
{
const unsigned char *current = str;
unsigned char *p;
unsigned char *result;
if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) {
if (ret_length != NULL) {
*ret_length = 0;
}
return NULL;
}
result = (unsigned char *)malloc(((length + 2) / 3) * 4);
p = result;
while (length > 2) { /* keep going until we have less than 24 bits */
*p++ = base64_table[current[0] >> 2];
*p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
*p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
*p++ = base64_table[current[2] & 0x3f];
current += 3;
length -= 3; /* we just handle 3 octets of data */
}
/* now deal with the tail end of things */
if (length != 0) {
*p++ = base64_table[current[0] >> 2];
if (length > 1) {
*p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
*p++ = base64_table[(current[1] & 0x0f) << 2];
*p++ = base64_pad;
} else {
*p++ = base64_table[(current[0] & 0x03) << 4];
*p++ = base64_pad;
*p++ = base64_pad;
}
}
if (ret_length != NULL) {
*ret_length = (int)(p - result);
}
*p = '\0';
return result;
}
[ 本帖最后由 sunceenjoy 于 2008-1-2 19:07 编辑 ] |