Ignore:
Timestamp:
Jun 30, 1999, 3:25:28 PM (26 years ago)
Author:
sandervl
Message:

Unicode changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/unicode.cpp

    r238 r250  
    1 /* $Id: unicode.cpp,v 1.9 1999-06-28 16:59:17 sandervl Exp $ */
     1/* $Id: unicode.cpp,v 1.10 1999-06-30 13:25:01 sandervl Exp $ */
    22
    33/*
     
    88 * Copyright 1999 Achim Hasenmueller
    99 * Copyright 1999 Christoph Bratschi
     10 *
     11 * Read comments about the implementation before using these functions
     12 * Attention: length values include terminating 0
    1013 */
    1114#include <os2win.h>
     
    107110}
    108111//******************************************************************************
     112// unilen: length of astring buffer (including 0 terminator)
     113// returns string length
    109114//******************************************************************************
    110115int WIN32API UnicodeToAsciiN(WCHAR *ustring, char *astring, int unilen)
     
    118123  char    * out_buf;
    119124
    120   if(ustring == NULL)
    121     return(NULL);
     125  if (ustring == NULL)
     126  {
     127    if (astring != NULL && unilen > 0) astring[0] = 0;
     128    return 0;
     129  }
     130
     131  if (astring == NULL || unilen <= 0) return 0;
    122132
    123133//  dprintf(("KERNEL32: UnicodeToAsciiN\n"));
    124134  if (getUconvObject())
    125135  {
    126     uni_chars_left = unilen + 1;
    127     out_bytes_left = uni_chars_left;
     136    if (unilen == 1)
     137    {
     138      astring[0] = 0;
     139      return 0; //no data
     140    }
     141
     142    uni_chars_left = unilen; //elements
     143    out_bytes_left = unilen; //size in bytes
    128144    in_buf  = (UniChar*)ustring;
    129145    out_buf = astring;
     
    132148                         (void**)&out_buf, &out_bytes_left,
    133149                         &num_subs);
     150
    134151//    dprintf(("KERNEL32: UnicodeToAsciiN(%d) '%s'\n", rc, astring ));
    135   }
    136   else
    137    /* idiots unicode conversion :) */
    138    for(i = 0; i < unilen; i++)
    139      astring[i] = (char)ustring[i];
    140 
    141   astring[unilen] = 0; // @@@PH: 1999/06/09 fix - always terminate string
    142 
    143   return(unilen);
    144 }
    145 //******************************************************************************
     152  } else
     153  {
     154    /* idiots unicode conversion :) */
     155    for(i = 0; i < unilen-1; i++)
     156    {
     157      astring[i] = (ustring[i] > 255) ? (char)20 : (char)ustring[i]; //CB: handle invalid characters as space
     158      if (ustring[i] == 0) return i; //asta la vista, baby
     159    }
     160  }
     161
     162  astring[unilen-1] = 0; // @@@PH: 1999/06/09 fix - always terminate string
     163
     164  return(unilen-1);
     165}
     166//******************************************************************************
     167// Converts unicode string to ascii string
     168// returns length of ascii string
    146169//******************************************************************************
    147170int WIN32API UnicodeToAscii(WCHAR *ustring, char *astring)
    148171{
    149172  /* forward to function with len parameter */
    150   return UnicodeToAsciiN(ustring, astring, UniStrlen((UniChar*)ustring));
    151 }
    152 //******************************************************************************
     173  return UnicodeToAsciiN(ustring, astring, UniStrlen((UniChar*)ustring)+1); //end included
     174}
     175//******************************************************************************
     176// Converts unicode string to ascii string
     177// returns pointer to ascii string
    153178//******************************************************************************
    154179char * WIN32API UnicodeToAsciiString(WCHAR *ustring)
     
    171196}
    172197//******************************************************************************
     198// asciilen: max length of unicode buffer (including end 0)
    173199//******************************************************************************
    174200void WIN32API AsciiToUnicodeN(char *ascii, WCHAR *unicode, int asciilen)
     
    187213           unicode));
    188214
    189   //CB: no input, set at least 0
     215  //CB: no input, set at least terminator
    190216  if (ascii == NULL)
    191217  {
     
    199225  if (getUconvObject())
    200226  {
     227    if (asciilen == 1)
     228    {
     229       unicode[0] = 0;
     230       return;
     231    }
     232
    201233    in_buf        = ascii;
    202     in_bytes_left = asciilen;
     234    in_bytes_left = asciilen; //buffer size in bytes
    203235    out_buf = (UniChar*)unicode;
    204236
    205     uni_chars_left = in_bytes_left +1;
     237    uni_chars_left = asciilen; //elements
    206238    dprintf(("KERNEL32: AsciiToUnicode %d\n", in_bytes_left));
    207239
     
    210242                        &out_buf,        &uni_chars_left,
    211243                        &num_subs );
     244
     245    //if (rc != ULS_SUCCESS && in_bytes_left > 0) //CB: never the case during my tests
     246    //   dprintf(("KERNEL32: AsciiToUnicode failed, %d bytes left!\n",in_bytes_left));
     247
    212248  } else
    213   {
    214     for(i=0;
    215         i < asciilen;
    216         i++)
     249  { //poor man's conversion
     250
     251    for(i = 0;i < asciilen-1;i++)
     252    {
    217253      unicode[i] = ascii[i];
    218   }
    219 
    220   unicode[asciilen] = 0;
    221 //SvL: Messes up the heap
    222 //  unicode[len+1] = 0; /* @@@PH 98/06/07 */
    223 }
    224 //******************************************************************************
     254      if (ascii[i] == 0) return; //work done
     255    }
     256  }
     257
     258  unicode[asciilen-1] = 0;
     259}
     260//******************************************************************************
     261// Copies the full string from ascii to unicode
    225262//******************************************************************************
    226263void WIN32API AsciiToUnicode(char *ascii, WCHAR *unicode)
     
    228265  /* achimha for security, strlen might trap if garbage in */
    229266  /* @@@PH 98/06/07 */
    230   if ( (ascii   == NULL) ||                     /* garbage in, garbage out ! */
    231        (unicode == NULL) )
     267  if (ascii == NULL)
     268  {
     269    if (unicode != NULL) unicode[0] = 0; //CB: set at least end
    232270    return;
     271  }
     272
     273  if (unicode == NULL) return;  /* garbage in, garbage out ! */
     274
    233275  /* forward to call with length parameter */
    234   AsciiToUnicodeN(ascii, unicode, strlen(ascii));
    235 }
    236 
    237 
     276  AsciiToUnicodeN(ascii, unicode, strlen(ascii)+1); //end included
     277}
     278
     279
     280
Note: See TracChangeset for help on using the changeset viewer.