some fixes for xml dumper / still buggy.
[libfirm] / ir / tv / strcalc.c
index 97bc58d..8a2d9ed 100644 (file)
@@ -12,7 +12,7 @@
 #include <assert.h>   /* assertions */
 #include <string.h>   /* memset/memcmp */
 #include <stdio.h>    /* output for error messages */
-#include <values.h>   /* definition of MIN_LONG, used in sc_get_val_from_long */
+#include <limits.h>   /* definition of LONG_MIN, used in sc_get_val_from_long */
 
 /*
  * local definitions and macros
@@ -607,11 +607,12 @@ static void _divmod(const char *dividend, const char *divisor, char *quot, char
   memset(quot, SC_0, CALC_BUFFER_SIZE);
   memset(rem, SC_0, CALC_BUFFER_SIZE);
 
+  /* if the divisor is zero this won't work (quot is zero) */
+  if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
+
   /* if the dividend is zero result is zero (quot is zero)*/
   if (sc_comp(dividend, quot) == 0)
     return;
-  /* if the divisor is zero this won't work (quot is zero) */
-  if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
 
   if (_sign(dividend) == -1)
   {
@@ -783,7 +784,7 @@ static void _shr(const char *val1, char *buffer, long offset, int radius, unsign
       break;
     }
   }
-  if ((carry_flag == 0) && (_val(val1[counter]) & shift != 0))
+  if ((carry_flag == 0) && (_val(val1[counter]) & shift) != 0)
     carry_flag = 1;
 
   /* shift digits to the right with offset, carry and all */
@@ -832,13 +833,6 @@ static void _rot(const char *val1, char *buffer, long offset, int radius, unsign
   char temp1[CALC_BUFFER_SIZE];
   char temp2[CALC_BUFFER_SIZE];
 
-  const char *shl;
-  char carry = SC_0;
-
-  int counter, old_counter;
-  int shift;
-  int bitoffset;
-
   offset = offset % radius;
 
   /* rotation by multiples of the typelength is identity */
@@ -1215,7 +1209,6 @@ int sc_get_highest_set_bit(const void *value)
 {
   const char *val = (const char*)value;
   int high, counter;
-  char sign;
 
   high = CALC_BUFFER_SIZE * 4;
 
@@ -1292,6 +1285,7 @@ unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
 
 /*
  * convert to a string
+ * XXX Doesn't check buffer bounds
  */
 const char *sc_print(const void *value, unsigned bits, enum base_t base)
 {
@@ -1311,8 +1305,8 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base)
   char *pos;
   const char *digits = small_digits;
 
-  pos = output_buffer + BIT_PATTERN_SIZE;
-  *pos = '\0';
+  pos = output_buffer + BIT_PATTERN_SIZE ;
+  *(--pos) = '\0';
 
   /* special case */
   if (bits == 0) {
@@ -1443,8 +1437,8 @@ void init_strcalc(int precision_in_bytes)
     CALC_BUFFER_SIZE = (4 * precision_in_bytes);
     MAX_VALUE_SIZE   = (2 * precision_in_bytes);
 
-    calc_buffer = malloc(CALC_BUFFER_SIZE * sizeof(char));
-    output_buffer = malloc(BIT_PATTERN_SIZE * sizeof(char));
+    calc_buffer = malloc(CALC_BUFFER_SIZE+1 * sizeof(char));
+    output_buffer = malloc(BIT_PATTERN_SIZE+1 * sizeof(char));
 
     if (calc_buffer == NULL || output_buffer == NULL)
     {