Magnetostatic equilibria and analogous Euler flows of arbitrarily complex topology. Part 1. Fundamentals