1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup DOSFS |
---|
5 | * |
---|
6 | * @brief Default Converter |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2013 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Dornierstr. 4 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <string.h> |
---|
24 | #include <errno.h> |
---|
25 | #include <ctype.h> |
---|
26 | #include <assert.h> |
---|
27 | #include <rtems/endian.h> |
---|
28 | #include <rtems/dosfs.h> |
---|
29 | #include "fat.h" |
---|
30 | #include "msdos.h" |
---|
31 | |
---|
32 | static int msdos_default_utf8_to_codepage( |
---|
33 | rtems_dosfs_convert_control *super, |
---|
34 | const uint8_t *src, |
---|
35 | const size_t src_size, |
---|
36 | char *dst, |
---|
37 | size_t *dst_size |
---|
38 | ) |
---|
39 | { |
---|
40 | int eno = 0; |
---|
41 | size_t bytes_to_copy = MIN( src_size, *dst_size ); |
---|
42 | |
---|
43 | (void) super; |
---|
44 | |
---|
45 | *dst_size = bytes_to_copy; |
---|
46 | |
---|
47 | memcpy( dst, src, bytes_to_copy ); |
---|
48 | |
---|
49 | return eno; |
---|
50 | } |
---|
51 | |
---|
52 | static int msdos_default_codepage_to_utf8( |
---|
53 | rtems_dosfs_convert_control *super, |
---|
54 | const char *src, |
---|
55 | const size_t src_size, |
---|
56 | uint8_t *dst, |
---|
57 | size_t *dst_size |
---|
58 | ) |
---|
59 | { |
---|
60 | int eno = 0; |
---|
61 | size_t bytes_to_copy = MIN( src_size, *dst_size ); |
---|
62 | |
---|
63 | (void) super; |
---|
64 | |
---|
65 | *dst_size = bytes_to_copy; |
---|
66 | |
---|
67 | memcpy( dst, src, bytes_to_copy ); |
---|
68 | |
---|
69 | return eno; |
---|
70 | } |
---|
71 | |
---|
72 | static int msdos_default_utf8_to_utf16( |
---|
73 | rtems_dosfs_convert_control *super, |
---|
74 | const uint8_t *src, |
---|
75 | const size_t src_size, |
---|
76 | uint16_t *dst, |
---|
77 | size_t *dst_size |
---|
78 | ) |
---|
79 | { |
---|
80 | int eno = 0; |
---|
81 | size_t bytes_to_copy = MIN( src_size, *dst_size / 2); |
---|
82 | size_t i; |
---|
83 | |
---|
84 | (void) super; |
---|
85 | |
---|
86 | *dst_size = 2 * bytes_to_copy; |
---|
87 | |
---|
88 | for ( i = 0; eno == 0 && i < bytes_to_copy; ++i ) { |
---|
89 | uint16_t utf16_native = src[i]; |
---|
90 | |
---|
91 | if ( utf16_native <= 127 ) { |
---|
92 | dst[i] = CT_LE_W( utf16_native ); |
---|
93 | } else { |
---|
94 | eno = EINVAL; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | return eno; |
---|
99 | } |
---|
100 | |
---|
101 | static int msdos_default_utf16_to_utf8( |
---|
102 | rtems_dosfs_convert_control *super, |
---|
103 | const uint16_t *src, |
---|
104 | const size_t src_size, |
---|
105 | uint8_t *dst, |
---|
106 | size_t *dst_size |
---|
107 | ) |
---|
108 | { |
---|
109 | int eno = 0; |
---|
110 | size_t bytes_to_copy = MIN( src_size / 2, *dst_size ); |
---|
111 | size_t i; |
---|
112 | |
---|
113 | (void) super; |
---|
114 | |
---|
115 | *dst_size = bytes_to_copy; |
---|
116 | |
---|
117 | for ( i = 0; eno == 0 && i < bytes_to_copy; ++i ) { |
---|
118 | uint16_t utf16_le = src[i]; |
---|
119 | uint16_t utf16_native = CF_LE_W( utf16_le ); |
---|
120 | |
---|
121 | if ( utf16_native <= 127 ) { |
---|
122 | dst[i] = (uint8_t) utf16_native; |
---|
123 | } else { |
---|
124 | eno = EINVAL; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | return eno; |
---|
129 | } |
---|
130 | |
---|
131 | static int msdos_default_normalize_and_fold( |
---|
132 | rtems_dosfs_convert_control *super, |
---|
133 | const uint8_t *src, |
---|
134 | const size_t src_size, |
---|
135 | uint8_t *dst, |
---|
136 | size_t *dst_size |
---|
137 | ) |
---|
138 | { |
---|
139 | int eno = 0; |
---|
140 | size_t bytes_to_copy = MIN( src_size, *dst_size ); |
---|
141 | size_t i; |
---|
142 | |
---|
143 | (void) super; |
---|
144 | |
---|
145 | *dst_size = bytes_to_copy; |
---|
146 | |
---|
147 | for ( i = 0; i < bytes_to_copy; ++i ) { |
---|
148 | dst[i] = tolower( src[i] ); |
---|
149 | } |
---|
150 | |
---|
151 | return eno; |
---|
152 | } |
---|
153 | |
---|
154 | static void msdos_default_destroy( |
---|
155 | rtems_dosfs_convert_control *super |
---|
156 | ) |
---|
157 | { |
---|
158 | free( super ); |
---|
159 | } |
---|
160 | |
---|
161 | static const rtems_dosfs_convert_handler msdos_default_convert_handler = { |
---|
162 | .utf8_to_codepage = msdos_default_utf8_to_codepage, |
---|
163 | .codepage_to_utf8 = msdos_default_codepage_to_utf8, |
---|
164 | .utf8_to_utf16 = msdos_default_utf8_to_utf16, |
---|
165 | .utf16_to_utf8 = msdos_default_utf16_to_utf8, |
---|
166 | .utf8_normalize_and_fold = msdos_default_normalize_and_fold, |
---|
167 | .destroy = msdos_default_destroy |
---|
168 | }; |
---|
169 | |
---|
170 | typedef struct { |
---|
171 | rtems_dosfs_convert_control super; |
---|
172 | uint8_t buffer[MSDOS_NAME_MAX_LFN_BYTES]; |
---|
173 | } msdos_default_convert_control; |
---|
174 | |
---|
175 | rtems_dosfs_convert_control *rtems_dosfs_create_default_converter(void) |
---|
176 | { |
---|
177 | msdos_default_convert_control *self = malloc( sizeof( *self ) ); |
---|
178 | |
---|
179 | if ( self != NULL ) { |
---|
180 | rtems_dosfs_convert_control *super = &self->super; |
---|
181 | |
---|
182 | super->handler = &msdos_default_convert_handler; |
---|
183 | super->buffer.data = &self->buffer; |
---|
184 | super->buffer.size = sizeof( self->buffer ); |
---|
185 | } |
---|
186 | |
---|
187 | return &self->super; |
---|
188 | } |
---|