| 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
| 3 | * |
| 4 | * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | * IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** \defgroup sha SHA and B64 helpers |
| 26 | * ##SHA and B64 helpers |
| 27 | * |
| 28 | * These provide SHA-1 and B64 helper apis |
| 29 | */ |
| 30 | ///@{ |
| 31 | #ifdef LWS_SHA1_USE_OPENSSL_NAME |
| 32 | #define lws_SHA1 SHA1 |
| 33 | #else |
| 34 | /** |
| 35 | * lws_SHA1(): make a SHA-1 digest of a buffer |
| 36 | * |
| 37 | * \param d: incoming buffer |
| 38 | * \param n: length of incoming buffer |
| 39 | * \param md: buffer for message digest (must be >= 20 bytes) |
| 40 | * |
| 41 | * Reduces any size buffer into a 20-byte SHA-1 hash. |
| 42 | */ |
| 43 | LWS_VISIBLE LWS_EXTERN unsigned char * |
| 44 | lws_SHA1(const unsigned char *d, size_t n, unsigned char *md); |
| 45 | #endif |
| 46 | /** |
| 47 | * lws_b64_encode_string(): encode a string into base 64 |
| 48 | * |
| 49 | * \param in: incoming buffer |
| 50 | * \param in_len: length of incoming buffer |
| 51 | * \param out: result buffer |
| 52 | * \param out_size: length of result buffer |
| 53 | * |
| 54 | * Encodes a string using b64 |
| 55 | */ |
| 56 | LWS_VISIBLE LWS_EXTERN int |
| 57 | lws_b64_encode_string(const char *in, int in_len, char *out, int out_size); |
| 58 | /** |
| 59 | * lws_b64_encode_string_url(): encode a string into base 64 |
| 60 | * |
| 61 | * \param in: incoming buffer |
| 62 | * \param in_len: length of incoming buffer |
| 63 | * \param out: result buffer |
| 64 | * \param out_size: length of result buffer |
| 65 | * |
| 66 | * Encodes a string using b64 with the "URL" variant (+ -> -, and / -> _) |
| 67 | */ |
| 68 | LWS_VISIBLE LWS_EXTERN int |
| 69 | lws_b64_encode_string_url(const char *in, int in_len, char *out, int out_size); |
| 70 | /** |
| 71 | * lws_b64_decode_string(): decode a string from base 64 |
| 72 | * |
| 73 | * \param in: incoming buffer |
| 74 | * \param out: result buffer |
| 75 | * \param out_size: length of result buffer |
| 76 | * |
| 77 | * Decodes a NUL-terminated string using b64 |
| 78 | * |
| 79 | * Returns used length of output buffer |
| 80 | */ |
| 81 | LWS_VISIBLE LWS_EXTERN int |
| 82 | lws_b64_decode_string(const char *in, char *out, int out_size); |
| 83 | /** |
| 84 | * lws_b64_decode_string_len(): decode a string from base 64 |
| 85 | * |
| 86 | * \param in: incoming buffer |
| 87 | * \param in_len: length of incoming buffer |
| 88 | * \param out: result buffer |
| 89 | * \param out_size: length of result buffer |
| 90 | * |
| 91 | * Decodes a range of chars using b64 |
| 92 | * |
| 93 | * Returns used length of output buffer |
| 94 | */ |
| 95 | LWS_VISIBLE LWS_EXTERN int |
| 96 | lws_b64_decode_string_len(const char *in, int in_len, char *out, int out_size); |
| 97 | |
| 98 | struct lws_b64state { |
| 99 | unsigned char quad[4]; |
| 100 | size_t done; |
| 101 | size_t len; |
| 102 | int i; |
| 103 | int c; |
| 104 | }; |
| 105 | |
| 106 | LWS_VISIBLE LWS_EXTERN void |
| 107 | lws_b64_decode_state_init(struct lws_b64state *state); |
| 108 | |
| 109 | LWS_VISIBLE LWS_EXTERN int |
| 110 | lws_b64_decode_stateful(struct lws_b64state *s, const char *in, size_t *in_len, |
| 111 | uint8_t *out, size_t *out_size, int final); |
| 112 | ///@} |
| 113 | |
| 114 | |