>but you can learn how it works so you can do it yourself
This part is definitely illegal. You can learn how it works but you cannot write the code yourself, you have to write the specs for someone else to do it, who hasn't done the reverse engineering.
That's clean room reverse engineering, a rigorous method to make absolutely sure that the results are legal so it can't be questioned later. It's not a legal requirement and it does not prevent lawsuits. It just makes it more likely that the legal battle will be won.
I think GP is referring to the DMCA, which forbids reverse-engineering unless it's to understand how to interface your own code with something (although I'm not a lawyer).
Yes, even under the DMCA it's totally legal to reverse engineer for the purposes of interoperability. Wine was created to make Windows applications work on Linux. I reverse engineered my laptop's crappy manufacturer software in order to make a Linux version.
This part is definitely illegal. You can learn how it works but you cannot write the code yourself, you have to write the specs for someone else to do it, who hasn't done the reverse engineering.