From 02029d218d53b4b8cd619a6cddb771b1642dd556 Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Fri, 2 Jan 2004 20:38:59 +0000 Subject: [PATCH] gst/matroska/: fix signed integer reading/writing. Original commit message from CVS: 2004-01-02 Ronald Bultje * gst/matroska/ebml-read.c: (gst_ebml_read_sint): * gst/matroska/ebml-write.c: (gst_ebml_write_sint): fix signed integer reading/writing. --- ChangeLog | 6 ++++++ gst/matroska/ebml-read.c | 15 ++++++++++----- gst/matroska/ebml-write.c | 8 +++++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index fe641b7f38..93989dda39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-01-02 Ronald Bultje + + * gst/matroska/ebml-read.c: (gst_ebml_read_sint): + * gst/matroska/ebml-write.c: (gst_ebml_write_sint): + fix signed integer reading/writing. + 2004-01-02 Benjamin Otte * ext/alsa/README: diff --git a/gst/matroska/ebml-read.c b/gst/matroska/ebml-read.c index 67b9b5f34d..c5b9c0daaa 100644 --- a/gst/matroska/ebml-read.c +++ b/gst/matroska/ebml-read.c @@ -464,7 +464,7 @@ gst_ebml_read_sint (GstEbmlRead *ebml, { GstBuffer *buf; guint8 *data; - guint size; + guint size, negative = 0, n = 0; if (!gst_ebml_read_buffer (ebml, id, &buf)) return FALSE; @@ -478,14 +478,19 @@ gst_ebml_read_sint (GstEbmlRead *ebml, gst_buffer_unref (buf); return FALSE; } + if (data[0] & 0x80) { + negative = 1; + data[0] &= ~0x80; + } *num = 0; - while (size > 0) { - *num = (*num << 8) | data[GST_BUFFER_SIZE (buf) - size]; - size--; + while (n < size) { + *num = (*num << 8) | data[n++]; } /* make signed */ - *num -= (1LL << ((8 * GST_BUFFER_SIZE (buf)) - 1)); + if (negative) { + *num = *num - (1LL << ((8 * size) - 1)); + } gst_buffer_unref (buf); diff --git a/gst/matroska/ebml-write.c b/gst/matroska/ebml-write.c index 46cfd4a543..b650486707 100644 --- a/gst/matroska/ebml-write.c +++ b/gst/matroska/ebml-write.c @@ -391,7 +391,13 @@ gst_ebml_write_sint (GstEbmlWrite *ebml, guint size = gst_ebml_write_get_uint_size (unum); /* make unsigned */ - unum = (num < 0 ? -num : num) + (1LLU << ((8 * size) - 1)); + if (num >= 0) { + unum = num; + } else { + unum = 0x80 << (size - 1); + unum += num; + unum |= 0x80 << (size - 1); + } /* write */ gst_ebml_write_element_id (buf, id);