static dispatch_data_t execute_sectransform(SecTransformRef transformRef, dispatch_data_t data) { const void * bytes; size_t size; dispatch_data_t map = dispatch_data_create_map(data, &bytes, &size); assert(map); CFDataRef dataRef = CFDataCreate(kCFAllocatorDefault, bytes, size); assert(dataRef); dispatch_release(map); SecTransformSetAttribute(transformRef, kSecTransformInputAttributeName, dataRef, NULL); CFDataRef transformedDataRef = SecTransformExecute(transformRef, NULL); assert(transformedDataRef); CFRelease(dataRef); dispatch_data_t output = dispatch_data_create(CFDataGetBytePtr(transformedDataRef), CFDataGetLength(transformedDataRef), dispatch_get_main_queue(), DISPATCH_DATA_DESTRUCTOR_DEFAULT); CFRelease(transformedDataRef); return output; }
static dispatch_data_t _dispatch_data_subrange_map(dispatch_data_t data, const void **ptr, size_t offset, size_t size) { dispatch_data_t subrange, map = NULL; subrange = dispatch_data_create_subrange(data, offset, size); if (dispatch_data_get_size(subrange) == size) { map = dispatch_data_create_map(subrange, ptr, NULL); } dispatch_release(subrange); return map; }
static bool dispatch_data_equal(dispatch_data_t a, dispatch_data_t b) { const void * ptr, * ptr2; size_t size, size2; bool equal = true; dispatch_data_t map = dispatch_data_create_map(a, &ptr, &size); \ assert(map); dispatch_data_t map2 = dispatch_data_create_map(b, &ptr2, &size2); \ assert(map2); if (size == size2) { if (memcmp(ptr, ptr2, size) != 0) { equal = false; } } else { equal = false; } dispatch_release(map); dispatch_release(map2); return equal; }
static void test_concat(void) { dispatch_group_enter(g); dispatch_async(dispatch_get_main_queue(), ^{ char* buffer1 = "This is buffer1 "; size_t size1 = 17; char* buffer2 = "This is buffer2 "; size_t size2 = 17; __block bool buffer2_destroyed = false; dispatch_data_t data1 = dispatch_data_create(buffer1, size1, NULL, NULL); dispatch_data_t data2 = dispatch_data_create(buffer2, size2, dispatch_get_main_queue(), ^{ buffer2_destroyed = true; }); dispatch_data_t concat = dispatch_data_create_concat(data1, data2); dispatch_release(data1); dispatch_release(data2); test_long("Data size of concatenated dispatch data", dispatch_data_get_size(concat), 34); const void* contig; size_t contig_size; dispatch_data_t contig_data = dispatch_data_create_map(concat, &contig, &contig_size); dispatch_release(concat); dispatch_release(contig_data); test_long("Contiguous memory size", contig_size, 34); dispatch_async(dispatch_get_main_queue(), ^{ test_long("buffer2 destroyed", buffer2_destroyed, true); dispatch_group_leave(g); });