-fixes
[oweals/gnunet.git] / src / util / crypto_hash.c
index 2ab682494118e85733d728b1a7d2475742fe934c..f896390455bfd5321c0fa89fcdd79ff1524a0e6b 100644 (file)
@@ -105,6 +105,11 @@ struct GNUNET_CRYPTO_FileHashContext
    */
   GNUNET_SCHEDULER_TaskIdentifier task;
 
+  /**
+   * Priority we use.
+   */
+  enum GNUNET_SCHEDULER_Priority priority;
+
   /**
    * Blocksize.
    */
@@ -162,7 +167,8 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     file_hash_finish (fhc, res);
     return;
   }
-  fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc);
+  fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
+                                                 &file_hash_task, fhc);
 }
 
 
@@ -213,6 +219,7 @@ GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
     GNUNET_free (fhc);
     return NULL;
   }
+  fhc->priority = priority;
   fhc->task =
       GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
   return fhc;
@@ -234,9 +241,16 @@ GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
 }
 
 
-
 /* ***************** binary-ASCII encoding *************** */
 
+/* FIXME: should use GNUNET_STRINGS_data_to_string and strings_to_data below!!! */
+
+/**
+ * Get the numeric value corresponding to a character.
+ *
+ * @param a a character
+ * @return corresponding numeric value
+ */
 static unsigned int
 getValue__ (unsigned char a)
 {
@@ -247,6 +261,7 @@ getValue__ (unsigned char a)
   return -1;
 }
 
+
 /**
  * Convert GNUNET_CRYPTO_hash to ASCII encoding.  The ASCII encoding is rather
  * GNUnet specific.  It was chosen such that it only uses characters
@@ -299,32 +314,40 @@ GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
   result->encoding[wpos] = '\0';
 }
 
+
 /**
  * Convert ASCII encoding back to GNUNET_CRYPTO_hash
  *
  * @param enc the encoding
+ * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
  * @param result where to store the GNUNET_CRYPTO_hash code
  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
  */
 int
-GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
+GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
+                                GNUNET_HashCode * result)
 {
   unsigned int rpos;
   unsigned int wpos;
   unsigned int bits;
   unsigned int vbit;
+  int ret;
 
-  if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
+  if (enclen != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
     return GNUNET_SYSERR;
 
   vbit = 2;                     /* padding! */
   wpos = sizeof (GNUNET_HashCode);
   rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
-  bits = getValue__ (enc[--rpos]) >> 3;
+  bits = (ret = getValue__ (enc[--rpos])) >> 3;
+  if (-1 == ret)
+    return GNUNET_SYSERR;
   while (wpos > 0)
   {
     GNUNET_assert (rpos > 0);
-    bits = (getValue__ (enc[--rpos]) << vbit) | bits;
+    bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
+    if (-1 == ret)
+      return GNUNET_SYSERR;
     vbit += 5;
     if (vbit >= 8)
     {
@@ -338,6 +361,7 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
   return GNUNET_OK;
 }
 
+
 /**
  * Compute the distance between 2 hashcodes.  The computation must be
  * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
@@ -359,6 +383,13 @@ GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
   return (x1 * x2);
 }
 
+
+/**
+ * Create a random hash code.
+ *
+ * @param mode desired quality level
+ * @param result hash code that is randomized
+ */
 void
 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
                                   GNUNET_HashCode * result)
@@ -369,6 +400,14 @@ GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
     result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
 }
 
+
+/**
+ * compute result(delta) = b - a
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to b - a
+ */
 void
 GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
                                const GNUNET_HashCode * b,
@@ -380,6 +419,14 @@ GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
     result->bits[i] = b->bits[i] - a->bits[i];
 }
 
+
+/**
+ * compute result(b) = a + delta
+ *
+ * @param a some hash code
+ * @param delta some hash code
+ * @param result set to a + delta
+ */
 void
 GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
                         const GNUNET_HashCode * delta, GNUNET_HashCode * result)
@@ -391,6 +438,13 @@ GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
 }
 
 
+/**
+ * compute result = a ^ b
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to a ^ b
+ */
 void
 GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
                         GNUNET_HashCode * result)
@@ -404,6 +458,10 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
 
 /**
  * Convert a hashcode into a key.
+ *
+ * @param hc hash code that serves to generate the key
+ * @param skey set to a valid session key
+ * @param iv set to a valid initialization vector
  */
 void
 GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
@@ -434,6 +492,7 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
   return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
 }
 
+
 /**
  * Determine how many low order bits match in two
  * GNUNET_HashCodes.  i.e. - 010011 and 011111 share
@@ -463,6 +522,9 @@ GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
 /**
  * Compare function for HashCodes, producing a total ordering
  * of all hashcodes.
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
  */
 int
@@ -488,6 +550,10 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
 /**
  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
  * in the XOR metric (Kademlia).
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
+ * @param target some hash code
  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
  */
 int