Any person who commits any public offense while in custody in any local detention facility, as defined in Section 6031.4, or any state prison, as defined in Section 4504, is guilty of a crime. That crime shall be punished as provided in the section prescribing the punishment for that public offense.