Changeset 5308

Show
Ignore:
Timestamp:
02/24/08 10:29:18 (8 months ago)
Author:
morris
Message:

Change buffer management to reuse the same buffers instead of allocating new ones

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/misc/wi-crypto.c

    r5284 r5308  
    608608 
    609609 
     610wi_uinteger_t wi_cipher_block_size(wi_cipher_t *cipher) { 
     611        return EVP_CIPHER_block_size(cipher->cipher); 
     612} 
     613 
     614 
     615 
    610616#pragma mark - 
    611617 
     
    613619        const void              *decrypted_buffer; 
    614620        void                    *encrypted_buffer; 
    615         wi_uinteger_t   decrypted_length, encrypted_length; 
     621        wi_uinteger_t   decrypted_length; 
     622        wi_integer_t    encrypted_length; 
    616623         
    617624        decrypted_buffer = wi_data_bytes(decrypted_data); 
    618625        decrypted_length = wi_data_length(decrypted_data); 
    619          
    620         if(!wi_cipher_encrypt_bytes(cipher, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 
     626        encrypted_buffer = wi_malloc(wi_cipher_block_size(cipher) + decrypted_length); 
     627        encrypted_length = wi_cipher_encrypt_bytes(cipher, decrypted_buffer, decrypted_length, encrypted_buffer); 
     628         
     629        if(encrypted_length < 0) 
    621630                return NULL; 
    622631         
     
    626635 
    627636 
    628 wi_boolean_t wi_cipher_encrypt_bytes(wi_cipher_t *cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    629         void            *encrypted_buffer; 
     637wi_integer_t wi_cipher_encrypt_bytes(wi_cipher_t *cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void *encrypted_buffer) { 
    630638        int                     encrypted_length, padded_length; 
    631639         
    632         encrypted_buffer = wi_malloc(decrypted_length + EVP_CIPHER_block_size(cipher->cipher)); 
    633  
    634640        if(EVP_EncryptUpdate(&cipher->encrypt_ctx, encrypted_buffer, &encrypted_length, decrypted_buffer, decrypted_length) != 1) { 
    635641                wi_error_set_openssl_error(); 
    636642                 
    637                 wi_free(encrypted_buffer); 
    638                  
    639                 return false; 
     643                return -1; 
    640644        } 
    641645         
     
    643647                wi_error_set_openssl_error(); 
    644648                 
    645                 wi_free(encrypted_buffer); 
    646                  
    647                 return false; 
     649                return -1; 
    648650        } 
    649651 
     
    651653                wi_error_set_openssl_error(); 
    652654                 
    653                 wi_free(encrypted_buffer); 
    654                  
    655                 return false; 
    656         } 
    657  
    658         *out_buffer = encrypted_buffer; 
    659         *out_length = encrypted_length + padded_length; 
    660          
    661         return true; 
     655                return -1; 
     656        } 
     657 
     658        return encrypted_length + padded_length; 
    662659} 
    663660 
     
    667664        const void              *encrypted_buffer; 
    668665        void                    *decrypted_buffer; 
    669         wi_uinteger_t   encrypted_length, decrypted_length; 
     666        wi_uinteger_t   encrypted_length; 
     667        wi_integer_t    decrypted_length; 
    670668         
    671669        encrypted_buffer = wi_data_bytes(encrypted_data); 
    672670        encrypted_length = wi_data_length(encrypted_data); 
    673          
    674         if(!wi_cipher_decrypt_bytes(cipher, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) 
     671        decrypted_buffer = wi_malloc(wi_cipher_block_size(cipher) + encrypted_length); 
     672        decrypted_length = wi_cipher_decrypt_bytes(cipher, encrypted_buffer, encrypted_length, decrypted_buffer); 
     673         
     674        if(decrypted_length < 0) 
    675675                return NULL; 
    676676         
     
    680680 
    681681 
    682 wi_boolean_t wi_cipher_decrypt_bytes(wi_cipher_t *cipher, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    683         void            *decrypted_buffer; 
     682wi_integer_t wi_cipher_decrypt_bytes(wi_cipher_t *cipher, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void *decrypted_buffer) { 
    684683        int                     decrypted_length, padded_length; 
    685684         
    686         decrypted_buffer = wi_malloc(encrypted_length + EVP_CIPHER_block_size(cipher->cipher)); 
    687          
    688685        if(EVP_DecryptUpdate(&cipher->decrypt_ctx, decrypted_buffer, &decrypted_length, encrypted_buffer, encrypted_length) != 1) { 
    689686                wi_error_set_openssl_error(); 
    690687                 
    691                 wi_free(decrypted_buffer); 
    692                  
    693                 return false; 
     688                return -1; 
    694689        } 
    695690         
     
    697692                wi_error_set_openssl_error(); 
    698693                 
    699                 wi_free(decrypted_buffer); 
    700                  
    701                 return false; 
     694                return -1; 
    702695        } 
    703696         
     
    705698                wi_error_set_openssl_error(); 
    706699                 
    707                 wi_free(decrypted_buffer); 
    708                  
    709                 return false; 
    710         } 
    711          
    712         *out_buffer = decrypted_buffer; 
    713         *out_length = decrypted_length + padded_length; 
    714          
    715         return true; 
     700                return -1; 
     701        } 
     702         
     703        return decrypted_length + padded_length; 
    716704} 
    717705 
  • libwired/trunk/libwired/misc/wi-crypto.h

    r4697 r5308  
    7777WI_EXPORT wi_string_t *                                 wi_cipher_name(wi_cipher_t *); 
    7878WI_EXPORT wi_uinteger_t                                 wi_cipher_bits(wi_cipher_t *); 
     79WI_EXPORT wi_uinteger_t                                 wi_cipher_block_size(wi_cipher_t *); 
    7980 
    8081WI_EXPORT wi_data_t *                                   wi_cipher_encrypt(wi_cipher_t *, wi_data_t *); 
    81 WI_EXPORT wi_boolean_t                                 wi_cipher_encrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
     82WI_EXPORT wi_integer_t                                 wi_cipher_encrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void *); 
    8283WI_EXPORT wi_data_t *                                   wi_cipher_decrypt(wi_cipher_t *, wi_data_t *); 
    83 WI_EXPORT wi_boolean_t                                 wi_cipher_decrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
     84WI_EXPORT wi_integer_t                                 wi_cipher_decrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void *); 
    8485 
    8586#endif /* WI_CRYPTO_H */ 
  • libwired/trunk/libwired/p7/wi-p7-socket.c

    r5296 r5308  
    153153        wi_p7_boolean_t                                                 remote_compatibility_check; 
    154154         
     155        void                                                                    *compression_buffer; 
     156        wi_uinteger_t                                                   compression_buffer_length; 
     157        void                                                                    *encryption_buffer; 
     158        wi_uinteger_t                                                   encryption_buffer_length; 
     159        void                                                                    *decryption_buffer; 
     160        wi_uinteger_t                                                   decryption_buffer_length; 
     161        void                                                                    *oobdata_read_buffer; 
     162        wi_uinteger_t                                                   oobdata_read_buffer_length; 
     163         
    155164        uint64_t                                                                read_raw_bytes, read_processed_bytes; 
    156165        uint64_t                                                                sent_raw_bytes, sent_processed_bytes; 
     
    181190 
    182191static wi_boolean_t                                                     _wi_p7_socket_configure_compression_streams(wi_p7_socket_t *); 
    183 static wi_boolean_t                                                    _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *, _wi_p7_socket_compression_t, const void *, uint32_t, void **, uint32_t *); 
     192static wi_integer_t                                                    _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *, _wi_p7_socket_compression_t, const void *, uint32_t); 
    184193static int                                                                      _wi_p7_socket_xflate_buffer(z_stream *, _wi_p7_socket_compression_t, const void *, uint32_t, uint32_t *, void *, uint32_t *); 
    185194 
     
    261270        wi_p7_socket_t          *p7_socket = instance; 
    262271         
     272        wi_free(p7_socket->compression_buffer); 
     273        wi_free(p7_socket->encryption_buffer); 
     274        wi_free(p7_socket->decryption_buffer); 
     275        wi_free(p7_socket->oobdata_read_buffer); 
     276         
    263277        wi_release(p7_socket->socket); 
    264278        wi_release(p7_socket->spec); 
     
    9981012static wi_boolean_t _wi_p7_socket_write_binary_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_p7_message_t *p7_message) { 
    9991013        const void                      *send_buffer; 
    1000         void                            *compressed_buffer = NULL, *encrypted_buffer = NULL; 
    10011014        char                            length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; 
    10021015        unsigned char           checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 
    1003         uint32_t                       send_size, compressed_size, encrypted_size; 
    1004         wi_boolean_t           result = false; 
     1016        wi_integer_t           compressed_size, encrypted_size; 
     1017        uint32_t                       send_size; 
    10051018         
    10061019        send_size       = p7_message->binary_size; 
     
    10101023         
    10111024        if(p7_socket->compression_enabled) { 
    1012                 if(!_wi_p7_socket_xcompress_buffer(p7_socket, 
    1013                                                                                    _WI_P7_SOCKET_COMPRESS, 
    1014                                                                                    send_buffer, 
    1015                                                                                    send_size, 
    1016                                                                                    &compressed_buffer, 
    1017                                                                                    &compressed_size)) { 
    1018                         goto end; 
    1019                 } 
     1025                compressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 
     1026                                                                                                                 _WI_P7_SOCKET_COMPRESS, 
     1027                                                                                                                 send_buffer, 
     1028                                                                                                                 send_size); 
     1029                 
     1030                if(compressed_size < 0) 
     1031                        return false; 
    10201032                 
    10211033                send_size       = compressed_size; 
    1022                 send_buffer     = compressed_buffer; 
     1034                send_buffer     = p7_socket->compression_buffer; 
    10231035        } 
    10241036         
    10251037        if(p7_socket->encryption_enabled) { 
    1026                 if(!wi_cipher_encrypt_bytes(p7_socket->cipher, 
    1027                                                                         send_buffer, 
    1028                                                                         send_size, 
    1029                                                                         &encrypted_buffer, 
    1030                                                                         &encrypted_size)) { 
    1031                         goto end; 
    1032                 } 
     1038                encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); 
     1039                 
     1040                if(!p7_socket->encryption_buffer) { 
     1041                        p7_socket->encryption_buffer_length = encrypted_size; 
     1042                        p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); 
     1043                } 
     1044                else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { 
     1045                        p7_socket->encryption_buffer_length = encrypted_size * 2; 
     1046                        p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); 
     1047                } 
     1048                 
     1049                encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, 
     1050                                                                                                 send_buffer, 
     1051                                                                                                 send_size, 
     1052                                                                                                 p7_socket->encryption_buffer); 
     1053                 
     1054                if(encrypted_size < 0) 
     1055                        return false; 
    10331056                 
    10341057                send_size       = encrypted_size; 
    1035                 send_buffer     = encrypted_buffer; 
     1058                send_buffer     = p7_socket->encryption_buffer; 
    10361059        } 
    10371060 
     
    10411064         
    10421065        if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) 
    1043                 goto end
     1066                return false
    10441067 
    10451068        if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) 
    1046                 goto end
     1069                return false
    10471070         
    10481071        if(p7_socket->checksum_enabled) { 
     
    10501073 
    10511074                if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) 
    1052                         goto end; 
    1053         } 
    1054          
    1055         result = true; 
    1056  
    1057 end: 
    1058         wi_free(compressed_buffer); 
    1059         wi_free(encrypted_buffer); 
    1060          
    1061         return result; 
     1075                        return false; 
     1076        } 
     1077         
     1078        return true; 
    10621079} 
    10631080 
     
    10781095static wi_p7_message_t * _wi_p7_socket_read_binary_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, uint32_t message_size) { 
    10791096        wi_p7_message_t         *p7_message; 
    1080         void                            *decompressed_buffer, *decrypted_buffer; 
    10811097        unsigned char           local_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 
    10821098        unsigned char           remote_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 
    1083         uint32_t                      decompressed_size, decrypted_size; 
     1099        wi_integer_t          decompressed_size, decrypted_size; 
    10841100        int32_t                         length; 
    10851101         
     
    10921108        p7_message = wi_autorelease(wi_p7_message_init_with_serialization(wi_p7_message_alloc(), 
    10931109                WI_P7_BINARY, wi_p7_socket_spec(p7_socket))); 
    1094  
    1095         if(!p7_message->binary_buffer) { 
    1096                 p7_message->binary_capacity     = message_size; 
    1097                 p7_message->binary_buffer       = wi_malloc(p7_message->binary_capacity); 
    1098         } 
    1099         else if(message_size > p7_message->binary_capacity) { 
    1100                 p7_message->binary_capacity     = message_size; 
    1101                 p7_message->binary_buffer       = wi_realloc(p7_message->binary_buffer, message_size); 
    1102         } 
     1110        p7_message->binary_capacity = message_size; 
     1111        p7_message->binary_buffer = wi_malloc(p7_message->binary_capacity); 
    11031112         
    11041113        length = wi_socket_read_buffer(p7_socket->socket, timeout, p7_message->binary_buffer, message_size); 
     
    11111120 
    11121121        if(p7_socket->encryption_enabled) { 
    1113                 if(!wi_cipher_decrypt_bytes(p7_socket->cipher, 
    1114                                                                         p7_message->binary_buffer, 
    1115                                                                         p7_message->binary_size, 
    1116                                                                         &decrypted_buffer, 
    1117                                                                         &decrypted_size)) { 
     1122                decrypted_size = p7_message->binary_size + wi_cipher_block_size(p7_socket->cipher); 
     1123                 
     1124                if(!p7_socket->decryption_buffer) { 
     1125                        p7_socket->decryption_buffer_length = decrypted_size; 
     1126                        p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); 
     1127                } 
     1128                else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { 
     1129                        p7_socket->decryption_buffer_length = decrypted_size * 2; 
     1130                        p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); 
     1131                } 
     1132                 
     1133                decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, 
     1134                                                                                                 p7_message->binary_buffer, 
     1135                                                                                                 p7_message->binary_size, 
     1136                                                                                                 p7_socket->decryption_buffer); 
     1137                 
     1138                if(decrypted_size < 0) 
    11181139                        return NULL; 
    1119                 } 
    1120                  
    1121                 wi_free(p7_message->binary_buffer); 
    1122                  
    1123                 p7_message->binary_size         = decrypted_size; 
    1124                 p7_message->binary_capacity     = decrypted_size; 
    1125                 p7_message->binary_buffer       = decrypted_buffer; 
     1140                 
     1141                if((wi_uinteger_t) decrypted_size > p7_message->binary_capacity) { 
     1142                        p7_message->binary_capacity = decrypted_size; 
     1143                        p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); 
     1144                } 
     1145                 
     1146                memcpy(p7_message->binary_buffer, p7_socket->decryption_buffer, decrypted_size); 
     1147                 
     1148                p7_message->binary_size = decrypted_size; 
    11261149        } 
    11271150         
    11281151        if(p7_socket->compression_enabled) { 
    1129                 if(!_wi_p7_socket_xcompress_buffer(p7_socket, 
    1130                                                                                    _WI_P7_SOCKET_DECOMPRESS, 
    1131                                                                                    p7_message->binary_buffer, 
    1132                                                                                    p7_message->binary_size, 
    1133                                                                                   &decompressed_buffer, 
    1134                                                                                   &decompressed_size)) { 
     1152                decompressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 
     1153                                                                                                                  _WI_P7_SOCKET_DECOMPRESS, 
     1154                                                                                                                  p7_message->binary_buffer, 
     1155                                                                                                                  p7_message->binary_size); 
     1156                 
     1157                if(decompressed_size < 0) 
    11351158                        return NULL; 
    1136                 } 
    1137                  
    1138                 wi_free(p7_message->binary_buffer); 
    1139  
    1140                 p7_message->binary_size         = decompressed_size; 
    1141                 p7_message->binary_capacity     = decompressed_size;  
    1142                 p7_message->binary_buffer       = decompressed_buffer; 
     1159                 
     1160                if((wi_uinteger_t) decompressed_size > p7_message->binary_capacity) { 
     1161                        p7_message->binary_capacity = decompressed_size; 
     1162                        p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); 
     1163                } 
     1164                 
     1165                memcpy(p7_message->binary_buffer, p7_socket->compression_buffer, decompressed_size); 
     1166                 
     1167                p7_message->binary_size = decompressed_size; 
    11431168        } 
    11441169         
     
    12361261 
    12371262 
    1238 static wi_boolean_t _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *p7_socket, _wi_p7_socket_compression_t compression, const void *in_buffer, uint32_t in_size, void **out_buffer, uint32_t *out_size) { 
    1239         const void              *input; 
    1240         void                    *output, *working_buffer; 
    1241         z_stream                *stream; 
    1242         uint32_t                offset, input_size, input_processed, output_size, working_size, total_working_size; 
    1243         int                             err; 
    1244          
    1245         stream                                  = (compression == _WI_P7_SOCKET_COMPRESS) ? &p7_socket->compression_stream : &p7_socket->decompression_stream; 
    1246         working_size                    = (compression == _WI_P7_SOCKET_COMPRESS) ? in_size : 4 * in_size; 
    1247         working_buffer                  = wi_malloc(working_size); 
    1248         input                                   = in_buffer; 
    1249         input_size                              = in_size; 
    1250         output                                  = working_buffer; 
    1251         output_size                             = working_size; 
    1252         total_working_size              = 0; 
     1263static wi_integer_t _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *p7_socket, _wi_p7_socket_compression_t compression, const void *in_buffer, uint32_t in_size) { 
     1264        const void                      *input; 
     1265        void                            *output; 
     1266        z_stream                        *stream; 
     1267        uint32_t                        offset, input_size, input_processed, output_size, total_size; 
     1268        int                                     err; 
     1269         
     1270        if(!p7_socket->compression_buffer) { 
     1271                p7_socket->compression_buffer_length = in_size * 4; 
     1272                p7_socket->compression_buffer = wi_malloc(p7_socket->compression_buffer_length); 
     1273        } 
     1274        else if(in_size > p7_socket->compression_buffer_length) { 
     1275                p7_socket->compression_buffer_length = in_size * 4; 
     1276                p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, p7_socket->compression_buffer_length); 
     1277        } 
     1278         
     1279        stream                  = (compression == _WI_P7_SOCKET_COMPRESS) ? &p7_socket->compression_stream : &p7_socket->decompression_stream; 
     1280        input                   = in_buffer; 
     1281        input_size              = in_size; 
     1282        output                  = p7_socket->compression_buffer; 
     1283        output_size             = p7_socket->compression_buffer_length; 
     1284        total_size              = 0; 
    12531285         
    12541286        while(true) { 
     
    12581290                        wi_error_set_zlib_error(err); 
    12591291                         
    1260                         wi_free(working_buffer); 
    1261                          
    1262                         return false; 
    1263                 } 
    1264                  
    1265                 total_working_size += output_size; 
     1292                        return -1; 
     1293                } 
     1294                 
     1295                total_size += output_size; 
    12661296                 
    12671297                if(stream->avail_out > 0) 
    12681298                        break; 
    12691299                 
    1270                 offset                          = (output - working_buffer) + output_size; 
    1271                 working_size            *= 4; 
    1272                 working_buffer          = wi_realloc(working_buffer, working_size); 
    1273                 output                          = working_buffer + offset; 
    1274                 output_size                     = working_size - offset; 
    1275                 input                           += input_processed; 
    1276                 input_size                      -= input_processed; 
     1300                p7_socket->compression_buffer_length *= 4; 
     1301                p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, p7_socket->compression_buffer_length); 
     1302 
     1303                offset                  = (output - p7_socket->compression_buffer) + output_size; 
     1304                output                  = p7_socket->compression_buffer + offset; 
     1305                output_size             = p7_socket->compression_buffer_length - offset; 
     1306                input                   += input_processed; 
     1307                input_size              -= input_processed; 
    12771308                 
    12781309                if(compression == _WI_P7_SOCKET_DECOMPRESS && input_size == 0) 
     
    12801311        } 
    12811312         
    1282         *out_buffer     = working_buffer; 
    1283         *out_size       = total_working_size; 
    1284          
    1285         return true; 
     1313        return total_size; 
    12861314} 
    12871315 
     
    15081536wi_boolean_t wi_p7_socket_write_oobdata(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, const void *buffer, uint32_t size) { 
    15091537        const void                      *send_buffer; 
    1510         void                            *compressed_buffer = NULL, *encrypted_buffer = NULL; 
    15111538        char                            length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; 
    15121539        unsigned char           checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 
    1513         uint32_t                       send_size, compressed_size, encrypted_size; 
    1514         wi_boolean_t           result = false; 
     1540        wi_integer_t           compressed_size, encrypted_size; 
     1541        uint32_t                       send_size; 
    15151542         
    15161543        send_size = size; 
     
    15211548 
    15221549        if(p7_socket->compression_enabled) { 
    1523                 if(!_wi_p7_socket_xcompress_buffer(p7_socket, 
    1524                                                                                    _WI_P7_SOCKET_COMPRESS, 
    1525                                                                                    send_buffer, 
    1526                                                                                    send_size, 
    1527                                                                                    &compressed_buffer, 
    1528                                                                                    &compressed_size)) { 
    1529                         goto end; 
    1530                 } 
     1550                compressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 
     1551                                                                                                                 _WI_P7_SOCKET_COMPRESS, 
     1552                                                                                                                 send_buffer, 
     1553                                                                                                                 send_size); 
     1554                 
     1555                if(compressed_size < 0) 
     1556                        return false; 
    15311557                 
    15321558                send_size       = compressed_size; 
    1533                 send_buffer     = compressed_buffer; 
     1559                send_buffer     = p7_socket->compression_buffer; 
    15341560        } 
    15351561         
    15361562        if(p7_socket->encryption_enabled) { 
    1537                 if(!wi_cipher_encrypt_bytes(p7_socket->cipher, 
    1538                                                                         send_buffer, 
    1539                                                                         send_size, 
    1540                                                                         &encrypted_buffer, 
    1541                                                                         &encrypted_size)) { 
    1542                         goto end; 
    1543                 } 
     1563                encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); 
     1564                 
     1565                if(!p7_socket->encryption_buffer) { 
     1566                        p7_socket->encryption_buffer_length = encrypted_size; 
     1567                        p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); 
     1568                } 
     1569                else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { 
     1570                        p7_socket->encryption_buffer_length = encrypted_size * 2; 
     1571                        p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); 
     1572                } 
     1573                 
     1574                encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, 
     1575                                                                                                 send_buffer, 
     1576                                                                                                 send_size, 
     1577                                                                                                 p7_socket->encryption_buffer); 
     1578                 
     1579                if(encrypted_size < 0) 
     1580                        return false; 
    15441581                 
    15451582                send_size       = encrypted_size; 
    1546                 send_buffer = encrypted_buffer; 
     1583                send_buffer = p7_socket->encryption_buffer; 
    15471584        } 
    15481585 
     
    15501587 
    15511588        if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) 
    1552                 goto end
     1589                return false
    15531590 
    15541591        if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) 
    1555                 goto end
     1592                return false
    15561593 
    15571594        if(p7_socket->checksum_enabled) { 
    15581595                if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) 
    1559                         goto end; 
    1560         } 
    1561          
    1562         result = true; 
    1563  
    1564 end: 
    1565         wi_free(compressed_buffer); 
    1566         wi_free(encrypted_buffer); 
    1567          
    1568         return result; 
     1596                        return false; 
     1597        } 
     1598         
     1599        return true; 
    15691600} 
    15701601 
     
    15721603 
    15731604wi_integer_t wi_p7_socket_read_oobdata(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, void **out_buffer) { 
    1574         void                            *receive_buffer = NULL, *decrypted_buffer, *decompressed_buffer
     1605        void                            *receive_buffer
    15751606        char                            length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; 
    15761607        unsigned char           local_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 
    15771608        unsigned char           remote_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 
    1578         wi_integer_t            result
    1579         uint32_t                        receive_size, received_size, decompressed_size, decrypted_size
     1609        wi_integer_t            result, decompressed_size, decrypted_size
     1610        uint32_t                        receive_size, received_size
    15801611         
    15811612        result = wi_socket_read_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)); 
     
    15941625        } 
    15951626         
    1596         receive_buffer = wi_malloc(receive_size); 
     1627        if(!p7_socket->oobdata_read_buffer) { 
     1628                p7_socket->oobdata_read_buffer_length = receive_size * 2; 
     1629                p7_socket->oobdata_read_buffer = wi_malloc(p7_socket->oobdata_read_buffer_length); 
     1630        } 
     1631        else if(receive_size > p7_socket->oobdata_read_buffer_length) { 
     1632                p7_socket->oobdata_read_buffer_length = receive_size * 2; 
     1633                p7_socket->oobdata_read_buffer = wi_realloc(p7_socket->oobdata_read_buffer, p7_socket->oobdata_read_buffer_length); 
     1634        } 
     1635         
     1636        receive_buffer = p7_socket->oobdata_read_buffer; 
    15971637        received_size = 0; 
    15981638         
     
    16071647         
    16081648        if(p7_socket->encryption_enabled) { 
    1609                 if(!wi_cipher_decrypt_bytes(p7_socket->cipher, 
    1610                                                                         receive_buffer, 
    1611                                                                         receive_size, 
    1612                                                                         &decrypted_buffer, 
    1613                                                                         &decrypted_size)) { 
     1649                decrypted_size = receive_size + wi_cipher_block_size(p7_socket->cipher); 
     1650                 
     1651                if(!p7_socket->decryption_buffer) { 
     1652                        p7_socket->decryption_buffer_length = decrypted_size; 
     1653                        p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); 
     1654                } 
     1655                else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { 
     1656                        p7_socket->decryption_buffer_length = decrypted_size * 2; 
     1657                        p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); 
     1658                } 
     1659                 
     1660                decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, 
     1661                                                                                                 receive_buffer, 
     1662                                                                                                 receive_size, 
     1663                                                                                                 p7_socket->decryption_buffer); 
     1664                 
     1665                if(decrypted_size < 0) { 
    16141666                        result = -1; 
    16151667                         
    16161668                        goto end; 
    16171669                } 
    1618                  
    1619                 wi_free(receive_buffer); 
    16201670 
    16211671                receive_size    = decrypted_size; 
    1622                 receive_buffer  = decrypted_buffer; 
     1672                receive_buffer  = p7_socket->decryption_buffer; 
    16231673        } 
    16241674         
    16251675        if(p7_socket->compression_enabled) { 
    1626                 if(!_wi_p7_socket_xcompress_buffer(p7_socket, 
    1627                                                                                    _WI_P7_SOCKET_DECOMPRESS, 
    1628                                                                                    receive_buffer, 
    1629                                                                                    receive_size, 
    1630                                                                                   &decompressed_buffer, 
    1631                                                                                   &decompressed_size)) { 
     1676                decompressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 
     1677                                                                                                                  _WI_P7_SOCKET_DECOMPRESS, 
     1678                                                                                                                  receive_buffer, 
     1679                                                                                                                  receive_size); 
     1680                 
     1681                if(decompressed_size < 0) { 
    16321682                        result = -1; 
    16331683 
    16341684                        goto end; 
    16351685                } 
    1636                  
    1637                 wi_free(receive_buffer); 
    16381686 
    16391687                receive_size    = decompressed_size; 
    1640                 receive_buffer  = decompressed_buffer; 
     1688                receive_buffer  = p7_socket->compression_buffer; 
    16411689        } 
    16421690         
     
    16631711        if(result > 0) 
    16641712                *out_buffer = receive_buffer; 
    1665         else if(receive_buffer) 
    1666                 wi_free(receive_buffer); 
    16671713         
    16681714        return result;