在研发过程,将做工程过程比较好的内容段记录起来,下面的内容段是关于C语言版的Base-64加密解密函数的内容,应该是对各位有些用。
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
static const char padding = '=';
static size_t digit_value(char ch);
@description:
Converts n bytes pointed to by src into base-64 representation.
{
size_t partial_block_bytes = n % BLOCK_BYTE;
size_t full_blocks = n / BLOCK_BYTE;
if (!dst)
return NULL;
size_t k;
for (k = 0; full_blocks--; bytes += BLOCK_BYTE) {
check_append_newline(dst + k, &k);
convert_block(dst, &k, bytes, BLOCK_BYTE);
}
check_append_newline(dst + k, &k);
convert_block(dst, &k, bytes, partial_block_bytes);
dst[k] = '0';
return dst;
}
@description:
Converts a valid base-64 string into a decoded array of bytes
and stores the number of decoded bytes in the object pointed
to by n.
{
size_t len = strlen(src);
if (len % BLOCK_CHAR)
free(dst);
}
}
size_t encoded[] = {
digit_value(src[0]),
digit_value(src[1]),
digit_value(src[2]),
digit_value(src[3])
};
if (encoded[i] == (size_t)-1) {
free(dst);
return NULL;
}
}
uint8_t bytes[] = {
(encoded[0] << 2) + ((encoded[1] & 0x30) >> 4),
((encoded[1] & 0x0f) << 4) + ((encoded[2] & 0x3c) >> 2),
((encoded[2] & 0x03) << 6) + encoded[3]
};
for (size_t i = 0; i < BLOCK_BYTE; ++i) {
if (bytes[i])
}
}
return dst;
}
@description:
Locates the index value of a base-64 character for decoding.
static size_t digit_value(char ch)
{
if (ch == padding)
return 0;
return p ? p - digits : (size_t)-1;
}
@description:
Appends a newline to dst if the value of end is at the correct
position. Returns the updated value of end, which may not change
if a newline was not appended.
{
memcpy(dst, newline, NEWLINE_LEN);
}
}
@description:
Converts a 24-bit block represented by 3 octets into four base-64
characters and stores them in dst starting at end. Returns the
updated value of end for convenience.
{
switch (bytes) {
case 3:
break;
case 2:
break;
case 1:
break;
}
}
#define TEST_DRIVER
#ifdef TEST_DRIVER
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
size_t n;
free(p);
free(q);
}
return 0;
}
#endif