@@ -1571,10 +1571,14 @@ static void unpack_sequence(struct cbprintf_state *state,
15711571 *
15721572 * @param _val reference to a scalar value
15731573 *
1574- * @param _type the type to which _val should be cast prior to storing it.
1574+ * @param ... Consist of mandatory type to which _val should be cast prior to
1575+ * storing it, optionally followed by the second argument to indicate type
1576+ * which is used to store the value. It must be provided if storage type size
1577+ * is different size than cast type.
15751578 */
1576- #define PACK_CAST_VALUE (_pst , _val , _type ) do { \
1577- _type v = (_type)(_val); \
1579+ #define PACK_CAST_VALUE (_pst , _val , ... /* _type, _storage_type */ ) do { \
1580+ GET_ARG_N(COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (1), (2)), \
1581+ __VA_ARGS__) v = (GET_ARG_N(1, __VA_ARGS__))(_val); \
15781582\
15791583 PACK_VALUE(_pst, v); \
15801584} while (0)
@@ -1593,26 +1597,34 @@ static void unpack_sequence(struct cbprintf_state *state,
15931597 *
15941598 * @param _state pointer to a cbprintf_state object.
15951599 *
1596- * @param _type the type of the packaged signed value.
1600+ * @param ... Consist of mandatory type to which _val should be cast after
1601+ * fetching it, optionally followed by the second argument to indicate type
1602+ * which was used to store the value. It must be provided if storage type size
1603+ * is different size than cast type.
15971604 */
1598- #define UNPACK_CAST_SINT_VALUE (_state , _type ) do { \
1599- _type v; \
1605+ #define UNPACK_CAST_SINT_VALUE (_state , ... /*_type, _storage_type(opt) */ ) do {\
1606+ GET_ARG_N(COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (1), (2)), \
1607+ __VA_ARGS__) v; \
16001608\
16011609 UNPACK_VALUE(_state, v); \
1602- (_state)->value.sint = (sint_value_type)v; \
1610+ (_state)->value.sint = (sint_value_type)((GET_ARG_N(1, __VA_ARGS__))v); \
16031611} while (0)
16041612
16051613/* Unpack a typed unsigned integral value into the argument value uint field.
16061614 *
16071615 * @param _state pointer to a cbprintf_state object.
16081616 *
1609- * @param _type the type of the packaged signed value.
1617+ * @param ... Consist of mandatory type to which _val should be cast after
1618+ * fetching it, optionally followed by the second argument to indicate type
1619+ * which was used to store the value. It must be provided if storage type size
1620+ * is different size than cast type.
16101621 */
1611- #define UNPACK_CAST_UINT_VALUE (_state , _type ) do { \
1612- _type v; \
1622+ #define UNPACK_CAST_UINT_VALUE (_state , ... /*_type, _storage_type(opt) */ ) do {\
1623+ GET_ARG_N(COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (1), (2)), \
1624+ __VA_ARGS__) v; \
16131625\
16141626 UNPACK_VALUE(_state, v); \
1615- (_state)->value.uint = (uint_value_type)v; \
1627+ (_state)->value.uint = (uint_value_type)((GET_ARG_N(1, __VA_ARGS__))v); \
16161628} while (0)
16171629
16181630/* Package a string value.
@@ -1715,10 +1727,10 @@ static void pack_conversion(struct package_state *pst)
17151727 PACK_CAST_VALUE (pst , value -> sint , int );
17161728 break ;
17171729 case LENGTH_HH :
1718- PACK_CAST_VALUE (pst , value -> sint , signed char );
1730+ PACK_CAST_VALUE (pst , value -> sint , signed char , int );
17191731 break ;
17201732 case LENGTH_H :
1721- PACK_CAST_VALUE (pst , value -> sint , short );
1733+ PACK_CAST_VALUE (pst , value -> sint , short , int );
17221734 break ;
17231735 case LENGTH_L :
17241736 PACK_CAST_VALUE (pst , value -> sint , long );
@@ -1750,10 +1762,10 @@ static void pack_conversion(struct package_state *pst)
17501762 PACK_CAST_VALUE (pst , value -> uint , unsigned int );
17511763 break ;
17521764 case LENGTH_HH :
1753- PACK_CAST_VALUE (pst , value -> uint , unsigned char );
1765+ PACK_CAST_VALUE (pst , value -> uint , unsigned char , int );
17541766 break ;
17551767 case LENGTH_H :
1756- PACK_CAST_VALUE (pst , value -> uint , unsigned short );
1768+ PACK_CAST_VALUE (pst , value -> uint , unsigned short , int );
17571769 break ;
17581770 case LENGTH_L :
17591771 PACK_CAST_VALUE (pst , value -> uint , unsigned long );
@@ -1832,10 +1844,10 @@ static void pull_pkg_args(struct cbprintf_state *state)
18321844 UNPACK_CAST_SINT_VALUE (state , int );
18331845 break ;
18341846 case LENGTH_HH :
1835- UNPACK_CAST_SINT_VALUE (state , signed char );
1847+ UNPACK_CAST_SINT_VALUE (state , signed char , int );
18361848 break ;
18371849 case LENGTH_H :
1838- UNPACK_CAST_SINT_VALUE (state , short );
1850+ UNPACK_CAST_SINT_VALUE (state , short , int );
18391851 break ;
18401852 case LENGTH_L :
18411853 UNPACK_CAST_SINT_VALUE (state , long );
@@ -1860,10 +1872,10 @@ static void pull_pkg_args(struct cbprintf_state *state)
18601872 UNPACK_CAST_UINT_VALUE (state , unsigned int );
18611873 break ;
18621874 case LENGTH_HH :
1863- UNPACK_CAST_UINT_VALUE (state , unsigned char );
1875+ UNPACK_CAST_UINT_VALUE (state , unsigned char , int );
18641876 break ;
18651877 case LENGTH_H :
1866- UNPACK_CAST_UINT_VALUE (state , unsigned short );
1878+ UNPACK_CAST_UINT_VALUE (state , unsigned short , int );
18671879 break ;
18681880 case LENGTH_L :
18691881 UNPACK_CAST_UINT_VALUE (state , unsigned long );
0 commit comments