Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | fd3d820 | 2016-01-19 20:17:07 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/transport/chttp2/timeout_encoding.h" |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 | |
Jan Tattermusch | 4b3ecdf | 2015-12-04 09:33:05 -0800 | [diff] [blame] | 39 | #include <grpc/support/port_platform.h> |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 40 | #include "src/core/support/string.h" |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 41 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 42 | static int64_t round_up(int64_t x, int64_t divisor) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | return (x / divisor + (x % divisor != 0)) * divisor; |
| 44 | } |
| 45 | |
| 46 | /* round an integer up to the next value with three significant figures */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 47 | static int64_t round_up_to_three_sig_figs(int64_t x) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | if (x < 1000) return x; |
| 49 | if (x < 10000) return round_up(x, 10); |
| 50 | if (x < 100000) return round_up(x, 100); |
| 51 | if (x < 1000000) return round_up(x, 1000); |
| 52 | if (x < 10000000) return round_up(x, 10000); |
| 53 | if (x < 100000000) return round_up(x, 100000); |
| 54 | if (x < 1000000000) return round_up(x, 1000000); |
| 55 | return round_up(x, 10000000); |
| 56 | } |
| 57 | |
| 58 | /* encode our minimum viable timeout value */ |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 59 | static void enc_tiny(char *buffer) { memcpy(buffer, "1n", 3); } |
| 60 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 61 | static void enc_ext(char *buffer, int64_t value, char ext) { |
| 62 | int n = int64_ttoa(value, buffer); |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 63 | buffer[n] = ext; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 64 | buffer[n + 1] = 0; |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 65 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 67 | static void enc_seconds(char *buffer, int64_t sec) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | if (sec % 3600 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 69 | enc_ext(buffer, sec / 3600, 'H'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | } else if (sec % 60 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 71 | enc_ext(buffer, sec / 60, 'M'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 73 | enc_ext(buffer, sec, 'S'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 77 | static void enc_nanos(char *buffer, int64_t x) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | x = round_up_to_three_sig_figs(x); |
| 79 | if (x < 100000) { |
| 80 | if (x % 1000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 81 | enc_ext(buffer, x / 1000, 'u'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 83 | enc_ext(buffer, x, 'n'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | } |
| 85 | } else if (x < 100000000) { |
| 86 | if (x % 1000000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 87 | enc_ext(buffer, x / 1000000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 89 | enc_ext(buffer, x / 1000, 'u'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | } |
| 91 | } else if (x < 1000000000) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 92 | enc_ext(buffer, x / 1000000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | } else { |
| 94 | /* note that this is only ever called with times of less than one second, |
| 95 | so if we reach here the time must have been rounded up to a whole second |
| 96 | (and no more) */ |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 97 | memcpy(buffer, "1S", 3); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 101 | static void enc_micros(char *buffer, int64_t x) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 102 | x = round_up_to_three_sig_figs(x); |
| 103 | if (x < 100000) { |
| 104 | if (x % 1000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 105 | enc_ext(buffer, x / 1000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 107 | enc_ext(buffer, x, 'u'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | } |
| 109 | } else if (x < 100000000) { |
| 110 | if (x % 1000000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 111 | enc_ext(buffer, x / 1000000, 'S'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 113 | enc_ext(buffer, x / 1000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | } |
| 115 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 116 | enc_ext(buffer, x / 1000000, 'S'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | void grpc_chttp2_encode_timeout(gpr_timespec timeout, char *buffer) { |
| 121 | if (timeout.tv_sec < 0) { |
| 122 | enc_tiny(buffer); |
| 123 | } else if (timeout.tv_sec == 0) { |
| 124 | enc_nanos(buffer, timeout.tv_nsec); |
| 125 | } else if (timeout.tv_sec < 1000 && timeout.tv_nsec != 0) { |
| 126 | enc_micros(buffer, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 127 | (int64_t)(timeout.tv_sec * 1000000) + |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | (timeout.tv_nsec / 1000 + (timeout.tv_nsec % 1000 != 0))); |
| 129 | } else { |
| 130 | enc_seconds(buffer, timeout.tv_sec + (timeout.tv_nsec != 0)); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static int is_all_whitespace(const char *p) { |
| 135 | while (*p == ' ') p++; |
| 136 | return *p == 0; |
| 137 | } |
| 138 | |
| 139 | int grpc_chttp2_decode_timeout(const char *buffer, gpr_timespec *timeout) { |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 140 | int32_t x = 0; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 141 | const uint8_t *p = (const uint8_t *)buffer; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | int have_digit = 0; |
| 143 | /* skip whitespace */ |
| 144 | for (; *p == ' '; p++) |
| 145 | ; |
| 146 | /* decode numeric part */ |
| 147 | for (; *p >= '0' && *p <= '9'; p++) { |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 148 | int32_t digit = (int32_t)(*p - (uint8_t)'0'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | have_digit = 1; |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 150 | /* spec allows max. 8 digits, but we allow values up to 1,000,000,000 */ |
| 151 | if (x >= (100 * 1000 * 1000)) { |
| 152 | if (x != (100 * 1000 * 1000) || digit != 0) { |
Craig Tiller | d262454 | 2016-03-21 14:31:01 -0700 | [diff] [blame] | 153 | *timeout = gpr_inf_future(GPR_TIMESPAN); |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 154 | return 1; |
| 155 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | } |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 157 | x = x * 10 + digit; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | } |
| 159 | if (!have_digit) return 0; |
| 160 | /* skip whitespace */ |
| 161 | for (; *p == ' '; p++) |
| 162 | ; |
| 163 | /* decode unit specifier */ |
| 164 | switch (*p) { |
| 165 | case 'n': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 166 | *timeout = gpr_time_from_nanos(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 167 | break; |
| 168 | case 'u': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 169 | *timeout = gpr_time_from_micros(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | break; |
| 171 | case 'm': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 172 | *timeout = gpr_time_from_millis(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | break; |
| 174 | case 'S': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 175 | *timeout = gpr_time_from_seconds(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | break; |
| 177 | case 'M': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 178 | *timeout = gpr_time_from_minutes(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 179 | break; |
| 180 | case 'H': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 181 | *timeout = gpr_time_from_hours(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | break; |
| 183 | default: |
| 184 | return 0; |
| 185 | } |
| 186 | p++; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 187 | return is_all_whitespace((const char *)p); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 188 | } |