Fix yaz-marcdump segfault YAZ-801
[yaz-moved-to-github.git] / src / ucs4.c
index dedf4ec..8b677c4 100644 (file)
@@ -1,12 +1,10 @@
-/*
- * Copyright (C) 1995-2008, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
- *
- * $Id: siconv.c,v 1.50 2008-03-12 08:53:28 adam Exp $
  */
 /**
  * \file
- * \brief ISO-5428 character mapping (iconv)
+ * \brief UCS4 decoding and encoding
  */
 
 #if HAVE_CONFIG_H
 #include <assert.h>
 #include <errno.h>
 #include <string.h>
-#include <ctype.h>
 
 #include "iconv-p.h"
 
-unsigned long yaz_read_UCS4(yaz_iconv_t cd, unsigned char *inp,
-                            size_t inbytesleft, size_t *no_read)
+static unsigned long read_UCS4(yaz_iconv_t cd, yaz_iconv_decoder_t d,
+                               unsigned char *inp,
+                               size_t inbytesleft, size_t *no_read)
 {
     unsigned long x = 0;
-    
+
     if (inbytesleft < 4)
     {
         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
@@ -38,11 +36,12 @@ unsigned long yaz_read_UCS4(yaz_iconv_t cd, unsigned char *inp,
     return x;
 }
 
-unsigned long yaz_read_UCS4LE(yaz_iconv_t cd, unsigned char *inp,
-                              size_t inbytesleft, size_t *no_read)
+static unsigned long read_UCS4LE(yaz_iconv_t cd, yaz_iconv_decoder_t d,
+                                 unsigned char *inp,
+                                 size_t inbytesleft, size_t *no_read)
 {
     unsigned long x = 0;
-    
+
     if (inbytesleft < 4)
     {
         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
@@ -56,8 +55,9 @@ unsigned long yaz_read_UCS4LE(yaz_iconv_t cd, unsigned char *inp,
     return x;
 }
 
-size_t yaz_write_UCS4(yaz_iconv_t cd, unsigned long x,
-                      char **outbuf, size_t *outbytesleft)
+static size_t write_UCS4(yaz_iconv_t cd, yaz_iconv_encoder_t en,
+                         unsigned long x,
+                         char **outbuf, size_t *outbytesleft)
 {
     unsigned char *outp = (unsigned char *) *outbuf;
     if (*outbytesleft >= 4)
@@ -77,8 +77,9 @@ size_t yaz_write_UCS4(yaz_iconv_t cd, unsigned long x,
     return 0;
 }
 
-size_t yaz_write_UCS4LE(yaz_iconv_t cd, unsigned long x,
-                        char **outbuf, size_t *outbytesleft)
+static size_t write_UCS4LE(yaz_iconv_t cd, yaz_iconv_encoder_t en,
+                           unsigned long x,
+                           char **outbuf, size_t *outbytesleft)
 {
     unsigned char *outp = (unsigned char *) *outbuf;
     if (*outbytesleft >= 4)
@@ -99,11 +100,40 @@ size_t yaz_write_UCS4LE(yaz_iconv_t cd, unsigned long x,
 }
 
 
+yaz_iconv_encoder_t yaz_ucs4_encoder(const char *tocode,
+                                     yaz_iconv_encoder_t e)
+
+{
+    if (!yaz_matchstr(tocode, "UCS4"))
+        e->write_handle = write_UCS4;
+    else if (!yaz_matchstr(tocode, "UCS4LE"))
+        e->write_handle = write_UCS4LE;
+    else
+        return 0;
+    return e;
+}
+
+yaz_iconv_decoder_t yaz_ucs4_decoder(const char *tocode,
+                                     yaz_iconv_decoder_t d)
+
+{
+    if (!yaz_matchstr(tocode, "UCS4"))
+        d->read_handle = read_UCS4;
+    else if (!yaz_matchstr(tocode, "UCS4LE"))
+        d->read_handle = read_UCS4LE;
+    else
+        return 0;
+    return d;
+}
+
+
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+