| 28 | } |
| 29 | |
| 30 | String Base64::Decode(const String& input) |
| 31 | { |
| 32 | BIO *biomem = BIO_new_mem_buf( |
| 33 | const_cast<char*>(input.CStr()), input.GetLength()); |
| 34 | BIO *bio64 = BIO_new(BIO_f_base64()); |
| 35 | BIO_push(bio64, biomem); |
| 36 | BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL); |
| 37 | |
| 38 | auto *outbuf = new char[input.GetLength()]; |
| 39 | |
| 40 | size_t len = 0; |
| 41 | int rc; |
| 42 | |
| 43 | while ((rc = BIO_read(bio64, outbuf + len, input.GetLength() - len)) > 0) |
| 44 | len += rc; |
| 45 | |
| 46 | String ret = String(outbuf, outbuf + len); |
| 47 | BIO_free_all(bio64); |
| 48 | delete [] outbuf; |
| 49 | |
| 50 | if (ret.IsEmpty() && !input.IsEmpty()) |
| 51 | throw std::invalid_argument("Not a valid base64 string"); |
| 52 | |
| 53 | return ret; |
| 54 | } |