/** * @abstract get the length when changing UTF16 to UTF8 coded string needed * @param pUTF16Str utf16 string * @return length of a UTF8 coding string */ unsigned int utf16_to_utf8_len(const unsigned char input[]) { unsigned int retlen = 0; unsigned int i = 0; unsigned short cvalue = 0; unsigned int length = 0; int size = 0; if (input == NULL) { return 0; } i = 0; length = utf16_strlen(input) * 2; while (input[i]!=0 || input[i+1]!=0) { cvalue = (unsigned short)((input[i]<<8) + input[i+1]); size = utf16_char_to_utf8(cvalue, NULL, length-i); if (size < 0) { // error code return 0; } retlen += size; i += 2; } return retlen; }
/* NOTE: despite what you would expect, 'file_name' is actually a path. * With windoze style backlashes, ofc. */ static struct efi_file_handle *file_open(struct file_system *fs, struct file_handle *parent, s16 *file_name, u64 mode) { struct file_handle *fh; char f0[MAX_UTF8_PER_UTF16] = {0}; int plen = 0; int flen = 0; if (file_name) { utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1); flen = utf16_strlen((u16 *)file_name); } /* we could have a parent, but also an absolute path: */ if (f0[0] == '\\') { plen = 0; } else if (parent) { plen = strlen(parent->path) + 1; } /* +2 is for null and '/' */ fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2); fh->base = efi_file_handle_protocol; fh->fs = fs; if (parent) { char *p = fh->path; if (plen > 0) { strcpy(p, parent->path); p += plen - 1; *p++ = '/'; } utf16_to_utf8((u8 *)p, (u16 *)file_name, flen); if (sanitize_path(fh->path)) goto error; /* check if file exists: */ if (set_blk_dev(fh)) goto error; if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path))) goto error; /* figure out if file is a directory: */ fh->isdir = is_dir(fh); } else { fh->isdir = 1; strcpy(fh->path, ""); } return &fh->base; error: free(fh); return NULL; }
bool calchook_register(const char16_t *name, calchook_callback callback) { if(name[utf16_strlen(reinterpret_cast<const uint16_t*>(name)) - 1] != '(' // Doesn't end on '(' || calchook_get(name)) // Already registered return false; calchook_registry.emplace_back(name, callback); return true; }
/******************************************************************************** void navcoreDisplay::drawRulerPos(int size, int yPos) Input: size of the current map length (in metres) and desired vertical position for the ruler Description: Draws a ruler over the map display, showing size in metres/km. ********************************************************************************/ void navcoreDisplay::drawRulerPos(int size, int yPos) { UTF16 label[256]; UTF16 unit[256]; int ymin, ymax, vLength=10; TPos txtPos; //are we drawing the ruler at the top or bottom of the map display? if (yPos==0) { ymin=yPos-vLength; ymax=yPos; txtPos.y=ymax-7; } else { ymin=yPos; ymax=yPos+vLength; txtPos.y=ymin+37; } //in the case where size corresponds to screen_height, need to convert it //to metres if (SCREEN_HEIGHT>SCREEN_WIDTH) { size = (SCREEN_HEIGHT / SCREEN_WIDTH) * size; } //draw the ruler and ticks GraphContext_DrawHLine(&context,10,80,yPos,RGB_FromInternal(0, context.depth)); GraphContext_DrawVLine(&context,10,ymin,ymax,RGB_FromInternal(0, context.depth)); // GraphContext_DrawHLine(&context,SCREEN_WIDTH/2,SCREEN_WIDTH,yPos, // RGB_FromInternal(0, context.depth)); GraphContext_DrawVLine(&context,80,ymin,ymax, RGB_FromInternal(0, context.depth)); //set the unit if (size >= 10000) { size /= 1000; wcscpy((wchar_t*)unit, L"k"); utf16_strcat(unit, (UTF16*)L"m"); } else { wcscpy((wchar_t*)unit, L"m"); } size /= 10; //cause it's only 1cm long. utf16_itoa(size,label); utf16_strncat(label, unit, 2); int a = utf16_strlen(label); //print the distances txtPos.x = 12 + (6-a)/2.0 * 70/6.0; // txtPos.x = 15; GraphContext_DrawText(&context, (TFont *)context.font, label, &txtPos, RGB_FromInternal(0, context.depth)); }
void utf16convert (const char* source, std::string &destin) { int const len = utf16_strlen(source); destin.clear(); destin.reserve(len); int index=0; while (index < len) { //destin << source [index*2]; destin.push_back(source [index*2]); index++; } }
/* parse an EFI_LOAD_OPTION, as described above */ static void parse_load_option(struct load_option *lo, void *ptr) { lo->attributes = *(u32 *)ptr; ptr += sizeof(u32); lo->file_path_length = *(u16 *)ptr; ptr += sizeof(u16); lo->label = ptr; ptr += (utf16_strlen(lo->label) + 1) * 2; lo->file_path = ptr; ptr += lo->file_path_length; lo->optional_data = ptr; }
const char16_t *calchook_ndls_run(void *, void *, unsigned int argc, char16_t *argv[]) { if(argc == 0) return u"\"Missing Argument!\""; unsigned int i; char *argv_prgm[argc]; for(i = 0; i < argc; ++i) { size_t len = utf16_strlen(reinterpret_cast<uint16_t*>(argv[i])); argv_prgm[i] = new char[2*len + 1]; if(!argv_prgm[i]) { // Free already allocated argv_prgm for(unsigned int j = 0; j < i; ++j) delete[] argv_prgm[j]; return u"\"Out of memory!\""; } // Strip quotes from strings if((*argv[i] == '"' && *(argv[i]+len-1) == '"') || (*argv[i] == '\'' && *(argv[i]+len-1) == '\'')) { *(argv[i]+len-1) = 0; utf162ascii(argv_prgm[i], reinterpret_cast<uint16_t*>(argv[i] + 1), 2*len); } else utf162ascii(argv_prgm[i], reinterpret_cast<uint16_t*>(argv[i]), 2*len); } int ret = ld_exec_with_args(argv_prgm[0], argc - 1, argv_prgm + 1, nullptr); // Free argv_prgm for(unsigned int j = 0; j < i; ++j) delete[] argv_prgm[j]; // The return value is never freed, so use a static buffer static char16_t ret16[16]; static char retascii[16]; snprintf(retascii, 16, "%d", ret); ascii2utf16(ret16, retascii, 16); return ret16; }
void romfs_visit_dir(romfs_context* ctx, u32 diroffset, u32 depth, u32 actions, const oschar_t* rootpath) { u32 siblingoffset; u32 childoffset; u32 fileoffset; oschar_t* currentpath; romfs_direntry* entry = &ctx->direntry; if (!romfs_dirblock_readentry(ctx, diroffset, entry)) return; // fprintf(stdout, "%08X %08X %08X %08X %08X ", // getle32(entry->parentoffset), getle32(entry->siblingoffset), getle32(entry->childoffset), // getle32(entry->fileoffset), getle32(entry->weirdoffset)); // fwprintf(stdout, L"%ls\n", entry->name); if (rootpath && os_strlen(rootpath)) { if (utf16_strlen((const utf16char_t*)entry->name) > 0) currentpath = os_AppendUTF16StrToPath(rootpath, (const utf16char_t*)entry->name); else // root dir, use the provided extract path instead of the empty root name. currentpath = os_CopyStr(rootpath); if (currentpath) { os_makedir(currentpath); } else { fputs("Error creating directory in root ", stderr); os_fputs(rootpath, stderr); fputs("\n", stderr); return; } } else { currentpath = os_CopyConvertUTF16Str((const utf16char_t*)entry->name); if (settings_get_list_romfs_files(ctx->usersettings)) { u32 i; for(i=0; i<depth; i++) printf(" "); os_fputs(currentpath, stdout); fputs("\n", stdout); } free(currentpath); currentpath = NULL; } siblingoffset = getle32(entry->siblingoffset); childoffset = getle32(entry->childoffset); fileoffset = getle32(entry->fileoffset); if (fileoffset != (~0)) romfs_visit_file(ctx, fileoffset, depth+1, actions, currentpath); if (childoffset != (~0)) romfs_visit_dir(ctx, childoffset, depth+1, actions, currentpath); if (siblingoffset != (~0)) romfs_visit_dir(ctx, siblingoffset, depth, actions, rootpath); free(currentpath); }
CalchookReg(const char16_t *name, calchook_callback callback) : callback(callback) { this->length = utf16_strlen(reinterpret_cast<const uint16_t*>(name)) - 1; this->name = new char16_t[this->length + 1]; memcpy(this->name, name, 2*this->length + 2); }