Coverage Report

Created: 2024-02-25 07:20

/src/libregf/libuna/libuna_codepage_mac_inuit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * MacInuit codepage functions
3
 *
4
 * Copyright (C) 2008-2024, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <types.h>
24
25
#include "libuna_codepage_mac_inuit.h"
26
#include "libuna_libcerror.h"
27
#include "libuna_types.h"
28
29
/* Extended ASCII to Unicode character lookup table for the MacInuit codepage
30
 * Unknown are filled with the Unicode replacement character 0xfffd
31
 */
32
const uint16_t libuna_codepage_mac_inuit_byte_stream_to_unicode_base_0x80[ 128 ] = {
33
  0x1403, 0x1404, 0x1405, 0x1406, 0x140a, 0x140b, 0x1431, 0x1432,
34
  0x1433, 0x1434, 0x1438, 0x1439, 0x1449, 0x144e, 0x144f, 0x1450,
35
  0x1451, 0x1455, 0x1456, 0x1466, 0x146d, 0x146e, 0x146f, 0x1470,
36
  0x1472, 0x1473, 0x1483, 0x148b, 0x148c, 0x148d, 0x148e, 0x1490,
37
  0x1491, 0x00b0, 0x14a1, 0x14a5, 0x14a6, 0x2022, 0x00b6, 0x14a7,
38
  0x00ae, 0x00a9, 0x2122, 0x14a8, 0x14aa, 0x14ab, 0x14bb, 0x14c2,
39
  0x14c3, 0x14c4, 0x14c5, 0x14c7, 0x14c8, 0x14d0, 0x14ef, 0x14f0,
40
  0x14f1, 0x14f2, 0x14f4, 0x14f5, 0x1505, 0x14d5, 0x14d6, 0x14d7,
41
  0x14d8, 0x14da, 0x14db, 0x14ea, 0x1528, 0x1529, 0x152a, 0x152b,
42
  0x152d, 0x2026, 0x00a0, 0x152e, 0x153e, 0x1555, 0x1556, 0x1557,
43
  0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x1558, 0x1559,
44
  0x155a, 0x155d, 0x1546, 0x1547, 0x1548, 0x1549, 0x154b, 0x154c,
45
  0x1550, 0x157f, 0x1580, 0x1581, 0x1582, 0x1583, 0x1584, 0x1585,
46
  0x158f, 0x1590, 0x1591, 0x1592, 0x1593, 0x1594, 0x1595, 0x1671,
47
  0x1672, 0x1673, 0x1674, 0x1675, 0x1676, 0x1596, 0x15a0, 0x15a1,
48
  0x15a2, 0x15a3, 0x15a4, 0x15a5, 0x15a6, 0x157c, 0x0141, 0x0142
49
};
50
51
/* Unicode to ASCII character lookup tables for the MacInuit codepage
52
 * Unknown are filled with the ASCII replacement character 0x1a
53
 */
54
const uint8_t libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1430[ 216 ] = {
55
  0x1a, 0x86, 0x87, 0x88, 0x89, 0x1a, 0x1a, 0x1a,
56
  0x8a, 0x8b, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
57
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
58
  0x1a, 0x8c, 0x1a, 0x1a, 0x1a, 0x1a, 0x8d, 0x8e,
59
  0x8f, 0x90, 0x1a, 0x1a, 0x1a, 0x91, 0x92, 0x1a,
60
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
61
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x93, 0x1a,
62
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x94, 0x95, 0x96,
63
  0x97, 0x1a, 0x98, 0x99, 0x1a, 0x1a, 0x1a, 0x1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
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
87
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xcc, 0x1a,
88
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xda, 0xdb,
89
  0xdc, 0xdd, 0x1a, 0xde, 0xdf, 0x1a, 0x1a, 0x1a,
90
  0xe0, 0x1a, 0x1a, 0x1a, 0x1a, 0xcd, 0xce, 0xcf,
91
  0xd6, 0xd7, 0xd8, 0x1a, 0x1a, 0xd9, 0x1a, 0x1a
92
};
93
94
const uint8_t libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1578[ 48 ] = {
95
  0x1a, 0x1a, 0x1a, 0x1a, 0xfd, 0x1a, 0x1a, 0xe1,
96
  0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0x1a, 0x1a,
97
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xe8,
98
  0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xf5, 0x1a,
99
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
100
  0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0x1a
101
};
102
103
const uint8_t libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x2010[ 24 ] = {
104
  0x1a, 0x1a, 0x1a, 0xd0, 0xd1, 0x1a, 0x1a, 0x1a,
105
  0xd4, 0xd5, 0x1a, 0x1a, 0xd2, 0xd3, 0x1a, 0x1a,
106
  0x1a, 0x1a, 0xa5, 0x1a, 0x1a, 0x1a, 0xc9, 0x1a
107
};
108
109
/* Copies an Unicode character from a MacInuit encoded byte stream
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libuna_codepage_mac_inuit_copy_from_byte_stream(
113
     libuna_unicode_character_t *unicode_character,
114
     const uint8_t *byte_stream,
115
     size_t byte_stream_size,
116
     size_t *byte_stream_index,
117
     libcerror_error_t **error )
118
0
{
119
0
  static char *function                             = "libuna_codepage_mac_inuit_copy_from_byte_stream";
120
0
  libuna_unicode_character_t safe_unicode_character = 0xfffd;
121
0
  size_t safe_byte_stream_index                     = 0;
122
0
  uint8_t byte_stream_character                     = 0;
123
124
0
  if( unicode_character == NULL )
125
0
  {
126
0
    libcerror_error_set(
127
0
     error,
128
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
129
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
130
0
     "%s: invalid Unicode character.",
131
0
     function );
132
133
0
    return( -1 );
134
0
  }
135
0
  if( byte_stream == NULL )
136
0
  {
137
0
    libcerror_error_set(
138
0
     error,
139
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
140
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
141
0
     "%s: invalid byte stream.",
142
0
     function );
143
144
0
    return( -1 );
145
0
  }
146
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
147
0
  {
148
0
    libcerror_error_set(
149
0
     error,
150
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
151
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
152
0
     "%s: invalid byte stream size value exceeds maximum.",
153
0
     function );
154
155
0
    return( -1 );
156
0
  }
157
0
  if( byte_stream_index == NULL )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
162
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
163
0
     "%s: invalid byte stream index.",
164
0
     function );
165
166
0
    return( -1 );
167
0
  }
168
0
  safe_byte_stream_index = *byte_stream_index;
169
170
0
  if( safe_byte_stream_index >= byte_stream_size )
171
0
  {
172
0
    libcerror_error_set(
173
0
     error,
174
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
175
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
176
0
     "%s: byte stream too small.",
177
0
     function );
178
179
0
    return( -1 );
180
0
  }
181
0
  byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
182
183
0
  if( byte_stream_character < 0x80 )
184
0
  {
185
0
    safe_unicode_character = byte_stream_character;
186
0
  }
187
0
  else
188
0
  {
189
0
    byte_stream_character -= 0x80;
190
191
0
    safe_unicode_character = libuna_codepage_mac_inuit_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
192
0
  }
193
0
  *unicode_character = safe_unicode_character;
194
0
  *byte_stream_index = safe_byte_stream_index;
195
196
0
  return( 1 );
197
0
}
198
199
/* Copies an Unicode character to a MacInuit encoded byte stream
200
 * Returns 1 if successful or -1 on error
201
 */
202
int libuna_codepage_mac_inuit_copy_to_byte_stream(
203
     libuna_unicode_character_t unicode_character,
204
     uint8_t *byte_stream,
205
     size_t byte_stream_size,
206
     size_t *byte_stream_index,
207
     libcerror_error_t **error )
208
0
{
209
0
  static char *function         = "libuna_codepage_mac_inuit_copy_to_byte_stream";
210
0
  size_t safe_byte_stream_index = 0;
211
0
  uint16_t byte_stream_value    = 0x001a;
212
213
0
  if( byte_stream == NULL )
214
0
  {
215
0
    libcerror_error_set(
216
0
     error,
217
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
218
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
219
0
     "%s: invalid byte stream.",
220
0
     function );
221
222
0
    return( -1 );
223
0
  }
224
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
225
0
  {
226
0
    libcerror_error_set(
227
0
     error,
228
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
229
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
230
0
     "%s: invalid byte stream size value exceeds maximum.",
231
0
     function );
232
233
0
    return( -1 );
234
0
  }
235
0
  if( byte_stream_index == NULL )
236
0
  {
237
0
    libcerror_error_set(
238
0
     error,
239
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
240
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
241
0
     "%s: invalid byte stream index.",
242
0
     function );
243
244
0
    return( -1 );
245
0
  }
246
0
  safe_byte_stream_index = *byte_stream_index;
247
248
0
  if( safe_byte_stream_index >= byte_stream_size )
249
0
  {
250
0
    libcerror_error_set(
251
0
     error,
252
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
253
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
254
0
     "%s: byte stream too small.",
255
0
     function );
256
257
0
    return( -1 );
258
0
  }
259
0
  if( ( unicode_character < 0x0080 )
260
0
   || ( unicode_character == 0x00a9 ) )
261
0
  {
262
0
    byte_stream_value = (uint16_t) unicode_character;
263
0
  }
264
0
  else if( ( unicode_character >= 0x1430 )
265
0
        && ( unicode_character < 0x1508 ) )
266
0
  {
267
0
    unicode_character -= 0x1430;
268
269
0
    byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1430[ unicode_character ];
270
0
  }
271
0
  else if( ( unicode_character >= 0x1528 )
272
0
        && ( unicode_character < 0x1560 ) )
273
0
  {
274
0
    unicode_character -= 0x1528;
275
276
0
    byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1528[ unicode_character ];
277
0
  }
278
0
  else if( ( unicode_character >= 0x1578 )
279
0
        && ( unicode_character < 0x15a8 ) )
280
0
  {
281
0
    unicode_character -= 0x1578;
282
283
0
    byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1578[ unicode_character ];
284
0
  }
285
0
  else if( ( unicode_character >= 0x2010 )
286
0
        && ( unicode_character < 0x2028 ) )
287
0
  {
288
0
    unicode_character -= 0x2010;
289
290
0
    byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x2010[ unicode_character ];
291
0
  }
292
0
  else switch( unicode_character )
293
0
  {
294
0
    case 0x00a0:
295
0
      byte_stream_value = 0xca;
296
0
      break;
297
298
0
    case 0x00ae:
299
0
      byte_stream_value = 0xa8;
300
0
      break;
301
302
0
    case 0x00b0:
303
0
      byte_stream_value = 0xa1;
304
0
      break;
305
306
0
    case 0x00b6:
307
0
      byte_stream_value = 0xa6;
308
0
      break;
309
310
0
    case 0x0141:
311
0
      byte_stream_value = 0xfe;
312
0
      break;
313
314
0
    case 0x0142:
315
0
      byte_stream_value = 0xff;
316
0
      break;
317
318
0
    case 0x1403:
319
0
      byte_stream_value = 0x80;
320
0
      break;
321
322
0
    case 0x1404:
323
0
      byte_stream_value = 0x81;
324
0
      break;
325
326
0
    case 0x1405:
327
0
      byte_stream_value = 0x82;
328
0
      break;
329
330
0
    case 0x1406:
331
0
      byte_stream_value = 0x83;
332
0
      break;
333
334
0
    case 0x140a:
335
0
      byte_stream_value = 0x84;
336
0
      break;
337
338
0
    case 0x140b:
339
0
      byte_stream_value = 0x85;
340
0
      break;
341
342
0
    case 0x1671:
343
0
      byte_stream_value = 0xef;
344
0
      break;
345
346
0
    case 0x1672:
347
0
      byte_stream_value = 0xf0;
348
0
      break;
349
350
0
    case 0x1673:
351
0
      byte_stream_value = 0xf1;
352
0
      break;
353
354
0
    case 0x1674:
355
0
      byte_stream_value = 0xf2;
356
0
      break;
357
358
0
    case 0x1675:
359
0
      byte_stream_value = 0xf3;
360
0
      break;
361
362
0
    case 0x1676:
363
0
      byte_stream_value = 0xf4;
364
0
      break;
365
366
0
    case 0x2122:
367
0
      byte_stream_value = 0xaa;
368
0
      break;
369
370
0
    default:
371
0
      byte_stream_value = 0x1a;
372
0
      break;
373
0
  }
374
0
  byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
375
376
0
  *byte_stream_index = safe_byte_stream_index;
377
378
0
  return( 1 );
379
0
}
380