This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Test-Simple-0.65.
[perl5.git] / ext / Encode / Encode / encode.h
1 #ifndef ENCODE_H
2 #define ENCODE_H
3
4 #ifndef U8
5 /* 
6    A tad devious this:
7    perl normally has a #define for U8 - if that isn't present then we
8    typedef it - leaving it #ifndef so we can do data parts without
9    getting extern references to the code parts
10 */
11 typedef unsigned char U8;
12 #endif
13
14 typedef struct encpage_s encpage_t;
15
16 struct encpage_s
17 {
18     /* fields ordered to pack nicely on 32-bit machines */
19     const U8 *const seq;   /* Packed output sequences we generate 
20                   if we match */
21     const encpage_t *const next;      /* Page to go to if we match */
22     const U8   min;        /* Min value of octet to match this entry */
23     const U8   max;        /* Max value of octet to match this entry */
24     const U8   dlen;       /* destination length - 
25                   size of entries in seq */
26     const U8   slen;       /* source length - 
27                   number of source octets needed */
28 };
29
30 /*
31   At any point in a translation there is a page pointer which points
32   at an array of the above structures.
33
34   Basic operation :
35   get octet from source stream.
36   if (octet >= min && octet < max) {
37     if slen is 0 then we cannot represent this character.
38     if we have less than slen octets (including this one) then 
39       we have a partial character.
40     otherwise
41       copy dlen octets from seq + dlen*(octet-min) to output
42       (dlen may be zero if we don't know yet.)
43       load page pointer with next to continue.
44       (is slen is one this is end of a character)
45       get next octet.
46   }
47   else {
48     increment the page pointer to look at next slot in the array
49   }
50
51   arrays SHALL be constructed so there is an entry which matches
52   ..0xFF at the end, and either maps it or indicates no
53   representation.
54
55   if MSB of slen is set then mapping is an approximate "FALLBACK" entry.
56
57 */
58
59
60 typedef struct encode_s encode_t;
61 struct encode_s
62 {
63     const encpage_t *const t_utf8;  /* Starting table for translation from 
64                        the encoding to UTF-8 form */
65     const encpage_t *const f_utf8;  /* Starting table for translation 
66                        from UTF-8 to the encoding */
67     const U8 *const rep;            /* Replacement character in this
68                        encoding e.g. "?" */
69     int        replen;              /* Number of octets in rep */
70     U8         min_el;              /* Minimum octets to represent a
71                        character */
72     U8         max_el;              /* Maximum octets to represent a
73                        character */
74     const char *const name[2];      /* name(s) of this encoding */
75 };
76
77 #ifdef U8
78 /* See comment at top of file for deviousness */
79
80 extern int do_encode(const encpage_t *enc, const U8 *src, STRLEN *slen,
81                      U8 *dst, STRLEN dlen, STRLEN *dout, int approx,
82              const U8 *term, STRLEN tlen);
83
84 extern void Encode_DefineEncoding(encode_t *enc);
85
86 #endif /* U8 */
87
88 #define ENCODE_NOSPACE  1
89 #define ENCODE_PARTIAL  2
90 #define ENCODE_NOREP    3
91 #define ENCODE_FALLBACK 4
92 #define ENCODE_FOUND_TERM 5
93
94 #define FBCHAR_UTF8             "\xEF\xBF\xBD"
95
96 #define  ENCODE_DIE_ON_ERR     0x0001 /* croaks immediately */
97 #define  ENCODE_WARN_ON_ERR    0x0002 /* warn on error; may proceed */
98 #define  ENCODE_RETURN_ON_ERR  0x0004 /* immediately returns on NOREP */
99 #define  ENCODE_LEAVE_SRC      0x0008 /* $src updated unless set */
100 #define  ENCODE_PERLQQ         0x0100 /* perlqq fallback string */
101 #define  ENCODE_HTMLCREF       0x0200 /* HTML character ref. fb mode */
102 #define  ENCODE_XMLCREF        0x0400 /* XML  character ref. fb mode */
103 #define  ENCODE_STOP_AT_PARTIAL 0x0800 /* stop at partial explicitly */
104
105 #define  ENCODE_FB_DEFAULT     0x0000
106 #define  ENCODE_FB_CROAK       0x0001
107 #define  ENCODE_FB_QUIET       ENCODE_RETURN_ON_ERR
108 #define  ENCODE_FB_WARN        (ENCODE_RETURN_ON_ERR|ENCODE_WARN_ON_ERR)
109 #define  ENCODE_FB_PERLQQ      (ENCODE_PERLQQ|ENCODE_LEAVE_SRC)
110 #define  ENCODE_FB_HTMLCREF    (ENCODE_HTMLCREF|ENCODE_LEAVE_SRC)
111 #define  ENCODE_FB_XMLCREF     (ENCODE_XMLCREF|ENCODE_LEAVE_SRC)
112
113 #endif /* ENCODE_H */