d298224b57bc15090c00bf5d1fa3346a6fe86cf2
[yaz-moved-to-github.git] / src / ucs4.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 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 unsigned long yaz_read_UCS4(yaz_iconv_t cd, unsigned char *inp,
22                             size_t inbytesleft, size_t *no_read)
23 {
24     unsigned long x = 0;
25     
26     if (inbytesleft < 4)
27     {
28         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
29         *no_read = 0;
30     }
31     else
32     {
33         x = (inp[0]<<24) | (inp[1]<<16) | (inp[2]<<8) | inp[3];
34         *no_read = 4;
35     }
36     return x;
37 }
38
39 unsigned long yaz_read_UCS4LE(yaz_iconv_t cd, unsigned char *inp,
40                               size_t inbytesleft, size_t *no_read)
41 {
42     unsigned long x = 0;
43     
44     if (inbytesleft < 4)
45     {
46         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
47         *no_read = 0;
48     }
49     else
50     {
51         x = (inp[3]<<24) | (inp[2]<<16) | (inp[1]<<8) | inp[0];
52         *no_read = 4;
53     }
54     return x;
55 }
56
57 static size_t write_UCS4(yaz_iconv_t cd, yaz_iconv_encoder_t en,
58                          unsigned long x,
59                          char **outbuf, size_t *outbytesleft)
60 {
61     unsigned char *outp = (unsigned char *) *outbuf;
62     if (*outbytesleft >= 4)
63     {
64         *outp++ = (unsigned char) (x>>24);
65         *outp++ = (unsigned char) (x>>16);
66         *outp++ = (unsigned char) (x>>8);
67         *outp++ = (unsigned char) x;
68         (*outbytesleft) -= 4;
69     }
70     else
71     {
72         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
73         return (size_t)(-1);
74     }
75     *outbuf = (char *) outp;
76     return 0;
77 }
78
79 static size_t write_UCS4LE(yaz_iconv_t cd, yaz_iconv_encoder_t en,
80                            unsigned long x,
81                            char **outbuf, size_t *outbytesleft)
82 {
83     unsigned char *outp = (unsigned char *) *outbuf;
84     if (*outbytesleft >= 4)
85     {
86         *outp++ = (unsigned char) x;
87         *outp++ = (unsigned char) (x>>8);
88         *outp++ = (unsigned char) (x>>16);
89         *outp++ = (unsigned char) (x>>24);
90         (*outbytesleft) -= 4;
91     }
92     else
93     {
94         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
95         return (size_t)(-1);
96     }
97     *outbuf = (char *) outp;
98     return 0;
99 }
100
101
102 yaz_iconv_encoder_t yaz_ucs4_encoder(const char *tocode,
103                                      yaz_iconv_encoder_t e)
104     
105 {
106     if (!yaz_matchstr(tocode, "UCS4"))
107         e->write_handle = write_UCS4;
108     else if (!yaz_matchstr(tocode, "UCS4LE"))
109         e->write_handle = write_UCS4LE;
110     else
111         return 0;
112     return e;
113 }
114
115
116
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * indent-tabs-mode: nil
121  * End:
122  * vim: shiftwidth=4 tabstop=8 expandtab
123  */