summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/dosfs/msdos_conv_utf8.c
blob: ddf2fdbaa72710eadd16a5064721669e52d76452 (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
/**
 * @file
 *
 * @ingroup DOSFS
 *
 * @brief UTF-8 Converter
 */

/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <stddef.h>
#include <assert.h>
#include <errno.h>
#include <iconv.h>
#include <rtems/dosfs.h>
#include <utf8proc/utf8proc.h>
#include "msdos.h"

#define INVALID_ICONV_DESC ( (iconv_t) -1 )

typedef struct {
  /*
   * This structure must be the first field, since otherwise the cast
   * operations later in the file are invalid.
   */
  rtems_dosfs_convert_control super;

  iconv_t desc_codepage_to_utf8;
  iconv_t desc_utf8_to_codepage;
  iconv_t desc_utf16_to_utf8;
  iconv_t desc_utf8_to_utf16;
  uint8_t buffer[MSDOS_NAME_MAX_UTF8_LFN_BYTES];
} msdos_utf8_convert_control;

static int msdos_utf8_convert_with_iconv(
  iconv_t     desc,
  const void *src,
  size_t      src_size,
  void       *dst,
  size_t     *dst_size
)
{
  int     eno;
  size_t  inbytes_left = src_size;
  size_t  outbytes_left = *dst_size;
  char   *inbuf = (void *) (uintptr_t) src;
  char   *outbuf = dst;
  size_t  iconv_status;

  iconv_status = iconv(
    desc,
    &inbuf,
    &inbytes_left,
    &outbuf,
    &outbytes_left
  );

  *dst_size -= outbytes_left;

  if ( iconv_status == 0 ) {
    eno = 0;
  } else if ( iconv_status == (size_t) -1 ) {
    /*
     * iconv() has detected an error.  The most likely reason seems to be a too
     * small outbuf.
    */
    eno = ENOMEM;
  } else {
    /*
     * The iconv_status contains the number of characters converted in a
     * non-reversible way.  We want to use reversible conversions only.
     * Characters permitted within DOSFS names seem to be reversible.
     */
    eno = EINVAL;
  }

  return eno;
}

static int msdos_utf8_codepage_to_utf8(
  rtems_dosfs_convert_control *super,
  const char                  *src,
  size_t                       src_size,
  uint8_t                     *dst,
  size_t                      *dst_size
)
{
  msdos_utf8_convert_control *self = (msdos_utf8_convert_control *) super;

  return msdos_utf8_convert_with_iconv(
    self->desc_codepage_to_utf8,
    src,
    src_size,
    dst,
    dst_size
  );
}

static int msdos_utf8_utf8_to_codepage(
  rtems_dosfs_convert_control *super,
  const uint8_t               *src,
  size_t                       src_size,
  char                        *dst,
  size_t                      *dst_size
)
{
  msdos_utf8_convert_control *self = (msdos_utf8_convert_control *) super;

  return msdos_utf8_convert_with_iconv(
    self->desc_utf8_to_codepage,
    src,
    src_size,
    dst,
    dst_size
  );
}

static int msdos_utf8_utf16_to_utf8(
  rtems_dosfs_convert_control *super,
  const uint16_t              *src,
  size_t                       src_size,
  uint8_t                     *dst,
  size_t                      *dst_size
)
{
  msdos_utf8_convert_control *self = (msdos_utf8_convert_control *) super;

  return msdos_utf8_convert_with_iconv(
    self->desc_utf16_to_utf8,
    src,
    src_size,
    dst,
    dst_size
  );
}

static int msdos_utf8_utf8_to_utf16(
  rtems_dosfs_convert_control *super,
  const uint8_t               *src,
  size_t                       src_size,
  uint16_t                    *dst,
  size_t                      *dst_size
)
{
  msdos_utf8_convert_control *self = (msdos_utf8_convert_control *) super;

  return msdos_utf8_convert_with_iconv(
    self->desc_utf8_to_utf16,
    src,
    src_size,
    dst,
    dst_size
  );
}

static int msdos_utf8proc_errmsg_to_errno( ssize_t errcode )
{
  int eno = 0;


  switch ( errcode ) {
    case 0:
      eno = 0;
      break;
    case UTF8PROC_ERROR_NOMEM:
      eno = ENOMEM;
      break;
    case UTF8PROC_ERROR_OVERFLOW:
      eno = EOVERFLOW;
      break;
    case UTF8PROC_ERROR_INVALIDUTF8:
      eno = EINVAL;
      break;
    case UTF8PROC_ERROR_NOTASSIGNED:
      eno = EINVAL;
      break;
    case UTF8PROC_ERROR_INVALIDOPTS:
      eno = EINVAL;
      break;
    default:
      eno = ENOENT;
      break;
  }

  return eno;
}

static int msdos_utf8_normalize_and_fold(
  rtems_dosfs_convert_control *super,
  const uint8_t *src,
  const size_t   src_size,
  uint8_t       *dst,
  size_t        *dst_size
)
{
  int      eno              = 0;
  int32_t *unicode_buf      = (int32_t *) dst;
  ssize_t  unicode_buf_size = *dst_size / sizeof( *unicode_buf );
  ssize_t  unicodes_to_reencode;
  ssize_t  result;

  (void) super;

  result = utf8proc_decompose(
    src,
    (ssize_t) src_size,
    unicode_buf,
    unicode_buf_size,
    UTF8PROC_STABLE | UTF8PROC_DECOMPOSE | UTF8PROC_CASEFOLD
  );

  if ( result >= 0 ) {
    if ( result < unicode_buf_size ) {
      unicodes_to_reencode = result;
    } else {
      unicodes_to_reencode = unicode_buf_size - 1;
      eno = ENOMEM;
    }

    result = utf8proc_reencode(
      unicode_buf,
      unicodes_to_reencode,
      UTF8PROC_STABLE | UTF8PROC_DECOMPOSE
    );

    if ( result >= 0 ) {
      *dst_size = result;
    } else {
      eno = msdos_utf8proc_errmsg_to_errno( result );
    }
  } else {
    eno = msdos_utf8proc_errmsg_to_errno( result );
  }

  return eno;
}

static void msdos_utf8_destroy(
  rtems_dosfs_convert_control *super
)
{
  msdos_utf8_convert_control *self = (msdos_utf8_convert_control *) super;
  int                         rv;

  if ( self->desc_utf16_to_utf8 != INVALID_ICONV_DESC ) {
    rv = iconv_close( self->desc_utf16_to_utf8 );
    assert( rv == 0 );
  }

  if ( self->desc_codepage_to_utf8 != INVALID_ICONV_DESC ) {
    rv = iconv_close( self->desc_codepage_to_utf8 );
    assert( rv == 0 );
  }

  if ( self->desc_utf8_to_codepage != INVALID_ICONV_DESC ) {
    rv = iconv_close( self->desc_utf8_to_codepage );
    assert( rv == 0 );
  }

  if ( self->desc_utf8_to_utf16 != INVALID_ICONV_DESC ) {
    rv = iconv_close( self->desc_utf8_to_utf16 );
    assert( rv == 0 );
  }

  free( self );
}

static const rtems_dosfs_convert_handler msdos_utf8_convert_handler = {
  .utf8_to_codepage = msdos_utf8_utf8_to_codepage,
  .codepage_to_utf8 = msdos_utf8_codepage_to_utf8,
  .utf8_to_utf16 = msdos_utf8_utf8_to_utf16,
  .utf16_to_utf8 = msdos_utf8_utf16_to_utf8,
  .utf8_normalize_and_fold = msdos_utf8_normalize_and_fold,
  .destroy = msdos_utf8_destroy
};

rtems_dosfs_convert_control *rtems_dosfs_create_utf8_converter(
  const char *codepage
)
{
  msdos_utf8_convert_control *self = malloc( sizeof( *self ) );

  if ( self != NULL ) {
    self->desc_codepage_to_utf8 = iconv_open( "UTF-8", codepage );
    self->desc_utf8_to_codepage = iconv_open( codepage, "UTF-8" );
    self->desc_utf16_to_utf8    = iconv_open( "UTF-8", "UTF-16LE" );
    self->desc_utf8_to_utf16    = iconv_open( "UTF-16LE", "UTF-8" );

    if (
      self->desc_utf16_to_utf8 != INVALID_ICONV_DESC
        && self->desc_utf8_to_codepage != INVALID_ICONV_DESC
        && self->desc_codepage_to_utf8 != INVALID_ICONV_DESC
        && self->desc_utf8_to_utf16 != INVALID_ICONV_DESC
    ) {
      rtems_dosfs_convert_control *super = &self->super;

      super->handler = &msdos_utf8_convert_handler;
      super->buffer.data = &self->buffer;
      super->buffer.size = sizeof( self->buffer );
    } else {
      msdos_utf8_destroy( &self->super );
      self = NULL;
    }
  }

  return &self->super;
}