Coverage Report

Created: 2024-02-25 07:20

/src/libluksde/libuna/libuna_codepage_mac_farsi.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * MacFarsi 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_farsi.h"
26
#include "libuna_libcerror.h"
27
#include "libuna_types.h"
28
29
/* Extended ASCII to Unicode character lookup table for the MacFarsi codepage
30
 * Unknown are filled with the Unicode replacement character 0xfffd
31
 */
32
const uint16_t libuna_codepage_mac_farsi_byte_stream_to_unicode_base_0x80[ 128 ] = {
33
  0x00c4, 0x00a0, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1,
34
  0x00e0, 0x00e2, 0x00e4, 0x06ba, 0x00ab, 0x00e7, 0x00e9, 0x00e8,
35
  0x00ea, 0x00eb, 0x00ed, 0x2026, 0x00ee, 0x00ef, 0x00f1, 0x00f3,
36
  0x00bb, 0x00f4, 0x00f6, 0x00f7, 0x00fa, 0x00f9, 0x00fb, 0x00fc,
37
  0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x066a, 0x0026, 0x0027,
38
  0x0028, 0x0029, 0x002a, 0x002b, 0x060c, 0x002d, 0x002e, 0x002f,
39
  0x06f0, 0x06f1, 0x06f2, 0x06f3, 0x06f4, 0x06f5, 0x06f6, 0x06f7,
40
  0x06f8, 0x06f9, 0x003a, 0x061b, 0x003c, 0x003d, 0x003e, 0x061f,
41
  0x274a, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
42
  0x0628, 0x0629, 0x062a, 0x062b, 0x062c, 0x062d, 0x062e, 0x062f,
43
  0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637,
44
  0x0638, 0x0639, 0x063a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
45
  0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647,
46
  0x0648, 0x0649, 0x064a, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f,
47
  0x0650, 0x0651, 0x0652, 0x067e, 0x0679, 0x0686, 0x06d5, 0x06a4,
48
  0x06af, 0x0688, 0x0691, 0x007b, 0x007c, 0x007d, 0x0698, 0x06d2
49
};
50
51
/* Unicode to ASCII character lookup tables for the MacFarsi codepage
52
 * Unknown are filled with the ASCII replacement character 0x1a
53
 */
54
const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0020[ 32 ] = {
55
  0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0x25, 0xa6, 0xa7,
56
  0xa8, 0xa9, 0xaa, 0xab, 0x2c, 0xad, 0xae, 0xaf,
57
  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
58
  0x38, 0x39, 0xba, 0x3b, 0xbc, 0xbd, 0xbe, 0x3f
59
};
60
61
const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x00a0[ 96 ] = {
62
  0x81, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
63
  0x1a, 0x1a, 0x1a, 0x8c, 0x1a, 0x1a, 0x1a, 0x1a,
64
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
65
  0x1a, 0x1a, 0x1a, 0x98, 0x1a, 0x1a, 0x1a, 0x1a,
66
  0x1a, 0x1a, 0x1a, 0x1a, 0x80, 0x1a, 0x1a, 0x82,
67
  0x1a, 0x83, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
68
  0x1a, 0x84, 0x1a, 0x1a, 0x1a, 0x1a, 0x85, 0x1a,
69
  0x1a, 0x1a, 0x1a, 0x1a, 0x86, 0x1a, 0x1a, 0x1a,
70
  0x88, 0x87, 0x89, 0x1a, 0x8a, 0x1a, 0x1a, 0x8d,
71
  0x8f, 0x8e, 0x90, 0x91, 0x1a, 0x92, 0x94, 0x95,
72
  0x1a, 0x96, 0x1a, 0x97, 0x99, 0x1a, 0x9a, 0x9b,
73
  0x1a, 0x9d, 0x9c, 0x9e, 0x9f, 0x1a, 0x1a, 0x1a
74
};
75
76
const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0608[ 80 ] = {
77
  0x1a, 0x1a, 0x1a, 0x1a, 0xac, 0x1a, 0x1a, 0x1a,
78
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
79
  0x1a, 0x1a, 0x1a, 0xbb, 0x1a, 0x1a, 0x1a, 0xbf,
80
  0x1a, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
81
  0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
82
  0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
83
  0xd8, 0xd9, 0xda, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
84
  0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
85
  0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
86
  0xf0, 0xf1, 0xf2, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
87
};
88
89
const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0668[ 88 ] = {
90
  0x1a, 0x1a, 0xa5, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
91
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
92
  0x1a, 0xf4, 0x1a, 0x1a, 0x1a, 0x1a, 0xf3, 0x1a,
93
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xf5, 0x1a,
94
  0xf9, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
95
  0x1a, 0xfa, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
96
  0xfe, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
97
  0x1a, 0x1a, 0x1a, 0x1a, 0xf7, 0x1a, 0x1a, 0x1a,
98
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xf8,
99
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
100
  0x1a, 0x1a, 0x8b, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
101
};
102
103
const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x06f0[ 16 ] = {
104
  0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
105
  0xb8, 0xb9, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
106
};
107
108
/* Copies an Unicode character from a MacFarsi encoded byte stream
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libuna_codepage_mac_farsi_copy_from_byte_stream(
112
     libuna_unicode_character_t *unicode_character,
113
     const uint8_t *byte_stream,
114
     size_t byte_stream_size,
115
     size_t *byte_stream_index,
116
     libcerror_error_t **error )
117
0
{
118
0
  static char *function                             = "libuna_codepage_mac_farsi_copy_from_byte_stream";
119
0
  libuna_unicode_character_t safe_unicode_character = 0xfffd;
120
0
  size_t safe_byte_stream_index                     = 0;
121
0
  uint8_t byte_stream_character                     = 0;
122
123
0
  if( unicode_character == NULL )
124
0
  {
125
0
    libcerror_error_set(
126
0
     error,
127
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
128
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
129
0
     "%s: invalid Unicode character.",
130
0
     function );
131
132
0
    return( -1 );
133
0
  }
134
0
  if( byte_stream == NULL )
135
0
  {
136
0
    libcerror_error_set(
137
0
     error,
138
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
139
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
140
0
     "%s: invalid byte stream.",
141
0
     function );
142
143
0
    return( -1 );
144
0
  }
145
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
146
0
  {
147
0
    libcerror_error_set(
148
0
     error,
149
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
150
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
151
0
     "%s: invalid byte stream size value exceeds maximum.",
152
0
     function );
153
154
0
    return( -1 );
155
0
  }
156
0
  if( byte_stream_index == NULL )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
162
0
     "%s: invalid byte stream index.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
0
  safe_byte_stream_index = *byte_stream_index;
168
169
0
  if( safe_byte_stream_index >= byte_stream_size )
170
0
  {
171
0
    libcerror_error_set(
172
0
     error,
173
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
174
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
175
0
     "%s: byte stream too small.",
176
0
     function );
177
178
0
    return( -1 );
179
0
  }
180
0
  byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
181
182
0
  if( byte_stream_character < 0x80 )
183
0
  {
184
0
    safe_unicode_character = byte_stream_character;
185
0
  }
186
0
  else
187
0
  {
188
0
    byte_stream_character -= 0x80;
189
190
0
    safe_unicode_character = libuna_codepage_mac_farsi_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
191
0
  }
192
0
  *unicode_character = safe_unicode_character;
193
0
  *byte_stream_index = safe_byte_stream_index;
194
195
0
  return( 1 );
196
0
}
197
198
/* Copies an Unicode character to a MacFarsi encoded byte stream
199
 * Returns 1 if successful or -1 on error
200
 */
201
int libuna_codepage_mac_farsi_copy_to_byte_stream(
202
     libuna_unicode_character_t unicode_character,
203
     uint8_t *byte_stream,
204
     size_t byte_stream_size,
205
     size_t *byte_stream_index,
206
     libcerror_error_t **error )
207
0
{
208
0
  static char *function         = "libuna_codepage_mac_farsi_copy_to_byte_stream";
209
0
  size_t safe_byte_stream_index = 0;
210
0
  uint16_t byte_stream_value    = 0x001a;
211
212
0
  if( byte_stream == NULL )
213
0
  {
214
0
    libcerror_error_set(
215
0
     error,
216
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
217
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
218
0
     "%s: invalid byte stream.",
219
0
     function );
220
221
0
    return( -1 );
222
0
  }
223
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
224
0
  {
225
0
    libcerror_error_set(
226
0
     error,
227
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
228
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
229
0
     "%s: invalid byte stream size value exceeds maximum.",
230
0
     function );
231
232
0
    return( -1 );
233
0
  }
234
0
  if( byte_stream_index == NULL )
235
0
  {
236
0
    libcerror_error_set(
237
0
     error,
238
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
239
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
240
0
     "%s: invalid byte stream index.",
241
0
     function );
242
243
0
    return( -1 );
244
0
  }
245
0
  safe_byte_stream_index = *byte_stream_index;
246
247
0
  if( safe_byte_stream_index >= byte_stream_size )
248
0
  {
249
0
    libcerror_error_set(
250
0
     error,
251
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
252
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
253
0
     "%s: byte stream too small.",
254
0
     function );
255
256
0
    return( -1 );
257
0
  }
258
0
  if( unicode_character < 0x0020 )
259
0
  {
260
0
    byte_stream_value = (uint16_t) unicode_character;
261
0
  }
262
0
  else if( ( unicode_character >= 0x0020 )
263
0
        && ( unicode_character < 0x0040 ) )
264
0
  {
265
0
    unicode_character -= 0x0020;
266
267
0
    byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0020[ unicode_character ];
268
0
  }
269
0
  else if( ( unicode_character >= 0x0040 )
270
0
        && ( unicode_character < 0x005b ) )
271
0
  {
272
0
    byte_stream_value = (uint16_t) unicode_character;
273
0
  }
274
0
  else if( ( unicode_character >= 0x0060 )
275
0
        && ( unicode_character < 0x007b ) )
276
0
  {
277
0
    byte_stream_value = (uint16_t) unicode_character;
278
0
  }
279
0
  else if( ( unicode_character >= 0x007e )
280
0
        && ( unicode_character < 0x0080 ) )
281
0
  {
282
0
    byte_stream_value = (uint16_t) unicode_character;
283
0
  }
284
0
  else if( ( unicode_character >= 0x00a0 )
285
0
        && ( unicode_character < 0x0100 ) )
286
0
  {
287
0
    unicode_character -= 0x00a0;
288
289
0
    byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
290
0
  }
291
0
  else if( ( unicode_character >= 0x0608 )
292
0
        && ( unicode_character < 0x0658 ) )
293
0
  {
294
0
    unicode_character -= 0x0608;
295
296
0
    byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0608[ unicode_character ];
297
0
  }
298
0
  else if( ( unicode_character >= 0x0668 )
299
0
        && ( unicode_character < 0x06c0 ) )
300
0
  {
301
0
    unicode_character -= 0x0668;
302
303
0
    byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0668[ unicode_character ];
304
0
  }
305
0
  else if( ( unicode_character >= 0x06f0 )
306
0
        && ( unicode_character < 0x0700 ) )
307
0
  {
308
0
    unicode_character -= 0x06f0;
309
310
0
    byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x06f0[ unicode_character ];
311
0
  }
312
0
  else switch( unicode_character )
313
0
  {
314
0
    case 0x005b:
315
0
      byte_stream_value = 0xdb;
316
0
      break;
317
318
0
    case 0x005c:
319
0
      byte_stream_value = 0xdc;
320
0
      break;
321
322
0
    case 0x005d:
323
0
      byte_stream_value = 0xdd;
324
0
      break;
325
326
0
    case 0x005e:
327
0
      byte_stream_value = 0xde;
328
0
      break;
329
330
0
    case 0x005f:
331
0
      byte_stream_value = 0xdf;
332
0
      break;
333
334
0
    case 0x007b:
335
0
      byte_stream_value = 0xfb;
336
0
      break;
337
338
0
    case 0x007c:
339
0
      byte_stream_value = 0xfc;
340
0
      break;
341
342
0
    case 0x007d:
343
0
      byte_stream_value = 0xfd;
344
0
      break;
345
346
0
    case 0x06d2:
347
0
      byte_stream_value = 0xff;
348
0
      break;
349
350
0
    case 0x06d5:
351
0
      byte_stream_value = 0xf6;
352
0
      break;
353
354
0
    case 0x2026:
355
0
      byte_stream_value = 0x93;
356
0
      break;
357
358
0
    case 0x274a:
359
0
      byte_stream_value = 0xc0;
360
0
      break;
361
362
0
    default:
363
0
      byte_stream_value = 0x1a;
364
0
      break;
365
0
  }
366
0
  byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
367
368
0
  *byte_stream_index = safe_byte_stream_index;
369
370
0
  return( 1 );
371
0
}
372