Fix decoding of Windows XP proprietary tags on big-endian machines.
[platform/upstream/libexif.git] / libexif / exif-utils.c
index bdcbef7..9083ddc 100644 (file)
@@ -1,6 +1,6 @@
 /* exif-utils.c
  *
- * Copyright © 2001 Lutz Müller <lutz@users.sourceforge.net>
+ * Copyright (c) 2001 Lutz Mueller <lutz@users.sourceforge.net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -14,8 +14,8 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA.
  */
 
 #include <config.h>
@@ -213,39 +213,45 @@ exif_set_srational (unsigned char *buf, ExifByteOrder order,
        exif_set_slong (buf + 4, order, value.denominator);
 }
 
+/*! This function converts rather UCS-2LE than UTF-16 to UTF-8.
+ * It should really be replaced by iconv().
+ */
 void
-exif_convert_utf16_to_utf8 (char *out, const unsigned short *in, int maxlen)
+exif_convert_utf16_to_utf8 (char *out, const unsigned char *in, int maxlen)
 {
-       /* This function converts rather UCS2 than UTF16 to UTF8 */
        if (maxlen <= 0) {
                return;
        }
-       while (*in) {
-               if (*in < 0x80) {
+       for (;;) {
+               ExifShort v = exif_get_short(in, EXIF_BYTE_ORDER_INTEL);
+               if (!v)
+                       break;
+               if (v < 0x80) {
                        if (maxlen > 1) {
-                               *out++ = (char)*in++;
+                               *out++ = (char)v;
                                maxlen--;
                        } else {
                                break;
                        }
-               } else if (*in < 0x800) {
+               } else if (v < 0x800) {
                        if (maxlen > 2) {
-                               *out++ = ((*in >> 6) & 0x1F) | 0xC0;
-                               *out++ = (*in++ & 0x3F) | 0x80;
+                               *out++ = ((v >> 6) & 0x1F) | 0xC0;
+                               *out++ = (v & 0x3F) | 0x80;
                                maxlen -= 2;
                        } else {
                                break;
                        }
                } else {
-                       if (maxlen > 2) {
-                               *out++ = ((*in >> 12) & 0x0F) | 0xE0;
-                               *out++ = ((*in >> 6) & 0x3F) | 0x80;
-                               *out++ = (*in++ & 0x3F) | 0x80;
+                       if (maxlen > 3) {
+                               *out++ = ((v >> 12) & 0x0F) | 0xE0;
+                               *out++ = ((v >> 6) & 0x3F) | 0x80;
+                               *out++ = (v & 0x3F) | 0x80;
                                maxlen -= 3;
                        } else {
                                break;
                        }
                }
+               in += 2;
        }
        *out = 0;
-}
\ No newline at end of file
+}