diff --git a/src/core/channel/call_op_string.c b/src/core/channel/call_op_string.c index 9a7838ce2fc..789913901ad 100644 --- a/src/core/channel/call_op_string.c +++ b/src/core/channel/call_op_string.c @@ -37,8 +37,8 @@ #include #include +#include "src/core/support/string.h" #include -#include #include #define MAX_APPEND 1024 diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c index 36312e54de8..04ce519ff37 100644 --- a/src/core/channel/channel_args.c +++ b/src/core/channel/channel_args.c @@ -33,9 +33,9 @@ #include #include "src/core/channel/channel_args.h" +#include "src/core/support/string.h" #include -#include #include diff --git a/src/core/channel/client_channel.c b/src/core/channel/client_channel.c index fa75561c78b..f6078aa8080 100644 --- a/src/core/channel/client_channel.c +++ b/src/core/channel/client_channel.c @@ -40,9 +40,9 @@ #include "src/core/channel/connected_channel.h" #include "src/core/channel/metadata_buffer.h" #include "src/core/iomgr/iomgr.h" +#include "src/core/support/string.h" #include #include -#include #include #include diff --git a/src/core/channel/connected_channel.c b/src/core/channel/connected_channel.c index e01cb81a890..cfa76f9e488 100644 --- a/src/core/channel/connected_channel.c +++ b/src/core/channel/connected_channel.c @@ -37,12 +37,12 @@ #include #include +#include "src/core/support/string.h" #include "src/core/transport/transport.h" #include #include #include #include -#include #define MAX_BUFFER_LENGTH 8192 /* the protobuf library will (by default) start warning at 100megs */ diff --git a/src/core/httpcli/httpcli.c b/src/core/httpcli/httpcli.c index 2143eeb63d0..d6fd8ca8658 100644 --- a/src/core/httpcli/httpcli.c +++ b/src/core/httpcli/httpcli.c @@ -44,9 +44,9 @@ #include "src/core/security/security_context.h" #include "src/core/security/google_root_certs.h" #include "src/core/security/secure_transport_setup.h" +#include "src/core/support/string.h" #include #include -#include typedef struct { gpr_slice request_text; diff --git a/src/core/httpcli/httpcli_security_context.c b/src/core/httpcli/httpcli_security_context.c index c7b9b330f0d..d074e163f16 100644 --- a/src/core/httpcli/httpcli_security_context.c +++ b/src/core/httpcli/httpcli_security_context.c @@ -36,9 +36,9 @@ #include #include "src/core/security/secure_transport_setup.h" +#include "src/core/support/string.h" #include #include -#include #include "src/core/tsi/ssl_transport_security.h" typedef struct { diff --git a/src/core/iomgr/resolve_address_posix.c b/src/core/iomgr/resolve_address_posix.c index c9c2c5378a5..f80eea7f46e 100644 --- a/src/core/iomgr/resolve_address_posix.c +++ b/src/core/iomgr/resolve_address_posix.c @@ -44,9 +44,9 @@ #include "src/core/iomgr/iomgr_internal.h" #include "src/core/iomgr/sockaddr_utils.h" #include "src/core/iomgr/socket_utils_posix.h" +#include "src/core/support/string.h" #include #include -#include #include #include diff --git a/src/core/iomgr/sockaddr_utils.c b/src/core/iomgr/sockaddr_utils.c index eca14a4f398..5bb11242843 100644 --- a/src/core/iomgr/sockaddr_utils.c +++ b/src/core/iomgr/sockaddr_utils.c @@ -37,8 +37,8 @@ #include #include +#include "src/core/support/string.h" #include -#include #include #include diff --git a/src/core/iomgr/socket_utils_common_posix.c b/src/core/iomgr/socket_utils_common_posix.c index 3a0639f3567..1854285b5a4 100644 --- a/src/core/iomgr/socket_utils_common_posix.c +++ b/src/core/iomgr/socket_utils_common_posix.c @@ -50,8 +50,8 @@ #include #include "src/core/iomgr/sockaddr_utils.h" +#include "src/core/support/string.h" #include -#include #include #include #include diff --git a/src/core/iomgr/tcp_posix.c b/src/core/iomgr/tcp_posix.c index 64996bd07d1..a9b59df8854 100644 --- a/src/core/iomgr/tcp_posix.c +++ b/src/core/iomgr/tcp_posix.c @@ -44,10 +44,10 @@ #include #include +#include "src/core/support/string.h" #include #include #include -#include #include #include diff --git a/src/core/security/credentials.c b/src/core/security/credentials.c index 628963e46cd..db73c01c1de 100644 --- a/src/core/security/credentials.c +++ b/src/core/security/credentials.c @@ -36,9 +36,9 @@ #include "src/core/httpcli/httpcli.h" #include "src/core/iomgr/iomgr.h" #include "src/core/security/json_token.h" +#include "src/core/support/string.h" #include #include -#include #include #include diff --git a/src/core/security/json_token.c b/src/core/security/json_token.c index 14ee758e8bf..82bd9b505a2 100644 --- a/src/core/security/json_token.c +++ b/src/core/security/json_token.c @@ -37,9 +37,9 @@ #include #include -#include #include "src/core/security/base64.h" +#include "src/core/support/string.h" #include #include diff --git a/src/core/security/secure_endpoint.c b/src/core/security/secure_endpoint.c index e73767c1aad..9f12cf5d60f 100644 --- a/src/core/security/secure_endpoint.c +++ b/src/core/security/secure_endpoint.c @@ -32,11 +32,11 @@ */ #include "src/core/security/secure_endpoint.h" +#include "src/core/support/string.h" #include #include #include #include -#include #include #include "src/core/tsi/transport_security_interface.h" diff --git a/src/core/security/security_context.c b/src/core/security/security_context.c index cce3c7fe04f..58cd458415d 100644 --- a/src/core/security/security_context.c +++ b/src/core/security/security_context.c @@ -39,12 +39,12 @@ #include "src/core/channel/http_client_filter.h" #include "src/core/security/credentials.h" #include "src/core/security/secure_endpoint.h" +#include "src/core/support/string.h" #include "src/core/surface/lame_client.h" #include "src/core/transport/chttp2/alpn.h" #include #include #include -#include #include "src/core/tsi/fake_transport_security.h" #include "src/core/tsi/ssl_transport_security.h" diff --git a/src/core/statistics/census_rpc_stats.c b/src/core/statistics/census_rpc_stats.c index 39094b5f65a..dd3c07e80b3 100644 --- a/src/core/statistics/census_rpc_stats.c +++ b/src/core/statistics/census_rpc_stats.c @@ -39,9 +39,9 @@ #include "src/core/statistics/census_tracing.h" #include "src/core/statistics/window_stats.h" #include "src/core/support/murmur_hash.h" +#include "src/core/support/string.h" #include #include -#include #include #define NUM_INTERVALS 3 diff --git a/src/core/statistics/census_tracing.c b/src/core/statistics/census_tracing.c index 1e616020715..3c4ba66f5f4 100644 --- a/src/core/statistics/census_tracing.c +++ b/src/core/statistics/census_tracing.c @@ -38,10 +38,10 @@ #include "src/core/statistics/census_rpc_stats.h" #include "src/core/statistics/hash_table.h" +#include "src/core/support/string.h" #include #include #include -#include #include #include diff --git a/src/core/support/cmdline.c b/src/core/support/cmdline.c index ff163a1f6cc..a55da9dd188 100644 --- a/src/core/support/cmdline.c +++ b/src/core/support/cmdline.c @@ -37,9 +37,9 @@ #include #include +#include "src/core/support/string.h" #include #include -#include typedef enum { ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype; diff --git a/src/core/support/host_port.c b/src/core/support/host_port.c index 02500551fcd..446c11ebec3 100644 --- a/src/core/support/host_port.c +++ b/src/core/support/host_port.c @@ -35,8 +35,8 @@ #include +#include "src/core/support/string.h" #include -#include int gpr_join_host_port(char **out, const char *host, int port) { if (host[0] != '[' && strchr(host, ':') != NULL) { diff --git a/src/core/support/string.c b/src/core/support/string.c index 7960547735b..9b5cac75966 100644 --- a/src/core/support/string.c +++ b/src/core/support/string.c @@ -31,7 +31,7 @@ * */ -#include +#include "src/core/support/string.h" #include #include @@ -122,3 +122,33 @@ int gpr_parse_bytes_to_uint32(const char *buf, size_t len, gpr_uint32 *result) { *result = out; return 1; } + +void gpr_reverse_bytes(char *str, int len) { + char *p1, *p2; + for (p1 = str, p2 = str + len - 1; p2 > p1; ++p1, --p2) { + char temp = *p1; + *p1 = *p2; + *p2 = temp; + } +} + +int gpr_ltoa(long value, char *string) { + int i = 0; + int neg = value < 0; + + if (value == 0) { + string[0] = '0'; + string[1] = 0; + return 1; + } + + if (neg) value = -value; + while (value) { + string[i++] = '0' + value % 10; + value /= 10; + } + if (neg) string[i++] = '-'; + gpr_reverse_bytes(string, i); + string[i] = 0; + return i; +} diff --git a/include/grpc/support/string.h b/src/core/support/string.h similarity index 88% rename from include/grpc/support/string.h rename to src/core/support/string.h index 68e7452a7fa..28b7029ecd6 100644 --- a/include/grpc/support/string.h +++ b/src/core/support/string.h @@ -60,6 +60,17 @@ char *gpr_hexdump(const char *buf, size_t len, gpr_uint32 flags); int gpr_parse_bytes_to_uint32(const char *data, size_t length, gpr_uint32 *result); +/* Minimum buffer size for calling ltoa */ +#define GPR_LTOA_MIN_BUFSIZE (3 * sizeof(long)) + +/* Convert a long to a string in base 10; returns the length of the + output string (or 0 on failure). + output must be at least GPR_LTOA_MIN_BUFSIZE bytes long. */ +int gpr_ltoa(long value, char *output); + +/* Reverse a run of bytes */ +void gpr_reverse_bytes(char *str, int len); + /* printf to a newly-allocated string. The set of supported formats may vary between platforms. diff --git a/src/core/surface/call.c b/src/core/surface/call.c index 05f66612548..bb5aa41f11b 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -35,11 +35,11 @@ #include "src/core/channel/channel_stack.h" #include "src/core/channel/metadata_buffer.h" #include "src/core/iomgr/alarm.h" +#include "src/core/support/string.h" #include "src/core/surface/channel.h" #include "src/core/surface/completion_queue.h" #include #include -#include #include #include diff --git a/src/core/surface/channel_create.c b/src/core/surface/channel_create.c index 41093d78eff..6939b92c60c 100644 --- a/src/core/surface/channel_create.c +++ b/src/core/surface/channel_create.c @@ -48,10 +48,10 @@ #include "src/core/iomgr/tcp_client.h" #include "src/core/surface/channel.h" #include "src/core/surface/client.h" +#include "src/core/support/string.h" #include "src/core/transport/chttp2_transport.h" #include #include -#include #include #include diff --git a/src/core/surface/client.c b/src/core/surface/client.c index 74c79bdf9b1..fe3a81f1b9f 100644 --- a/src/core/surface/client.c +++ b/src/core/surface/client.c @@ -34,9 +34,9 @@ #include "src/core/surface/client.h" #include "src/core/surface/call.h" +#include "src/core/support/string.h" #include #include -#include typedef struct { void *unused; } call_data; diff --git a/src/core/surface/completion_queue.c b/src/core/surface/completion_queue.c index 652f23e8889..e1cc114cda6 100644 --- a/src/core/surface/completion_queue.c +++ b/src/core/surface/completion_queue.c @@ -37,13 +37,13 @@ #include #include "src/core/iomgr/pollset.h" +#include "src/core/support/string.h" #include "src/core/surface/call.h" #include "src/core/surface/event_string.h" #include "src/core/surface/surface_trace.h" #include #include #include -#include #define NUM_TAG_BUCKETS 31 diff --git a/src/core/surface/event_string.c b/src/core/surface/event_string.c index 8ae2af74726..e38ef06c9ff 100644 --- a/src/core/surface/event_string.c +++ b/src/core/surface/event_string.c @@ -35,7 +35,7 @@ #include -#include +#include "src/core/support/string.h" #include static size_t addhdr(char *p, grpc_event *ev) { diff --git a/src/core/surface/lame_client.c b/src/core/surface/lame_client.c index a5244dbe61f..056c98646b8 100644 --- a/src/core/surface/lame_client.c +++ b/src/core/surface/lame_client.c @@ -36,11 +36,11 @@ #include #include "src/core/channel/channel_stack.h" +#include "src/core/support/string.h" #include "src/core/surface/channel.h" #include "src/core/surface/call.h" #include #include -#include typedef struct { void *unused; } call_data; diff --git a/src/core/surface/secure_channel_create.c b/src/core/surface/secure_channel_create.c index 3d5727927d0..a231b2708e6 100644 --- a/src/core/surface/secure_channel_create.c +++ b/src/core/surface/secure_channel_create.c @@ -48,13 +48,13 @@ #include "src/core/security/auth.h" #include "src/core/security/security_context.h" #include "src/core/security/secure_transport_setup.h" +#include "src/core/support/string.h" #include "src/core/surface/channel.h" #include "src/core/surface/client.h" #include "src/core/transport/chttp2_transport.h" #include #include #include -#include #include #include #include "src/core/tsi/transport_security_interface.h" diff --git a/src/core/surface/server.c b/src/core/surface/server.c index cbdd3bfa308..9585e4e8ea6 100644 --- a/src/core/surface/server.c +++ b/src/core/surface/server.c @@ -40,12 +40,12 @@ #include "src/core/channel/channel_args.h" #include "src/core/channel/connected_channel.h" #include "src/core/iomgr/iomgr.h" +#include "src/core/support/string.h" #include "src/core/surface/call.h" #include "src/core/surface/channel.h" #include "src/core/surface/completion_queue.h" #include #include -#include #include typedef enum { PENDING_START, ALL_CALLS, CALL_LIST_COUNT } call_list; diff --git a/src/core/transport/chttp2/frame_data.c b/src/core/transport/chttp2/frame_data.c index 00b020b31b1..dee61cee500 100644 --- a/src/core/transport/chttp2/frame_data.c +++ b/src/core/transport/chttp2/frame_data.c @@ -35,9 +35,9 @@ #include +#include "src/core/support/string.h" #include #include -#include #include #include "src/core/transport/transport.h" diff --git a/src/core/transport/chttp2/hpack_parser.c b/src/core/transport/chttp2/hpack_parser.c index 64e08ffac72..c98b90e5d11 100644 --- a/src/core/transport/chttp2/hpack_parser.c +++ b/src/core/transport/chttp2/hpack_parser.c @@ -38,10 +38,10 @@ #include #include "src/core/transport/chttp2/bin_encoder.h" +#include "src/core/support/string.h" #include #include #include -#include #include typedef enum { diff --git a/src/core/transport/chttp2/timeout_encoding.c b/src/core/transport/chttp2/timeout_encoding.c index 2706c369a6d..23c4554cf27 100644 --- a/src/core/transport/chttp2/timeout_encoding.c +++ b/src/core/transport/chttp2/timeout_encoding.c @@ -36,6 +36,8 @@ #include #include +#include "src/core/support/string.h" + static int round_up(int x, int divisor) { return (x / divisor + (x % divisor != 0)) * divisor; } @@ -53,15 +55,21 @@ static int round_up_to_three_sig_figs(int x) { } /* encode our minimum viable timeout value */ -static void enc_tiny(char *buffer) { strcpy(buffer, "1n"); } +static void enc_tiny(char *buffer) { memcpy(buffer, "1n", 3); } + +static void enc_ext(char *buffer, long value, char ext) { + int n = gpr_ltoa(value, buffer); + buffer[n] = ext; + buffer[n+1] = 0; +} static void enc_seconds(char *buffer, long sec) { if (sec % 3600 == 0) { - sprintf(buffer, "%ldH", sec / 3600); + enc_ext(buffer, sec / 3600, 'H'); } else if (sec % 60 == 0) { - sprintf(buffer, "%ldM", sec / 60); + enc_ext(buffer, sec / 60, 'M'); } else { - sprintf(buffer, "%ldS", sec); + enc_ext(buffer, sec, 'S'); } } @@ -69,23 +77,23 @@ static void enc_nanos(char *buffer, int x) { x = round_up_to_three_sig_figs(x); if (x < 100000) { if (x % 1000 == 0) { - sprintf(buffer, "%du", x / 1000); + enc_ext(buffer, x / 1000, 'u'); } else { - sprintf(buffer, "%dn", x); + enc_ext(buffer, x, 'n'); } } else if (x < 100000000) { if (x % 1000000 == 0) { - sprintf(buffer, "%dm", x / 1000000); + enc_ext(buffer, x / 1000000, 'm'); } else { - sprintf(buffer, "%du", x / 1000); + enc_ext(buffer, x / 1000, 'u'); } } else if (x < 1000000000) { - sprintf(buffer, "%dm", x / 1000000); + enc_ext(buffer, x / 1000000, 'm'); } else { /* note that this is only ever called with times of less than one second, so if we reach here the time must have been rounded up to a whole second (and no more) */ - strcpy(buffer, "1S"); + memcpy(buffer, "1S", 3); } } @@ -93,18 +101,18 @@ static void enc_micros(char *buffer, int x) { x = round_up_to_three_sig_figs(x); if (x < 100000) { if (x % 1000 == 0) { - sprintf(buffer, "%dm", x / 1000); + enc_ext(buffer, x / 1000, 'm'); } else { - sprintf(buffer, "%du", x); + enc_ext(buffer, x, 'u'); } } else if (x < 100000000) { if (x % 1000000 == 0) { - sprintf(buffer, "%dS", x / 1000000); + enc_ext(buffer, x / 1000000, 'S'); } else { - sprintf(buffer, "%dm", x / 1000); + enc_ext(buffer, x / 1000, 'm'); } } else { - sprintf(buffer, "%dS", x / 1000000); + enc_ext(buffer, x / 1000000, 'S'); } } diff --git a/src/core/transport/chttp2/timeout_encoding.h b/src/core/transport/chttp2/timeout_encoding.h index a4582566adc..d1e47760324 100644 --- a/src/core/transport/chttp2/timeout_encoding.h +++ b/src/core/transport/chttp2/timeout_encoding.h @@ -34,8 +34,11 @@ #ifndef __GRPC_INTERNAL_TRANSPORT_CHTTP2_TIMEOUT_ENCODING_H_ #define __GRPC_INTERNAL_TRANSPORT_CHTTP2_TIMEOUT_ENCODING_H_ +#include "src/core/support/string.h" #include +#define GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE (GPR_LTOA_MIN_BUFSIZE + 1) + /* Encode/decode timeouts to the GRPC over HTTP2 format; encoding may round up arbitrarily */ void grpc_chttp2_encode_timeout(gpr_timespec timeout, char *buffer); diff --git a/src/core/transport/chttp2_transport.c b/src/core/transport/chttp2_transport.c index 17b37d6d4ae..6dfffa1fc50 100644 --- a/src/core/transport/chttp2_transport.c +++ b/src/core/transport/chttp2_transport.c @@ -37,6 +37,7 @@ #include #include +#include "src/core/support/string.h" #include "src/core/transport/chttp2/frame_data.h" #include "src/core/transport/chttp2/frame_goaway.h" #include "src/core/transport/chttp2/frame_ping.h" @@ -53,7 +54,6 @@ #include #include #include -#include #include #define DEFAULT_WINDOW 65535 diff --git a/test/core/echo/echo_test.c b/test/core/echo/echo_test.c index 16d381fb654..6449b2414f3 100644 --- a/test/core/echo/echo_test.c +++ b/test/core/echo/echo_test.c @@ -42,10 +42,10 @@ #include #include "src/core/iomgr/socket_utils_posix.h" +#include "src/core/support/string.h" #include #include #include -#include #include "test/core/util/port.h" int test_client(const char *root, const char *host, int port) { diff --git a/test/core/echo/server.c b/test/core/echo/server.c index 35f118dc9b1..57b083779c3 100644 --- a/test/core/echo/server.c +++ b/test/core/echo/server.c @@ -39,11 +39,11 @@ #include #include +#include "src/core/support/string.h" #include "test/core/util/test_config.h" #include #include #include -#include #include #include "test/core/util/port.h" diff --git a/test/core/end2end/cq_verifier.c b/test/core/end2end/cq_verifier.c index 82c0ef1700b..49b131c2362 100644 --- a/test/core/end2end/cq_verifier.c +++ b/test/core/end2end/cq_verifier.c @@ -45,10 +45,10 @@ #include #include "src/core/surface/event_string.h" +#include "src/core/support/string.h" #include #include #include -#include #include #include diff --git a/test/core/fling/fling_stream_test.c b/test/core/fling/fling_stream_test.c index f6fe69824b3..7f52fb1bad1 100644 --- a/test/core/fling/fling_stream_test.c +++ b/test/core/fling/fling_stream_test.c @@ -41,9 +41,9 @@ #include #include +#include "src/core/support/string.h" #include #include -#include #include "test/core/util/port.h" int main(int argc, char **argv) { diff --git a/test/core/fling/fling_test.c b/test/core/fling/fling_test.c index 4607aa5f988..b2272f20c8e 100644 --- a/test/core/fling/fling_test.c +++ b/test/core/fling/fling_test.c @@ -43,7 +43,7 @@ #include #include -#include +#include "src/core/support/string.h" #include "test/core/util/port.h" int main(int argc, char **argv) { diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index 9c60f4c2338..ec21e0d42f7 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -37,9 +37,9 @@ #include "src/core/httpcli/httpcli.h" #include "src/core/security/json_token.h" +#include "src/core/support/string.h" #include #include -#include #include #include "test/core/util/test_config.h" #include diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c index e87a606aba2..a01ec6f87f3 100644 --- a/test/core/support/string_test.c +++ b/test/core/support/string_test.c @@ -31,7 +31,7 @@ * */ -#include +#include "src/core/support/string.h" #include #include diff --git a/test/core/transport/chttp2/bin_encoder_test.c b/test/core/transport/chttp2/bin_encoder_test.c index ea24f5cbd7a..048ed7edd3c 100644 --- a/test/core/transport/chttp2/bin_encoder_test.c +++ b/test/core/transport/chttp2/bin_encoder_test.c @@ -35,9 +35,9 @@ #include +#include "src/core/support/string.h" #include #include -#include static int all_ok = 1; diff --git a/test/core/transport/chttp2/stream_encoder_test.c b/test/core/transport/chttp2/stream_encoder_test.c index cebc2634fba..eb0f688f58e 100644 --- a/test/core/transport/chttp2/stream_encoder_test.c +++ b/test/core/transport/chttp2/stream_encoder_test.c @@ -35,10 +35,10 @@ #include +#include "src/core/support/string.h" #include "src/core/transport/chttp2/hpack_parser.h" #include #include -#include #include "test/core/util/parse_hexstring.h" #include "test/core/util/slice_splitter.h" #include "test/core/util/test_config.h" diff --git a/test/core/transport/chttp2/timeout_encoding_test.c b/test/core/transport/chttp2/timeout_encoding_test.c index 4bb84e3f0bf..ffa0070e341 100644 --- a/test/core/transport/chttp2/timeout_encoding_test.c +++ b/test/core/transport/chttp2/timeout_encoding_test.c @@ -43,7 +43,7 @@ #define LOG_TEST() gpr_log(GPR_INFO, "%s", __FUNCTION__) static void assert_encodes_as(gpr_timespec ts, const char *s) { - char buffer[32]; + char buffer[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE]; grpc_chttp2_encode_timeout(ts, buffer); gpr_log(GPR_INFO, "check '%s' == '%s'", buffer, s); GPR_ASSERT(0 == strcmp(buffer, s)); diff --git a/test/core/transport/transport_end2end_tests.c b/test/core/transport/transport_end2end_tests.c index 5d26ef53b90..8e9b4a2cc9e 100644 --- a/test/core/transport/transport_end2end_tests.c +++ b/test/core/transport/transport_end2end_tests.c @@ -37,10 +37,10 @@ #include #include +#include "src/core/support/string.h" #include "src/core/transport/transport.h" #include #include -#include #include #include