bool deserialize_data(const char *p_stream, uint32_t p_stream_size, uint32_t &r_offset, void *&r_data, uint32_t &r_size) { uint32_t t_size = 0, t_data_size = 0; bool t_success = true; void *t_data = nil; t_success = deserialize_uint32(p_stream, p_stream_size, r_offset, t_data_size); if (t_success) { if (t_data_size == 0) { r_size = 0; return true; } if (r_data == nil) { t_size = t_data_size; MCMemoryAllocate(t_size, t_data); } else { t_size = r_size; t_data = r_data; } t_success = (t_data != nil && t_data_size <= t_size); } if (t_success) { MCMemoryCopy(t_data, p_stream + r_offset, t_data_size); r_data = t_data; r_size = t_size; r_offset += t_data_size; } return t_success; }
int MCR_exec(regexp *prog, MCStringRef string, MCRange p_range) { // AL-2014-06-25: [[ Bug 12676 ]] Ensure string is not unnativized by MCR_exec int status; int flags = 0; if (MCStringIsNative(string)) { uindex_t t_length; unichar_t *t_string_chars; t_string_chars = nil; MCMemoryAllocate(MCStringGetLength(string) * sizeof(unichar_t), t_string_chars); t_length = MCStringGetChars(string, p_range, t_string_chars); status = regexec(&prog->rexp, t_string_chars, t_length, NSUBEXP, prog->matchinfo, flags); MCMemoryDeallocate(t_string_chars); } else status = regexec(&prog->rexp, MCStringGetCharPtr(string) + p_range . offset, p_range . length, NSUBEXP, prog->matchinfo, flags); if (status != REG_OKAY) { if (status == REG_NOMATCH) { return (0); } //MCValueRelease(regexperror); regerror(status, NULL, regexperror); return(0); } return (1); }
bool MCTileCacheSoftwareCompositor_CompositeRect(void *p_context, int32_t p_x, int32_t p_y, uint32_t p_color) { MCTileCacheSoftwareCompositorContext *self; self = (MCTileCacheSoftwareCompositorContext *)p_context; if (self -> tile_row == nil && !MCMemoryAllocate(self -> tile_size * sizeof(uint32_t), self -> tile_row)) return false; if (self -> tile_row_color != p_color) { for(int32_t i = 0; i < self -> tile_size; i++) self -> tile_row[i] = p_color; self -> tile_row_color = p_color; } MCRectangle t_dst_rect; t_dst_rect . x = p_x; t_dst_rect . y = p_y; t_dst_rect . width = self -> tile_size; t_dst_rect . height = self -> tile_size; t_dst_rect = MCU_intersect_rect(t_dst_rect, self -> clip); void *t_dst_ptr; t_dst_ptr = (uint8_t *)self -> bits + self -> stride * (t_dst_rect . y - self -> dirty . y) + (t_dst_rect . x - self -> dirty . x) * sizeof(uint32_t); for(uint32_t y = 0; y < t_dst_rect . height; y++) self -> combiner((uint8_t *)t_dst_ptr + y * self -> stride, self -> stride, self -> tile_row, self -> tile_size * sizeof(uint32_t), t_dst_rect . width, 1, self -> opacity); return true; }
static bool load_custom_font_file_into_buffer_from_path(const char *p_path, char *&r_buffer, uint32_t &r_size) { bool t_success; t_success = true; char *t_font_path; t_font_path = nil; if (t_success) t_success = MCCStringFormat(t_font_path, "%s/%s%s", MCcmd, s_font_folder, p_path); if (t_success) t_success = MCS_exists(t_font_path, true); IO_handle t_font_file_handle; t_font_file_handle = nil; if (t_success) { t_font_file_handle = MCS_open(t_font_path, IO_READ_MODE, false, false, 0); t_success = t_font_file_handle != nil; } uint32_t t_file_size; t_file_size = 0; char *t_buffer; t_buffer = nil; if (t_success) { t_file_size = MCS_fsize(t_font_file_handle); t_success = MCMemoryAllocate(t_file_size + 1, t_buffer); } if (t_success) { IO_stat t_read_stat; uint32_t t_bytes_read; t_bytes_read = 0; while (t_success && t_bytes_read < t_file_size) { uint32_t t_count; t_count = t_file_size - t_bytes_read; t_read_stat = MCS_read(t_buffer + t_bytes_read, 1, t_count, t_font_file_handle); t_bytes_read += t_count; t_success = (t_read_stat == IO_NORMAL || (t_read_stat == IO_EOF && t_bytes_read == t_file_size)); } } if (t_success) { r_buffer = t_buffer; r_size = t_file_size; } else /*UNCHECKED */ MCMemoryDelete(t_buffer); /*UNCHECKED */ MCCStringFree(t_font_path); return t_success; }
// MW-2011-09-13: [[ Masks ]] Updated to store data in an MCWindowMask struct. MCWindowShape *MCImage::makewindowshape(void) { bool t_success = true; MCWindowShape *t_mask = nil; CGImageRef t_mask_image = nil; MCImageBitmap *t_bitmap = nil; uint8_t *t_alpha = nil; uindex_t t_alpha_stride = 0; uindex_t t_width, t_height; t_success = lockbitmap(t_bitmap, true); if (t_success) t_success = MCImageBitmapHasTransparency(t_bitmap); if (t_success) { t_width = t_bitmap->width; t_height = t_bitmap->height; t_alpha_stride = (t_width + 3) & ~3; t_success = MCMemoryAllocate(t_alpha_stride * t_height, t_alpha); } if (t_success) { surface_extract_alpha(t_bitmap->data, t_bitmap->stride, t_alpha, t_alpha_stride, t_width, t_height); t_success = MCAlphaToCGImage(t_width, t_height, t_alpha, t_alpha_stride, t_mask_image); } if (t_success) t_success = MCMemoryNew(t_mask); unlockbitmap(t_bitmap); if (!t_success) { CGImageRelease(t_mask_image); MCMemoryDeallocate(t_mask); MCMemoryDeallocate(t_alpha); return nil; } t_mask->width = t_width; t_mask->height = t_height; t_mask->is_sharp = false; t_mask->data = (char*)t_alpha; t_mask->stride = t_alpha_stride; t_mask->handle = t_mask_image; return t_mask; }
static bool MCConvertNativeFromUTF16(const uint16_t *p_chars, uint32_t p_char_count, uint8_t*& r_output, uint32_t& r_output_length) { uint8_t *t_output; uint32_t t_output_length; if (!MCMemoryAllocate(p_char_count, t_output)) return false; uint32_t t_index; t_index = 0; t_output_length = 0; while(t_index < p_char_count) { if (p_chars[t_index] < 128 && (t_index == p_char_count - 1 || p_chars[t_index + 1] < 128)) { t_output[t_output_length++] = (char)p_chars[t_index]; t_index += 1; } else { uint32_t t_start; t_start = t_index; uint32_t t_codepoint; t_codepoint = MCUnicodeCodepointAdvance((const uint2 *)p_chars, p_char_count, t_index); while(t_index < p_char_count) { uint4 t_old_index; t_old_index = t_index; t_codepoint = MCUnicodeCodepointAdvance((const uint2 *)p_chars, p_char_count, t_index); if (MCUnicodeCodepointIsBase(t_codepoint)) { t_index = t_old_index; break; } } uint8_t t_char; if (!MCUnicodeMapToNative(p_chars + t_start, t_index - t_start, t_char)) t_char = '?'; t_output[t_output_length++] = t_char; } } MCMemoryReallocate(t_output, t_output_length, t_output); r_output = t_output; r_output_length = t_output_length; return true; }
bool MCTileCacheSoftwareCompositor_AllocateTile(void *p_context, int32_t p_size, const void *p_bits, uint32_t p_stride, void*& r_tile) { void *t_data; if (!MCMemoryAllocate(p_size * p_size * sizeof(uint32_t), t_data)) return false; // Copy across each scanline of the tile into the buffer. for(int32_t y = 0; y < p_size; y++) memcpy((uint8_t *)t_data + y * p_size * sizeof(uint32_t), (uint8_t *)p_bits + p_stride * y, p_size * sizeof(uint32_t)); r_tile = t_data; return true; }
bool MCCrypt_random_bytes(uint32_t p_bytecount, void *&r_bytes) { bool t_success = true; unsigned char *t_bytes = nil; t_success = InitSSLCrypt() == True; if (t_success) t_success = MCMemoryAllocate(p_bytecount, t_bytes); if (t_success) t_success = RAND_bytes(t_bytes, p_bytecount) == 1; if (t_success) r_bytes = t_bytes; return t_success; }
bool byte_to_hex(uint8_t *p_src, uint32_t p_len, MCStringRef &r_hex) { char_t *t_dst, *t_ptr; if (!MCMemoryAllocate(p_len * 2, t_dst)) return false; t_ptr = t_dst; for (uint32_t i = 0; i < p_len; i++) { *t_ptr++ = s_hex_char[(p_src[i] >> 4)]; *t_ptr++ = s_hex_char[(p_src[i] & 0xF)]; } return MCStringCreateWithNativeCharsAndRelease(t_dst, p_len * 2, r_hex); }
static bool WindowsGetModuleDescription(MCStringRef p_path, MCStringRef& r_description) { bool t_success; t_success = true; MCAutoStringRefAsWString t_path; /* UNCHECKED */ t_path.Lock(p_path); DWORD t_size; t_size = 0; if (t_success) { t_size = GetFileVersionInfoSizeW(*t_path, nil); if (t_size == 0) t_success = false; } void *t_data; t_data = nil; if (t_success) t_success = MCMemoryAllocate(t_size, t_data); if (t_success && !GetFileVersionInfoW(*t_path, 0, t_size, t_data)) t_success = false; UINT t_desc_length; WCHAR *t_desc; t_desc_length = 0; t_desc = nil; if (t_success && !VerQueryValueW(t_data, L"\\StringFileInfo\\040904b0\\FileDescription", (void **)&t_desc, &t_desc_length)) t_success = false; if (t_success) t_success = MCStringCreateWithWString(t_desc, r_description); MCMemoryDeallocate(t_data); // Make sure a description gets set if (!t_success) r_description = MCValueRetain(kMCEmptyString); return t_success; }
bool MCTileCacheCoreGraphicsCompositor_AllocateTile(void *p_context, int32_t p_size, const void *p_bits, uint32_t p_stride, void*& r_tile) { MCTileCacheCoreGraphicsCompositorContext *self; self = (MCTileCacheCoreGraphicsCompositorContext *)p_context; // If the stride is exactly one tile wide, we don't need a copy. void *t_data; t_data = nil; if (p_stride == p_size * sizeof(uint32_t)) t_data = (void *)p_bits; else if (MCMemoryAllocate(p_size * p_size * sizeof(uint32_t), t_data)) { // Copy across each scanline of the tile into the buffer. for(int32_t y = 0; y < p_size; y++) memcpy((uint8_t *)t_data + y * p_size * sizeof(uint32_t), (uint8_t *)p_bits + p_stride * y, p_size * sizeof(uint32_t)); } CGImageRef t_tile; t_tile = nil; if (t_data != nil) { // IM-2013-08-21: [[ RefactorGraphics ]] Refactor CGImage creation code to be pixel-format independent CGBitmapInfo t_bm_info; t_bm_info = MCGPixelFormatToCGBitmapInfo(kMCGPixelFormatNative, true); CGContextRef t_cgcontext; t_cgcontext = CGBitmapContextCreate((void *)t_data, p_size, p_size, 8, p_size * sizeof(uint32_t), self -> colorspace, t_bm_info); if (t_cgcontext != nil) { t_tile = CGBitmapContextCreateImage(t_cgcontext); CGContextRelease(t_cgcontext); } } if (t_data != p_bits) MCMemoryDeallocate(t_data); if (t_tile == nil) return false; r_tile = t_tile; return true; }
static bool MCConvertNativeFromISO8859_1(const uint8_t *p_input, uint32_t p_input_length, uint8_t*& r_output, uint32_t& r_output_length) { uint8_t *t_output; if (!MCMemoryAllocate(p_input_length, t_output)) return false; for(uint32_t i = 0; i < p_input_length; i++) { uint2 t_input_char; t_input_char = MCUnicodeMapFromNative_MacRoman(p_input[i]); if (!MCUnicodeMapToNative(&t_input_char, 1, t_output[i])) t_output[i] = '?'; } r_output = t_output; r_output_length = p_input_length; return true; }
bool read_binary(MCSystemFileHandle *p_file, void *&r_data, uint32_t &r_length) { if (!read_uint32(p_file, r_length)) return false; uint32_t t_read; void *t_data = NULL; if (!MCMemoryAllocate(r_length, t_data)) return false; if (p_file->Read(t_data, r_length, t_read) && t_read == r_length) { r_data = t_data; return true; } else { MCMemoryDeallocate(t_data); return false; } }
bool read_all(IO_handle p_stream, uint8_t *&r_data, uindex_t &r_data_size) { bool t_success = true; uint8_t *t_buffer = nil; uindex_t t_size = 0; t_size = MCS_fsize(p_stream) - MCS_tell(p_stream); t_success = MCMemoryAllocate(t_size, t_buffer); if (t_success) t_success = IO_NORMAL == MCS_readfixed(t_buffer, t_size, p_stream); if (t_success) { r_data = t_buffer; r_data_size = t_size; } else MCMemoryDeallocate(t_buffer); return t_success; }
bool MCStreamCache::AppendToCache(void *p_buffer, uint32_t p_length, uint32_t &r_written) { bool t_success = true; if (m_cache_length + p_length > m_buffer_limit) { if (m_cache_file == NULL) { t_success = MCMultiPartCreateTempFile(cgi_get_upload_temp_dir(), m_cache_file, m_cache_filename); if (t_success && m_cache_buffer != NULL) t_success = (IO_NORMAL == MCS_write(m_cache_buffer, 1, m_cache_length, m_cache_file)); MCMemoryDeallocate(m_cache_buffer); m_cache_buffer = NULL; } if (t_success) t_success = (IO_NORMAL == MCS_write(p_buffer, 1, p_length, m_cache_file)); m_cache_length += p_length; r_written = p_length; } else { if (m_cache_buffer == NULL) t_success = MCMemoryAllocate(m_buffer_limit, m_cache_buffer); if (t_success) { MCMemoryCopy((uint8_t*)m_cache_buffer + m_cache_length, p_buffer, p_length); m_cache_length += p_length; r_written = p_length; } } return t_success; }
bool read_cstring(MCSystemFileHandle *p_file, char *&r_string) { uint32_t t_strlen; if (!read_uint32(p_file, t_strlen)) return false; uint32_t t_read; char *t_str = NULL; if (!MCMemoryAllocate(t_strlen + 1, t_str)) return false; if (p_file->Read(t_str, t_strlen, t_read) && t_read == t_strlen) { t_str[t_strlen] = '\0'; r_string = t_str; return true; } else { MCMemoryDeallocate(t_str); return false; } }
bool MCStreamCache::Ensure(uint32_t p_offset) { if (p_offset <= m_cache_length) return true; bool t_success = true; void *t_buffer; t_success = MCMemoryAllocate(m_buffer_limit, t_buffer); while (t_success && p_offset > m_cache_length) { uint32_t t_to_read; uint32_t t_read; t_to_read = MCMin(p_offset - m_cache_length, m_buffer_limit); t_success = Read(t_buffer, m_cache_length, t_to_read, t_read) && (t_read == t_to_read); } MCMemoryDeallocate(t_buffer); return t_success; }
static bool cgi_native_from_encoding(MCSOutputTextEncoding p_encoding, const char *p_text, uint32_t p_text_length, char *&r_native, uint32_t &r_native_length) { bool t_success = true; uint8_t *t_native = NULL; uint32_t t_native_length = 0; if (p_encoding == kMCSOutputTextEncodingUTF8) { int32_t t_unicode_length; t_unicode_length = UTF8ToUnicode(p_text, p_text_length, NULL, 0); uint16_t *t_unicode = NULL; t_success = MCMemoryAllocate(t_unicode_length, t_unicode); if (t_success) { UTF8ToUnicode(p_text, p_text_length, t_unicode, t_unicode_length); t_success = MCConvertNativeFromUTF16(t_unicode, t_unicode_length / 2, t_native, t_native_length); } MCMemoryDeallocate(t_unicode); } else if (p_encoding == kMCSOutputTextEncodingWindows1252) t_success = MCConvertNativeFromWindows1252((uint8_t*)p_text, p_text_length, t_native, t_native_length); else if (p_encoding == kMCSOutputTextEncodingMacRoman) t_success = MCConvertNativeFromMacRoman((uint8_t*)p_text, p_text_length, t_native, t_native_length); else if (p_encoding == kMCSOutputTextEncodingISO8859_1) t_success = MCConvertNativeFromISO8859_1((uint8_t*)p_text, p_text_length, t_native, t_native_length); if (t_success) { r_native = (char*)t_native; r_native_length = t_native_length; } return t_success; }
bool MCImageEncodePNG(MCImageIndexedBitmap *p_indexed, IO_handle p_stream, uindex_t &r_bytes_written) { bool t_success = true; MCPNGWriteContext t_context; t_context.stream = p_stream; t_context.byte_count = 0; png_structp t_png_ptr = nil; png_infop t_info_ptr = nil; png_color *t_png_palette = nil; png_byte *t_png_transparency = nil; png_bytep t_data_ptr = nil; uindex_t t_stride = 0; /*init png stuff*/ if (t_success) { t_success = nil != (t_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, (png_error_ptr)NULL, (png_error_ptr)NULL)); } if (t_success) t_success = nil != (t_info_ptr = png_create_info_struct(t_png_ptr)); /*in case of png error*/ if (setjmp(png_jmpbuf(t_png_ptr))) t_success = false; if (t_success) png_set_write_fn(t_png_ptr,(png_voidp)&t_context,fakewrite,fakeflush); if (t_success) { png_set_IHDR(t_png_ptr, t_info_ptr, p_indexed->width, p_indexed->height, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_set_gAMA(t_png_ptr, t_info_ptr, 1/MCgamma); } if (t_success) t_success = MCMemoryNewArray(p_indexed->palette_size, t_png_palette); /*create palette for 8 bit*/ if (t_success) { for (uindex_t i = 0; i < p_indexed->palette_size ; i++) { t_png_palette[i].red = p_indexed->palette[i].red >> 8; t_png_palette[i].green = p_indexed->palette[i].green >> 8; t_png_palette[i].blue = p_indexed->palette[i].blue >> 8; } png_set_PLTE(t_png_ptr, t_info_ptr, t_png_palette, p_indexed->palette_size); } if (MCImageIndexedBitmapHasTransparency(p_indexed)) { if (t_success) t_success = MCMemoryAllocate(p_indexed->palette_size, t_png_transparency); if (t_success) { memset(t_png_transparency, 0xFF, p_indexed->palette_size); t_png_transparency[p_indexed->transparent_index] = 0x00; png_set_tRNS(t_png_ptr, t_info_ptr, t_png_transparency, p_indexed->palette_size, NULL); } } if (t_success) png_write_info(t_png_ptr, t_info_ptr); if (t_success) { t_data_ptr = (png_bytep)p_indexed->data; t_stride = p_indexed->stride; } if (t_success) { for (uindex_t i = 0; i < p_indexed->height; i++) { png_write_row(t_png_ptr, t_data_ptr); t_data_ptr += t_stride; } } if (t_success) png_write_end(t_png_ptr, t_info_ptr); if (t_png_ptr != nil) png_destroy_write_struct(&t_png_ptr, &t_info_ptr); if (t_png_palette != nil) MCMemoryDeleteArray(t_png_palette); if (t_png_transparency != nil) MCMemoryDeallocate(t_png_transparency); if (t_success) r_bytes_written = t_context.byte_count; return t_success; }
bool MCGRasterToCGImage(const MCGRaster &p_raster, MCGRectangle p_src_rect, CGColorSpaceRef p_colorspace, bool p_copy, bool p_invert, CGImageRef &r_image) { bool t_success = true; int32_t t_x, t_y; uint32_t t_width, t_height; t_x = p_src_rect.origin.x; t_y = p_src_rect.origin.y; t_width = p_src_rect.size.width; t_height = p_src_rect.size.height; /* OVERHAUL - REVISIT: pixel formats */ const uint8_t *t_src_ptr = (uint8_t*)p_raster.pixels; t_src_ptr += t_y * p_raster.stride + t_x * sizeof(uint32_t); uint32_t t_dst_stride; if (p_invert) p_copy = true; CGImageRef t_image = nil; CGDataProviderRef t_data_provider = nil; if (!p_copy) { t_dst_stride = p_raster.stride; t_success = nil != (t_data_provider = CGDataProviderCreateWithData(nil, t_src_ptr, t_height * p_raster.stride, nil)); } else { uint8_t* t_buffer = nil; t_dst_stride = t_width * sizeof(uint32_t); uindex_t t_buffer_size = t_height * t_dst_stride; t_success = MCMemoryAllocate(t_buffer_size, t_buffer); if (t_success) { int32_t t_src_stride; uint8_t* t_dst_ptr = t_buffer; if (!p_invert) { t_src_stride = p_raster.stride; } else { t_src_ptr += ((int32_t)t_height - 1) * p_raster.stride; t_src_stride = -p_raster.stride; } for (uindex_t y = 0; y < t_height; y++) { MCMemoryCopy(t_dst_ptr, t_src_ptr, t_dst_stride); t_dst_ptr += t_dst_stride; t_src_ptr += t_src_stride; } } if (t_success) t_success = nil != (t_data_provider = CGDataProviderCreateWithData(nil, t_buffer, t_buffer_size, __CGDataProviderDeallocate)); if (!t_success) MCMemoryDeallocate(t_buffer); } // IM-2013-08-21: [[ RefactorGraphics ]] Refactor CGImage creation code to be pixel-format independent CGBitmapInfo t_bm_info; t_bm_info = MCGPixelFormatToCGBitmapInfo(kMCGPixelFormatNative, true); if (t_success) t_success = nil != (t_image = CGImageCreate(t_width, t_height, 8, 32, t_dst_stride, p_colorspace, t_bm_info, t_data_provider, nil, true, kCGRenderingIntentDefault)); CGDataProviderRelease(t_data_provider); if (t_success) r_image = t_image; return t_success; }
static bool MCRegistryListValues(HKEY p_root, const char *p_key, MCRegistryListValuesCallback p_callback, void *p_context) { bool t_success; t_success = true; // Attempt to open the given key. HKEY t_handle; t_handle = nil; if (t_success) if (RegOpenKeyExA(p_root, p_key, 0, KEY_QUERY_VALUE, &t_handle) != ERROR_SUCCESS) t_success = false; // Next determine the maximum length of the value names. DWORD t_max_name_length; if (t_success) if (RegQueryInfoKeyA(t_handle, nil, nil, nil, nil, nil, nil, nil, &t_max_name_length, nil, nil, nil) != ERROR_SUCCESS) t_success = false; // Allocate a buffer big enough for the name char *t_name_buffer; t_name_buffer = nil; if (t_success) t_success = MCMemoryNewArray(t_max_name_length + 1, t_name_buffer); if (t_success) { DWORD t_index; t_index = 0; while(t_success) { DWORD t_name_length, t_value_length; t_name_length = t_max_name_length + 1; t_value_length = 0; LSTATUS t_result; if (t_success) { t_result = RegEnumValueA(t_handle, t_index, t_name_buffer, &t_name_length, nil, nil, nil, &t_value_length); if (t_result == ERROR_NO_MORE_ITEMS) break; if (t_result != ERROR_SUCCESS) t_success = false; } void *t_value_buffer; t_value_buffer = nil; if (t_success) t_success = MCMemoryAllocate(t_value_length, t_value_buffer); DWORD t_type; if (t_success) { t_name_length = t_max_name_length + 1; if (RegEnumValueA(t_handle, t_index, t_name_buffer, &t_name_length, nil, &t_type, (LPBYTE)t_value_buffer, &t_value_length) != ERROR_SUCCESS) t_success = false; } if (t_success && p_callback != nil) p_callback(p_context, t_name_buffer, t_type, t_value_buffer, t_value_length); MCMemoryDeallocate(t_value_buffer); t_index++; } } MCMemoryDeleteArray(t_name_buffer); if (t_handle != nil) RegCloseKey(t_handle); return t_success; }
bool MCGRasterCreateCGDataProvider(const MCGRaster &p_raster, const MCGIntegerRectangle &p_src_rect, bool p_copy, bool p_invert, CGDataProviderRef &r_data_provider, uint32_t &r_stride) { MCAssert(p_src_rect.origin.x >= 0 && p_src_rect.origin.y >= 0); MCAssert(p_src_rect.origin.x + p_src_rect.size.width <= p_raster.width); MCAssert(p_src_rect.origin.y + p_src_rect.size.height <= p_raster.height); bool t_success = true; int32_t t_x, t_y; uint32_t t_width, t_height; t_x = p_src_rect.origin.x; t_y = p_src_rect.origin.y; t_width = p_src_rect.size.width; t_height = p_src_rect.size.height; uint8_t *t_src_ptr = (uint8_t*)MCGRasterGetPixelPtr(p_raster, t_x, t_y); uint32_t t_dst_stride; if (p_invert) p_copy = true; CGDataProviderRef t_data_provider = nil; if (!p_copy) { t_dst_stride = p_raster.stride; t_data_provider = CGDataProviderCreateWithData(nil, t_src_ptr, t_height * p_raster.stride, __CGDataProviderDeallocate); t_success = t_data_provider != nil; if (!t_success) MCMemoryDeallocate(t_src_ptr); } else { uint8_t* t_buffer = nil; t_dst_stride = t_width * sizeof(uint32_t); uindex_t t_buffer_size = t_height * t_dst_stride; t_success = MCMemoryAllocate(t_buffer_size, t_buffer); if (t_success) { int32_t t_src_stride; uint8_t* t_dst_ptr = t_buffer; if (!p_invert) { t_src_stride = p_raster.stride; } else { t_src_ptr += ((int32_t)t_height - 1) * p_raster.stride; t_src_stride = -p_raster.stride; } for (uindex_t y = 0; y < t_height; y++) { MCMemoryCopy(t_dst_ptr, t_src_ptr, t_dst_stride); t_dst_ptr += t_dst_stride; t_src_ptr += t_src_stride; } } if (t_success) { t_data_provider = CGDataProviderCreateWithData(nil, t_buffer, t_buffer_size, __CGDataProviderDeallocate); t_success = t_data_provider != nil; } if (!t_success) MCMemoryDeallocate(t_buffer); } if (t_success) { r_data_provider = t_data_provider; r_stride = t_dst_stride; } return t_success; }
bool MCCrypt_rsa_op(bool p_encrypt, RSA_KEYTYPE p_key_type, const char *p_message_in, uint32_t p_message_in_length, const char *p_key, uint32_t p_key_length, const char *p_passphrase, char *&r_message_out, uint32_t &r_message_out_length, char *&r_result, uint32_t &r_error) { bool t_success = true; EVP_PKEY *t_key = NULL; RSA *t_rsa = NULL; int32_t t_rsa_size; uint8_t *t_output_buffer = NULL; int32_t t_output_length; if (!InitSSLCrypt()) { t_success = false; MCCStringClone("error: ssl library initialization failed", r_result); } if (t_success) { if (!load_pem_key(p_key, p_key_length, p_key_type, p_passphrase, t_key)) { t_success = false; MCCStringClone("error: invalid key", r_result); } } if (t_success) { t_rsa = EVP_PKEY_get1_RSA(t_key); if (t_rsa == NULL) { t_success = false; MCCStringClone("error: not an RSA key", r_result); } } if (t_success) { t_rsa_size = RSA_size(t_rsa); if (!MCMemoryAllocate(t_rsa_size, t_output_buffer)) { t_success = false; r_error = EE_NO_MEMORY; } } int (*t_rsa_func)(int, const unsigned char*, unsigned char*, RSA*, int) = NULL; if (t_success) { if (p_encrypt) { if (p_key_type == RSAKEY_PRIVKEY) t_rsa_func = RSA_private_encrypt; else t_rsa_func = RSA_public_encrypt; if (p_message_in_length >= unsigned(t_rsa_size - 11)) { t_success = false; MCCStringClone("error: message too large", r_result); } } else { if (p_key_type == RSAKEY_PRIVKEY) t_rsa_func = RSA_private_decrypt; else t_rsa_func = RSA_public_decrypt; if (p_message_in_length != t_rsa_size) { t_success = false; MCCStringClone("error: invalid message size", r_result); } } } if (t_success) { t_output_length = t_rsa_func(p_message_in_length, (const uint8_t*)p_message_in, t_output_buffer, t_rsa, RSA_PKCS1_PADDING); if (t_output_length < 0) { t_success = false; MCCStringClone("error: SSL operation failed", r_result); } } if (t_rsa != NULL) RSA_free(t_rsa); if (t_key != NULL) EVP_PKEY_free(t_key); if (t_success) { r_message_out = (char*)t_output_buffer; r_message_out_length = t_output_length; } else { uint32_t t_err; t_err = ERR_get_error(); if (t_err) { const char *t_ssl_error = ERR_reason_error_string(t_err); MCCStringAppendFormat(r_result, " (SSL error: %s)", t_ssl_error); } MCMemoryDeallocate(t_output_buffer); } return t_success; }
// MW-2013-05-21: [[ RandomBytes ]] Implementation of random byte generation for // Mac (Desktop and Server). bool MCS_random_bytes(size_t p_count, MCDataRef& r_buffer) { // IM-2014-08-06: [[ Bug 13038 ]] Enable this implementation on OSX + server // so we can remove the reliance on SSL from MCU_random_bytes // Now, on Lion and above SecRandomCopyBytes is available, so use that if // possible. ( Note that as we can't link against the 10.7 SDK, we have to // weak-link manually :( ). uint8_t *t_buffer; /* UNCHECKED */ MCMemoryAllocate(p_count, t_buffer); if (MCmajorosversion >= 0x1070) { if (s_sec_random_copy_bytes == nil) { CFBundleRef t_security_bundle; t_security_bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security")); if (t_security_bundle != nil) { s_sec_random_copy_bytes = (SecRandomCopyBytesPtr)CFBundleGetFunctionPointerForName(t_security_bundle, CFSTR("SecRandomCopyBytes")); CFRelease(t_security_bundle); } } if (s_sec_random_copy_bytes != nil) { // IM-2014-04-16: [[ Bug 11860 ]] SecRandomCopyBytes returns 0 on success if (0 == s_sec_random_copy_bytes(NULL, p_count, t_buffer)) return MCDataCreateWithBytesAndRelease((byte_t*)t_buffer, p_count, r_buffer); return false; } } // Otherwise attempt to use /dev/urandom int t_fd; t_fd = open("/dev/urandom", O_RDONLY); if (t_fd < 0) return false; // Loop until we've read enough bytes (or an error occurs) uint8_t *t_bytes; size_t t_bytes_read; t_bytes_read = 0; t_bytes = t_buffer; while(t_bytes_read < p_count) { int t_read_count; t_read_count = read(t_fd, t_bytes, p_count - t_bytes_read); // If we read nothing, give up. if (t_read_count == 0) break; // If an non-interrupt error occured, give up. if (t_read_count < 0 && errno != EINTR) break; // Otherwise advance pointers, adjust counts. t_bytes += t_read_count; t_bytes_read += t_read_count; } // Close the random device. close(t_fd); // If we read the correct number of bytes, then we are done. if (t_bytes_read == p_count) return MCDataCreateWithBytesAndRelease((byte_t*)t_buffer, p_count, r_buffer); return false; }