base64のデコードでBIO_readが失敗するとき
base64のデコードでBIO_readが失敗するときは、
BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL);
するとよい。
static CFDataRef DecodeBase64(CFStringRef srcRef) { if (srcRef == NULL) return NULL; CFIndex length = CFStringGetLength(srcRef); CFIndex bytesSize; CFIndex convertedCharacterLength = CFStringGetBytes(srcRef, CFRangeMake(0, length), kCFStringEncodingASCII, 0, false, NULL, 0, &bytesSize); if (convertedCharacterLength != length) return NULL; if (bytesSize <= 0) return NULL; UInt8 *srcBytes = (UInt8 *)malloc(bytesSize); CFStringGetBytes(srcRef, CFRangeMake(0, length), kCFStringEncodingASCII, 0, false, srcBytes, bytesSize, NULL); BIO *base64 = BIO_new(BIO_f_base64()); BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL); BIO *biomem = BIO_new_mem_buf(srcBytes, bytesSize); BIO *biochain = BIO_push(base64, biomem); CFMutableDataRef dataRef = CFDataCreateMutable(kCFAllocatorDefault, 0); UInt8 buffer[1024]; CFIndex readLen; while ( (readLen = BIO_read(biochain, buffer, 1024) ) > 0) { CFDataAppendBytes(dataRef, buffer, readLen); } BIO_free_all(biochain); free(srcBytes); return dataRef; }