Rework the `nbWrite()` function so, that it gets safe
in terms of:
- operating on data written/data remaining integers
- has protection guards for extra big chunks to write at once
Refactor to use size_t/ssize_t instead of primitive int,
where possible (e.g. bytesWritten).
Let the `nbWrite()` use reference instead of pointer
for the bytes written parameter.
Fixes Coverity Scan report:
4. tainted_data_return: Called function write(fd, sp, nToWrite),
and a possible return value may be less than zero.
5. cast_overflow: An assign that casts to a different type,
which might trigger an overflow.
Overflowed integer argument (INTEGER_OVERFLOW)
13. overflow_sink: nToWrite, which might have overflowed,
is passed to write(fd, sp, nToWrite).
7. overflow: The expression nToWrite is considered
to have possibly overflowed.
Change-Id: I32aa6aae5ef5715d61a91714e62b0a094bc03f21