| 1 |
/* $Id: utf8.c,v 1.2 2024/12/05 00:40:24 tom Exp $ */ |
| 2 |
|
| 3 |
#include <vttest.h> |
| 4 |
|
| 5 |
int |
| 6 |
conv_to_utf8(unsigned char *target, unsigned source, unsigned limit) |
| 7 |
{ |
| 8 |
#define CH(n) CharOf((source) >> ((n) * 8)) |
| 9 |
int rc = 0; |
| 10 |
|
| 11 |
if (source <= 0x0000007f) |
| 12 |
rc = 1; |
| 13 |
else if (source <= 0x000007ff) |
| 14 |
rc = 2; |
| 15 |
else if (source <= 0x0000ffff) |
| 16 |
rc = 3; |
| 17 |
else if (source <= 0x001fffff) |
| 18 |
rc = 4; |
| 19 |
else if (source <= 0x03ffffff) |
| 20 |
rc = 5; |
| 21 |
else /* (source <= 0x7fffffff) */ |
| 22 |
rc = 6; |
| 23 |
|
| 24 |
if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */ |
| 25 |
rc = 0; |
| 26 |
} |
| 27 |
|
| 28 |
if (target != NULL) { |
| 29 |
switch (rc) { |
| 30 |
case 1: |
| 31 |
target[0] = CH(0); |
| 32 |
break; |
| 33 |
|
| 34 |
case 2: |
| 35 |
target[1] = CharOf(0x80 | (CH(0) & 0x3f)); |
| 36 |
target[0] = CharOf(0xc0 | (CH(0) >> 6) | ((CH(1) & 0x07) << 2)); |
| 37 |
break; |
| 38 |
|
| 39 |
case 3: |
| 40 |
target[2] = CharOf(0x80 | (CH(0) & 0x3f)); |
| 41 |
target[1] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); |
| 42 |
target[0] = CharOf(0xe0 | ((int) (CH(1) & 0xf0) >> 4)); |
| 43 |
break; |
| 44 |
|
| 45 |
case 4: |
| 46 |
target[3] = CharOf(0x80 | (CH(0) & 0x3f)); |
| 47 |
target[2] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); |
| 48 |
target[1] = CharOf(0x80 | |
| 49 |
((int) (CH(1) & 0xf0) >> 4) | |
| 50 |
((int) (CH(2) & 0x03) << 4)); |
| 51 |
target[0] = CharOf(0xf0 | ((int) (CH(2) & 0x1f) >> 2)); |
| 52 |
break; |
| 53 |
|
| 54 |
case 5: |
| 55 |
target[4] = CharOf(0x80 | (CH(0) & 0x3f)); |
| 56 |
target[3] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); |
| 57 |
target[2] = CharOf(0x80 | |
| 58 |
((int) (CH(1) & 0xf0) >> 4) | |
| 59 |
((int) (CH(2) & 0x03) << 4)); |
| 60 |
target[1] = CharOf(0x80 | (CH(2) >> 2)); |
| 61 |
target[0] = CharOf(0xf8 | (CH(3) & 0x03)); |
| 62 |
break; |
| 63 |
|
| 64 |
case 6: |
| 65 |
target[5] = CharOf(0x80 | (CH(0) & 0x3f)); |
| 66 |
target[4] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); |
| 67 |
target[3] = CharOf(0x80 | (CH(1) >> 4) | ((CH(2) & 0x03) << 4)); |
| 68 |
target[2] = CharOf(0x80 | (CH(2) >> 2)); |
| 69 |
target[1] = CharOf(0x80 | (CH(3) & 0x3f)); |
| 70 |
target[0] = CharOf(0xfc | ((int) (CH(3) & 0x40) >> 6)); |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return rc; /* number of bytes needed in target */ |
| 76 |
#undef CH |
| 77 |
} |
| 78 |
|
| 79 |
int |
| 80 |
conv_to_utf32(unsigned *target, const char *source, unsigned limit) |
| 81 |
{ |
| 82 |
#define CH(n) CharOf((*target) >> ((n) * 8)) |
| 83 |
int rc = 0; |
| 84 |
int j; |
| 85 |
unsigned mask = 0; |
| 86 |
|
| 87 |
/* |
| 88 |
* Find the number of bytes we will need from the source. |
| 89 |
*/ |
| 90 |
if ((*source & 0x80) == 0) { |
| 91 |
rc = 1; |
| 92 |
mask = (unsigned) *source; |
| 93 |
} else if ((*source & 0xe0) == 0xc0) { |
| 94 |
rc = 2; |
| 95 |
mask = (unsigned) (*source & 0x1f); |
| 96 |
} else if ((*source & 0xf0) == 0xe0) { |
| 97 |
rc = 3; |
| 98 |
mask = (unsigned) (*source & 0x0f); |
| 99 |
} else if ((*source & 0xf8) == 0xf0) { |
| 100 |
rc = 4; |
| 101 |
mask = (unsigned) (*source & 0x07); |
| 102 |
} else if ((*source & 0xfc) == 0xf8) { |
| 103 |
rc = 5; |
| 104 |
mask = (unsigned) (*source & 0x03); |
| 105 |
} else if ((*source & 0xfe) == 0xfc) { |
| 106 |
rc = 6; |
| 107 |
mask = (unsigned) (*source & 0x01); |
| 108 |
} |
| 109 |
|
| 110 |
if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */ |
| 111 |
rc = 0; |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* sanity-check. |
| 116 |
*/ |
| 117 |
if (rc > 1) { |
| 118 |
for (j = 1; j < rc; j++) { |
| 119 |
if ((source[j] & 0xc0) != 0x80) |
| 120 |
break; |
| 121 |
} |
| 122 |
if (j != rc) { |
| 123 |
rc = 0; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (target != NULL) { |
| 128 |
int shift = 0; |
| 129 |
*target = 0; |
| 130 |
for (j = 1; j < rc; j++) { |
| 131 |
*target |= (unsigned) (source[rc - j] & 0x3f) << shift; |
| 132 |
shift += 6; |
| 133 |
} |
| 134 |
*target |= mask << shift; |
| 135 |
} |
| 136 |
return rc; |
| 137 |
#undef CH |
| 138 |
} |