summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib
diff options
context:
space:
mode:
author R. Belmont <rb6502@users.noreply.github.com>2012-06-17 20:00:37 +0000
committer R. Belmont <rb6502@users.noreply.github.com>2012-06-17 20:00:37 +0000
commit6b1346e86504fabfe3e33a1baca108319f7f1661 (patch)
tree695dfd78d8b7bf2bd4e5b8787232b79087762609 /src/lib
parenta2c84ce32b7ce67162053c621b045f90ba3bd9ce (diff)
CHD fixes: [jmak]
- Fixed uninitialized variables that were creeping into output - Changed qsort() callback to never return "equal", working around unstable system libc implementations
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/util/cdrom.c1
-rw-r--r--src/lib/util/chd.c1
-rw-r--r--src/lib/util/huffman.c23
3 files changed, 22 insertions, 3 deletions
diff --git a/src/lib/util/cdrom.c b/src/lib/util/cdrom.c
index 2a5b2d4eb2a..964051b6fcf 100644
--- a/src/lib/util/cdrom.c
+++ b/src/lib/util/cdrom.c
@@ -734,6 +734,7 @@ chd_error cdrom_parse_metadata(chd_file *chd, cdrom_toc *toc)
{
/* parse the metadata */
type[0] = subtype[0] = 0;
+ pgtype[0] = pgsub[0] = 0;
if (sscanf(metadata, CDROM_TRACK_METADATA_FORMAT, &tracknum, type, subtype, &frames) != 4)
return CHDERR_INVALID_DATA;
if (tracknum == 0 || tracknum > CD_MAX_TRACKS)
diff --git a/src/lib/util/chd.c b/src/lib/util/chd.c
index 28a4563ed1f..dd373c05c6a 100644
--- a/src/lib/util/chd.c
+++ b/src/lib/util/chd.c
@@ -1788,6 +1788,7 @@ chd_error chd_file::compress_v5_map()
compressed[12] = lengthbits;
compressed[13] = selfbits;
compressed[14] = parentbits;
+ compressed[15] = 0;
// write the result
m_mapoffset = file_append(compressed, complen + 16);
diff --git a/src/lib/util/huffman.c b/src/lib/util/huffman.c
index 21ef7adc029..60120bbdf32 100644
--- a/src/lib/util/huffman.c
+++ b/src/lib/util/huffman.c
@@ -535,7 +535,11 @@ int CLIB_DECL huffman_context_base::tree_node_compare(const void *item1, const v
{
const node_t *node1 = *(const node_t **)item1;
const node_t *node2 = *(const node_t **)item2;
- return node2->m_weight - node1->m_weight;
+ if (node2->m_weight != node1->m_weight)
+ return node2->m_weight - node1->m_weight;
+ if (node2->m_bits - node1->m_bits == 0)
+ fprintf(stderr, "identical node sort keys, should not happen!\n");
+ return (int)node1->m_bits - (int)node2->m_bits;
}
@@ -555,16 +559,28 @@ int huffman_context_base::build_tree(UINT32 totaldata, UINT32 totalweight)
{
list[listitems++] = &m_huffnode[curcode];
m_huffnode[curcode].m_count = m_datahisto[curcode];
+ m_huffnode[curcode].m_bits = curcode;
// scale the weight by the current effective length, ensuring we don't go to 0
m_huffnode[curcode].m_weight = UINT64(m_datahisto[curcode]) * UINT64(totalweight) / UINT64(totaldata);
if (m_huffnode[curcode].m_weight == 0)
m_huffnode[curcode].m_weight = 1;
}
-
+/*
+ fprintf(stderr, "Pre-sort:\n");
+ for (int i = 0; i < listitems; i++) {
+ fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits);
+ }
+*/
// sort the list by weight, largest weight first
qsort(list, listitems, sizeof(list[0]), tree_node_compare);
-
+/*
+ fprintf(stderr, "Post-sort:\n");
+ for (int i = 0; i < listitems; i++) {
+ fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits);
+ }
+ fprintf(stderr, "===================\n");
+*/
// now build the tree
int nextalloc = m_numcodes;
while (listitems > 1)
@@ -597,6 +613,7 @@ int huffman_context_base::build_tree(UINT32 totaldata, UINT32 totalweight)
{
node_t &node = m_huffnode[curcode];
node.m_numbits = 0;
+ node.m_bits = 0;
// if we have a non-zero weight, compute the number of bits
if (node.m_weight > 0)