Unicode
Unicode_Substr(ConstUnicode str,    // IN
               UnicodeIndex start,  // IN
               UnicodeIndex length) // IN
{
   UnicodePinIndices(str, &start, &length);

   return Util_SafeStrndup(((const char *)str) + start, length);
}
Esempio n. 2
0
static MsgList *
MsgId2MsgList(const char *idFmt)  // IN message ID and English message
{
    MsgList *m;
    const char *idp, *strp;

    /* All message strings must be prefixed by the message ID. */
    ASSERT(Msg_HasMsgID(idFmt));

    /*
     * Find the beginning of the ID (idp) and the string (strp).
     * The string should have the correct MSG_MAGIC(...)... form.
     */

    idp = idFmt + MSG_MAGIC_LEN + 1;
    strp = strchr(idp, ')') + 1;

    m = Util_SafeMalloc(sizeof *m);
    m->format = Util_SafeStrdup(strp);
    m->next = NULL;
    m->args = NULL;
    m->numArgs = 0;

    if (vmx86_debug) {
        uint32 i;
        static const char *prfx[] = {
            "msg.",    // bora/lib, VMX, ...
            "vob.",    // Vmkernel OBservation
            "vpxa.",   // VirtualCenter host agent
            "vpxd.",   // VirtualCenter server
            "hostd.",  // Host agent
            // Additional prefixes go here, but do not add "button."
        };

        for (i = 0; i < ARRAYSIZE(prfx); i++) {
            if (!Str_Strncasecmp(idp, prfx[i], strlen(prfx[i]))) {
                break;
            }
        }
        if (i >= ARRAYSIZE(prfx)) {
            Panic("%s error: Invalid msg prefix in <%s>\n", __FUNCTION__, idp);
        }
    }

    m->id = Util_SafeStrndup(idp, strp - idp - 1 /* ')' character */);

    return m;
}
Unicode
UnicodeAllocInternal(const void *buffer,      // IN
                     ssize_t lengthInBytes,   // IN
                     StringEncoding encoding, // IN
		     Bool strict)             // IN
{
   char *utf8Result = NULL;

   ASSERT(buffer != NULL);
   ASSERT(lengthInBytes >= 0);
   ASSERT(Unicode_IsEncodingValid(encoding));

   if (!strict) {
      CodeSet_GenericToGeneric(Unicode_EncodingEnumToName(encoding),
                               buffer, lengthInBytes,
			       "UTF-8", CSGTG_TRANSLIT, &utf8Result, NULL);
      return utf8Result;
   }

   switch (encoding) {
   case STRING_ENCODING_US_ASCII:
   case STRING_ENCODING_UTF8:
      if (Unicode_IsBufferValid(buffer, lengthInBytes, encoding)) {
	 utf8Result = Util_SafeStrndup(buffer, lengthInBytes);
      }
      break;
   case STRING_ENCODING_UTF16_LE:
      // utf8Result will be left NULL on failure.
      CodeSet_Utf16leToUtf8((const char *)buffer,
                            lengthInBytes,
                            &utf8Result,
                            NULL);
      break;
   default:
      CodeSet_GenericToGeneric(Unicode_EncodingEnumToName(encoding),
                               buffer, lengthInBytes,
			       "UTF-8", 0, &utf8Result, NULL);
      break;
   }

   return (Unicode)utf8Result;
}