summaryrefslogtreecommitdiff
path: root/ACE/ace/UTF16_Encoding_Converter.inl
blob: d241c5ec8d8c38e8f256e68ce56a3d626e4b4573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* -*- C++ -*- */
// ======================================================================
//
// The actual conversion methods are covered by the copyright information
// below.
// Chad Elliott 4/28/2005
//
// Copyright 2001-2004 Unicode, Inc.
//
// Limitations on Rights to Redistribute This Code
//
// Unicode, Inc. hereby grants the right to freely use the information
// supplied in this file in the creation of products supporting the
// Unicode Standard, and to make copies of this file in any form
// for internal or external distribution as long as this notice
// remains attached.
//
// ======================================================================

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_INLINE bool
ACE_UTF16_Encoding_Converter::is_legal_utf8 (const ACE_Byte* source,
                                             size_t length) const
{
  ACE_Byte a;
  const ACE_Byte* srcptr = source + length;

  switch (length)
    {
    default:
      return false;

    // Everything else falls through when "true"...
    case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
    case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
    case 2: if ((a = (*--srcptr)) > 0xBF) return false;

    switch (*source)
      {
      // no fall-through in this inner switch
      case 0xE0:
        if (a < 0xA0)
          return false;
        break;
      case 0xED:
        if (a > 0x9F)
          return false;
        break;
      case 0xF0:
        if (a < 0x90)
          return false;
        break;
      case 0xF4:
        if (a > 0x8F)
          return false;
        break;
      default:
        if (a < 0x80)
          return false;
    }

    case 1:
      if (*source >= 0x80 && *source < 0xC2)
        return false;
    }

  if (*source > 0xF4)
    return false;

  return true;
}

ACE_END_VERSIONED_NAMESPACE_DECL