A functional equation and its application to the characterization of the Weibull and stable distributions