3.7 Example: NWLocalToUnicode with Buffer Overflow

   /* exul2u_1.c  -  NWLocalToUnicode with buffer overflow example */ 
   #define NWL_EXCLUDE_TIME 
   #define NWL_EXCLUDE_FILE 
   #include <nwlocale.h> 
   #include <unicode.h> 
   #include <string.h> 
   #include <assert.h> 
   void main() 
   { 
      LCONV lconv; 
      nint err; 
      nptr h = NULL; 
      nuint8 src[5] = {’a’, ’b’, ’c’, ’d’, 0};   /* Local input string */ 
      unicode dest[3];    /* This size is too small to hold the result! */ 
      nuint8 noMap = 0;   /* Use the default replacement char. */ 
      nuint len; 
   
      /* Get the country ID and code page from the operating system. */ 
      NWLlocaleconv(&lconv); 
      err = NWInitUnicodeTables(lconv.country_id, lconv.code_page); 
      assert (err == 0); 
      err = NWGetLocalToUnicodeHandle(&h); 
      assert (err == 0); 
      err = NWLocalToUnicode(h, dest, sizeof(dest)/sizeof(unicode), src, 
                             noMap, &len); 
      assert (err == 0); 
   
   /*   Win32 clients call Windows MultiByteToWideChar. 
        If buffer overflows, it does not terminate the output string, 
        and returns zero length. 
        It ignores noMap and uses the windows default replacement char.
   */ 
   
   #ifdef WIN32 
      assert (unincmp(dest, L"abc", 3) == 0); 
      assert (len == 0); 
   
   /*   Other platforms terminate the string and return the 
        number of chars written. */ 
   
   #else 
      assert (unicmp(dest, L"ab") == 0); 
      assert (len == 3);            /* 2 uni chars and a null were written */ 
   #endif 
      NWFreeUnicodeTables(); 
   }