/** * g_freopen: * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows) * @mode: a string describing the mode in which the file should be * opened * @stream: an existing stream which will be reused, or %NULL * * A wrapper for the POSIX freopen() function. The freopen() function * opens a file and associates it with an existing stream. * * See the C library manual for more details about freopen(). * * Returns: A <type>FILE</type> pointer if the file was successfully * opened, or %NULL if an error occurred. * * Since: 2.6 */ FILE * ws_stdio_freopen (const gchar *filename, const gchar *mode, FILE *stream) { wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); wchar_t *wmode; FILE *retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return NULL; } wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL); if (wmode == NULL) { g_free (wfilename); errno = EINVAL; return NULL; } retval = _wfreopen (wfilename, wmode, stream); save_errno = errno; g_free (wfilename); g_free (wmode); errno = save_errno; return retval; }
static gunichar2* marshal_bstr_alloc(const gchar* str) { #ifdef WIN32 gunichar2* ret = NULL; gunichar2* temp = NULL; temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL); ret = SysAllocString (temp); g_free (temp); return ret; #else gchar* ret = NULL; int slen = strlen (str); gunichar2* temp; /* allocate len + 1 utf16 characters plus 4 byte integer for length*/ ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32)); if (ret == NULL) return NULL; temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL); memcpy (ret + sizeof(guint32), temp, slen * sizeof(gunichar2)); * ((guint32 *) ret) = slen * sizeof(gunichar2); ret [4 + slen * sizeof(gunichar2)] = 0; ret [5 + slen * sizeof(gunichar2)] = 0; return (gunichar2*)(ret + 4); #endif }
void subsurface_set_conf(char *name, const char *value) { /* since we are using the pointer 'value' as both an actual * pointer to the string setting and as a way to pass the * numbers 0 and 1 to this function for booleans, one of the * calls to RegSetValueEx needs to pass &value (when we want * to pass the boolean value), the other one passes value (the * address of the string. */ int wlen; wchar_t *wname = NULL, *wstring = NULL; wname = (wchar_t *)g_utf8_to_utf16(name, -1, NULL, NULL, NULL); if (!wname) return; wlen = g_utf8_strlen((char *)value, -1); wstring = (wchar_t *)g_utf8_to_utf16((char *)value, -1, NULL, NULL, NULL); if (!wstring || !wlen) { free(wname); return; } RegSetValueExW(hkey, (LPCWSTR)wname, 0, REG_SZ, (const BYTE *)wstring, wlen * sizeof(wchar_t)); free(wstring); free(wname); }
/* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't * use the 'file pointer' opened in glib (and returned from this function) * in this library, as they may have unrelated C runtimes. */ static FILE * gst_fopen (const gchar * filename, const gchar * mode) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); wchar_t *wmode; FILE *retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return NULL; } wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL); if (wmode == NULL) { g_free (wfilename); errno = EINVAL; return NULL; } retval = _wfopen (wfilename, wmode); save_errno = errno; g_free (wfilename); g_free (wmode); errno = save_errno; return retval; #else return fopen (filename, mode); #endif }
static gboolean file_filter_to_win32 (GtkFileFilter *filter, COMDLG_FILTERSPEC *spec) { const char *name; char **patterns; char *pattern_list; patterns = _gtk_file_filter_get_as_patterns (filter); if (patterns == NULL) return FALSE; pattern_list = g_strjoinv (";", patterns); g_strfreev (patterns); name = gtk_file_filter_get_name (filter); if (name == NULL) name = pattern_list; spec->pszName = g_utf8_to_utf16 (name, -1, NULL, NULL, NULL); spec->pszSpec = g_utf8_to_utf16 (pattern_list, -1, NULL, NULL, NULL); g_free (pattern_list); return TRUE; }
/* fopen() with utf8 filename and mode, setting errno. */ FILE * vips__fopen( const char *filename, const char *mode ) { FILE *fp; #ifdef OS_WIN32 wchar_t *path16, *mode16; if( !(path16 = (wchar_t *) g_utf8_to_utf16( filename, -1, NULL, NULL, NULL )) ) { errno = EACCES; return( NULL ); } if( !(mode16 = (wchar_t *) g_utf8_to_utf16( mode, -1, NULL, NULL, NULL )) ) { g_free( path16 ); errno = EACCES; return( NULL ); } fp = _wfopen( path16, mode16 ); g_free( path16 ); g_free( mode16 ); #else /*!OS_WIN32*/ fp = fopen( filename, mode ); #endif return( fp ); }
/** * mono_unicode_from_external: * @in: pointers to the buffer. * @bytes: number of bytes in the string. * * Tries to turn a NULL-terminated string into UTF16. * * First, see if it's valid UTF8, in which case just turn it directly * into UTF16. Next, run through the colon-separated encodings in * MONO_EXTERNAL_ENCODINGS and do an iconv conversion on each, * returning the first successful conversion to UTF16. If no * conversion succeeds, return NULL. * * Callers must free the returned string if not NULL. bytes holds the number * of bytes in the returned string, not including the terminator. */ gunichar2 * mono_unicode_from_external (const gchar *in, gsize *bytes) { gchar *res=NULL; gchar **encodings; const gchar *encoding_list; int i; glong lbytes; if(in==NULL) { return(NULL); } encoding_list=g_getenv ("MONO_EXTERNAL_ENCODINGS"); if(encoding_list==NULL) { encoding_list = ""; } encodings=g_strsplit (encoding_list, ":", 0); for(i=0;encodings[i]!=NULL; i++) { /* "default_locale" is a special case encoding */ if(!strcmp (encodings[i], "default_locale")) { gchar *utf8=g_locale_to_utf8 (in, -1, NULL, NULL, NULL); if(utf8!=NULL) { res=(gchar *) g_utf8_to_utf16 (utf8, -1, NULL, &lbytes, NULL); *bytes = (gsize) lbytes; } g_free (utf8); } else { /* Don't use UTF16 here. It returns the <FF FE> prepended to the string */ res = g_convert (in, strlen (in), "UTF8", encodings[i], NULL, bytes, NULL); if (res != NULL) { gchar *ptr = res; res = (gchar *) g_utf8_to_utf16 (res, -1, NULL, &lbytes, NULL); *bytes = (gsize) lbytes; g_free (ptr); } } if(res!=NULL) { g_strfreev (encodings); *bytes *= 2; return((gunichar2 *)res); } } g_strfreev (encodings); if(g_utf8_validate (in, -1, NULL)) { gunichar2 *unires=g_utf8_to_utf16 (in, -1, NULL, (glong *)bytes, NULL); *bytes *= 2; return(unires); } return(NULL); }
/** * g_rename: * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows) * @newfilename: a pathname in the GLib file name encoding * * A wrapper for the POSIX rename() function. The rename() function * renames a file, moving it between directories if required. * * See your C library manual for more details about how rename() works * on your system. It is not possible in general on Windows to rename * a file that is open to some process. * * Returns: 0 if the renaming succeeded, -1 if an error occurred * * Since: 2.6 */ int g_rename (const gchar *oldfilename, const gchar *newfilename) { #ifdef G_OS_WIN32 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL); wchar_t *wnewfilename; int retval; int save_errno = 0; if (woldfilename == NULL) { errno = EINVAL; return -1; } wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL); if (wnewfilename == NULL) { g_free (woldfilename); errno = EINVAL; return -1; } if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING)) retval = 0; else { retval = -1; switch (GetLastError ()) { #define CASE(a,b) case ERROR_##a: save_errno = b; break CASE (FILE_NOT_FOUND, ENOENT); CASE (PATH_NOT_FOUND, ENOENT); CASE (ACCESS_DENIED, EACCES); CASE (NOT_SAME_DEVICE, EXDEV); CASE (LOCK_VIOLATION, EACCES); CASE (SHARING_VIOLATION, EACCES); CASE (FILE_EXISTS, EEXIST); CASE (ALREADY_EXISTS, EEXIST); #undef CASE default: save_errno = EIO; } } g_free (woldfilename); g_free (wnewfilename); errno = save_errno; return retval; #else return rename (oldfilename, newfilename); #endif }
SMKY_STATUS SmkyManufacturerDatabase::dbCallback( SMKY_REQ_MODE eMdbRequestType, /**< I - MDB request type. Should be one of the values defined above */ uint16_t wWordLen, /**< I - word length */ uint16_t wMaxWordLen, /**< I - maximum word length */ uint16_t *psBuildTxtBuf, /**< O - word to return */ uint16_t *pwActWordLen, /**< O - length of the returned word */ uint32_t *pdwWordListIdx) /**< I/O - MDB word list index */ { //g_debug("In MDB Callback: idx=%lu, type=%u (%s)", *pdwWordListIdx, eMdbRequestType, (eMdbRequestType == SMKY_REQ_MODE_GETEXACTWORDS) ? "exact words" : (eMdbRequestType == SMKY_REQ_MODE_GETALLWORDS) ? "all words" : "neither exact nor all words"); if (eMdbRequestType != SMKY_REQ_MODE_GETEXACTWORDS && eMdbRequestType != SMKY_REQ_MODE_GETALLWORDS) return SMKY_STATUS_ERROR; // first and follow-up callbacks: return words one-by-one, using *pdwWordListIdx as the index of the word to return in the list built above while (m_lastHunspellResult && (int) *pdwWordListIdx < m_lastHunspellResultCount) { const char * word = m_lastHunspellResult[*pdwWordListIdx]; *pdwWordListIdx += 1; if (word && *word) { glong len(0); auto_g_free_array<gunichar2> utf16 = g_utf8_to_utf16(word, -1, NULL, &len, NULL); if (utf16) { memcpy(psBuildTxtBuf, utf16, len * sizeof(uint16_t)); *pwActWordLen = len; DEBUG_CALLBACK("SmkyManufacturerDatabase::dbCallback: returning Hunspell word: '%s' for '%s' (%d-%d)", word, m_lastHunspellQuery.c_str(), wWordLen, wMaxWordLen); return SMKY_STATUS_NONE; } else g_warning("SmkyManufacturerDatabase::dbCallback: Hunspell returned a word that can't be converted to utf16: %s", word); } } while (*pdwWordListIdx < m_lastFirstLastLetterResults.size()) { glong len(0); const char * word = m_lastFirstLastLetterResults[*pdwWordListIdx]; auto_g_free_array<gunichar2> utf16 = g_utf8_to_utf16(word, -1, NULL, &len, NULL); *pdwWordListIdx += 1; if (utf16) { memcpy(psBuildTxtBuf, utf16, len * sizeof(uint16_t)); *pwActWordLen = len; DEBUG_CALLBACK("SmkyManufacturerDatabase::dbCallback: returning first-last letter word: '%s' (%d-%d)", word, wWordLen, wMaxWordLen); return SMKY_STATUS_NONE; } else g_warning("SmkyManufacturerDatabase::dbCallback: m_lastFirstLastLetterResults contains a word that can't be converted to utf16: %s", word); } return SMKY_STATUS_ERROR; // we're done }
/** * Set the string value of a key/name registry entry */ bool RegistryTool::setStringValue(const Glib::ustring &keyNameArg, const Glib::ustring &valueName, const Glib::ustring &value) { Glib::ustring keyName = keyNameArg; bool ret = false; HKEY rootKey = HKEY_LOCAL_MACHINE; //default root //Trim out the root key if necessary for (KeyTableEntry *entry = keyTable; entry->key; entry++) { if (keyName.compare(0, entry->strlen, entry->str)==0) { rootKey = entry->key; keyName = keyName.substr(entry->strlen); } } //printf("trimmed string: '%s'\n", keyName.c_str()); //Get or create the key gunichar2 *keyw = g_utf8_to_utf16(keyName.data(), -1, 0,0,0); gunichar2 *valuenamew = g_utf8_to_utf16(valueName.data(), -1, 0,0,0); HKEY key; if (RegCreateKeyExW(rootKey, (WCHAR*) keyw, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key, NULL)) { fprintf(stderr, "RegistryTool: Could not create the registry key '%s'\n", keyName.c_str()); goto fail; } // Set the value if (RegSetValueExW(key, (WCHAR*) valuenamew, 0, REG_SZ, (LPBYTE) value.data(), (DWORD) (value.size() + 1))) { fprintf(stderr, "RegistryTool: Could not set the value '%s'\n", value.c_str()); goto failkey; } ret = true; failkey: RegCloseKey(key); fail: g_free(keyw); g_free(valuenamew); return ret; }
void * frida_winjector_helper_factory_spawn (const gchar * path, const gchar * parameters, FridaWinjectorPrivilegeLevel level, GError ** error) { HANDLE process_handle; SHELLEXECUTEINFOW ei = { 0, }; WCHAR * path_utf16; WCHAR * parameters_utf16; CoInitializeEx (NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); ei.cbSize = sizeof (ei); ei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE | SEE_MASK_WAITFORINPUTIDLE; if (level == FRIDA_WINJECTOR_PRIVILEGE_LEVEL_ELEVATED) ei.lpVerb = L"runas"; else ei.lpVerb = L"open"; path_utf16 = (WCHAR *) g_utf8_to_utf16 (path, -1, NULL, NULL, NULL); ei.lpFile = path_utf16; parameters_utf16 = (WCHAR *) g_utf8_to_utf16 (parameters, -1, NULL, NULL, NULL); ei.lpParameters = parameters_utf16; ei.nShow = SW_HIDE; if (ShellExecuteExW (&ei)) { process_handle = ei.hProcess; } else { process_handle = NULL; g_set_error (error, FRIDA_ERROR, FRIDA_ERROR_PERMISSION_DENIED, "Unable to spawn helper executable at '%s': 0x%08lx", path, GetLastError ()); } g_free (parameters_utf16); g_free (path_utf16); CoUninitialize (); return process_handle; }
int ug_copy_file (const gchar *src_file_utf8, const gchar *new_file_utf8) { gboolean retval; gunichar2* src_file_wcs; gunichar2* new_file_wcs; src_file_wcs = g_utf8_to_utf16 (src_file_utf8, -1, NULL, NULL, NULL); new_file_wcs = g_utf8_to_utf16 (new_file_utf8, -1, NULL, NULL, NULL); retval = CopyFileW (src_file_wcs, new_file_wcs, FALSE); g_free (src_file_wcs); g_free (new_file_wcs); if (retval == 0) return -1; return 0; }
/** * g_win32_locale_filename_from_utf8: * @utf8filename: a UTF-8 encoded filename. * * Converts a filename from UTF-8 to the system codepage. * * On NT-based Windows, on NTFS file systems, file names are in * Unicode. It is quite possible that Unicode file names contain * characters not representable in the system codepage. (For instance, * Greek or Cyrillic characters on Western European or US Windows * installations, or various less common CJK characters on CJK Windows * installations.) * * In such a case, and if the filename refers to an existing file, and * the file system stores alternate short (8.3) names for directory * entries, the short form of the filename is returned. Note that the * "short" name might in fact be longer than the Unicode name if the * Unicode name has very short pathname components containing * non-ASCII characters. If no system codepage name for the file is * possible, %NULL is returned. * * The return value is dynamically allocated and should be freed with * g_free() when no longer needed. * * Returns: The converted filename, or %NULL on conversion * failure and lack of short names. * * Since: 2.8 */ gchar * g_win32_locale_filename_from_utf8 (const gchar *utf8filename) { gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL); if (retval == NULL) { /* Conversion failed, so convert to wide chars, check if there * is a 8.3 version, and use that. */ wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL); if (wname != NULL) { wchar_t wshortname[MAX_PATH + 1]; if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname))) { gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL); retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL); g_free (tem); } g_free (wname); } } return retval; }
/* * Used to get printer device information from a printer name. This * can fail if the user has no right to read printer properties, so * this function can return NULL. */ GtkPrintWin32Devnames * gtk_print_win32_devnames_from_printer_name (const char *printer_name) { HANDLE hprinter; gunichar2* win32_printer_name; GtkPrintWin32Devnames *devnames; win32_printer_name = g_utf8_to_utf16 (printer_name, -1, NULL, NULL, NULL); if (OpenPrinterW (win32_printer_name, &hprinter, NULL)) { DWORD needed; PRINTER_INFO_2W* printer_info; GetPrinterW (hprinter, 2, NULL, 0, &needed); printer_info = (PRINTER_INFO_2W* )g_malloc ((gsize) needed); GetPrinterW (hprinter, 2, (LPBYTE) printer_info, needed, &needed); devnames = g_new (GtkPrintWin32Devnames, 1); devnames->driver = g_utf16_to_utf8 (printer_info->pDriverName, -1, NULL, NULL, NULL); devnames->device = g_strdup (printer_name); devnames->output = g_utf16_to_utf8 (printer_info->pPortName, -1, NULL, NULL, NULL); devnames->flags = 0; ClosePrinter (hprinter); g_free (printer_info); } else { /* Could not open printer */ devnames = NULL; } g_free (win32_printer_name); return devnames; }
/** * g_utime: * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows) * @utb: a pointer to a struct utimbuf. * * A wrapper for the POSIX utime() function. The utime() function * sets the access and modification timestamps of a file. * * See your C library manual for more details about how utime() works * on your system. * * Returns: 0 if the operation was successful, -1 if an error * occurred * * Since: 2.18 */ int g_utime (const gchar *filename, struct utimbuf *utb) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return -1; } retval = _wutime (wfilename, (struct _utimbuf*) utb); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #else return utime (filename, utb); #endif }
/* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't * use the 'file descriptor' opened in glib (and returned from this function) * in this library, as they may have unrelated C runtimes. */ static int gst_open (const gchar * filename, int flags, int mode) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return -1; } retval = _wopen (wfilename, flags, mode); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #elif defined (__BIONIC__) return open (filename, flags | O_LARGEFILE, mode); #else return open (filename, flags, mode); #endif }
// http://doc.qt.nokia.com/4.6/datastreamformat.html // A QString has the length in the first 4 bytes, then the string in UTF-16 encoding // Has to be stored as big endian! static gchar* char2qstring(const gchar* in, gsize* size) { glong read, written; GError* error = NULL; gunichar2* out = g_utf8_to_utf16(in, -1, &read, &written, &error); if(error) { dt_print(DT_DEBUG_PWSTORAGE,"[pwstorage_kwallet] ERROR: error converting string: %s\n", error->message); g_error_free(error); return NULL; } glong i; for(i=0; i<written; ++i) { out[i] = g_htons(out[i]); } guint bytes = sizeof(gunichar2)*written; guint BE_bytes = GUINT_TO_BE(bytes); *size = sizeof(guint)+bytes; gchar* result = g_malloc(*size); memcpy(result, &BE_bytes, sizeof(guint)); memcpy(result+sizeof(guint), out, bytes); return result; }
void _frida_fruity_host_session_provider_extract_details_for_device_with_udid (const char * udid, char ** name, FridaImageData ** icon, GError ** error) { gboolean result = FALSE; WCHAR * udid_utf16 = NULL; FridaMobileDeviceInfo * mdev = NULL; FridaImageDeviceInfo * idev = NULL; FridaImageData * idev_icon; udid_utf16 = (WCHAR *) g_utf8_to_utf16 (udid, -1, NULL, NULL, NULL); mdev = find_mobile_device_by_udid (udid_utf16); if (mdev == NULL) goto beach; idev = find_image_device_by_location (mdev->location); if (idev == NULL) goto beach; idev_icon = _frida_image_data_from_resource_url (idev->icon_url, FRIDA_ICON_SMALL); if (idev_icon == NULL) goto beach; *name = g_utf16_to_utf8 ((gunichar2 *) idev->friendly_name, -1, NULL, NULL, NULL); *icon = idev_icon; result = TRUE; beach: if (!result) g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "Failed to extract details for device by UDID"); frida_image_device_info_free (idev); frida_mobile_device_info_free (mdev); g_free (udid_utf16); }
/** * g_chdir: * @path: a pathname in the GLib file name encoding (UTF-8 on Windows) * * A wrapper for the POSIX chdir() function. The function changes the * current directory of the process to @path. * * See your C library manual for more details about chdir(). * * Returns: 0 on success, -1 if an error occurred. * * Since: 2.8 */ int g_chdir (const gchar *path) { #ifdef G_OS_WIN32 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL); int retval; int save_errno; if (wpath == NULL) { errno = EINVAL; return -1; } retval = _wchdir (wpath); save_errno = errno; g_free (wpath); errno = save_errno; return retval; #else return chdir (path); #endif }
// WIN32 static const gchar* create_fsel_3(const gchar *dirname, const gchar *filename, const gchar *ext, gboolean save) { #ifdef WIN32 OPENFILENAME_MAYALIAS o; char lpstrFile[2048] = "\0"; char lpstrFilter[512]; char *p; gchar **sarray; int i, n; int have_widechar = G_WIN32_HAVE_WIDECHAR_API(); void *sdirname; // clear structure memset (&o, 0, sizeof(OPENFILENAME)); // set default filename if(filename) { void *temp; if (have_widechar) { temp = g_utf8_to_utf16(filename,-1,NULL,NULL,NULL); if(!temp) return NULL; wcsncpy((wchar_t *)lpstrFile, temp, sizeof(lpstrFile)>>1); } else { temp = g_locale_from_utf8(filename,-1,NULL,NULL,NULL); if(!temp) return NULL; strncpy(lpstrFile, temp, sizeof(lpstrFile)); } g_free(temp); }
/** * g_open: * @filename: (type filename): a pathname in the GLib file name encoding * (UTF-8 on Windows) * @flags: as in open() * @mode: as in open() * * A wrapper for the POSIX open() function. The open() function is * used to convert a pathname into a file descriptor. * * On POSIX systems file descriptors are implemented by the operating * system. On Windows, it's the C library that implements open() and * file descriptors. The actual Win32 API for opening files is quite * different, see MSDN documentation for CreateFile(). The Win32 API * uses file handles, which are more randomish integers, not small * integers like file descriptors. * * Because file descriptors are specific to the C library on Windows, * the file descriptor returned by this function makes sense only to * functions in the same C library. Thus if the GLib-using code uses a * different C library than GLib does, the file descriptor returned by * this function cannot be passed to C library functions like write() * or read(). * * See your C library manual for more details about open(). * * Returns: a new file descriptor, or -1 if an error occurred. * The return value can be used exactly like the return value * from open(). * * Since: 2.6 */ int g_open (const gchar *filename, int flags, int mode) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return -1; } retval = _wopen (wfilename, flags, mode); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #else int fd; do fd = open (filename, flags, mode); while (G_UNLIKELY (fd == -1 && errno == EINTR)); return fd; #endif }
CString TextEncoding::encode(const UChar* characters, size_t length, UnencodableHandling handling) const { if (!m_name) return CString(); if (!length) return ""; #if USE(ICU_UNICODE) // FIXME: What's the right place to do normalization? // It's a little strange to do it inside the encode function. // Perhaps normalization should be an explicit step done before calling encode. const UChar* source = characters; size_t sourceLength = length; Vector<UChar> normalizedCharacters; UErrorCode err = U_ZERO_ERROR; if (unorm_quickCheck(source, sourceLength, UNORM_NFC, &err) != UNORM_YES) { // First try using the length of the original string, since normalization to NFC rarely increases length. normalizedCharacters.grow(sourceLength); int32_t normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, normalizedCharacters.data(), length, &err); if (err == U_BUFFER_OVERFLOW_ERROR) { err = U_ZERO_ERROR; normalizedCharacters.resize(normalizedLength); normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, normalizedCharacters.data(), normalizedLength, &err); } ASSERT(U_SUCCESS(err)); source = normalizedCharacters.data(); sourceLength = normalizedLength; } return newTextCodec(*this)->encode(source, sourceLength, handling); #elif USE(QT4_UNICODE) QString str(reinterpret_cast<const QChar*>(characters), length); str = str.normalized(QString::NormalizationForm_C); return newTextCodec(*this)->encode(reinterpret_cast<const UChar *>(str.utf16()), str.length(), handling); #elif USE(GLIB_UNICODE) GOwnPtr<char> UTF8Source; UTF8Source.set(g_utf16_to_utf8(characters, length, 0, 0, 0)); if (!UTF8Source) { // If conversion to UTF-8 failed, try with the string without normalization return newTextCodec(*this)->encode(characters, length, handling); } GOwnPtr<char> UTF8Normalized; UTF8Normalized.set(g_utf8_normalize(UTF8Source.get(), -1, G_NORMALIZE_NFC)); long UTF16Length; GOwnPtr<UChar> UTF16Normalized; UTF16Normalized.set(g_utf8_to_utf16(UTF8Normalized.get(), -1, 0, &UTF16Length, 0)); return newTextCodec(*this)->encode(UTF16Normalized.get(), UTF16Length, handling); #elif OS(WINCE) // normalization will be done by Windows CE API OwnPtr<TextCodec> textCodec = newTextCodec(*this); return textCodec.get() ? textCodec->encode(characters, length, handling) : CString(); #endif }
/* utf8 version of getenv, needed to get win32 filename paths */ char * getenv_utf8(const char *varname) { char *envvar; wchar_t *envvarw; wchar_t *varnamew; envvar = getenv(varname); /* since GLib 2.6 we need an utf8 version of the filename */ /* using the wide char version of getenv should work under all circumstances */ /* convert given varname to utf16, needed by _wgetenv */ varnamew = g_utf8_to_utf16(varname, -1, NULL, NULL, NULL); if (varnamew == NULL) { return envvar; } /* use wide char version of getenv */ envvarw = _wgetenv(varnamew); g_free(varnamew); if (envvarw == NULL) { return envvar; } /* convert value to utf8 */ envvar = g_utf16_to_utf8(envvarw, -1, NULL, NULL, NULL); /* XXX - memleak */ return envvar; }
RESULT test_utf8_seq () { const gchar *src = "\xE5\xB9\xB4\x27"; glong in_read, out_read; //gunichar2 expected [6]; GError *error = NULL; gunichar2 *dst; //printf ("got: %s\n", src); dst = g_utf8_to_utf16 (src, (glong)strlen (src), &in_read, &out_read, &error); if (error != NULL){ return error->message; } if (in_read != 4) { return FAILED ("in_read is expected to be 4 but was %d\n", in_read); } if (out_read != 2) { return FAILED ("out_read is expected to be 2 but was %d\n", out_read); } g_free (dst); return OK; }
const gchar * g_getenv (const gchar *variable) { GQuark quark; gchar *value; wchar_t dummy[2], *wname, *wvalue; int len; g_return_val_if_fail (variable != NULL, NULL); g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL); /* On Windows NT, it is relatively typical that environment * variables contain references to other environment variables. If * so, use ExpandEnvironmentStrings(). (In an ideal world, such * environment variables would be stored in the Registry as * REG_EXPAND_SZ type values, and would then get automatically * expanded before a program sees them. But there is broken software * that stores environment variables as REG_SZ values even if they * contain references to other environment variables.) */ wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL); len = GetEnvironmentVariableW (wname, dummy, 2); if (len == 0) { g_free (wname); return NULL; } else if (len == 1) len = 2; wvalue = g_new (wchar_t, len); if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1) { g_free (wname); g_free (wvalue); return NULL; } if (wcschr (wvalue, L'%') != NULL) { wchar_t *tem = wvalue; len = ExpandEnvironmentStringsW (wvalue, dummy, 2); if (len > 0) { wvalue = g_new (wchar_t, len); if (ExpandEnvironmentStringsW (tem, wvalue, len) != len) { g_free (wvalue); wvalue = tem; } else g_free (tem); }
/** * g_stat: * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows) * @buf: a pointer to a <structname>stat</structname> struct, which * will be filled with the file information * * A wrapper for the POSIX stat() function. The stat() function * returns information about a file. On Windows the stat() function in * the C library checks only the FAT-style READONLY attribute and does * not look at the ACL at all. Thus on Windows the protection bits in * the st_mode field are a fabrication of little use. * * On Windows the Microsoft C libraries have several variants of the * <structname>stat</structname> struct and stat() function with names * like "_stat", "_stat32", "_stat32i64" and "_stat64i32". The one * used here is for 32-bit code the one with 32-bit size and time * fields, specifically called "_stat32". * * In Microsoft's compiler, by default "struct stat" means one with * 64-bit time fields while in MinGW "struct stat" is the legacy one * with 32-bit fields. To hopefully clear up this messs, the gstdio.h * header defines a type GStatBuf which is the appropriate struct type * depending on the platform and/or compiler being used. On POSIX it * is just "struct stat", but note that even on POSIX platforms, * "stat" might be a macro. * * See your C library manual for more details about stat(). * * Returns: 0 if the information was successfully retrieved, -1 if an error * occurred * * Since: 2.6 */ int g_stat (const gchar *filename, GStatBuf *buf) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; int len; if (wfilename == NULL) { errno = EINVAL; return -1; } len = wcslen (wfilename); while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1])) len--; if (len > 0 && (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename)) wfilename[len] = '\0'; retval = _wstat (wfilename, buf); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #else return stat (filename, buf); #endif }
std::vector<gunichar2> BootupAnimation::convertToUtf16(const std::string& s) const { glong charsWritten = 0; gunichar2* utf16Str = g_utf8_to_utf16(s.c_str(), -1, NULL, &charsWritten, NULL); std::vector<gunichar2> res; if (!utf16Str || charsWritten <= 0) { g_warning("%s: Failed to convert banner message to utf16: %s", __PRETTY_FUNCTION__, s.c_str()); res.resize(s.size()); for (unsigned int i = 0; i < s.size(); i++) { res[i] = s[i]; } } else { res.resize(charsWritten); for (int i = 0; i < charsWritten; i++) { res[i] = utf16Str[i]; } } g_free(utf16Str); return res; }
/** * g_rmdir: * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows) * * A wrapper for the POSIX rmdir() function. The rmdir() function * deletes a directory from the filesystem. * * See your C library manual for more details about how rmdir() works * on your system. * * Returns: 0 if the directory was successfully removed, -1 if an error * occurred * * Since: 2.6 */ int g_rmdir (const gchar *filename) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return -1; } retval = _wrmdir (wfilename); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #else return rmdir (filename); #endif }
/** * g_access: * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows) * @mode: as in access() * * A wrapper for the POSIX access() function. This function is used to * test a pathname for one or several of read, write or execute * permissions, or just existence. * * On Windows, the file protection mechanism is not at all POSIX-like, * and the underlying function in the C library only checks the * FAT-style READONLY attribute, and does not look at the ACL of a * file at all. This function is this in practise almost useless on * Windows. Software that needs to handle file permissions on Windows * more exactly should use the Win32 API. * * See your C library manual for more details about access(). * * Returns: zero if the pathname refers to an existing file system * object that has all the tested permissions, or -1 otherwise or on * error. * * Since: 2.8 */ int g_access (const gchar *filename, int mode) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return -1; } #ifndef X_OK #define X_OK 1 #endif retval = _waccess (wfilename, mode & ~X_OK); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #else return access (filename, mode); #endif }
/** * g_stat: * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows) * @buf: a pointer to a <structname>stat</structname> struct, which * will be filled with the file information * * A wrapper for the POSIX stat() function. The stat() function * returns information about a file. * * See the C library manual for more details about stat(). * * Returns: 0 if the information was successfully retrieved, -1 if an error * occurred * * Since: 2.6 */ int ws_stdio_stat64 (const gchar *filename, ws_statb64 *buf) { wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; size_t len; if (wfilename == NULL) { errno = EINVAL; return -1; } len = wcslen (wfilename); while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1])) len--; if (len > 0 && (!g_path_is_absolute (filename) || len > (size_t) (g_path_skip_root (filename) - filename))) wfilename[len] = '\0'; retval = _wstati64 (wfilename, buf); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; }