Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <stdio.h>
     30 #include <wchar.h>
     31 
     32 #include "UniquePtr.h"
     33 
     34 namespace {
     35 const size_t MBS_FAILURE = static_cast<size_t>(-1);
     36 }
     37 
     38 int swprintf(wchar_t* wcs, size_t maxlen, const wchar_t* format, ...) {
     39   va_list ap;
     40   va_start(ap, format);
     41   int result = vswprintf(wcs, maxlen, format, ap);
     42   va_end(ap);
     43   return result;
     44 }
     45 
     46 int vswprintf(wchar_t* wcs, size_t maxlen, const wchar_t* fmt, va_list ap) {
     47   mbstate_t mbstate;
     48   memset(&mbstate, 0, sizeof(mbstate));
     49 
     50   // At most, each wide character (UTF-32) can be expanded to four narrow
     51   // characters (UTF-8).
     52   const size_t max_mb_len = maxlen * 4;
     53   const size_t mb_fmt_len = wcslen(fmt) * 4 + 1;
     54   UniquePtr<char[]> mbfmt(new char[mb_fmt_len]);
     55   if (wcsrtombs(mbfmt.get(), &fmt, mb_fmt_len, &mbstate) == MBS_FAILURE) {
     56     return -1;
     57   }
     58 
     59   UniquePtr<char[]> mbs(new char[max_mb_len]);
     60   int nprinted = vsnprintf(mbs.get(), max_mb_len, mbfmt.get(), ap);
     61   if (nprinted == -1) {
     62     return -1;
     63   }
     64 
     65   const char* mbsp = mbs.get();
     66   if (mbsrtowcs(wcs, &mbsp, maxlen, &mbstate) == MBS_FAILURE) {
     67     return -1;
     68   }
     69 
     70   // Can't use return value from vsnprintf because that number is in narrow
     71   // characters, not wide characters.
     72   int result = wcslen(wcs);
     73 
     74   // swprintf differs from snprintf in that it returns -1 if the output was
     75   // truncated.
     76   //
     77   // Truncation can occur in two places:
     78   // 1) vsnprintf truncated, in which case the return value is greater than the
     79   //    length we passed.
     80   // 2) Since the char buffer we pass to vsnprintf might be oversized, that
     81   //    might not truncate while mbsrtowcs will. In this case, mbsp will point
     82   //    to the next unconverted character instead of nullptr.
     83   if (nprinted >= max_mb_len || mbsp != nullptr) {
     84     return -1;
     85   }
     86 
     87   return result;
     88 }
     89