📄 endecode.cpp
字号:
/*
void
EncodeFileBase64(const char *in, const char *out) {
// Step 1 : find the size of the source file
HANDLE file = CreateFile(in, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
int source_size = GetFileSize(file, NULL);
CloseHandle(file);
// Step 2 : open the source and target file
FILE *i = fopen(in, "rb");
FILE *o = fopen(out, "wb");
// Step 3: encode data stream to BASE64
// The encoded output stream must be represented in lines of no more
// than 76 characters each.
BYTE value[3];
int line_width = 0;
for (int j = 0; j < source_size / 3; ++j) {
// read next value
fread(value, 3, 1, i);
// convert to 4 6-bit pairs
fputc(EncodeByte((BYTE)((value[0] & 0xFC) >> 2)), o);
fputc(EncodeByte((BYTE)(((value[0] & 0x03) << 4) | ((value[1] & 0xF0) >> 4))), o);
fputc(EncodeByte((BYTE)(((value[1] & 0x0F) << 2) | ((value[2] & 0xC0) >> 6))), o);
fputc(EncodeByte((BYTE)( value[2] & 0x3F)), o);
if ((line_width += 4) == 76) {
line_width = 0;
fputc('\r', o);
fputc('\n', o);
}
}
// Step 4: padding
// Special processing is performed if fewer than 24 bits are available
// at the end of the data being encoded. A full encoding quantum is
// always completed at the end of a body. When fewer than 24 input bits
// are available in an input group, zero bits are added (on the right)
// to form an integral number of 6-bit groups.
switch(source_size % 3) {
case 1 :
{
fread(&value, 1, 1, i);
fputc(EncodeByte((BYTE)((value[0] & 0xFC) >> 2)), o);
fputc(EncodeByte((BYTE)((value[0] & 0x03) << 4)), o);
fputc('=', o);
fputc('=', o);
break;
}
case 2 :
{
fread(&value, 2, 1, i);
fputc(EncodeByte((BYTE)((value[0] & 0xFC) >> 2)), o);
fputc(EncodeByte((BYTE)(((value[0] & 0x03) << 4) | ((value[1] & 0xF0) >> 4))), o);
fputc(EncodeByte((BYTE)((value[1] & 0x0F) << 2)), o);
fputc('=', o);
break;
}
};
// Step 5: close the files again
fclose(o);
fclose(i);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -