Coverage Report

Created: 2024-02-25 07:20

/src/libfsxfs/libuna/libuna_codepage_mac_greek.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * MacGreek 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_greek.h"
26
#include "libuna_libcerror.h"
27
#include "libuna_types.h"
28
29
/* Extended ASCII to Unicode character lookup table for the MacGreek codepage
30
 * Unknown are filled with the Unicode replacement character 0xfffd
31
 */
32
const uint16_t libuna_codepage_mac_greek_byte_stream_to_unicode_base_0x80[ 128 ] = {
33
  0x00c4, 0x00b9, 0x00b2, 0x00c9, 0x00b3, 0x00d6, 0x00dc, 0x0385,
34
  0x00e0, 0x00e2, 0x00e4, 0x0384, 0x00a8, 0x00e7, 0x00e9, 0x00e8,
35
  0x00ea, 0x00eb, 0x00a3, 0x2122, 0x00ee, 0x00ef, 0x2022, 0x00bd,
36
  0x2030, 0x00f4, 0x00f6, 0x00a6, 0x20ac, 0x00f9, 0x00fb, 0x00fc,
37
  0x2020, 0x0393, 0x0394, 0x0398, 0x039b, 0x039e, 0x03a0, 0x00df,
38
  0x00ae, 0x00a9, 0x03a3, 0x03aa, 0x00a7, 0x2260, 0x00b0, 0x00b7,
39
  0x0391, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x0392, 0x0395, 0x0396,
40
  0x0397, 0x0399, 0x039a, 0x039c, 0x03a6, 0x03ab, 0x03a8, 0x03a9,
41
  0x03ac, 0x039d, 0x00ac, 0x039f, 0x03a1, 0x2248, 0x03a4, 0x00ab,
42
  0x00bb, 0x2026, 0x00a0, 0x03a5, 0x03a7, 0x0386, 0x0388, 0x0153,
43
  0x2013, 0x2015, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x0389,
44
  0x038a, 0x038c, 0x038e, 0x03ad, 0x03ae, 0x03af, 0x03cc, 0x038f,
45
  0x03cd, 0x03b1, 0x03b2, 0x03c8, 0x03b4, 0x03b5, 0x03c6, 0x03b3,
46
  0x03b7, 0x03b9, 0x03be, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf,
47
  0x03c0, 0x03ce, 0x03c1, 0x03c3, 0x03c4, 0x03b8, 0x03c9, 0x03c2,
48
  0x03c7, 0x03c5, 0x03b6, 0x03ca, 0x03cb, 0x0390, 0x03b0, 0x00ad
49
};
50
51
/* Unicode to ASCII character lookup tables for the MacGreek codepage
52
 * Unknown are filled with the ASCII replacement character 0x1a
53
 */
54
const uint8_t libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x00a0[ 96 ] = {
55
  0xca, 0x1a, 0x1a, 0x92, 0x1a, 0xb4, 0x9b, 0xac,
56
  0x8c, 0xa9, 0x1a, 0xc7, 0xc2, 0xff, 0xa8, 0x1a,
57
  0xae, 0xb1, 0x82, 0x84, 0x1a, 0x1a, 0x1a, 0xaf,
58
  0x1a, 0x81, 0x1a, 0xc8, 0x1a, 0x97, 0x1a, 0x1a,
59
  0x1a, 0x1a, 0x1a, 0x1a, 0x80, 0x1a, 0x1a, 0x1a,
60
  0x1a, 0x83, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
61
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x85, 0x1a,
62
  0x1a, 0x1a, 0x1a, 0x1a, 0x86, 0x1a, 0x1a, 0xa7,
63
  0x88, 0x1a, 0x89, 0x1a, 0x8a, 0x1a, 0x1a, 0x8d,
64
  0x8f, 0x8e, 0x90, 0x91, 0x1a, 0x1a, 0x94, 0x95,
65
  0x1a, 0x1a, 0x1a, 0x1a, 0x99, 0x1a, 0x9a, 0xd6,
66
  0x1a, 0x9d, 0x1a, 0x9e, 0x9f, 0x1a, 0x1a, 0x1a
67
};
68
69
const uint8_t libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x0380[ 80 ] = {
70
  0x1a, 0x1a, 0x1a, 0x1a, 0x8b, 0x87, 0xcd, 0x1a,
71
  0xce, 0xd7, 0xd8, 0x1a, 0xd9, 0x1a, 0xda, 0xdf,
72
  0xfd, 0xb0, 0xb5, 0xa1, 0xa2, 0xb6, 0xb7, 0xb8,
73
  0xa3, 0xb9, 0xba, 0xa4, 0xbb, 0xc1, 0xa5, 0xc3,
74
  0xa6, 0xc4, 0x1a, 0xaa, 0xc6, 0xcb, 0xbc, 0xcc,
75
  0xbe, 0xbf, 0xab, 0xbd, 0xc0, 0xdb, 0xdc, 0xdd,
76
  0xfe, 0xe1, 0xe2, 0xe7, 0xe4, 0xe5, 0xfa, 0xe8,
77
  0xf5, 0xe9, 0xeb, 0xec, 0xed, 0xee, 0xea, 0xef,
78
  0xf0, 0xf2, 0xf7, 0xf3, 0xf4, 0xf9, 0xe6, 0xf8,
79
  0xe3, 0xf6, 0xfb, 0xfc, 0xde, 0xe0, 0xf1, 0x1a
80
};
81
82
const uint8_t libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x2010[ 40 ] = {
83
  0x1a, 0x1a, 0x1a, 0xd0, 0x1a, 0xd1, 0x1a, 0x1a,
84
  0xd4, 0xd5, 0x1a, 0x1a, 0xd2, 0xd3, 0x1a, 0x1a,
85
  0xa0, 0x1a, 0x96, 0x1a, 0x1a, 0x1a, 0xc9, 0x1a,
86
  0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
87
  0x98, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
88
};
89
90
/* Copies an Unicode character from a MacGreek encoded byte stream
91
 * Returns 1 if successful or -1 on error
92
 */
93
int libuna_codepage_mac_greek_copy_from_byte_stream(
94
     libuna_unicode_character_t *unicode_character,
95
     const uint8_t *byte_stream,
96
     size_t byte_stream_size,
97
     size_t *byte_stream_index,
98
     libcerror_error_t **error )
99
0
{
100
0
  static char *function                             = "libuna_codepage_mac_greek_copy_from_byte_stream";
101
0
  libuna_unicode_character_t safe_unicode_character = 0xfffd;
102
0
  size_t safe_byte_stream_index                     = 0;
103
0
  uint8_t byte_stream_character                     = 0;
104
105
0
  if( unicode_character == NULL )
106
0
  {
107
0
    libcerror_error_set(
108
0
     error,
109
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
110
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
111
0
     "%s: invalid Unicode character.",
112
0
     function );
113
114
0
    return( -1 );
115
0
  }
116
0
  if( byte_stream == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122
0
     "%s: invalid byte stream.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
128
0
  {
129
0
    libcerror_error_set(
130
0
     error,
131
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
132
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
133
0
     "%s: invalid byte stream size value exceeds maximum.",
134
0
     function );
135
136
0
    return( -1 );
137
0
  }
138
0
  if( byte_stream_index == NULL )
139
0
  {
140
0
    libcerror_error_set(
141
0
     error,
142
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
143
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
144
0
     "%s: invalid byte stream index.",
145
0
     function );
146
147
0
    return( -1 );
148
0
  }
149
0
  safe_byte_stream_index = *byte_stream_index;
150
151
0
  if( safe_byte_stream_index >= byte_stream_size )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
157
0
     "%s: byte stream too small.",
158
0
     function );
159
160
0
    return( -1 );
161
0
  }
162
0
  byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
163
164
0
  if( byte_stream_character < 0x80 )
165
0
  {
166
0
    safe_unicode_character = byte_stream_character;
167
0
  }
168
0
  else
169
0
  {
170
0
    byte_stream_character -= 0x80;
171
172
0
    safe_unicode_character = libuna_codepage_mac_greek_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
173
0
  }
174
0
  *unicode_character = safe_unicode_character;
175
0
  *byte_stream_index = safe_byte_stream_index;
176
177
0
  return( 1 );
178
0
}
179
180
/* Copies an Unicode character to a MacGreek encoded byte stream
181
 * Returns 1 if successful or -1 on error
182
 */
183
int libuna_codepage_mac_greek_copy_to_byte_stream(
184
     libuna_unicode_character_t unicode_character,
185
     uint8_t *byte_stream,
186
     size_t byte_stream_size,
187
     size_t *byte_stream_index,
188
     libcerror_error_t **error )
189
0
{
190
0
  static char *function         = "libuna_codepage_mac_greek_copy_to_byte_stream";
191
0
  size_t safe_byte_stream_index = 0;
192
0
  uint16_t byte_stream_value    = 0x001a;
193
194
0
  if( byte_stream == NULL )
195
0
  {
196
0
    libcerror_error_set(
197
0
     error,
198
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
199
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
200
0
     "%s: invalid byte stream.",
201
0
     function );
202
203
0
    return( -1 );
204
0
  }
205
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
206
0
  {
207
0
    libcerror_error_set(
208
0
     error,
209
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
210
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
211
0
     "%s: invalid byte stream size value exceeds maximum.",
212
0
     function );
213
214
0
    return( -1 );
215
0
  }
216
0
  if( byte_stream_index == NULL )
217
0
  {
218
0
    libcerror_error_set(
219
0
     error,
220
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
221
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
222
0
     "%s: invalid byte stream index.",
223
0
     function );
224
225
0
    return( -1 );
226
0
  }
227
0
  safe_byte_stream_index = *byte_stream_index;
228
229
0
  if( safe_byte_stream_index >= byte_stream_size )
230
0
  {
231
0
    libcerror_error_set(
232
0
     error,
233
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
234
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
235
0
     "%s: byte stream too small.",
236
0
     function );
237
238
0
    return( -1 );
239
0
  }
240
0
  if( unicode_character < 0x0080 )
241
0
  {
242
0
    byte_stream_value = (uint16_t) unicode_character;
243
0
  }
244
0
  else if( ( unicode_character >= 0x00a0 )
245
0
        && ( unicode_character < 0x0100 ) )
246
0
  {
247
0
    unicode_character -= 0x00a0;
248
249
0
    byte_stream_value = libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
250
0
  }
251
0
  else if( ( unicode_character >= 0x0380 )
252
0
        && ( unicode_character < 0x03d0 ) )
253
0
  {
254
0
    unicode_character -= 0x0380;
255
256
0
    byte_stream_value = libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x0380[ unicode_character ];
257
0
  }
258
0
  else if( ( unicode_character >= 0x2010 )
259
0
        && ( unicode_character < 0x2038 ) )
260
0
  {
261
0
    unicode_character -= 0x2010;
262
263
0
    byte_stream_value = libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x2010[ unicode_character ];
264
0
  }
265
0
  else switch( unicode_character )
266
0
  {
267
0
    case 0x0153:
268
0
      byte_stream_value = 0xcf;
269
0
      break;
270
271
0
    case 0x20ac:
272
0
      byte_stream_value = 0x9c;
273
0
      break;
274
275
0
    case 0x2122:
276
0
      byte_stream_value = 0x93;
277
0
      break;
278
279
0
    case 0x2248:
280
0
      byte_stream_value = 0xc5;
281
0
      break;
282
283
0
    case 0x2260:
284
0
      byte_stream_value = 0xad;
285
0
      break;
286
287
0
    case 0x2264:
288
0
      byte_stream_value = 0xb2;
289
0
      break;
290
291
0
    case 0x2265:
292
0
      byte_stream_value = 0xb3;
293
0
      break;
294
295
0
    default:
296
0
      byte_stream_value = 0x1a;
297
0
      break;
298
0
  }
299
0
  byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
300
301
0
  *byte_stream_index = safe_byte_stream_index;
302
303
0
  return( 1 );
304
0
}
305