/* ------------------------------------------------------------------------- */
void PrintUnicodeString( PCH pchBytes, ULONG length )
{
    PSZ pszOutput,
        psz;
    UniChar *puInput,
            *pu;
    size_t stIn, stOut, stSub;

    if (( puInput = (UniChar *) calloc( length+sizeof(UniChar), 1 )) == NULL ) return;
    UniStrncpy( puInput, (UniChar *) pchBytes, length / sizeof(UniChar) );
    stIn  = length / sizeof(UniChar);
    stOut = stIn * 4;
    stSub = 0;
    pszOutput = (PSZ) calloc( stOut+1, 1 );
    pu  = puInput;
    psz = pszOutput;
    if ( psz && ( UniUconvFromUcs( uconv, &pu, &stIn, (PPVOID) &psz,
                                   &stOut, &stSub ) == ULS_SUCCESS ))
        printf("%s", pszOutput );
    if ( puInput ) free( puInput );
    if ( pszOutput ) free( pszOutput );
}
Example #2
0
size_t
iconv (iconv_t conv,
       const char **in, size_t *in_left,
       char **out, size_t *out_left)
{
  int rc;
  size_t sl = *in_left, nonid;
  UniChar *ucs = (UniChar *) alloca (sl * sizeof (UniChar));
  UniChar *orig_ucs = ucs;
  size_t retval = 0;

  rc = UniUconvToUcs (conv->from, (void **)in, in_left, &ucs, &sl, &retval);
  if (rc)
    goto error;
  sl = ucs - orig_ucs;
  ucs = orig_ucs;
  /* UniUconvFromUcs will stop at first nul byte (huh? indeed?)
     while we want ALL the bytes converted.  */
#if 1
  rc = UniUconvFromUcs (conv->to, &ucs, &sl, (void **)out, out_left, &nonid);
  if (rc)
    goto error;
  retval += nonid;
#else
  while (sl)
    {
      size_t usl = 0;
      while (sl && (ucs[usl] != 0))
        usl++, sl--;
      rc = UniUconvFromUcs (conv->to, &ucs, &usl, (void **)out, out_left, &nonid);
      if (rc)
        goto error;
      retval += nonid;
      if (sl && *out_left)
        {
          *(*out)++ = 0;
          (*out_left)--;
          ucs++; sl--;
        }
    }
#endif
  return 0;

error:
  /* Convert OS/2 error code to errno.  */
  switch (rc)
  {
    case ULS_ILLEGALSEQUENCE:
      errno = EILSEQ;
      break;
    case ULS_INVALID:
      errno = EINVAL;
      break;
    case ULS_BUFFERFULL:
      errno = E2BIG;
      break;
    default:
      errno = EBADF;
      break;
  }
  return (size_t)(-1);
}