unicode-encoding.c 9.43 KB
Newer Older
1 2 3
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

4 5 6 7 8 9 10 11
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

static gint exit_status = 0;

12
static void
13 14 15 16 17 18 19 20 21 22 23
croak (char *format, ...)
{
  va_list va;
  
  va_start (va, format);
  vfprintf (stderr, format, va);
  va_end (va);

  exit (1);
}

24
static void
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 75 76 77 78 79
fail (char *format, ...)
{
  va_list va;
  
  va_start (va, format);
  vfprintf (stderr, format, va);
  va_end (va);

  exit_status |= 1;
}

typedef enum
{
  VALID,
  INCOMPLETE,
  NOTUNICODE,
  OVERLONG,
  MALFORMED
} Status;

static gboolean
ucs4_equal (gunichar *a, gunichar *b)
{
  while (*a && *b && (*a == *b))
    {
      a++;
      b++;
    }

  return (*a == *b);
}

static gboolean
utf16_equal (gunichar2 *a, gunichar2 *b)
{
  while (*a && *b && (*a == *b))
    {
      a++;
      b++;
    }

  return (*a == *b);
}

static gint
utf16_count (gunichar2 *a)
{
  gint result = 0;
  
  while (a[result])
    result++;

  return result;
}

80 81 82 83 84 85 86 87 88 89
static void
print_ucs4 (const gchar *prefix, gunichar *ucs4, gint ucs4_len)
{
  gint i;
  g_print ("%s ", prefix);
  for (i = 0; i < ucs4_len; i++)
    g_print ("%x ", ucs4[i]);
  g_print ("\n");
}

90 91 92 93 94 95 96 97 98 99
static void
process (gint      line,
	 gchar    *utf8,
	 Status    status,
	 gunichar *ucs4,
	 gint      ucs4_len)
{
  const gchar *end;
  gboolean is_valid = g_utf8_validate (utf8, -1, &end);
  GError *error = NULL;
100
  glong items_read, items_written;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

  switch (status)
    {
    case VALID:
      if (!is_valid)
	{
	  fail ("line %d: valid but g_utf8_validate returned FALSE\n", line);
	  return;
	}
      break;
    case NOTUNICODE:
    case INCOMPLETE:
    case OVERLONG:
    case MALFORMED:
      if (is_valid)
	{
	  fail ("line %d: invalid but g_utf8_validate returned TRUE\n", line);
	  return;
	}
      break;
    }

  if (status == INCOMPLETE)
    {
125
      gunichar *ucs4_result;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

      ucs4_result = g_utf8_to_ucs4 (utf8, -1, NULL, NULL, &error);

      if (!error || !g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT))
	{
	  fail ("line %d: incomplete input not properly detected\n", line);
	  return;
	}
      g_clear_error (&error);

      ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, NULL, &error);

      if (!ucs4_result || items_read == strlen (utf8))
	{
	  fail ("line %d: incomplete input not properly detected\n", line);
	  return;
	}

      g_free (ucs4_result);
    }

  if (status == VALID || status == NOTUNICODE)
    {
      gunichar *ucs4_result;

      ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, &items_written, &error);
      if (!ucs4_result)
	{
154
	  fail ("line %d: conversion with status %d to ucs4 failed: %s\n", line, status, error->message);
155 156
	  return;
	}
157

158 159 160 161
      if (!ucs4_equal (ucs4_result, ucs4) ||
	  items_read != strlen (utf8) ||
	  items_written != ucs4_len)
	{
162 163 164
	  fail ("line %d: results of conversion with status %d to ucs4 do not match expected.\n", line, status);
          print_ucs4 ("expected: ", ucs4, ucs4_len);
          print_ucs4 ("received: ", ucs4_result, items_written);
165 166 167 168
	  return;
	}

      g_free (ucs4_result);
169 170 171 172 173 174
    }

  if (status == VALID)
     {
      gunichar *ucs4_result;
      gchar *utf8_result;
175 176

      ucs4_result = g_utf8_to_ucs4_fast (utf8, -1, &items_written);
177

178 179 180
      if (!ucs4_equal (ucs4_result, ucs4) ||
	  items_written != ucs4_len)
	{
181 182 183
	  fail ("line %d: results of fast conversion with status %d to ucs4 do not match expected.\n", line, status);
          print_ucs4 ("expected: ", ucs4, ucs4_len);
          print_ucs4 ("received: ", ucs4_result, items_written);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	  return;
	}

      utf8_result = g_ucs4_to_utf8 (ucs4_result, -1, &items_read, &items_written, &error);
      if (!utf8_result)
	{
	  fail ("line %d: conversion back to utf8 failed: %s", line, error->message);
	  return;
	}

      if (strcmp (utf8_result, utf8) != 0 ||
	  items_read != ucs4_len ||
	  items_written != strlen (utf8))
	{
	  fail ("line %d: conversion back to utf8 did not match original\n", line);
	  return;
	}

      g_free (utf8_result);
      g_free (ucs4_result);
    }

  if (status == VALID)
    {
      gunichar2 *utf16_expected_tmp;
      gunichar2 *utf16_expected;
      gunichar2 *utf16_from_utf8;
      gunichar2 *utf16_from_ucs4;
      gunichar *ucs4_result;
213
      gsize bytes_written;
214 215 216
      gint n_chars;
      gchar *utf8_result;

217
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
218 219 220 221 222 223
#define TARGET "UTF-16LE"
#else
#define TARGET "UTF-16"
#endif

      if (!(utf16_expected_tmp = (gunichar2 *)g_convert (utf8, -1, TARGET, "UTF-8",
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
							 NULL, &bytes_written, NULL)))
	{
	  fail ("line %d: could not convert to UTF-16 via g_convert\n", line);
	  return;
	}

      /* zero-terminate and remove BOM
       */
      n_chars = bytes_written / 2;
      if (utf16_expected_tmp[0] == 0xfeff) /* BOM */
	{
	  n_chars--;
	  utf16_expected = g_new (gunichar2, n_chars + 1);
	  memcpy (utf16_expected, utf16_expected_tmp + 1, sizeof(gunichar2) * n_chars);
	}
      else if (utf16_expected_tmp[0] == 0xfffe) /* ANTI-BOM */
	{
241
	  fail ("line %d: conversion via iconv to \"UTF-16\" is not native-endian\n", line);
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	  return;
	}
      else
	{
	  utf16_expected = g_new (gunichar2, n_chars + 1);
	  memcpy (utf16_expected, utf16_expected_tmp, sizeof(gunichar2) * n_chars);
	}

      utf16_expected[n_chars] = '\0';
      
      if (!(utf16_from_utf8 = g_utf8_to_utf16 (utf8, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion to ucs16 failed: %s\n", line, error->message);
	  return;
	}

      if (items_read != strlen (utf8) ||
	  utf16_count (utf16_from_utf8) != items_written)
	{
	  fail ("line %d: length error in conversion to ucs16\n", line);
	  return;
	}

      if (!(utf16_from_ucs4 = g_ucs4_to_utf16 (ucs4, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion to ucs16 failed: %s\n", line, error->message);
	  return;
	}

      if (items_read != ucs4_len ||
	  utf16_count (utf16_from_ucs4) != items_written)
	{
	  fail ("line %d: length error in conversion to ucs16\n", line);
	  return;
	}

      if (!utf16_equal (utf16_from_utf8, utf16_expected) ||
	  !utf16_equal (utf16_from_ucs4, utf16_expected))
	{
	  fail ("line %d: results of conversion to ucs16 do not match\n", line);
	  return;
	}

      if (!(utf8_result = g_utf16_to_utf8 (utf16_from_utf8, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion back to utf8 failed: %s\n", line, error->message);
	  return;
	}

      if (items_read != utf16_count (utf16_from_utf8) ||
	  items_written != strlen (utf8))
	{
	  fail ("line %d: length error in conversion from ucs16 to utf8\n", line);
	  return;
	}

      if (!(ucs4_result = g_utf16_to_ucs4 (utf16_from_ucs4, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion back to utf8/ucs4 failed\n", line);
	  return;
	}

      if (items_read != utf16_count (utf16_from_utf8) ||
	  items_written != ucs4_len)
	{
	  fail ("line %d: length error in conversion from ucs16 to ucs4\n", line);
	  return;
	}

      if (strcmp (utf8, utf8_result) != 0 ||
	  !ucs4_equal (ucs4, ucs4_result))
	{
	  fail ("line %d: conversion back to utf8/ucs4 did not match original\n", line);
	  return;
	}
      
      g_free (utf16_expected_tmp);
      g_free (utf16_expected);
      g_free (utf16_from_utf8);
      g_free (utf16_from_ucs4);
      g_free (utf8_result);
      g_free (ucs4_result);
    }
}

int
main (int argc, char **argv)
{
  gchar *testfile;
  gchar *contents;
  GError *error = NULL;
  gchar *p, *end;
  char *tmp;
  gint state = 0;
  gint line = 1;
  gint start_line = 0;		/* Quiet GCC */
  gchar *utf8 = NULL;		/* Quiet GCC */
  GArray *ucs4;
  Status status = VALID;	/* Quiet GCC */

342 343 344 345
  g_test_init (&argc, &argv, NULL);

  testfile = g_test_build_filename (G_TEST_DIST, "utf8.txt", NULL);

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  g_file_get_contents (testfile, &contents, NULL, &error);
  if (error)
    croak ("Cannot open utf8.txt: %s", error->message);

  ucs4 = g_array_new (TRUE, FALSE, sizeof(gunichar));

  p = contents;

  /* Loop over lines */
  while (*p)
    {
      while (*p && (*p == ' ' || *p == '\t'))
	p++;

      end = p;
361
      while (*end && (*end != '\r' && *end != '\n'))
362 363
	end++;
      
364
      if (!*p || *p == '#' || *p == '\r' || *p == '\n')
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	goto next_line;

      tmp = g_strstrip (g_strndup (p, end - p));
      
      switch (state)
	{
	case 0:
	  /* UTF-8 string */
	  start_line = line;
	  utf8 = tmp;
	  tmp = NULL;
	  break;
	  
	case 1:
	  /* Status */
	  if (!strcmp (tmp, "VALID"))
	    status = VALID;
	  else if (!strcmp (tmp, "INCOMPLETE"))
	    status = INCOMPLETE;
	  else if (!strcmp (tmp, "NOTUNICODE"))
	    status = NOTUNICODE;
	  else if (!strcmp (tmp, "OVERLONG"))
	    status = OVERLONG;
	  else if (!strcmp (tmp, "MALFORMED"))
	    status = MALFORMED;
	  else
	    croak ("Invalid status on line %d\n", line);

	  if (status != VALID && status != NOTUNICODE)
	    state++;		/* No UCS-4 data */
	  
	  break;
	  
	case 2:
	  /* UCS-4 version */

	  p = strtok (tmp, " \t");
	  while (p)
	    {
	      gchar *endptr;
	      
	      gunichar ch = strtoul (p, &endptr, 16);
	      if (*endptr != '\0')
		croak ("Invalid UCS-4 character on line %d\n", line);

	      g_array_append_val (ucs4, ch);
	      
	      p = strtok (NULL, " \t");
	    }

	  break;
	}

      g_free (tmp);
      state = (state + 1) % 3;

      if (state == 0)
	{
	  process (start_line, utf8, status, (gunichar *)ucs4->data, ucs4->len);
	  g_array_set_size (ucs4, 0);
	  g_free (utf8);
	}
      
    next_line:
      p = end;
430 431
      if (*p && *p == '\r')
	p++;
432 433 434 435 436 437
      if (*p && *p == '\n')
	p++;
      
      line++;
    }

438 439 440
  g_free (testfile);
  g_array_free (ucs4, TRUE);
  g_free (contents);
441
  return exit_status;
442
}