static bool handle_completion(AutoCompleteState& autoComplete, TextBuffer& text){ // Find the preceeding backslash or character halting auto-completion const size_t bs = text.prev_any_of(utf8_string(chars::backslash) + utf8_string(chars::space) + utf8_string(chars::comma) + utf8_string(chars::eol)); if (bs == utf8_string::npos || text.at(bs) != chars::backslash){ return false; } auto completion = autoComplete.Empty() ? autoComplete.Complete(text.get().substr(bs, text.caret() - bs)) : autoComplete.Next(); text.select(CaretRange(bs, text.caret())); text.insert(completion); return true; }
// Split the filename into root and extension, which if concatenated // are equal to the filename. std::pair<utf8_string, utf8_string> split_extension(const FileName& f){ const utf8_string s = f.Str(); auto pos = s.rfind(chars::full_stop); if (pos == utf8_string::npos){ return {s, utf8_string()}; } else{ return {slice_up_to(s, pos), slice_from(s, pos)}; } }
char *copy_utf8_string(const char *str, uint32_t length) { tls_copy_t *tls = copy_get_tls(); tls->active = 1; if(setjmp(tls->jb) == 0) { char *ret = utf8_string(str, length); tls->active = 0; return ret; } tls->active = 0; return NULL; }
static void log_string(const char *str, int length) { if (str == NULL) { bson_append_string_n( g_bson, g_istr, "", 0 ); return; } int ret; char * utf8s = utf8_string(str, length); int utf8len = * (int *) utf8s; ret = bson_append_binary( g_bson, g_istr, BSON_BIN_BINARY, utf8s+4, utf8len ); if (ret == BSON_ERROR) { char tmp[64]; snprintf(tmp, 64, "dbg bson err string %x utf8len %d", g_bson->err, utf8len); debug_message(tmp); } free(utf8s); }
int main() { pipe_init("\\\\.\\PIPE\\cuckoo", 0); hook_init(GetModuleHandle(NULL)); mem_init(); assert(native_init() == 0); uint8_t buf[16]; uint16_t val[2]; assert(utf8_encode(0x00000001, buf) == 1 && memcmp(buf, "\x01", 1) == 0); assert(utf8_encode(0x0000007f, buf) == 1 && memcmp(buf, "\x7f", 1) == 0); assert(utf8_encode(0x00000080, buf) == 2 && memcmp(buf, "\xc2\x80", 2) == 0); assert(utf8_encode(0x000007ff, buf) == 2 && memcmp(buf, "\xdf\xbf", 2) == 0); assert(utf8_encode(0x00000800, buf) == 3 && memcmp(buf, "\xe0\xa0\x80", 3) == 0); assert(utf8_encode(0x0000ffff, buf) == 3 && memcmp(buf, "\xef\xbf\xbf", 3) == 0); assert(utf8_encode(0x00010000, buf) == 4 && memcmp(buf, "\xf0\x90\x80\x80", 4) == 0); assert(utf8_encode(0x001fffff, buf) == 4 && memcmp(buf, "\xf7\xbf\xbf\xbf", 4) == 0); assert(utf8_encode(0x00200000, buf) == 5 && memcmp(buf, "\xf8\x88\x80\x80\x80", 5) == 0); assert(utf8_encode(0x03ffffff, buf) == 5 && memcmp(buf, "\xfb\xbf\xbf\xbf\xbf", 5) == 0); assert(utf8_encode(0x04000000, buf) == 6 && memcmp(buf, "\xfc\x84\x80\x80\x80\x80", 6) == 0); assert(utf8_encode(0x7fffffff, buf) == 6 && memcmp(buf, "\xfd\xbf\xbf\xbf\xbf\xbf", 6) == 0); // It's kind of a hassle to get utf8_wstring() to work here, so we'll just // do similar work with the byte count, which calculates the required // amount of bytes for a sequence. The following sequences represent the // various utf8 boundaries, i.e., their maximum values before needing an // extra byte etc. assert(utf8_bytecnt_unicode((val[0] = 0xd800, val[1] = 0xdc00, val), 2) == 1); assert(utf8_bytecnt_unicode((val[0] = 0xd801, val[1] = 0xdc00, val), 2) == 2); assert(utf8_bytecnt_unicode((val[0] = 0xd802, val[1] = 0xdc00, val), 2) == 3); assert(utf8_bytecnt_unicode((val[0] = 0xd840, val[1] = 0xdc00, val), 2) == 4); // We used to have some issues with signed chars and the MSB being set as // we wouldn't cast these as unsigned characters. This would result in // utf8_length(0xffffff81) returning -1, rather than utf8_length(0x81) // returning 2. Similarly to incorrect return values of utf8_length() we'd // also experience out-of-bounds writes as our buffer would be indexed by // -1, resulting in undefined behavior. assert(utf8_bytecnt_ascii("\x81", 1) == 2); assert(utf8_bytecnt_unicode(L"\u8081", 1) == 3); assert(memcmp(utf8_string("\x81", 1), "\x02\x00\x00\x00\xc2\x81", 6) == 0); assert(memcmp(utf8_wstring(L"\u8081", 1), "\x03\x00\x00\x00\xe8\x82\x81", 7) == 0); }
static void log_string(bson *b, const char *idx, const char *str, int length) { if(str == NULL) { bson_append_string_n(b, idx, "", 0); return; } int ret, utf8len; if(range_is_readable(str, length) != 0) { char *utf8s = utf8_string(str, length); utf8len = *(int *) utf8s; ret = bson_append_binary(b, idx, BSON_BIN_BINARY, utf8s+4, utf8len); if(ret == BSON_ERROR) { pipe("CRITICAL:Error creating bson string, error, %x utf8len %d.", b->err, utf8len); } mem_free(utf8s); } else { bson_append_binary(b, idx, BSON_BIN_BINARY, "<INVALID POINTER>", 17); } }
SaveResult write_bmp(const FilePath& filePath, const Bitmap& bmp, BitmapQuality quality) { BinaryWriter out(filePath); if (!out.good()){ return SaveResult::SaveFailed(error_open_file_write(filePath)); } const auto bitsPerPixel = bits_per_pixel(quality); const IntSize size(bmp.GetSize()); const int rowStride = bmp_row_stride(bitsPerPixel, size.w); switch(quality){ // Note: No default, to ensure warning if unhandled enum value case BitmapQuality::COLOR_8BIT: { const auto pixelData = quantized(bmp, Dithering::ON); PaletteColors paletteColors(pixelData.palette.size()); write_struct(out, create_bitmap_file_header(paletteColors, rowStride, size.h)); write_struct(out, create_bitmap_info_header_8bipp(bmp.GetSize(), default_DPI(), PaletteColors(pixelData.palette.size()), false)); write_8bipp_BI_RGB(out, pixelData); return SaveResult::SaveSuccessful(); } case BitmapQuality::GRAY_8BIT: { MappedColors pixelData(desaturate_AlphaMap(bmp), grayscale_color_table()); PaletteColors paletteColors(pixelData.palette.size()); write_struct(out, create_bitmap_file_header(paletteColors, rowStride, size.h)); write_struct(out, create_bitmap_info_header_8bipp(bmp.GetSize(), default_DPI(), PaletteColors(pixelData.palette.size()), false)); write_8bipp_BI_RGB(out, pixelData); return SaveResult::SaveSuccessful(); } case BitmapQuality::COLOR_24BIT: { write_struct(out, create_bitmap_file_header(PaletteColors(0), rowStride, size.h)); write_struct(out, create_bitmap_info_header_24bipp(bmp.GetSize(), default_DPI(), false)); write_24bipp_BI_RGB(out, bmp); return SaveResult::SaveSuccessful(); } } assert(false); return SaveResult::SaveFailed(utf8_string("Internal error in save_bitmap")); }
SaveResult SaveResult::SaveSuccessful(){ return SaveResult(true, utf8_string("")); }
static PyObject* as_repr(activeSettingsObject*){ return build_unicode(utf8_string(ACTIVE_SETTINGS_NAME)); }
FileExtension::FileExtension(const char* s) : m_impl(new ExtensionImpl(utf8_string(s))) {}
static int dm5_dive(void *param, int columns, char **data, char **column) { UNUSED(columns); UNUSED(column); int i; int tempformat = 0; int interval, retval = 0, block_size; struct parser_state *state = (struct parser_state *)param; sqlite3 *handle = state->sql_handle; unsigned const char *sampleBlob; char *err = NULL; char get_events_template[] = "select * from Mark where DiveId = %d"; char get_tags_template[] = "select Text from DiveTag where DiveId = %d"; char get_cylinders_template[] = "select * from DiveMixture where DiveId = %d"; char get_gaschange_template[] = "select GasChangeTime,Oxygen,Helium from DiveGasChange join DiveMixture on DiveGasChange.DiveMixtureId=DiveMixture.DiveMixtureId where DiveId = %d"; char get_events[512]; dive_start(state); state->cur_dive->number = atoi(data[0]); state->cur_dive->when = (time_t)(atol(data[1])); if (data[2]) utf8_string(data[2], &state->cur_dive->notes); if (data[3]) state->cur_dive->duration.seconds = atoi(data[3]); if (data[15]) state->cur_dive->dc.duration.seconds = atoi(data[15]); /* * TODO: the deviceid hash should be calculated here. */ settings_start(state); dc_settings_start(state); if (data[4]) { utf8_string(data[4], &state->cur_settings.dc.serial_nr); state->cur_settings.dc.deviceid = atoi(data[4]); } if (data[5]) utf8_string(data[5], &state->cur_settings.dc.model); dc_settings_end(state); settings_end(state); if (data[6]) state->cur_dive->dc.maxdepth.mm = lrint(strtod_flags(data[6], NULL, 0) * 1000); if (data[8]) state->cur_dive->dc.airtemp.mkelvin = C_to_mkelvin(atoi(data[8])); if (data[9]) state->cur_dive->dc.watertemp.mkelvin = C_to_mkelvin(atoi(data[9])); if (data[4]) { state->cur_dive->dc.deviceid = atoi(data[4]); } if (data[5]) utf8_string(data[5], &state->cur_dive->dc.model); snprintf(get_events, sizeof(get_events) - 1, get_cylinders_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm5_cylinders, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm5_cylinders failed.\n"); return 1; } if (data[14]) state->cur_dive->dc.surface_pressure.mbar = (atoi(data[14]) / 100); interval = data[16] ? atoi(data[16]) : 0; /* * sampleBlob[0] version number, indicates the size of one sample * * Following ones describe single sample, bugs in interpretation of the binary blob are likely: * * sampleBlob[3] depth * sampleBlob[7-9] pressure * sampleBlob[11] temperature - either full Celsius or float, might be different field for some version of DM */ sampleBlob = (unsigned const char *)data[24]; if (sampleBlob) { switch (sampleBlob[0]) { case 1: // Log is converted from DM4 to DM5 block_size = 16; break; case 2: block_size = 19; break; case 3: block_size = 23; break; case 4: // Temperature is stored in float tempformat = 1; block_size = 26; break; case 5: // Temperature is stored in float tempformat = 1; block_size = 30; break; default: block_size = 16; break; } } for (i = 0; interval && sampleBlob && i * interval < state->cur_dive->duration.seconds; i++) { float *depth = (float *)&sampleBlob[i * block_size + 3]; int32_t pressure = (sampleBlob[i * block_size + 9] << 16) + (sampleBlob[i * block_size + 8] << 8) + sampleBlob[i * block_size + 7]; sample_start(state); state->cur_sample->time.seconds = i * interval; state->cur_sample->depth.mm = lrintf(depth[0] * 1000.0f); if (tempformat == 1) { float *temp = (float *)&(sampleBlob[i * block_size + 11]); state->cur_sample->temperature.mkelvin = C_to_mkelvin(*temp); } else { if ((sampleBlob[i * block_size + 11]) != 0x7F) { state->cur_sample->temperature.mkelvin = C_to_mkelvin(sampleBlob[i * block_size + 11]); } } /* * Limit cylinder pressures to somewhat sensible values */ if (pressure >= 0 && pressure < 350000) state->cur_sample->pressure[0].mbar = pressure; sample_end(state); } /* * Log was converted from DM4, thus we need to parse the profile * from DM4 format */ if (i == 0) { float *profileBlob; unsigned char *tempBlob; int *pressureBlob; profileBlob = (float *)data[17]; tempBlob = (unsigned char *)data[18]; pressureBlob = (int *)data[19]; for (i = 0; interval && i * interval < state->cur_dive->duration.seconds; i++) { sample_start(state); state->cur_sample->time.seconds = i * interval; if (profileBlob) state->cur_sample->depth.mm = lrintf(profileBlob[i] * 1000.0f); else state->cur_sample->depth.mm = state->cur_dive->dc.maxdepth.mm; if (data[18] && data[18][0]) state->cur_sample->temperature.mkelvin = C_to_mkelvin(tempBlob[i]); if (data[19] && data[19][0]) state->cur_sample->pressure[0].mbar = pressureBlob[i]; sample_end(state); } } snprintf(get_events, sizeof(get_events) - 1, get_gaschange_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm5_gaschange, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm5_gaschange failed.\n"); return 1; } snprintf(get_events, sizeof(get_events) - 1, get_events_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_events, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_events failed.\n"); return 1; } snprintf(get_events, sizeof(get_events) - 1, get_tags_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_tags, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_tags failed.\n"); return 1; } dive_end(state); return SQLITE_OK; }
static int dm4_dive(void *param, int columns, char **data, char **column) { UNUSED(columns); UNUSED(column); int i; int interval, retval = 0; struct parser_state *state = (struct parser_state *)param; sqlite3 *handle = state->sql_handle; float *profileBlob; unsigned char *tempBlob; int *pressureBlob; char *err = NULL; char get_events_template[] = "select * from Mark where DiveId = %d"; char get_tags_template[] = "select Text from DiveTag where DiveId = %d"; char get_events[64]; dive_start(state); state->cur_dive->number = atoi(data[0]); state->cur_dive->when = (time_t)(atol(data[1])); if (data[2]) utf8_string(data[2], &state->cur_dive->notes); /* * DM4 stores Duration and DiveTime. It looks like DiveTime is * 10 to 60 seconds shorter than Duration. However, I have no * idea what is the difference and which one should be used. * Duration = data[3] * DiveTime = data[15] */ if (data[3]) state->cur_dive->duration.seconds = atoi(data[3]); if (data[15]) state->cur_dive->dc.duration.seconds = atoi(data[15]); /* * TODO: the deviceid hash should be calculated here. */ settings_start(state); dc_settings_start(state); if (data[4]) utf8_string(data[4], &state->cur_settings.dc.serial_nr); if (data[5]) utf8_string(data[5], &state->cur_settings.dc.model); state->cur_settings.dc.deviceid = 0xffffffff; dc_settings_end(state); settings_end(state); if (data[6]) state->cur_dive->dc.maxdepth.mm = lrint(strtod_flags(data[6], NULL, 0) * 1000); if (data[8]) state->cur_dive->dc.airtemp.mkelvin = C_to_mkelvin(atoi(data[8])); if (data[9]) state->cur_dive->dc.watertemp.mkelvin = C_to_mkelvin(atoi(data[9])); /* * TODO: handle multiple cylinders */ cylinder_start(state); if (data[22] && atoi(data[22]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].start.mbar = atoi(data[22]); else if (data[10] && atoi(data[10]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].start.mbar = atoi(data[10]); if (data[23] && atoi(data[23]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].end.mbar = (atoi(data[23])); if (data[11] && atoi(data[11]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].end.mbar = (atoi(data[11])); if (data[12]) state->cur_dive->cylinder[state->cur_cylinder_index].type.size.mliter = lrint((strtod_flags(data[12], NULL, 0)) * 1000); if (data[13]) state->cur_dive->cylinder[state->cur_cylinder_index].type.workingpressure.mbar = (atoi(data[13])); if (data[20]) state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.o2.permille = atoi(data[20]) * 10; if (data[21]) state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.he.permille = atoi(data[21]) * 10; cylinder_end(state); if (data[14]) state->cur_dive->dc.surface_pressure.mbar = (atoi(data[14]) * 1000); interval = data[16] ? atoi(data[16]) : 0; profileBlob = (float *)data[17]; tempBlob = (unsigned char *)data[18]; pressureBlob = (int *)data[19]; for (i = 0; interval && i * interval < state->cur_dive->duration.seconds; i++) { sample_start(state); state->cur_sample->time.seconds = i * interval; if (profileBlob) state->cur_sample->depth.mm = lrintf(profileBlob[i] * 1000.0f); else state->cur_sample->depth.mm = state->cur_dive->dc.maxdepth.mm; if (data[18] && data[18][0]) state->cur_sample->temperature.mkelvin = C_to_mkelvin(tempBlob[i]); if (data[19] && data[19][0]) state->cur_sample->pressure[0].mbar = pressureBlob[i]; sample_end(state); } snprintf(get_events, sizeof(get_events) - 1, get_events_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_events, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_events failed.\n"); return 1; } snprintf(get_events, sizeof(get_events) - 1, get_tags_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_tags, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_tags failed.\n"); return 1; } dive_end(state); /* for (i=0; i<columns;++i) { fprintf(stderr, "%s\t", column[i]); } fprintf(stderr, "\n"); for (i=0; i<columns;++i) { fprintf(stderr, "%s\t", data[i]); } fprintf(stderr, "\n"); //exit(0); */ return SQLITE_OK; }