zoslib
z/OS C/C++ Library
Loading...
Searching...
No Matches
zos-char-util.h
Go to the documentation of this file.
1
2// Licensed Materials - Property of IBM
3// ZOSLIB
4// (C) Copyright IBM Corp. 2020. All Rights Reserved.
5// US Government Users Restricted Rights - Use, duplication
6// or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
8
9// APIs that implement Coded Character Sets (ASCII, EBCDIC) processing of
10// strings and files, and their conversion.
11
12#ifndef ZOS_CHAR_UTIL_H_
13#define ZOS_CHAR_UTIL_H_
14
15#include "zos-macros.h"
16
17#include <_Nascii.h>
18#include <sys/types.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
31__Z_EXPORT void *_convert_e2a(void *dst, const void *src, size_t size);
32
40__Z_EXPORT void *_convert_a2e(void *dst, const void *src, size_t size);
41
51__Z_EXPORT int __guess_fd_ue(int fd, char *errmsg, size_t er_size,
52 int is_new_fd);
53
61__Z_EXPORT int __guess_ue(const void *src, size_t size, char *errmsg,
62 size_t er_size);
63
70__Z_EXPORT int __guess_ae(const void *src, size_t size);
71
75__Z_EXPORT int conv_utf8_utf16(char *, size_t, const char *, size_t);
76
80__Z_EXPORT int conv_utf16_utf8(char *, size_t, const char *, size_t);
81
82#if DEBUG_ONLY
89__Z_EXPORT size_t __e2a_l(char *bufptr, size_t szLen);
90
97__Z_EXPORT size_t __a2e_l(char *bufptr, size_t szLen);
98
104__Z_EXPORT size_t __e2a_s(char *string);
105
111__Z_EXPORT size_t __a2e_s(char *string);
112#endif
113
121__Z_EXPORT void __set_autocvt_on_fd_stream(int fd, unsigned short ccsid,
122 unsigned char txtflag,
123 int on_untagged_only);
124
132
139__Z_EXPORT int __file_needs_conversion_init(const char *name, int fd);
140
145__Z_EXPORT void __fd_close(int fd);
146
147#define _str_e2a(_str) \
148 ({ \
149 const char *src = (const char *)(_str); \
150 int len = strlen(src) + 1; \
151 char tgt[len]; \
152 (char *)_convert_e2a(tgt, src, len); \
153 })
154
155#define _str_a2e(_str) \
156 ({ \
157 const char *src = (const char *)(_str); \
158 int len = strlen(src) + 1; \
159 char tgt[len]; \
160 (char *)_convert_a2e(tgt, src, len); \
161 })
162
163#define AEWRAP(_rc, _x) \
164 (__isASCII() ? ((_rc) = (_x), 0) \
165 : (__ae_thread_swapmode(__AE_ASCII_MODE), ((_rc) = (_x)), \
166 __ae_thread_swapmode(__AE_EBCDIC_MODE), 1))
167
168#define AEWRAP_VOID(_x) \
169 (__isASCII() ? ((_x), 0) \
170 : (__ae_thread_swapmode(__AE_ASCII_MODE), (_x), \
171 __ae_thread_swapmode(__AE_EBCDIC_MODE), 1))
172
173inline void* __Z_EXPORT __convert_one_to_one(const void *table, void *dst,
174 size_t size, const void *src) {
175 void *rst = dst;
176 __asm volatile(" troo 2,%2,1 \n jo *-4 \n"
177 : "+NR:r3"(size), "+NR:r2"(dst), "+r"(src)
178 : "NR:r1"(table)
179 : "r0");
180 return rst;
181}
182
183#ifdef __cplusplus
184
185class __Z_EXPORT __auto_ascii {
186 int ascii_mode;
187
188public:
189 __auto_ascii();
190 ~__auto_ascii();
191};
192
193class __Z_EXPORT __conv_off {
194 int convert_state;
195
196public:
197 __conv_off();
198 ~__conv_off();
199};
200
201#endif // ifdef __cplusplus
202
203inline unsigned strlen_ae(const unsigned char *str, int *code_page,
204 int max_len, int *ambiguous) {
205 static int last_ccsid = 819;
206 static const unsigned char _tab_a[256] __attribute__((aligned(8))) = {
207 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
208 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
211 0, 0, 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, 0, 0, 0, 0, 0,
227 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
228 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
229 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
230 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
231 };
232 unsigned long bytes;
233 unsigned long code_out;
234 const unsigned char *start;
235
236 bytes = max_len;
237 code_out = 0;
238 start = str;
239 __asm volatile(" trte %1,%3,0\n"
240 " jo *-4\n"
241 : "+NR:r3"(bytes), "+NR:r2"(str), "+r"(bytes), "+r"(code_out)
242 : "NR:r1"(_tab_a)
243 :);
244 unsigned a_len = str - start;
245
246 bytes = max_len;
247 code_out = 0;
248 str = start;
249 __asm volatile(" trte %1,%3,0\n"
250 " jo *-4\n"
251 : "+NR:r3"(bytes), "+NR:r2"(str), "+r"(bytes), "+r"(code_out)
252 : "NR:r1"(_tab_e)
253 :);
254 unsigned e_len = str - start;
255 if (a_len > e_len) {
256 *code_page = 819;
257 last_ccsid = 819;
258 *ambiguous = 0;
259 return a_len;
260 } else if (e_len > a_len) {
261 *code_page = 1047;
262 last_ccsid = 1047;
263 *ambiguous = 0;
264 return e_len;
265 }
266 *code_page = last_ccsid;
267 *ambiguous = 1;
268 return a_len;
269}
270
271inline unsigned strlen_e(const unsigned char *str, unsigned size) {
272 static const unsigned char _tab_e[256] __attribute__((aligned(8))) = {
273 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
274 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
275 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
276 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
277 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
278 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
279 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
280 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
282 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
284 };
285
286 unsigned long bytes = size;
287 unsigned long code_out = 0;
288 const unsigned char *start = str;
289
290 __asm volatile(" trte %1,%3,0\n"
291 " jo *-4\n"
292 : "+NR:r3"(bytes), "+NR:r2"(str), "+r"(bytes), "+r"(code_out)
293 : "NR:r1"(_tab_e)
294 :);
295
296 return str - start;
297}
298
299const unsigned char __ibm1047_iso88591[256] __attribute__((aligned(8))) = {
300 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, 0x97, 0x8d, 0x8e, 0x0b,
301 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x9d, 0x0a, 0x08, 0x87,
302 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, 0x80, 0x81, 0x82, 0x83,
303 0x84, 0x85, 0x17, 0x1b, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07,
304 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9a, 0x9b,
305 0x14, 0x15, 0x9e, 0x1a, 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
306 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, 0x26, 0xe9, 0xea, 0xeb,
307 0xe8, 0xed, 0xee, 0xef, 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e,
308 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, 0xc7, 0xd1, 0xa6, 0x2c,
309 0x25, 0x5f, 0x3e, 0x3f, 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
310 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, 0xd8, 0x61, 0x62, 0x63,
311 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1,
312 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0xaa, 0xba,
313 0xe6, 0xb8, 0xc6, 0xa4, 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
314 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, 0xac, 0xa3, 0xa5, 0xb7,
315 0xa9, 0xa7, 0xb6, 0xbc, 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7,
316 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xad, 0xf4,
317 0xf6, 0xf2, 0xf3, 0xf5, 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
318 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, 0x5c, 0xf7, 0x53, 0x54,
319 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5,
320 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xb3, 0xdb,
321 0xdc, 0xd9, 0xda, 0x9f};
322
323const unsigned char __iso88591_ibm1047[256] __attribute__((aligned(8))) = {
324 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x15, 0x0b,
325 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26,
326 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, 0x40, 0x5a, 0x7f, 0x7b,
327 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
328 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e,
329 0x4c, 0x7e, 0x6e, 0x6f, 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
330 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xe2,
331 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d,
332 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92,
333 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
334 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, 0x20, 0x21, 0x22, 0x23,
335 0x24, 0x25, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b,
336 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b,
337 0x04, 0x14, 0x3e, 0xff, 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5,
338 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, 0x90, 0x8f, 0xea, 0xfa,
339 0xbe, 0xa0, 0xb6, 0xb3, 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab,
340 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, 0x74, 0x71, 0x72, 0x73,
341 0x78, 0x75, 0x76, 0x77, 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf,
342 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, 0x44, 0x45, 0x42, 0x46,
343 0x43, 0x47, 0x9c, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,
344 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, 0x70, 0xdd, 0xde, 0xdb,
345 0xdc, 0x8d, 0x8e, 0xdf};
346
347#ifdef __cplusplus
348}
349#endif
350#endif // ZOS_CHAR_UTIL_H_
__Z_EXPORT void __set_autocvt_on_fd_stream(int fd, unsigned short ccsid, unsigned char txtflag, int on_untagged_only)
Definition zos-char-util.cc:565
__Z_EXPORT void __fd_close(int fd)
Definition zos-char-util.cc:498
__Z_EXPORT int conv_utf16_utf8(char *, size_t, const char *, size_t)
Definition zos-char-util.cc:439
__Z_EXPORT int __guess_ue(const void *src, size_t size, char *errmsg, size_t er_size)
Definition zos-char-util.cc:232
__Z_EXPORT void * _convert_a2e(void *dst, const void *src, size_t size)
Definition zos-char-util.cc:221
__Z_EXPORT int __guess_ae(const void *src, size_t size)
Definition zos-char-util.cc:289
unsigned strlen_ae(const unsigned char *str, int *code_page, int max_len, int *ambiguous)
Definition zos-char-util.h:203
__Z_EXPORT int __guess_fd_ue(int fd, char *errmsg, size_t er_size, int is_new_fd)
Definition zos-char-util.cc:258
__Z_EXPORT int conv_utf8_utf16(char *, size_t, const char *, size_t)
Definition zos-char-util.cc:435
const unsigned char __ibm1047_iso88591[256] __attribute__((aligned(8)))
__Z_EXPORT void * _convert_e2a(void *dst, const void *src, size_t size)
Definition zos-char-util.cc:210
__Z_EXPORT int __file_needs_conversion_init(const char *name, int fd)
Definition zos-char-util.cc:513
unsigned strlen_e(const unsigned char *str, unsigned size)
Definition zos-char-util.h:271
__Z_EXPORT int __file_needs_conversion(int fd)
Definition zos-char-util.cc:503
void *__Z_EXPORT __convert_one_to_one(const void *table, void *dst, size_t size, const void *src)
Definition zos-char-util.h:173
#define __Z_EXPORT
Definition zos-macros.h:13