Coverage Report

Created: 2024-02-25 07:20

/src/liblnk/libuna/libuna_codepage_mac_dingbats.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * MacDingbats 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_dingbats.h"
26
#include "libuna_libcerror.h"
27
#include "libuna_types.h"
28
29
/* Extended ASCII to Unicode character lookup table for the MacDingbats codepage
30
 * Unknown are filled with the Unicode replacement character 0xfffd
31
 */
32
const uint16_t libuna_codepage_mac_dingbats_byte_stream_to_unicode_base_0x20[ 224 ] = {
33
  0x0020, 0x2701, 0x2702, 0x2703, 0x2704, 0x260e, 0x2706, 0x2707,
34
  0x2708, 0x2709, 0x261b, 0x261e, 0x270c, 0x270d, 0x270e, 0x270f,
35
  0x2710, 0x2711, 0x2712, 0x2713, 0x2714, 0x2715, 0x2716, 0x2717,
36
  0x2718, 0x2719, 0x271a, 0x271b, 0x271c, 0x271d, 0x271e, 0x271f,
37
  0x2720, 0x2721, 0x2722, 0x2723, 0x2724, 0x2725, 0x2726, 0x2727,
38
  0x2605, 0x2729, 0x272a, 0x272b, 0x272c, 0x272d, 0x272e, 0x272f,
39
  0x2730, 0x2731, 0x2732, 0x2733, 0x2734, 0x2735, 0x2736, 0x2737,
40
  0x2738, 0x2739, 0x273a, 0x273b, 0x273c, 0x273d, 0x273e, 0x273f,
41
  0x2740, 0x2741, 0x2742, 0x2743, 0x2744, 0x2745, 0x2746, 0x2747,
42
  0x2748, 0x2749, 0x274a, 0x274b, 0x25cf, 0x274d, 0x25a0, 0x274f,
43
  0x2750, 0x2751, 0x2752, 0x25b2, 0x25bc, 0x25c6, 0x2756, 0x25d7,
44
  0x2758, 0x2759, 0x275a, 0x275b, 0x275c, 0x275d, 0x275e, 0x007f,
45
  0x2768, 0x2769, 0x276a, 0x276b, 0x276c, 0x276d, 0x276e, 0x276f,
46
  0x2770, 0x2771, 0x2772, 0x2773, 0x2774, 0x2775, 0xfffd, 0xfffd,
47
  0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
48
  0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
49
  0xfffd, 0x2761, 0x2762, 0x2763, 0x2764, 0x2765, 0x2766, 0x2767,
50
  0x2663, 0x2666, 0x2665, 0x2660, 0x2460, 0x2461, 0x2462, 0x2463,
51
  0x2464, 0x2465, 0x2466, 0x2467, 0x2468, 0x2469, 0x2776, 0x2777,
52
  0x2778, 0x2779, 0x277a, 0x277b, 0x277c, 0x277d, 0x277e, 0x277f,
53
  0x2780, 0x2781, 0x2782, 0x2783, 0x2784, 0x2785, 0x2786, 0x2787,
54
  0x2788, 0x2789, 0x278a, 0x278b, 0x278c, 0x278d, 0x278e, 0x278f,
55
  0x2790, 0x2791, 0x2792, 0x2793, 0x2794, 0x2192, 0x2194, 0x2195,
56
  0x2798, 0x2799, 0x279a, 0x279b, 0x279c, 0x279d, 0x279e, 0x279f,
57
  0x27a0, 0x27a1, 0x27a2, 0x27a3, 0x27a4, 0x27a5, 0x27a6, 0x27a7,
58
  0x27a8, 0x27a9, 0x27aa, 0x27ab, 0x27ac, 0x27ad, 0x27ae, 0x27af,
59
  0xfffd, 0x27b1, 0x27b2, 0x27b3, 0x27b4, 0x27b5, 0x27b6, 0x27b7,
60
  0x27b8, 0x27b9, 0x27ba, 0x27bb, 0x27bc, 0x27bd, 0x27be, 0xfffd
61
};
62
63
/* Unicode to ASCII character lookup tables for the MacDingbats codepage
64
 * Unknown are filled with the ASCII replacement character 0x1a
65
 */
66
const uint8_t libuna_codepage_mac_dingbats_unicode_to_byte_stream_base_0x2460[ 16 ] = {
67
  0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
68
  0xb4, 0xb5, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
69
};
70
71
const uint8_t libuna_codepage_mac_dingbats_unicode_to_byte_stream_base_0x2700[ 192 ] = {
72
  0x1a, 0x21, 0x22, 0x23, 0x24, 0x1a, 0x26, 0x27,
73
  0x28, 0x29, 0x1a, 0x1a, 0x2c, 0x2d, 0x2e, 0x2f,
74
  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
75
  0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
76
  0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
77
  0x1a, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
78
  0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
79
  0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
80
  0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
81
  0x68, 0x69, 0x6a, 0x6b, 0x1a, 0x6d, 0x1a, 0x6f,
82
  0x70, 0x71, 0x72, 0x1a, 0x1a, 0x1a, 0x76, 0x1a,
83
  0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x1a,
84
  0x1a, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
85
  0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
86
  0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0xb6, 0xb7,
87
  0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
88
  0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
89
  0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
90
  0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0x1a, 0x1a, 0x1a,
91
  0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
92
  0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
93
  0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
94
  0x1a, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
95
  0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0x1a
96
};
97
98
/* Copies an Unicode character from a MacDingbats encoded byte stream
99
 * Returns 1 if successful or -1 on error
100
 */
101
int libuna_codepage_mac_dingbats_copy_from_byte_stream(
102
     libuna_unicode_character_t *unicode_character,
103
     const uint8_t *byte_stream,
104
     size_t byte_stream_size,
105
     size_t *byte_stream_index,
106
     libcerror_error_t **error )
107
0
{
108
0
  static char *function                             = "libuna_codepage_mac_dingbats_copy_from_byte_stream";
109
0
  libuna_unicode_character_t safe_unicode_character = 0xfffd;
110
0
  size_t safe_byte_stream_index                     = 0;
111
0
  uint8_t byte_stream_character                     = 0;
112
113
0
  if( unicode_character == NULL )
114
0
  {
115
0
    libcerror_error_set(
116
0
     error,
117
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
118
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
119
0
     "%s: invalid Unicode character.",
120
0
     function );
121
122
0
    return( -1 );
123
0
  }
124
0
  if( byte_stream == 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 byte stream.",
131
0
     function );
132
133
0
    return( -1 );
134
0
  }
135
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
136
0
  {
137
0
    libcerror_error_set(
138
0
     error,
139
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
140
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
141
0
     "%s: invalid byte stream size value exceeds maximum.",
142
0
     function );
143
144
0
    return( -1 );
145
0
  }
146
0
  if( byte_stream_index == NULL )
147
0
  {
148
0
    libcerror_error_set(
149
0
     error,
150
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
151
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
152
0
     "%s: invalid byte stream index.",
153
0
     function );
154
155
0
    return( -1 );
156
0
  }
157
0
  safe_byte_stream_index = *byte_stream_index;
158
159
0
  if( safe_byte_stream_index >= byte_stream_size )
160
0
  {
161
0
    libcerror_error_set(
162
0
     error,
163
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
164
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
165
0
     "%s: byte stream too small.",
166
0
     function );
167
168
0
    return( -1 );
169
0
  }
170
0
  byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
171
172
0
  if( byte_stream_character < 0x20 )
173
0
  {
174
0
    safe_unicode_character = byte_stream_character;
175
0
  }
176
0
  else
177
0
  {
178
0
    byte_stream_character -= 0x20;
179
180
0
    safe_unicode_character = libuna_codepage_mac_dingbats_byte_stream_to_unicode_base_0x20[ byte_stream_character ];
181
0
  }
182
0
  *unicode_character = safe_unicode_character;
183
0
  *byte_stream_index = safe_byte_stream_index;
184
185
0
  return( 1 );
186
0
}
187
188
/* Copies an Unicode character to a MacDingbats encoded byte stream
189
 * Returns 1 if successful or -1 on error
190
 */
191
int libuna_codepage_mac_dingbats_copy_to_byte_stream(
192
     libuna_unicode_character_t unicode_character,
193
     uint8_t *byte_stream,
194
     size_t byte_stream_size,
195
     size_t *byte_stream_index,
196
     libcerror_error_t **error )
197
0
{
198
0
  static char *function         = "libuna_codepage_mac_dingbats_copy_to_byte_stream";
199
0
  size_t safe_byte_stream_index = 0;
200
0
  uint16_t byte_stream_value    = 0x001a;
201
202
0
  if( byte_stream == NULL )
203
0
  {
204
0
    libcerror_error_set(
205
0
     error,
206
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
207
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
208
0
     "%s: invalid byte stream.",
209
0
     function );
210
211
0
    return( -1 );
212
0
  }
213
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
214
0
  {
215
0
    libcerror_error_set(
216
0
     error,
217
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
218
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
219
0
     "%s: invalid byte stream size value exceeds maximum.",
220
0
     function );
221
222
0
    return( -1 );
223
0
  }
224
0
  if( byte_stream_index == NULL )
225
0
  {
226
0
    libcerror_error_set(
227
0
     error,
228
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
229
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
230
0
     "%s: invalid byte stream index.",
231
0
     function );
232
233
0
    return( -1 );
234
0
  }
235
0
  safe_byte_stream_index = *byte_stream_index;
236
237
0
  if( safe_byte_stream_index >= byte_stream_size )
238
0
  {
239
0
    libcerror_error_set(
240
0
     error,
241
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
242
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
243
0
     "%s: byte stream too small.",
244
0
     function );
245
246
0
    return( -1 );
247
0
  }
248
0
  if( ( unicode_character <= 0x0020 )
249
0
   || ( unicode_character == 0x007f ) )
250
0
  {
251
0
    byte_stream_value = (uint16_t) unicode_character;
252
0
  }
253
0
  else if( ( unicode_character >= 0x2460 )
254
0
        && ( unicode_character < 0x2470 ) )
255
0
  {
256
0
    unicode_character -= 0x2460;
257
258
0
    byte_stream_value = libuna_codepage_mac_dingbats_unicode_to_byte_stream_base_0x2460[ unicode_character ];
259
0
  }
260
0
  else if( ( unicode_character >= 0x2700 )
261
0
        && ( unicode_character < 0x27c0 ) )
262
0
  {
263
0
    unicode_character -= 0x2700;
264
265
0
    byte_stream_value = libuna_codepage_mac_dingbats_unicode_to_byte_stream_base_0x2700[ unicode_character ];
266
0
  }
267
0
  else switch( unicode_character )
268
0
  {
269
0
    case 0x2192:
270
0
      byte_stream_value = 0xd5;
271
0
      break;
272
273
0
    case 0x2194:
274
0
      byte_stream_value = 0xd6;
275
0
      break;
276
277
0
    case 0x2195:
278
0
      byte_stream_value = 0xd7;
279
0
      break;
280
281
0
    case 0x25a0:
282
0
      byte_stream_value = 0x6e;
283
0
      break;
284
285
0
    case 0x25b2:
286
0
      byte_stream_value = 0x73;
287
0
      break;
288
289
0
    case 0x25bc:
290
0
      byte_stream_value = 0x74;
291
0
      break;
292
293
0
    case 0x25c6:
294
0
      byte_stream_value = 0x75;
295
0
      break;
296
297
0
    case 0x25cf:
298
0
      byte_stream_value = 0x6c;
299
0
      break;
300
301
0
    case 0x25d7:
302
0
      byte_stream_value = 0x77;
303
0
      break;
304
305
0
    case 0x2605:
306
0
      byte_stream_value = 0x48;
307
0
      break;
308
309
0
    case 0x260e:
310
0
      byte_stream_value = 0x25;
311
0
      break;
312
313
0
    case 0x261b:
314
0
      byte_stream_value = 0x2a;
315
0
      break;
316
317
0
    case 0x261e:
318
0
      byte_stream_value = 0x2b;
319
0
      break;
320
321
0
    case 0x2660:
322
0
      byte_stream_value = 0xab;
323
0
      break;
324
325
0
    case 0x2663:
326
0
      byte_stream_value = 0xa8;
327
0
      break;
328
329
0
    case 0x2665:
330
0
      byte_stream_value = 0xaa;
331
0
      break;
332
333
0
    case 0x2666:
334
0
      byte_stream_value = 0xa9;
335
0
      break;
336
337
0
    default:
338
0
      byte_stream_value = 0x1a;
339
0
      break;
340
0
  }
341
0
  byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
342
343
0
  *byte_stream_index = safe_byte_stream_index;
344
345
0
  return( 1 );
346
0
}
347