Use more sensible types.

Change function signatures to accept a size_t for parameters
representing lengths. Use int or unsigned int for counters that do not
represent a size.

Remove useless casts.