Factor iconv conversions to separate C files.
[yaz-moved-to-github.git] / src / ucs4.c
1 /*
2  * Copyright (C) 1995-2008, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: siconv.c,v 1.50 2008-03-12 08:53:28 adam Exp $
6  */
7 /**
8  * \file
9  * \brief ISO-5428 character mapping (iconv)
10  */
11
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include <assert.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 #include "iconv-p.h"
22
23 unsigned long yaz_read_UCS4(yaz_iconv_t cd, unsigned char *inp,
24                             size_t inbytesleft, size_t *no_read)
25 {
26     unsigned long x = 0;
27     
28     if (inbytesleft < 4)
29     {
30         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
31         *no_read = 0;
32     }
33     else
34     {
35         x = (inp[0]<<24) | (inp[1]<<16) | (inp[2]<<8) | inp[3];
36         *no_read = 4;
37     }
38     return x;
39 }
40
41 unsigned long yaz_read_UCS4LE(yaz_iconv_t cd, 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 size_t yaz_write_UCS4(yaz_iconv_t cd, unsigned long x,
60                       char **outbuf, size_t *outbytesleft)
61 {
62     unsigned char *outp = (unsigned char *) *outbuf;
63     if (*outbytesleft >= 4)
64     {
65         *outp++ = (unsigned char) (x>>24);
66         *outp++ = (unsigned char) (x>>16);
67         *outp++ = (unsigned char) (x>>8);
68         *outp++ = (unsigned char) x;
69         (*outbytesleft) -= 4;
70     }
71     else
72     {
73         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
74         return (size_t)(-1);
75     }
76     *outbuf = (char *) outp;
77     return 0;
78 }
79
80 size_t yaz_write_UCS4LE(yaz_iconv_t cd, 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
103 /*
104  * Local variables:
105  * c-basic-offset: 4
106  * indent-tabs-mode: nil
107  * End:
108  * vim: shiftwidth=4 tabstop=8 expandtab
109  */