Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libesedb/libuna/libuna_codepage_windows_1252.c
Line
Count
Source
1
/*
2
 * Windows 1252 codepage (Western European/Latin 1) functions
3
 *
4
 * Copyright (C) 2008-2026, 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_windows_1252.h"
26
#include "libuna_libcerror.h"
27
#include "libuna_types.h"
28
29
/* Extended ASCII to Unicode character lookup table for the Windows 1252 codepage
30
 * Unknown are filled with the Unicode replacement character 0xfffd
31
 */
32
static const uint16_t libuna_codepage_windows_1252_byte_stream_to_unicode_base_0x80[ 32 ] = {
33
  0x20ac, 0xfffd, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
34
  0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0xfffd, 0x017d, 0xfffd,
35
  0xfffd, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
36
  0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0xfffd, 0x017e, 0x0178
37
};
38
39
/* Unicode to ASCII character lookup tables for the Windows 1252 codepage
40
 * Unknown are filled with the ASCII replacement character 0x1a
41
 */
42
static const uint8_t libuna_codepage_windows_1252_unicode_to_byte_stream_base_0x2010[ 24 ] = {
43
  0x1a, 0x1a, 0x1a, 0x96, 0x97, 0x1a, 0x1a, 0x1a,
44
  0x91, 0x92, 0x82, 0x1a, 0x93, 0x94, 0x84, 0x1a,
45
  0x86, 0x87, 0x95, 0x1a, 0x1a, 0x1a, 0x85, 0x1a
46
};
47
48
/* Copies an Unicode character from a Windows 1252 encoded byte stream
49
 * Returns 1 if successful or -1 on error
50
 */
51
int libuna_codepage_windows_1252_copy_from_byte_stream(
52
     libuna_unicode_character_t *unicode_character,
53
     const uint8_t *byte_stream,
54
     size_t byte_stream_size,
55
     size_t *byte_stream_index,
56
     libcerror_error_t **error )
57
95.2M
{
58
95.2M
  static char *function                             = "libuna_codepage_windows_1252_copy_from_byte_stream";
59
95.2M
  libuna_unicode_character_t safe_unicode_character = 0xfffd;
60
95.2M
  size_t safe_byte_stream_index                     = 0;
61
95.2M
  uint8_t byte_stream_character                     = 0;
62
63
95.2M
  if( unicode_character == NULL )
64
0
  {
65
0
    libcerror_error_set(
66
0
     error,
67
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
68
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
69
0
     "%s: invalid Unicode character.",
70
0
     function );
71
72
0
    return( -1 );
73
0
  }
74
95.2M
  if( byte_stream == NULL )
75
0
  {
76
0
    libcerror_error_set(
77
0
     error,
78
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
79
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
80
0
     "%s: invalid byte stream.",
81
0
     function );
82
83
0
    return( -1 );
84
0
  }
85
95.2M
  if( byte_stream_size > (size_t) SSIZE_MAX )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
90
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
91
0
     "%s: invalid byte stream size value exceeds maximum.",
92
0
     function );
93
94
0
    return( -1 );
95
0
  }
96
95.2M
  if( byte_stream_index == NULL )
97
0
  {
98
0
    libcerror_error_set(
99
0
     error,
100
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
101
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
102
0
     "%s: invalid byte stream index.",
103
0
     function );
104
105
0
    return( -1 );
106
0
  }
107
95.2M
  safe_byte_stream_index = *byte_stream_index;
108
109
95.2M
  if( safe_byte_stream_index >= byte_stream_size )
110
0
  {
111
0
    libcerror_error_set(
112
0
     error,
113
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
114
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
115
0
     "%s: byte stream too small.",
116
0
     function );
117
118
0
    return( -1 );
119
0
  }
120
95.2M
  byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
121
122
95.2M
  if( ( byte_stream_character < 0x80 )
123
10.6M
   || ( byte_stream_character >= 0xa0 ) )
124
91.1M
  {
125
91.1M
    safe_unicode_character = byte_stream_character;
126
91.1M
  }
127
4.11M
  else
128
4.11M
  {
129
4.11M
    byte_stream_character -= 0x80;
130
131
4.11M
    safe_unicode_character = libuna_codepage_windows_1252_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
132
4.11M
  }
133
95.2M
  *unicode_character = safe_unicode_character;
134
95.2M
  *byte_stream_index = safe_byte_stream_index;
135
136
95.2M
  return( 1 );
137
95.2M
}
138
139
/* Copies an Unicode character to a Windows 1252 encoded byte stream
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libuna_codepage_windows_1252_copy_to_byte_stream(
143
     libuna_unicode_character_t unicode_character,
144
     uint8_t *byte_stream,
145
     size_t byte_stream_size,
146
     size_t *byte_stream_index,
147
     libcerror_error_t **error )
148
72.8M
{
149
72.8M
  static char *function         = "libuna_codepage_windows_1252_copy_to_byte_stream";
150
72.8M
  size_t safe_byte_stream_index = 0;
151
72.8M
  uint16_t byte_stream_value    = 0x001a;
152
153
72.8M
  if( byte_stream == NULL )
154
0
  {
155
0
    libcerror_error_set(
156
0
     error,
157
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159
0
     "%s: invalid byte stream.",
160
0
     function );
161
162
0
    return( -1 );
163
0
  }
164
72.8M
  if( byte_stream_size > (size_t) SSIZE_MAX )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
170
0
     "%s: invalid byte stream size value exceeds maximum.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
72.8M
  if( byte_stream_index == NULL )
176
0
  {
177
0
    libcerror_error_set(
178
0
     error,
179
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
180
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
181
0
     "%s: invalid byte stream index.",
182
0
     function );
183
184
0
    return( -1 );
185
0
  }
186
72.8M
  safe_byte_stream_index = *byte_stream_index;
187
188
72.8M
  if( safe_byte_stream_index >= byte_stream_size )
189
0
  {
190
0
    libcerror_error_set(
191
0
     error,
192
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
193
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
194
0
     "%s: byte stream too small.",
195
0
     function );
196
197
0
    return( -1 );
198
0
  }
199
72.8M
  if( unicode_character < 0x0080 )
200
40.3M
  {
201
40.3M
    byte_stream_value = (uint16_t) unicode_character;
202
40.3M
  }
203
32.4M
  else if( ( unicode_character >= 0x00a0 )
204
32.4M
        && ( unicode_character < 0x0100 ) )
205
1.91M
  {
206
1.91M
    byte_stream_value = (uint16_t) unicode_character;
207
1.91M
  }
208
30.5M
  else if( ( unicode_character >= 0x2010 )
209
28.3M
        && ( unicode_character < 0x2028 ) )
210
1.78M
  {
211
1.78M
    unicode_character -= 0x2010;
212
213
1.78M
    byte_stream_value = libuna_codepage_windows_1252_unicode_to_byte_stream_base_0x2010[ unicode_character ];
214
1.78M
  }
215
28.7M
  else switch( unicode_character )
216
28.7M
  {
217
6.15k
    case 0x0152:
218
6.15k
      byte_stream_value = 0x8c;
219
6.15k
      break;
220
221
130k
    case 0x0153:
222
130k
      byte_stream_value = 0x9c;
223
130k
      break;
224
225
115k
    case 0x0160:
226
115k
      byte_stream_value = 0x8a;
227
115k
      break;
228
229
5.04k
    case 0x0161:
230
5.04k
      byte_stream_value = 0x9a;
231
5.04k
      break;
232
233
4.44k
    case 0x0178:
234
4.44k
      byte_stream_value = 0x9f;
235
4.44k
      break;
236
237
137k
    case 0x017d:
238
137k
      byte_stream_value = 0x8e;
239
137k
      break;
240
241
4.65k
    case 0x017e:
242
4.65k
      byte_stream_value = 0x9e;
243
4.65k
      break;
244
245
9.25k
    case 0x0192:
246
9.25k
      byte_stream_value = 0x83;
247
9.25k
      break;
248
249
5.40k
    case 0x02c6:
250
5.40k
      byte_stream_value = 0x88;
251
5.40k
      break;
252
253
7.83k
    case 0x02dc:
254
7.83k
      byte_stream_value = 0x98;
255
7.83k
      break;
256
257
7.38k
    case 0x2030:
258
7.38k
      byte_stream_value = 0x89;
259
7.38k
      break;
260
261
7.69k
    case 0x2039:
262
7.69k
      byte_stream_value = 0x8b;
263
7.69k
      break;
264
265
5.98k
    case 0x203a:
266
5.98k
      byte_stream_value = 0x9b;
267
5.98k
      break;
268
269
1.33M
    case 0x20ac:
270
1.33M
      byte_stream_value = 0x80;
271
1.33M
      break;
272
273
4.76k
    case 0x2122:
274
4.76k
      byte_stream_value = 0x99;
275
4.76k
      break;
276
277
26.9M
    default:
278
26.9M
      byte_stream_value = 0x1a;
279
26.9M
      break;
280
28.7M
  }
281
72.8M
  byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
282
283
72.8M
  *byte_stream_index = safe_byte_stream_index;
284
285
72.8M
  return( 1 );
286
72.8M
}
287