in src/sasl_frame_codec.c [236:337]
int sasl_frame_codec_encode_frame(SASL_FRAME_CODEC_HANDLE sasl_frame_codec, AMQP_VALUE sasl_frame_value, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context)
{
int result;
SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec;
/* Codes_SRS_SASL_FRAME_CODEC_01_030: [If sasl_frame_codec or sasl_frame_value is NULL, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
if ((sasl_frame_codec == NULL) ||
(sasl_frame_value == NULL))
{
/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
LogError("Bad arguments: sasl_frame_codec = %p, sasl_frame_value = %p",
sasl_frame_codec, sasl_frame_value);
result = MU_FAILURE;
}
else
{
AMQP_VALUE descriptor;
uint64_t sasl_frame_descriptor_ulong;
size_t encoded_size;
if ((descriptor = amqpvalue_get_inplace_descriptor(sasl_frame_value)) == NULL)
{
/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
LogError("Cannot get SASL frame descriptor AMQP value");
result = MU_FAILURE;
}
else if (amqpvalue_get_ulong(descriptor, &sasl_frame_descriptor_ulong) != 0)
{
/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
LogError("Cannot get SASL frame descriptor ulong");
result = MU_FAILURE;
}
/* Codes_SRS_SASL_FRAME_CODEC_01_047: [The frame body of a SASL frame MUST contain exactly one AMQP type, whose type encoding MUST have provides="sasl-frame".] */
else if ((sasl_frame_descriptor_ulong < SASL_MECHANISMS) ||
(sasl_frame_descriptor_ulong > SASL_OUTCOME))
{
/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
LogError("Bad SASL frame descriptor");
result = MU_FAILURE;
}
/* Codes_SRS_SASL_FRAME_CODEC_01_032: [The payload frame size shall be computed based on the encoded size of the sasl_frame_value and its fields.] */
/* Codes_SRS_SASL_FRAME_CODEC_01_033: [The encoded size of the sasl_frame_value and its fields shall be obtained by calling amqpvalue_get_encoded_size.] */
else if (amqpvalue_get_encoded_size(sasl_frame_value, &encoded_size) != 0)
{
/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
LogError("Cannot get SASL frame encoded size");
result = MU_FAILURE;
}
/* Codes_SRS_SASL_FRAME_CODEC_01_016: [The maximum size of a SASL frame is defined by MIN-MAX-FRAME-SIZE.] */
/* Codes_SRS_SASL_FRAME_CODEC_01_017: [The minimum size of a SASL frame is defined by MIN_FRAME_SIZE (1 byte).] */
else if (encoded_size < MIN_FRAME_SIZE || encoded_size > (MIX_MAX_FRAME_SIZE - 8))
{
/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
LogError("SASL frame encoded size out of bounds (%u)", (unsigned int)encoded_size);
result = MU_FAILURE;
}
else
{
unsigned char* sasl_frame_bytes = (unsigned char*)malloc(encoded_size);
if (sasl_frame_bytes == NULL)
{
LogError("Cannot allocate SASL frame bytes");
result = MU_FAILURE;
}
else
{
PAYLOAD payload;
payload.bytes = sasl_frame_bytes;
payload.length = 0;
if (amqpvalue_encode(sasl_frame_value, encode_bytes, &payload) != 0)
{
LogError("Cannot encode SASL frame value");
result = MU_FAILURE;
}
else
{
/* Codes_SRS_SASL_FRAME_CODEC_01_031: [sasl_frame_codec_encode_frame shall encode the frame header and its contents by using frame_codec_encode_frame.] */
/* Codes_SRS_SASL_FRAME_CODEC_01_012: [Bytes 6 and 7 of the header are ignored.] */
/* Codes_SRS_SASL_FRAME_CODEC_01_013: [Implementations SHOULD set these to 0x00.] */
/* Codes_SRS_SASL_FRAME_CODEC_01_014: [The extended header is ignored.] */
/* Codes_SRS_SASL_FRAME_CODEC_01_015: [Implementations SHOULD therefore set DOFF to 0x02.] */
if (frame_codec_encode_frame(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL, &payload, 1, NULL, 0, on_bytes_encoded, callback_context) != 0)
{
/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
LogError("Cannot encode SASL frame");
result = MU_FAILURE;
}
else
{
result = 0;
}
}
free(sasl_frame_bytes);
}
}
}
return result;
}