src/bip44.c
size_t bip44_printToStr(const bip44_path_t* pathSpec, char* out, size_t outSize)
{
	ASSERT(outSize < BUFFER_SIZE_PARANOIA);
	// we need space for the terminating \0
	// and one more byte to check whether
	// snprintf has enough space to print everything
	ASSERT(outSize >= BIP44_PATH_STRING_SIZE_MAX + 1);
	char* ptr = out;
	char* end = (out + outSize);

#define WRITE(fmt, ...) \
	{ \
		ASSERT(ptr <= end); \
		STATIC_ASSERT(sizeof(end - ptr) == sizeof(size_t), "bad size_t size"); \
		size_t availableSize = (size_t) (end - ptr); \
		/* Note(ppershing): We do not bother checking return */ \
		/* value of snprintf as it always returns 0. */ \
		/* Go figure ... */ \
		snprintf(ptr, availableSize, fmt, ##__VA_ARGS__); \
		size_t res = strlen(ptr); \
		/* if snprintf filled all the remaining space, there is no space for '\0', */ \
		/* or the information is not displayed in full, */ \
		/* and that's a serious security risk */ \
		ASSERT(res + 1 < availableSize); \
		ptr += res; \
	}

	WRITE("m");

	ASSERT(pathSpec->length <= ARRAY_LEN(pathSpec->path));

	for (size_t i = 0; i < pathSpec->length; i++) {
		const uint32_t value = pathSpec->path[i];

		if ((value & HARDENED_BIP32) == HARDENED_BIP32) {
			WRITE("/%u'", (value & ~HARDENED_BIP32));
		} else {
			WRITE("/%u", value);
		}
	}
#undef WRITE
	ASSERT(ptr >= out);
	ASSERT(ptr + 1 < end);

	return ptr - out;
}
// returns the length of the resulting string
returns the length of the resulting string
returns the length of the resulting string
size_t bip44_printToStr(const bip44_path_t* pathSpec, char* out, size_t outSize)
{
	ASSERT(outSize < BUFFER_SIZE_PARANOIA);
	ASSERT(outSize >= BIP44_PATH_STRING_SIZE_MAX + 1);
	char* ptr = out;
	char* end = (out + outSize);
#define WRITE(fmt, ...) \
	{ \
		ASSERT(ptr <= end); \
		STATIC_ASSERT(sizeof(end - ptr) == sizeof(size_t), "bad size_t size"); \
		size_t availableSize = (size_t) (end - ptr); \
		/* Note(ppershing): We do not bother checking return */ \
		/* value of snprintf as it always returns 0. */ \
		/* Go figure ... */ \
		snprintf(ptr, availableSize, fmt, ##__VA_ARGS__); \
		size_t res = strlen(ptr); \
		/* if snprintf filled all the remaining space, there is no space for '\0', */ \
		/* or the information is not displayed in full, */ \
		/* and that's a serious security risk */ \
		ASSERT(res + 1 < availableSize); \
		ptr += res; \
	}
	WRITE("m");
	ASSERT(pathSpec->length <= ARRAY_LEN(pathSpec->path));
	for (size_t i = 0; i < pathSpec->length; i++) {
		const uint32_t value = pathSpec->path[i];
		if ((value & HARDENED_BIP32) == HARDENED_BIP32) {
			WRITE("/%u'", (value & ~HARDENED_BIP32));
		} else {
			WRITE("/%u", value);
		}
	}
#undef WRITE
	ASSERT(ptr >= out);
	ASSERT(ptr + 1 < end);
	return ptr - out;
}
size_t bip44_printToStr ( const bip44_path_t * pathSpec , char * out , size_t outSize ) { ASSERT ( outSize < BUFFER_SIZE_PARANOIA ) ; ASSERT ( outSize >= BIP44_PATH_STRING_SIZE_MAX + 1 ) ; char * ptr = out ; char * end = ( out + outSize ) ; #define WRITE ( fmt , ... )  \
	{ \
		ASSERT(ptr <= end); \
		STATIC_ASSERT(sizeof(end - ptr) == sizeof(size_t), "bad size_t size"); \
		size_t availableSize = (size_t) (end - ptr); \
		/* Note(ppershing): We do not bother checking return */ \
		/* value of snprintf as it always returns 0. */ \
		/* Go figure ... */ \
		snprintf(ptr, availableSize, fmt, ##__VA_ARGS__); \
		size_t res = strlen(ptr); \
		/* if snprintf filled all the remaining space, there is no space for '\0', */ \
		/* or the information is not displayed in full, */ \
		/* and that's a serious security risk */ \
		ASSERT(res + 1 < availableSize); \
		ptr += res; \
	} WRITE ( " " ) ; ASSERT ( pathSpec -> length <= ARRAY_LEN ( pathSpec -> path ) ) ; for ( size_t i = 0 ; i < pathSpec -> length ; i ++ ) { const uint32_t value = pathSpec -> path [ i ] ; if ( ( value & HARDENED_BIP32 ) == HARDENED_BIP32 ) { WRITE ( " " , ( value & ~ HARDENED_BIP32 ) ) ; } else { WRITE ( " " , value ) ; } } #undef  WRITE ASSERT ( ptr >= out ) ; ASSERT ( ptr + 1 < end ) ; return ptr - out ; }
returns the length of the resulting string
returns the length of the resulting string
// we need space for the terminating \0 // and one more byte to check whether // snprintf has enough space to print everything