3.9 Example: NWUnicodeToLocal with Buffer Overflow

   /* exuu2l_1.c  -  NWUnicodeToLocal 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; 
      unicode src[5] = {’a’, ’b’, ’c’, ’d’, 0}; /* Unicode input string */ 
      nuint8 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 = NWGetUnicodeToLocalHandle(&h); 
      assert (err == 0); 
      err = NWUnicodeToLocal(h, dest, sizeof(dest), src, noMap, &len); 
      assert (err == 0); 
   
   /*   Win32 clients call Windows WideCharToMultiByte. 
        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 (strncmp(dest, "abc", 3) == 0); 
      assert (len == 0); 
   
   /* Other platforms terminate the string and return the 
      number of chars written.
   */ 
   
   #else 
      assert (strcmp(dest, "ab") == 0); 
      assert (len == 3);            /* 2 bytes and a null were written */ 
   #endif 
      NWFreeUnicodeTables(); 
   }