aboutsummaryrefslogtreecommitdiff
path: root/src/utfconv.h
blob: d5d9092ef7645fd9f80dcd4f988bc6518fea0641 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef MBSEC_H
#define MBSEC_H

#ifdef __GNUC__
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED
#endif

#ifdef _WIN32

#include <stdlib.h>
#include <windows.h>

#include "arena_allocator.h"

#define UTFCONV_ERROR_INVALID_CONVERSION "Input contains invalid byte sequences."

#define utfconv_fromutf8(A, str) utfconv_fromlutf8(A, str, -1)

static UNUSED LPWSTR utfconv_fromlutf8(lxl_arena *A, const char *str, int len) {
  int output_len = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
  if (!output_len) return NULL;
  LPWSTR output = lxl_arena_malloc(A, sizeof(WCHAR) * output_len);
  if (!output) return NULL;
  output_len = MultiByteToWideChar(CP_UTF8, 0, str, len, output, output_len);
  if (!output_len) return (lxl_arena_free(A, output), NULL);
  return output;
}

#define utfconv_fromwstr(A, str) utfconv_fromlwstr(A, str, -1)

static UNUSED const char *utfconv_fromlwstr(lxl_arena *A, LPWSTR str, int len) {
  int output_len = WideCharToMultiByte(CP_UTF8, 0, str, len, NULL, 0, NULL, NULL);
  if (!output_len) return NULL;
  char *output = lxl_arena_malloc(A, sizeof(char) * output_len);
  if (!output) return NULL;
  output_len = WideCharToMultiByte(CP_UTF8, 0, str, len, output, output_len, NULL, NULL);
  if (!output_len) return (lxl_arena_free(A, output), NULL);
  return (const char *) output;
}

static UNUSED LPWSTR utfconv_utf8towc(const char *str) {
  LPWSTR output;
  int len;

  // len includes \0
  len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
  if (len == 0)
    return NULL;

  output = (LPWSTR) malloc(sizeof(WCHAR) * len);
  if (output == NULL)
    return NULL;

  len = MultiByteToWideChar(CP_UTF8, 0, str, -1, output, len);
  if (len == 0) {
    free(output);
    return NULL;
  }

  return output;
}

static UNUSED char *utfconv_wctoutf8(LPCWSTR str) {
  char *output;
  int len;

  // len includes \0
  len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
  if (len == 0)
    return NULL;

  output = (char *) malloc(sizeof(char) * len);
  if (output == NULL)
    return NULL;

  len = WideCharToMultiByte(CP_UTF8, 0, str, -1, output, len, NULL, NULL);
  if (len == 0) {
    free(output);
    return NULL;
  }

  return output;
}

#endif

#endif