Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / transport / timeout_encoding.cc
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/transport/timeout_encoding.h"
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "src/core/lib/gpr/string.h"
27
28 static int64_t round_up(int64_t x, int64_t divisor) {
29   return (x / divisor + (x % divisor != 0)) * divisor;
30 }
31
32 /* round an integer up to the next value with three significant figures */
33 static int64_t round_up_to_three_sig_figs(int64_t x) {
34   if (x < 1000) return x;
35   if (x < 10000) return round_up(x, 10);
36   if (x < 100000) return round_up(x, 100);
37   if (x < 1000000) return round_up(x, 1000);
38   if (x < 10000000) return round_up(x, 10000);
39   if (x < 100000000) return round_up(x, 100000);
40   if (x < 1000000000) return round_up(x, 1000000);
41   return round_up(x, 10000000);
42 }
43
44 /* encode our minimum viable timeout value */
45 static void enc_tiny(char* buffer) { memcpy(buffer, "1n", 3); }
46
47 /* encode our maximum timeout value, about 1157 days */
48 static void enc_huge(char* buffer) { memcpy(buffer, "99999999S", 10); }
49
50 static void enc_ext(char* buffer, int64_t value, char ext) {
51   int n = int64_ttoa(value, buffer);
52   buffer[n] = ext;
53   buffer[n + 1] = 0;
54 }
55
56 static void enc_seconds(char* buffer, int64_t sec) {
57   sec = round_up_to_three_sig_figs(sec);
58   if (sec % 3600 == 0) {
59     enc_ext(buffer, sec / 3600, 'H');
60   } else if (sec % 60 == 0) {
61     enc_ext(buffer, sec / 60, 'M');
62   } else {
63     enc_ext(buffer, sec, 'S');
64   }
65 }
66
67 static void enc_millis(char* buffer, int64_t x) {
68   x = round_up_to_three_sig_figs(x);
69   if (x < GPR_MS_PER_SEC) {
70     enc_ext(buffer, x, 'm');
71   } else {
72     if (x % GPR_MS_PER_SEC == 0) {
73       enc_seconds(buffer, x / GPR_MS_PER_SEC);
74     } else {
75       enc_ext(buffer, x, 'm');
76     }
77   }
78 }
79
80 void grpc_http2_encode_timeout(grpc_millis timeout, char* buffer) {
81   const grpc_millis kMaxTimeout = 99999999000;
82   if (timeout <= 0) {
83     enc_tiny(buffer);
84   } else if (timeout < 1000 * GPR_MS_PER_SEC) {
85     enc_millis(buffer, timeout);
86   } else if (timeout >= kMaxTimeout) {
87     enc_huge(buffer);
88   } else {
89     enc_seconds(buffer,
90                 timeout / GPR_MS_PER_SEC + (timeout % GPR_MS_PER_SEC != 0));
91   }
92 }
93
94 static int is_all_whitespace(const char* p, const char* end) {
95   while (p != end && *p == ' ') p++;
96   return p == end;
97 }
98
99 int grpc_http2_decode_timeout(const grpc_slice& text, grpc_millis* timeout) {
100   grpc_millis x = 0;
101   const uint8_t* p = GRPC_SLICE_START_PTR(text);
102   const uint8_t* end = GRPC_SLICE_END_PTR(text);
103   int have_digit = 0;
104   /* skip whitespace */
105   for (; p != end && *p == ' '; p++)
106     ;
107   /* decode numeric part */
108   for (; p != end && *p >= '0' && *p <= '9'; p++) {
109     int32_t digit = static_cast<int32_t>(*p - static_cast<uint8_t>('0'));
110     have_digit = 1;
111     /* spec allows max. 8 digits, but we allow values up to 1,000,000,000 */
112     if (x >= (100 * 1000 * 1000)) {
113       if (x != (100 * 1000 * 1000) || digit != 0) {
114         *timeout = GRPC_MILLIS_INF_FUTURE;
115         return 1;
116       }
117     }
118     x = x * 10 + digit;
119   }
120   if (!have_digit) return 0;
121   /* skip whitespace */
122   for (; p != end && *p == ' '; p++)
123     ;
124   if (p == end) return 0;
125   /* decode unit specifier */
126   switch (*p) {
127     case 'n':
128       *timeout = x / GPR_NS_PER_MS + (x % GPR_NS_PER_MS != 0);
129       break;
130     case 'u':
131       *timeout = x / GPR_US_PER_MS + (x % GPR_US_PER_MS != 0);
132       break;
133     case 'm':
134       *timeout = x;
135       break;
136     case 'S':
137       *timeout = x * GPR_MS_PER_SEC;
138       break;
139     case 'M':
140       *timeout = x * 60 * GPR_MS_PER_SEC;
141       break;
142     case 'H':
143       *timeout = x * 60 * 60 * GPR_MS_PER_SEC;
144       break;
145     default:
146       return 0;
147   }
148   p++;
149   return is_all_whitespace(reinterpret_cast<const char*>(p),
150                            reinterpret_cast<const char*>(end));
151 }