crypt32(23/25): Change finalized from a boolean to a state...

Juan Lang juan.lang at gmail.com
Thu Jul 12 17:26:44 CDT 2007


and use it to simplify message updating
--Juan
-------------- next part --------------
From a65357157f5f20bd2477985276b3d495351c93b8 Mon Sep 17 00:00:00 2001
From: Juan Lang <juanlang at juan.corp.google.com>
Date: Thu, 12 Jul 2007 15:05:02 -0700
Subject: [PATCH] Change finalized from a boolean to a state, and use it to simplify message
updating
---
 dlls/crypt32/msg.c       |   76 ++++++++++++++++++++++++----------------------
 dlls/crypt32/tests/msg.c |    1 -
 2 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/dlls/crypt32/msg.c b/dlls/crypt32/msg.c
index 4c495de..001e128 100644
--- a/dlls/crypt32/msg.c
+++ b/dlls/crypt32/msg.c
@@ -38,13 +38,18 @@ typedef BOOL (*CryptMsgGetParamFunc)(HCR
 typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
  DWORD cbData, BOOL fFinal);
 
+typedef enum _CryptMsgState {
+    MsgStateInit,
+    MsgStateFinalized
+} CryptMsgState;
+
 typedef struct _CryptMsgBase
 {
     LONG                 ref;
     DWORD                open_flags;
     BOOL                 streamed;
     CMSG_STREAM_INFO     stream_info;
-    BOOL                 finalized;
+    CryptMsgState        state;
     CryptMsgCloseFunc    close;
     CryptMsgUpdateFunc   update;
     CryptMsgGetParamFunc get_param;
@@ -69,7 +74,7 @@ static inline void CryptMsgBase_Init(Cry
     msg->close = close;
     msg->get_param = get_param;
     msg->update = update;
-    msg->finalized = FALSE;
+    msg->state = MsgStateInit;
 }
 
 typedef struct _CDataEncodeMsg
@@ -161,12 +166,8 @@ static BOOL CDataEncodeMsg_Update(HCRYPT
     CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
     BOOL ret = FALSE;
 
-    if (msg->base.finalized)
-        SetLastError(CRYPT_E_MSG_ERROR);
-    else if (msg->base.streamed)
+    if (msg->base.streamed)
     {
-        if (fFinal)
-            msg->base.finalized = TRUE;
         __TRY
         {
             if (!msg->begun)
@@ -223,7 +224,6 @@ static BOOL CDataEncodeMsg_Update(HCRYPT
         }
         else
         {
-            msg->base.finalized = TRUE;
             if (!cbData)
                 SetLastError(E_INVALIDARG);
             else
@@ -380,7 +380,7 @@ static BOOL CRYPT_EncodePKCSDigestedData
         items[cItem].encodeFunc =
          CRYPT_AsnEncodePKCSContentInfoInternal;
         cItem++;
-        if (msg->base.finalized)
+        if (msg->base.state == MsgStateFinalized)
         {
             size = sizeof(DWORD);
             ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
@@ -454,7 +454,7 @@ static BOOL CHashEncodeMsg_GetParam(HCRY
          0);
         break;
     case CMSG_VERSION_PARAM:
-        if (!msg->base.finalized)
+        if (msg->base.state != MsgStateFinalized)
             SetLastError(CRYPT_E_MSG_ERROR);
         else
         {
@@ -479,39 +479,31 @@ static BOOL CHashEncodeMsg_Update(HCRYPT
 
     TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
 
-    if (msg->base.finalized)
-        SetLastError(CRYPT_E_MSG_ERROR);
+    msg->begun = TRUE;
+    if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
+    {
+        /* Doesn't do much, as stream output is never called, and you
+         * can't get the content.
+         */
+        ret = CryptHashData(msg->hash, pbData, cbData, 0);
+    }
     else
     {
-        msg->begun = TRUE;
-        if (fFinal)
-            msg->base.finalized = TRUE;
-        if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
-        {
-            /* Doesn't do much, as stream output is never called, and you
-             * can't get the content.
-             */
-            ret = CryptHashData(msg->hash, pbData, cbData, 0);
-        }
+        if (!fFinal)
+            SetLastError(CRYPT_E_MSG_ERROR);
         else
         {
-            if (!fFinal)
-                SetLastError(CRYPT_E_MSG_ERROR);
-            else
+            ret = CryptHashData(msg->hash, pbData, cbData, 0);
+            if (ret)
             {
-                ret = CryptHashData(msg->hash, pbData, cbData, 0);
-                if (ret)
+                msg->data.pbData = CryptMemAlloc(cbData);
+                if (msg->data.pbData)
                 {
-                    msg->data.pbData = CryptMemAlloc(cbData);
-                    if (msg->data.pbData)
-                    {
-                        memcpy(msg->data.pbData + msg->data.cbData, pbData,
-                         cbData);
-                        msg->data.cbData += cbData;
-                    }
-                    else
-                        ret = FALSE;
+                    memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
+                    msg->data.cbData += cbData;
                 }
+                else
+                    ret = FALSE;
             }
         }
     }
@@ -717,9 +709,19 @@ BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCr
  DWORD cbData, BOOL fFinal)
 {
     CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
+    BOOL ret = FALSE;
 
     TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
-    return msg->update(hCryptMsg, pbData, cbData, fFinal);
+
+    if (msg->state == MsgStateFinalized)
+        SetLastError(CRYPT_E_MSG_ERROR);
+    else
+    {
+        ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
+        if (fFinal)
+            msg->state = MsgStateFinalized;
+    }
+    return ret;
 }
 
 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
diff --git a/dlls/crypt32/tests/msg.c b/dlls/crypt32/tests/msg.c
index 688ae83..a41a310 100644
--- a/dlls/crypt32/tests/msg.c
+++ b/dlls/crypt32/tests/msg.c
@@ -994,7 +994,6 @@ static void test_decode_msg_update(void)
     /* Can't update after a final update */
     SetLastError(0xdeadbeef);
     ret = CryptMsgUpdate(msg, dataEmptyContent, sizeof(dataEmptyContent), TRUE);
-    todo_wine
     ok(!ret && GetLastError() == CRYPT_E_MSG_ERROR,
      "Expected CRYPT_E_MSG_ERROR, got %x\n", GetLastError());
     CryptMsgClose(msg);
-- 
1.4.1


More information about the wine-patches mailing list