comparison OrthancServer/FromDcmtkBridge.cpp @ 1876:9b458e4484a1

truncation of serialized DICOM buffers if calcElementLength was overestimated
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Dec 2015 10:17:26 +0100
parents a7bea843a7bc
children b1291df2f780
comparison
equal deleted inserted replaced
1875:a5cd02894534 1876:9b458e4484a1
1038 DcmFileFormat ff(&dataSet); 1038 DcmFileFormat ff(&dataSet);
1039 ff.validateMetaInfo(xfer); 1039 ff.validateMetaInfo(xfer);
1040 ff.removeInvalidGroups(); 1040 ff.removeInvalidGroups();
1041 1041
1042 // Create a memory buffer with the proper size 1042 // Create a memory buffer with the proper size
1043 uint32_t s = ff.calcElementLength(xfer, encodingType); 1043 {
1044 buffer.resize(s); 1044 const uint32_t estimatedSize = ff.calcElementLength(xfer, encodingType); // (*)
1045 DcmOutputBufferStream ob(&buffer[0], s); 1045 buffer.resize(estimatedSize);
1046 }
1047
1048 DcmOutputBufferStream ob(&buffer[0], buffer.size());
1046 1049
1047 // Fill the memory buffer with the meta-header and the dataset 1050 // Fill the memory buffer with the meta-header and the dataset
1048 ff.transferInit(); 1051 ff.transferInit();
1049 OFCondition c = ff.write(ob, xfer, encodingType, NULL, 1052 OFCondition c = ff.write(ob, xfer, encodingType, NULL,
1050 /*opt_groupLength*/ EGL_recalcGL, 1053 /*opt_groupLength*/ EGL_recalcGL,
1051 /*opt_paddingType*/ EPD_withoutPadding); 1054 /*opt_paddingType*/ EPD_withoutPadding);
1052 ff.transferEnd(); 1055 ff.transferEnd();
1053 1056
1054 // Handle errors
1055 if (c.good()) 1057 if (c.good())
1056 { 1058 {
1059 // The DICOM file is successfully written, truncate the target
1060 // buffer if its size was overestimated by (*)
1061 ob.flush();
1062
1063 size_t effectiveSize = static_cast<size_t>(ob.tell());
1064 if (effectiveSize < buffer.size())
1065 {
1066 buffer.resize(effectiveSize);
1067 }
1068
1057 return true; 1069 return true;
1058 } 1070 }
1059 else 1071 else
1060 { 1072 {
1073 // Error
1061 buffer.clear(); 1074 buffer.clear();
1062 return false; 1075 return false;
1063 } 1076 }
1064 } 1077 }
1065 1078