/src/libesedb/libuna/libuna_codepage_mac_roman.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * MacRoman 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_roman.h" | 
| 26 |  | #include "libuna_libcerror.h" | 
| 27 |  | #include "libuna_types.h" | 
| 28 |  |  | 
| 29 |  | /* Extended ASCII to Unicode character lookup table for the MacRoman codepage | 
| 30 |  |  * Unknown are filled with the Unicode replacement character 0xfffd | 
| 31 |  |  */ | 
| 32 |  | const uint16_t libuna_codepage_mac_roman_byte_stream_to_unicode_base_0x80[ 128 ] = { | 
| 33 |  |   0x00c4, 0x00c5, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1, | 
| 34 |  |   0x00e0, 0x00e2, 0x00e4, 0x00e3, 0x00e5, 0x00e7, 0x00e9, 0x00e8, | 
| 35 |  |   0x00ea, 0x00eb, 0x00ed, 0x00ec, 0x00ee, 0x00ef, 0x00f1, 0x00f3, | 
| 36 |  |   0x00f2, 0x00f4, 0x00f6, 0x00f5, 0x00fa, 0x00f9, 0x00fb, 0x00fc, | 
| 37 |  |   0x2020, 0x00b0, 0x00a2, 0x00a3, 0x00a7, 0x2022, 0x00b6, 0x00df, | 
| 38 |  |   0x00ae, 0x00a9, 0x2122, 0x00b4, 0x00a8, 0x2260, 0x00c6, 0x00d8, | 
| 39 |  |   0x221e, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x00b5, 0x2202, 0x2211, | 
| 40 |  |   0x220f, 0x03c0, 0x222b, 0x00aa, 0x00ba, 0x03a9, 0x00e6, 0x00f8, | 
| 41 |  |   0x00bf, 0x00a1, 0x00ac, 0x221a, 0x0192, 0x2248, 0x2206, 0x00ab, | 
| 42 |  |   0x00bb, 0x2026, 0x00a0, 0x00c0, 0x00c3, 0x00d5, 0x0152, 0x0153, | 
| 43 |  |   0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x25ca, | 
| 44 |  |   0x00ff, 0x0178, 0x2044, 0x20ac, 0x2039, 0x203a, 0xfb01, 0xfb02, | 
| 45 |  |   0x2021, 0x00b7, 0x201a, 0x201e, 0x2030, 0x00c2, 0x00ca, 0x00c1, | 
| 46 |  |   0x00cb, 0x00c8, 0x00cd, 0x00ce, 0x00cf, 0x00cc, 0x00d3, 0x00d4, | 
| 47 |  |   0xf8ff, 0x00d2, 0x00da, 0x00db, 0x00d9, 0x0131, 0x02c6, 0x02dc, | 
| 48 |  |   0x00af, 0x02d8, 0x02d9, 0x02da, 0x00b8, 0x02dd, 0x02db, 0x02c7 | 
| 49 |  | }; | 
| 50 |  |  | 
| 51 |  | /* Unicode to ASCII character lookup tables for the MacRoman codepage | 
| 52 |  |  * Unknown are filled with the ASCII replacement character 0x1a | 
| 53 |  |  */ | 
| 54 |  | const uint8_t libuna_codepage_mac_roman_unicode_to_byte_stream_base_0x00a0[ 96 ] = { | 
| 55 |  |   0xca, 0xc1, 0xa2, 0xa3, 0x1a, 0xb4, 0x1a, 0xa4, | 
| 56 |  |   0xac, 0xa9, 0xbb, 0xc7, 0xc2, 0x1a, 0xa8, 0xf8, | 
| 57 |  |   0xa1, 0xb1, 0x1a, 0x1a, 0xab, 0xb5, 0xa6, 0xe1, | 
| 58 |  |   0xfc, 0x1a, 0xbc, 0xc8, 0x1a, 0x1a, 0x1a, 0xc0, | 
| 59 |  |   0xcb, 0xe7, 0xe5, 0xcc, 0x80, 0x81, 0xae, 0x82, | 
| 60 |  |   0xe9, 0x83, 0xe6, 0xe8, 0xed, 0xea, 0xeb, 0xec, | 
| 61 |  |   0x1a, 0x84, 0xf1, 0xee, 0xef, 0xcd, 0x85, 0x1a, | 
| 62 |  |   0xaf, 0xf4, 0xf2, 0xf3, 0x86, 0x1a, 0x1a, 0xa7, | 
| 63 |  |   0x88, 0x87, 0x89, 0x8b, 0x8a, 0x8c, 0xbe, 0x8d, | 
| 64 |  |   0x8f, 0x8e, 0x90, 0x91, 0x93, 0x92, 0x94, 0x95, | 
| 65 |  |   0x1a, 0x96, 0x98, 0x97, 0x99, 0x9b, 0x9a, 0xd6, | 
| 66 |  |   0xbf, 0x9d, 0x9c, 0x9e, 0x9f, 0x1a, 0x1a, 0xd8 | 
| 67 |  | }; | 
| 68 |  |  | 
| 69 |  | const uint8_t libuna_codepage_mac_roman_unicode_to_byte_stream_base_0x2010[ 56 ] = { | 
| 70 |  |   0x1a, 0x1a, 0x1a, 0xd0, 0xd1, 0x1a, 0x1a, 0x1a, | 
| 71 |  |   0xd4, 0xd5, 0xe2, 0x1a, 0xd2, 0xd3, 0xe3, 0x1a, | 
| 72 |  |   0xa0, 0xe0, 0xa5, 0x1a, 0x1a, 0x1a, 0xc9, 0x1a, | 
| 73 |  |   0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, | 
| 74 |  |   0xe4, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, | 
| 75 |  |   0x1a, 0xdc, 0xdd, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, | 
| 76 |  |   0x1a, 0x1a, 0x1a, 0x1a, 0xda, 0x1a, 0x1a, 0x1a | 
| 77 |  | }; | 
| 78 |  |  | 
| 79 |  | /* Copies an Unicode character from a MacRoman encoded byte stream | 
| 80 |  |  * Returns 1 if successful or -1 on error | 
| 81 |  |  */ | 
| 82 |  | int libuna_codepage_mac_roman_copy_from_byte_stream( | 
| 83 |  |      libuna_unicode_character_t *unicode_character, | 
| 84 |  |      const uint8_t *byte_stream, | 
| 85 |  |      size_t byte_stream_size, | 
| 86 |  |      size_t *byte_stream_index, | 
| 87 |  |      libcerror_error_t **error ) | 
| 88 | 0 | { | 
| 89 | 0 |   static char *function                             = "libuna_codepage_mac_roman_copy_from_byte_stream"; | 
| 90 | 0 |   libuna_unicode_character_t safe_unicode_character = 0xfffd; | 
| 91 | 0 |   size_t safe_byte_stream_index                     = 0; | 
| 92 | 0 |   uint8_t byte_stream_character                     = 0; | 
| 93 |  | 
 | 
| 94 | 0 |   if( unicode_character == NULL ) | 
| 95 | 0 |   { | 
| 96 | 0 |     libcerror_error_set( | 
| 97 | 0 |      error, | 
| 98 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 99 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 100 | 0 |      "%s: invalid Unicode character.", | 
| 101 | 0 |      function ); | 
| 102 |  | 
 | 
| 103 | 0 |     return( -1 ); | 
| 104 | 0 |   } | 
| 105 | 0 |   if( byte_stream == 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 byte stream.", | 
| 112 | 0 |      function ); | 
| 113 |  | 
 | 
| 114 | 0 |     return( -1 ); | 
| 115 | 0 |   } | 
| 116 | 0 |   if( byte_stream_size > (size_t) SSIZE_MAX ) | 
| 117 | 0 |   { | 
| 118 | 0 |     libcerror_error_set( | 
| 119 | 0 |      error, | 
| 120 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 121 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, | 
| 122 | 0 |      "%s: invalid byte stream size value exceeds maximum.", | 
| 123 | 0 |      function ); | 
| 124 |  | 
 | 
| 125 | 0 |     return( -1 ); | 
| 126 | 0 |   } | 
| 127 | 0 |   if( byte_stream_index == NULL ) | 
| 128 | 0 |   { | 
| 129 | 0 |     libcerror_error_set( | 
| 130 | 0 |      error, | 
| 131 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 132 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 133 | 0 |      "%s: invalid byte stream index.", | 
| 134 | 0 |      function ); | 
| 135 |  | 
 | 
| 136 | 0 |     return( -1 ); | 
| 137 | 0 |   } | 
| 138 | 0 |   safe_byte_stream_index = *byte_stream_index; | 
| 139 |  | 
 | 
| 140 | 0 |   if( safe_byte_stream_index >= byte_stream_size ) | 
| 141 | 0 |   { | 
| 142 | 0 |     libcerror_error_set( | 
| 143 | 0 |      error, | 
| 144 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 145 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, | 
| 146 | 0 |      "%s: byte stream too small.", | 
| 147 | 0 |      function ); | 
| 148 |  | 
 | 
| 149 | 0 |     return( -1 ); | 
| 150 | 0 |   } | 
| 151 | 0 |   byte_stream_character = byte_stream[ safe_byte_stream_index++ ]; | 
| 152 |  | 
 | 
| 153 | 0 |   if( byte_stream_character < 0x80 ) | 
| 154 | 0 |   { | 
| 155 | 0 |     safe_unicode_character = byte_stream_character; | 
| 156 | 0 |   } | 
| 157 | 0 |   else | 
| 158 | 0 |   { | 
| 159 | 0 |     byte_stream_character -= 0x80; | 
| 160 |  | 
 | 
| 161 | 0 |     safe_unicode_character = libuna_codepage_mac_roman_byte_stream_to_unicode_base_0x80[ byte_stream_character ]; | 
| 162 | 0 |   } | 
| 163 | 0 |   *unicode_character = safe_unicode_character; | 
| 164 | 0 |   *byte_stream_index = safe_byte_stream_index; | 
| 165 |  | 
 | 
| 166 | 0 |   return( 1 ); | 
| 167 | 0 | } | 
| 168 |  |  | 
| 169 |  | /* Copies an Unicode character to a MacRoman encoded byte stream | 
| 170 |  |  * Returns 1 if successful or -1 on error | 
| 171 |  |  */ | 
| 172 |  | int libuna_codepage_mac_roman_copy_to_byte_stream( | 
| 173 |  |      libuna_unicode_character_t unicode_character, | 
| 174 |  |      uint8_t *byte_stream, | 
| 175 |  |      size_t byte_stream_size, | 
| 176 |  |      size_t *byte_stream_index, | 
| 177 |  |      libcerror_error_t **error ) | 
| 178 | 0 | { | 
| 179 | 0 |   static char *function         = "libuna_codepage_mac_roman_copy_to_byte_stream"; | 
| 180 | 0 |   size_t safe_byte_stream_index = 0; | 
| 181 | 0 |   uint16_t byte_stream_value    = 0x001a; | 
| 182 |  | 
 | 
| 183 | 0 |   if( byte_stream == NULL ) | 
| 184 | 0 |   { | 
| 185 | 0 |     libcerror_error_set( | 
| 186 | 0 |      error, | 
| 187 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 188 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 189 | 0 |      "%s: invalid byte stream.", | 
| 190 | 0 |      function ); | 
| 191 |  | 
 | 
| 192 | 0 |     return( -1 ); | 
| 193 | 0 |   } | 
| 194 | 0 |   if( byte_stream_size > (size_t) SSIZE_MAX ) | 
| 195 | 0 |   { | 
| 196 | 0 |     libcerror_error_set( | 
| 197 | 0 |      error, | 
| 198 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 199 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, | 
| 200 | 0 |      "%s: invalid byte stream size value exceeds maximum.", | 
| 201 | 0 |      function ); | 
| 202 |  | 
 | 
| 203 | 0 |     return( -1 ); | 
| 204 | 0 |   } | 
| 205 | 0 |   if( byte_stream_index == NULL ) | 
| 206 | 0 |   { | 
| 207 | 0 |     libcerror_error_set( | 
| 208 | 0 |      error, | 
| 209 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 210 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 211 | 0 |      "%s: invalid byte stream index.", | 
| 212 | 0 |      function ); | 
| 213 |  | 
 | 
| 214 | 0 |     return( -1 ); | 
| 215 | 0 |   } | 
| 216 | 0 |   safe_byte_stream_index = *byte_stream_index; | 
| 217 |  | 
 | 
| 218 | 0 |   if( safe_byte_stream_index >= byte_stream_size ) | 
| 219 | 0 |   { | 
| 220 | 0 |     libcerror_error_set( | 
| 221 | 0 |      error, | 
| 222 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 223 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, | 
| 224 | 0 |      "%s: byte stream too small.", | 
| 225 | 0 |      function ); | 
| 226 |  | 
 | 
| 227 | 0 |     return( -1 ); | 
| 228 | 0 |   } | 
| 229 | 0 |   if( unicode_character < 0x0080 ) | 
| 230 | 0 |   { | 
| 231 | 0 |     byte_stream_value = (uint16_t) unicode_character; | 
| 232 | 0 |   } | 
| 233 | 0 |   else if( ( unicode_character >= 0x00a0 ) | 
| 234 | 0 |         && ( unicode_character < 0x0100 ) ) | 
| 235 | 0 |   { | 
| 236 | 0 |     unicode_character -= 0x00a0; | 
| 237 |  | 
 | 
| 238 | 0 |     byte_stream_value = libuna_codepage_mac_roman_unicode_to_byte_stream_base_0x00a0[ unicode_character ]; | 
| 239 | 0 |   } | 
| 240 | 0 |   else if( ( unicode_character >= 0x2010 ) | 
| 241 | 0 |         && ( unicode_character < 0x2048 ) ) | 
| 242 | 0 |   { | 
| 243 | 0 |     unicode_character -= 0x2010; | 
| 244 |  | 
 | 
| 245 | 0 |     byte_stream_value = libuna_codepage_mac_roman_unicode_to_byte_stream_base_0x2010[ unicode_character ]; | 
| 246 | 0 |   } | 
| 247 | 0 |   else switch( unicode_character ) | 
| 248 | 0 |   { | 
| 249 | 0 |     case 0x0131: | 
| 250 | 0 |       byte_stream_value = 0xf5; | 
| 251 | 0 |       break; | 
| 252 |  |  | 
| 253 | 0 |     case 0x0152: | 
| 254 | 0 |       byte_stream_value = 0xce; | 
| 255 | 0 |       break; | 
| 256 |  |  | 
| 257 | 0 |     case 0x0153: | 
| 258 | 0 |       byte_stream_value = 0xcf; | 
| 259 | 0 |       break; | 
| 260 |  |  | 
| 261 | 0 |     case 0x0178: | 
| 262 | 0 |       byte_stream_value = 0xd9; | 
| 263 | 0 |       break; | 
| 264 |  |  | 
| 265 | 0 |     case 0x0192: | 
| 266 | 0 |       byte_stream_value = 0xc4; | 
| 267 | 0 |       break; | 
| 268 |  |  | 
| 269 | 0 |     case 0x02c6: | 
| 270 | 0 |       byte_stream_value = 0xf6; | 
| 271 | 0 |       break; | 
| 272 |  |  | 
| 273 | 0 |     case 0x02c7: | 
| 274 | 0 |       byte_stream_value = 0xff; | 
| 275 | 0 |       break; | 
| 276 |  |  | 
| 277 | 0 |     case 0x02d8: | 
| 278 | 0 |       byte_stream_value = 0xf9; | 
| 279 | 0 |       break; | 
| 280 |  |  | 
| 281 | 0 |     case 0x02d9: | 
| 282 | 0 |       byte_stream_value = 0xfa; | 
| 283 | 0 |       break; | 
| 284 |  |  | 
| 285 | 0 |     case 0x02da: | 
| 286 | 0 |       byte_stream_value = 0xfb; | 
| 287 | 0 |       break; | 
| 288 |  |  | 
| 289 | 0 |     case 0x02db: | 
| 290 | 0 |       byte_stream_value = 0xfe; | 
| 291 | 0 |       break; | 
| 292 |  |  | 
| 293 | 0 |     case 0x02dc: | 
| 294 | 0 |       byte_stream_value = 0xf7; | 
| 295 | 0 |       break; | 
| 296 |  |  | 
| 297 | 0 |     case 0x02dd: | 
| 298 | 0 |       byte_stream_value = 0xfd; | 
| 299 | 0 |       break; | 
| 300 |  |  | 
| 301 | 0 |     case 0x03a9: | 
| 302 | 0 |       byte_stream_value = 0xbd; | 
| 303 | 0 |       break; | 
| 304 |  |  | 
| 305 | 0 |     case 0x03c0: | 
| 306 | 0 |       byte_stream_value = 0xb9; | 
| 307 | 0 |       break; | 
| 308 |  |  | 
| 309 | 0 |     case 0x20ac: | 
| 310 | 0 |       byte_stream_value = 0xdb; | 
| 311 | 0 |       break; | 
| 312 |  |  | 
| 313 | 0 |     case 0x2122: | 
| 314 | 0 |       byte_stream_value = 0xaa; | 
| 315 | 0 |       break; | 
| 316 |  |  | 
| 317 | 0 |     case 0x2202: | 
| 318 | 0 |       byte_stream_value = 0xb6; | 
| 319 | 0 |       break; | 
| 320 |  |  | 
| 321 | 0 |     case 0x2206: | 
| 322 | 0 |       byte_stream_value = 0xc6; | 
| 323 | 0 |       break; | 
| 324 |  |  | 
| 325 | 0 |     case 0x220f: | 
| 326 | 0 |       byte_stream_value = 0xb8; | 
| 327 | 0 |       break; | 
| 328 |  |  | 
| 329 | 0 |     case 0x2211: | 
| 330 | 0 |       byte_stream_value = 0xb7; | 
| 331 | 0 |       break; | 
| 332 |  |  | 
| 333 | 0 |     case 0x221a: | 
| 334 | 0 |       byte_stream_value = 0xc3; | 
| 335 | 0 |       break; | 
| 336 |  |  | 
| 337 | 0 |     case 0x221e: | 
| 338 | 0 |       byte_stream_value = 0xb0; | 
| 339 | 0 |       break; | 
| 340 |  |  | 
| 341 | 0 |     case 0x222b: | 
| 342 | 0 |       byte_stream_value = 0xba; | 
| 343 | 0 |       break; | 
| 344 |  |  | 
| 345 | 0 |     case 0x2248: | 
| 346 | 0 |       byte_stream_value = 0xc5; | 
| 347 | 0 |       break; | 
| 348 |  |  | 
| 349 | 0 |     case 0x2260: | 
| 350 | 0 |       byte_stream_value = 0xad; | 
| 351 | 0 |       break; | 
| 352 |  |  | 
| 353 | 0 |     case 0x2264: | 
| 354 | 0 |       byte_stream_value = 0xb2; | 
| 355 | 0 |       break; | 
| 356 |  |  | 
| 357 | 0 |     case 0x2265: | 
| 358 | 0 |       byte_stream_value = 0xb3; | 
| 359 | 0 |       break; | 
| 360 |  |  | 
| 361 | 0 |     case 0x25ca: | 
| 362 | 0 |       byte_stream_value = 0xd7; | 
| 363 | 0 |       break; | 
| 364 |  |  | 
| 365 | 0 |     case 0xf8ff: | 
| 366 | 0 |       byte_stream_value = 0xf0; | 
| 367 | 0 |       break; | 
| 368 |  |  | 
| 369 | 0 |     case 0xfb01: | 
| 370 | 0 |       byte_stream_value = 0xde; | 
| 371 | 0 |       break; | 
| 372 |  |  | 
| 373 | 0 |     case 0xfb02: | 
| 374 | 0 |       byte_stream_value = 0xdf; | 
| 375 | 0 |       break; | 
| 376 |  |  | 
| 377 | 0 |     default: | 
| 378 | 0 |       byte_stream_value = 0x1a; | 
| 379 | 0 |       break; | 
| 380 | 0 |   } | 
| 381 | 0 |   byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff ); | 
| 382 |  | 
 | 
| 383 | 0 |   *byte_stream_index = safe_byte_stream_index; | 
| 384 |  | 
 | 
| 385 | 0 |   return( 1 ); | 
| 386 | 0 | } | 
| 387 |  |  |