|
Introduction | License | b64 C-API | b64 C++-API |
Installing b64 | Building b64 | Linking to b64 | Contact & Feedback |
b64_encode() encodes a binary block into a block of printable characters, as follows:
int ints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; size_t cch = b64_encode(&ints[0], sizeof(ints), NULL, 0); /* Ask for length required */ char *enc = (char*)malloc(cch); /* No error checking here ... real code must do so */ b64_encode(&ints[0], sizeof(ints), enc, cch); printf("Converted: [%.*s]\n", cch, enc);this prints:
Converted: [AQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAA==]
b64_encode2() provides a superset of the functionality of b64_encode(). It is able to split the output in multiples of 4-characters, which supports the requirement to split any lines longer than 64-characters with carriage-return/new-line pairs of RFC-1113. Specifying B64_F_LINE_LEN_64 in the flags parameter splits on 64 bytes, or you can specify B64_F_LINE_LEN_USE_PARAM and a number divisible by 4 as the lineLength parameter. Specifying B64_F_LINE_LEN_USE_PARAM with a lineLength of 0 never splits, which is equivalent to the behaviour of b64_encode(). The following example illustrates how to split on 16-characters at a time:
int ints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; size_t cch = b64_encode2(&ints[0], sizeof(ints), NULL, 0, B64_F_LINE_LEN_USE_PARAM, 16, NULL); /* Ask for length required */ char *enc = (char*)malloc(cch); /* No error checking here ... real code must do so */ b64_encode(&ints[0], sizeof(ints), enc, cch, B64_F_LINE_LEN_USE_PARAM, 16, NULL); printf("Converted: [%.*s]\n", cch, enc);this prints:
Converted: [AQAAAAIAAAADAAAA BAAAAAUAAAAGAAAA BwAAAAgAAAAJAAAA CgAAAA==]
b64_decode() decodes a block of printable characters into a binary block, as follows:
size_t cb = b64_decode(enc, cch, NULL, 0); /* Ask for size required */ int *dec = (int*)alloca(cb); /* No error checking here ... real code must do so */ b64_decode(enc, cch, dec, cb); assert(0 == memcmp(&ints[0], &dec[0], sizeof(ints)));
The b64::encode() and b64::decode() functions correspond to the b64::b64_encode() and b64::b64_decode() ones from the b64 C-API. They are used as follows:
int ints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; b64::string_t enc = b64::encode(&ints[0], sizeof(ints)); std::cout << enc << std::endl; b64::blob_t dec = b64::decode(enc); assert(0 == ::memcmp(&ints[0], &dec[0], sizeof(ints)));
There are three overloads of the b64::encode() and b64::decode() functions, to provide for encoding from:
void const*
) and a length blob_t
instancechar const*
) and a length string_t
instance c_str_ptr()
and c_str_len()
have been defined; this includes many types within the STLSoft libraries
|
b64 Library documentation © Synesis Software Pty Ltd, 2004-2007 |