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 <stdlib.h> |
---|
24 | #include <string.h> |
---|
25 | #include <errno.h> |
---|
26 | #include <ctype.h> |
---|
27 | #include <assert.h> |
---|
28 | #include <rtems/endian.h> |
---|
29 | #include <rtems/dosfs.h> |
---|
30 | #include "fat.h" |
---|
31 | #include "msdos.h" |
---|
32 | |
---|
33 | static int msdos_default_utf8_to_codepage( |
---|
34 | rtems_dosfs_convert_control *super, |
---|
35 | const uint8_t *src, |
---|
36 | const size_t src_size, |
---|
37 | char *dst, |
---|
38 | size_t *dst_size |
---|
39 | ) |
---|
40 | { |
---|
41 | int eno = 0; |
---|
42 | size_t bytes_to_copy = MIN( src_size, *dst_size ); |
---|
43 | |
---|
44 | (void) super; |
---|
45 | |
---|
46 | *dst_size = bytes_to_copy; |
---|
47 | |
---|
48 | memcpy( dst, src, bytes_to_copy ); |
---|
49 | |
---|
50 | return eno; |
---|
51 | } |
---|
52 | |
---|
53 | static int msdos_default_codepage_to_utf8( |
---|
54 | rtems_dosfs_convert_control *super, |
---|
55 | const char *src, |
---|
56 | const size_t src_size, |
---|
57 | uint8_t *dst, |
---|
58 | size_t *dst_size |
---|
59 | ) |
---|
60 | { |
---|
61 | int eno = 0; |
---|
62 | size_t bytes_to_copy = MIN( src_size, *dst_size ); |
---|
63 | |
---|
64 | (void) super; |
---|
65 | |
---|
66 | *dst_size = bytes_to_copy; |
---|
67 | |
---|
68 | memcpy( dst, src, bytes_to_copy ); |
---|
69 | |
---|
70 | return eno; |
---|
71 | } |
---|
72 | |
---|
73 | static int msdos_default_utf8_to_utf16( |
---|
74 | rtems_dosfs_convert_control *super, |
---|
75 | const uint8_t *src, |
---|
76 | const size_t src_size, |
---|
77 | uint16_t *dst, |
---|
78 | size_t *dst_size |
---|
79 | ) |
---|
80 | { |
---|
81 | int eno = 0; |
---|
82 | size_t bytes_to_copy = MIN( src_size, *dst_size / 2); |
---|
83 | size_t i; |
---|
84 | |
---|
85 | (void) super; |
---|
86 | |
---|
87 | *dst_size = 2 * bytes_to_copy; |
---|
88 | |
---|
89 | for ( i = 0; eno == 0 && i < bytes_to_copy; ++i ) { |
---|
90 | uint16_t utf16_native = src[i]; |
---|
91 | |
---|
92 | if ( utf16_native <= 127 ) { |
---|
93 | dst[i] = CT_LE_W( utf16_native ); |
---|
94 | } else { |
---|
95 | eno = EINVAL; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | return eno; |
---|
100 | } |
---|
101 | |
---|
102 | static int msdos_default_utf16_to_utf8( |
---|
103 | rtems_dosfs_convert_control *super, |
---|
104 | const uint16_t *src, |
---|
105 | const size_t src_size, |
---|
106 | uint8_t *dst, |
---|
107 | size_t *dst_size |
---|
108 | ) |
---|
109 | { |
---|
110 | int eno = 0; |
---|
111 | size_t bytes_to_copy = MIN( src_size / 2, *dst_size ); |
---|
112 | size_t i; |
---|
113 | |
---|
114 | (void) super; |
---|
115 | |
---|
116 | *dst_size = bytes_to_copy; |
---|
117 | |
---|
118 | for ( i = 0; eno == 0 && i < bytes_to_copy; ++i ) { |
---|
119 | uint16_t utf16_le = src[i]; |
---|
120 | uint16_t utf16_native = CF_LE_W( utf16_le ); |
---|
121 | |
---|
122 | if ( utf16_native <= 127 ) { |
---|
123 | dst[i] = (uint8_t) utf16_native; |
---|
124 | } else { |
---|
125 | eno = EINVAL; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | return eno; |
---|
130 | } |
---|
131 | |
---|
132 | static int msdos_default_normalize_and_fold( |
---|
133 | rtems_dosfs_convert_control *super, |
---|
134 | const uint8_t *src, |
---|
135 | const size_t src_size, |
---|
136 | uint8_t *dst, |
---|
137 | size_t *dst_size |
---|
138 | ) |
---|
139 | { |
---|
140 | int eno = 0; |
---|
141 | size_t bytes_to_copy = MIN( src_size, *dst_size ); |
---|
142 | size_t i; |
---|
143 | |
---|
144 | (void) super; |
---|
145 | |
---|
146 | *dst_size = bytes_to_copy; |
---|
147 | |
---|
148 | for ( i = 0; i < bytes_to_copy; ++i ) { |
---|
149 | dst[i] = tolower( src[i] ); |
---|
150 | } |
---|
151 | |
---|
152 | return eno; |
---|
153 | } |
---|
154 | |
---|
155 | static void msdos_default_destroy( |
---|
156 | rtems_dosfs_convert_control *super |
---|
157 | ) |
---|
158 | { |
---|
159 | free( super ); |
---|
160 | } |
---|
161 | |
---|
162 | static const rtems_dosfs_convert_handler msdos_default_convert_handler = { |
---|
163 | .utf8_to_codepage = msdos_default_utf8_to_codepage, |
---|
164 | .codepage_to_utf8 = msdos_default_codepage_to_utf8, |
---|
165 | .utf8_to_utf16 = msdos_default_utf8_to_utf16, |
---|
166 | .utf16_to_utf8 = msdos_default_utf16_to_utf8, |
---|
167 | .utf8_normalize_and_fold = msdos_default_normalize_and_fold, |
---|
168 | .destroy = msdos_default_destroy |
---|
169 | }; |
---|
170 | |
---|
171 | typedef struct { |
---|
172 | rtems_dosfs_convert_control super; |
---|
173 | uint8_t buffer[MSDOS_NAME_MAX_LFN_BYTES]; |
---|
174 | } msdos_default_convert_control; |
---|
175 | |
---|
176 | rtems_dosfs_convert_control *rtems_dosfs_create_default_converter(void) |
---|
177 | { |
---|
178 | msdos_default_convert_control *self = malloc( sizeof( *self ) ); |
---|
179 | |
---|
180 | if ( self != NULL ) { |
---|
181 | rtems_dosfs_convert_control *super = &self->super; |
---|
182 | |
---|
183 | super->handler = &msdos_default_convert_handler; |
---|
184 | super->buffer.data = &self->buffer; |
---|
185 | super->buffer.size = sizeof( self->buffer ); |
---|
186 | } |
---|
187 | |
---|
188 | return &self->super; |
---|
189 | } |
---|