问题描述
我有一个JPEG图像,它表示为base64编码的字符串。我想使用Win32 API WriteFile()
函数将其保存为解码的字节数组。
因为我将使用WriteFile()
,所以我需要一个C字符串,并且我需要知道它的长度,strlen()
不好,因为据我所知,它计入\0
不能是文件的确切结尾。因此,我需要一个解码base64并返回char*
并输出确切字节数的函数。
我已阅读this answer,并选择了代码from here(某些内容已更改,我将其标记了):
static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char * base64_decode(const unsigned char *src,size_t len,size_t *out_len)
{
unsigned char dtable[256],*out,*pos,block[4],tmp;
size_t i,count,olen;
int pad = 0;
memset(dtable,0x80,256); // CHANGED
for (i = 0; i < sizeof(base64_table) - 1; i++)
dtable[base64_table[i]] = (unsigned char) i;
dtable['='] = 0;
count = 0;
for (i = 0; i < len; i++) {
if (dtable[src[i]] != 0x80)
count++;
}
if (count == 0 || count % 4)
return NULL;
olen = count / 4 * 3;
pos = out = new unsigned char[olen]; // CHANGED
if (out == NULL)
return NULL;
count = 0;
for (i = 0; i < len; i++) {
tmp = dtable[src[i]];
if (tmp == 0x80)
continue;
if (src[i] == '=')
pad++;
block[count] = tmp;
count++;
if (count == 4) {
*pos++ = (block[0] << 2) | (block[1] >> 4);
*pos++ = (block[1] << 4) | (block[2] >> 2);
*pos++ = (block[2] << 6) | block[3];
count = 0;
if (pad) {
if (pad == 1)
pos--;
else if (pad == 2)
pos -= 2;
else {
/* Invalid padding */
free(out); // CHANGED
return NULL;
}
break;
}
}
}
*out_len = pos - out;
return out;
}
unsigned char base[]="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc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VHkV9QhnqwobIzbpWnopueQS8rjI4lpy38UIUZYkJVGHRVH74lx5lJs4ep2ZsksoDjctBsEIWr8OZ9pOHqakqv0mG3aC+UuaCW33sd9U3LSTEXY8k/ByELGjbJeFVFWcXmY6GEtLhmPbHO0DS+W3w95WkuhCw2XpymOi0fBx/ZKjq+gQhMxbicuhoEIQqyU//2Q==";
unsigned char *g = base64_decode(base,2568,&re); // length is appearing when you hover mouse on char[] in Visual Studio
// after call re equals 1921
HANDLE f2 = CreateFile(L"img.jpeg",GENERIC_WRITE,CREATE_ALWAYS,FILE_ATTRIBUTE_norMAL,NULL);
DWORD wr2;
WriteFile(f2,g,re,&wr2,0);
CloseHandle(f2);
我正在获取的文件不可见,“照片”应用程序显示文件已损坏。主要问题-它的重量为1.87 kb,但应该为2.31(我从浏览器下载了此图像)。
我在做什么错了?
解决方法
正如@IngoLeonhardt指出的那样,我不应该将data:image/jpeg;base64,
部分传递给该函数。现在可以了。