1 /*
   2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   3  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
   4  *              at Electronni Visti IA, Kiev, Ukraine.
   5  *                      All rights reserved.
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  * 1. Redistributions of source code must retain the above copyright
  11  *    notice, this list of conditions and the following disclaimer.
  12  * 2. Redistributions in binary form must reproduce the above copyright
  13  *    notice, this list of conditions and the following disclaimer in the
  14  *    documentation and/or other materials provided with the distribution.
  15  *
  16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
  17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26  * SUCH DAMAGE.
  27  */
  28 
  29 #include "lint.h"
  30 #include "file64.h"
  31 #include <alloca.h>
  32 #include <stdlib.h>
  33 #include <string.h>
  34 #include <errno.h>
  35 #include <wchar.h>
  36 #include "collate.h"
  37 
  38 #define ALLOCA_LIMIT    16
  39 
  40 /*
  41  * In order to properly handle multibyte locales, its easiet to just
  42  * convert to wide characters and then use wcscoll.  However if an
  43  * error occurs, we gracefully fall back to simple strcmp.  Caller
  44  * should check errno.
  45  */
  46 int
  47 strcoll(const char *s1, const char *s2)
  48 {
  49         int ret;
  50         wchar_t *t1 = NULL, *t2 = NULL;
  51         wchar_t *w1 = NULL, *w2 = NULL;
  52         size_t sz1, sz2;
  53 
  54         if (_collate_load_error)
  55                 goto error;
  56 
  57         sz1 = strlen(s1) + 1;
  58         sz2 = strlen(s2) + 1;
  59 
  60         /*
  61          * Simple assumption: conversion to wide format is strictly
  62          * reducing, i.e. a single byte (or multibyte character)
  63          * cannot result in multiple wide characters.
  64          *
  65          * We gain a bit of performance by giving preference to alloca
  66          * for small string allocations.
  67          */
  68         if (sz1 > ALLOCA_LIMIT) {
  69                 if ((t1 = malloc(sz1 * sizeof (wchar_t))) == NULL)
  70                         goto error;
  71                 w1 = t1;
  72         } else {
  73                 if ((w1 = alloca(sz1 * sizeof (wchar_t))) == NULL)
  74                         goto error;
  75         }
  76         if (sz2 > ALLOCA_LIMIT) {
  77                 if ((t2 = malloc(sz2 * sizeof (wchar_t))) == NULL)
  78                         goto error;
  79                 w2 = t2;
  80         } else {
  81                 if ((w2 = alloca(sz2 * sizeof (wchar_t))) == NULL)
  82                         goto error;
  83         }
  84 
  85         if ((mbstowcs(w1, s1, sz1)) == (size_t)-1)
  86                 goto error;
  87 
  88         if ((mbstowcs(w2, s2, sz2)) == (size_t)-1)
  89                 goto error;
  90 
  91         ret = wcscoll(w1, w2);
  92         if (t1)
  93                 free(t1);
  94         if (t2)
  95                 free(t2);
  96 
  97         return (ret);
  98 
  99 error:
 100         if (t1)
 101                 free(t1);
 102         if (t2)
 103                 free(t2);
 104         return (strcmp(s1, s2));
 105 }