This add AES support.
[m6w6/libmemcached] / libmemcached / response.cc
index 511cf786db0bbfb5151ef2fc024eae5a4ed121bc..faf20ff94149f15448c4c6a36a7d0d456419cb37 100644 (file)
@@ -55,6 +55,9 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st
   string_ptr+= 6; /* "VALUE " */
 
 
+  // Just used for cases of AES decrypt currently
+  memcached_return_t rc= MEMCACHED_SUCCESS;
+
   /* We load the key */
   {
     char *key= result->item_key;
@@ -169,7 +172,34 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st
     memcached_string_set_length(&result->value, value_length);
   }
 
-  return MEMCACHED_SUCCESS;
+  if (memcached_is_encrypted(instance->root) and memcached_result_length(result))
+  {
+    hashkit_string_st *destination;
+
+    if ((destination= hashkit_decrypt(&instance->root->hashkit,
+                                      memcached_result_value(result), memcached_result_length(result))) == NULL)
+    {
+      rc= memcached_set_error(*instance->root, MEMCACHED_FAILURE, 
+                              MEMCACHED_AT, memcached_literal_param("hashkit_decrypt() failed"));
+    }
+    else
+    {
+      memcached_result_reset_value(result);
+      if (memcached_failed(memcached_result_set_value(result, hashkit_string_c_str(destination), hashkit_string_length(destination))))
+      {
+        rc= memcached_set_error(*instance->root, MEMCACHED_FAILURE, 
+                                MEMCACHED_AT, memcached_literal_param("hashkit_decrypt() failed"));
+      }
+    }
+
+    if (memcached_failed(rc))
+    {
+      memcached_result_reset(result);
+    }
+    hashkit_string_free(destination);
+  }
+
+  return rc;
 
 read_error:
   memcached_io_reset(instance);
@@ -212,7 +242,7 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta
         if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
         {
           instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
-          return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
+          return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
         }
         instance->major_version= uint8_t(version);
 
@@ -223,7 +253,7 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta
         if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
         {
           instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
-          return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version"));
+          return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version"));
         }
         instance->minor_version= uint8_t(version);
 
@@ -234,7 +264,7 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta
         if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
         {
           instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
-          return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
+          return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
         }
         instance->micro_version= uint8_t(version);
 
@@ -573,7 +603,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan
         if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
         {
           instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
-          return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
+          return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
         }
         instance->major_version= uint8_t(version);
 
@@ -581,7 +611,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan
         if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
         {
           instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
-          return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
+          return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
         }
         instance->minor_version= uint8_t(version);
 
@@ -589,7 +619,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan
         if (errno == ERANGE)
         {
           instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
-          return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
+          return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
         }
         instance->micro_version= uint8_t(version);
       }
@@ -764,7 +794,6 @@ static memcached_return_t _read_one_response(memcached_server_write_instance_st
   else
   {
     rc= textual_read_one_response(instance, buffer, buffer_length, result);
-    assert(rc != MEMCACHED_PROTOCOL_ERROR);
   }
 
   if (memcached_fatal(rc))