Refactor ZOOM record render code and fix #2892.
[yaz-moved-to-github.git] / src / ucs4.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief UCS4 decoding and encoding
8  */
9
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <assert.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <ctype.h>
18
19 #include "iconv-p.h"
20
21 static unsigned long read_UCS4(yaz_iconv_t cd, yaz_iconv_decoder_t d,
22                                unsigned char *inp,
23                                size_t inbytesleft, size_t *no_read)
24 {
25     unsigned long x = 0;
26     
27     if (inbytesleft < 4)
28     {
29         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
30         *no_read = 0;
31     }
32     else
33     {
34         x = (inp[0]<<24) | (inp[1]<<16) | (inp[2]<<8) | inp[3];
35         *no_read = 4;
36     }
37     return x;
38 }
39
40 static unsigned long read_UCS4LE(yaz_iconv_t cd, yaz_iconv_decoder_t d,
41                                  unsigned char *inp,
42                                  size_t inbytesleft, size_t *no_read)
43 {
44     unsigned long x = 0;
45     
46     if (inbytesleft < 4)
47     {
48         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
49         *no_read = 0;
50     }
51     else
52     {
53         x = (inp[3]<<24) | (inp[2]<<16) | (inp[1]<<8) | inp[0];
54         *no_read = 4;
55     }
56     return x;
57 }
58
59 static size_t write_UCS4(yaz_iconv_t cd, yaz_iconv_encoder_t en,
60                          unsigned long x,
61                          char **outbuf, size_t *outbytesleft)
62 {
63     unsigned char *outp = (unsigned char *) *outbuf;
64     if (*outbytesleft >= 4)
65     {
66         *outp++ = (unsigned char) (x>>24);
67         *outp++ = (unsigned char) (x>>16);
68         *outp++ = (unsigned char) (x>>8);
69         *outp++ = (unsigned char) x;
70         (*outbytesleft) -= 4;
71     }
72     else
73     {
74         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
75         return (size_t)(-1);
76     }
77     *outbuf = (char *) outp;
78     return 0;
79 }
80
81 static size_t write_UCS4LE(yaz_iconv_t cd, yaz_iconv_encoder_t en,
82                            unsigned long x,
83                            char **outbuf, size_t *outbytesleft)
84 {
85     unsigned char *outp = (unsigned char *) *outbuf;
86     if (*outbytesleft >= 4)
87     {
88         *outp++ = (unsigned char) x;
89         *outp++ = (unsigned char) (x>>8);
90         *outp++ = (unsigned char) (x>>16);
91         *outp++ = (unsigned char) (x>>24);
92         (*outbytesleft) -= 4;
93     }
94     else
95     {
96         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
97         return (size_t)(-1);
98     }
99     *outbuf = (char *) outp;
100     return 0;
101 }
102
103
104 yaz_iconv_encoder_t yaz_ucs4_encoder(const char *tocode,
105                                      yaz_iconv_encoder_t e)
106     
107 {
108     if (!yaz_matchstr(tocode, "UCS4"))
109         e->write_handle = write_UCS4;
110     else if (!yaz_matchstr(tocode, "UCS4LE"))
111         e->write_handle = write_UCS4LE;
112     else
113         return 0;
114     return e;
115 }
116
117 yaz_iconv_decoder_t yaz_ucs4_decoder(const char *tocode,
118                                      yaz_iconv_decoder_t d)
119     
120 {
121     if (!yaz_matchstr(tocode, "UCS4"))
122         d->read_handle = read_UCS4;
123     else if (!yaz_matchstr(tocode, "UCS4LE"))
124         d->read_handle = read_UCS4LE;
125     else
126         return 0;
127     return d;
128 }
129
130
131
132 /*
133  * Local variables:
134  * c-basic-offset: 4
135  * c-file-style: "Stroustrup"
136  * indent-tabs-mode: nil
137  * End:
138  * vim: shiftwidth=4 tabstop=8 expandtab
139  */
140