PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/jsonrpc-c/netstring.c

https://gitlab.com/javajamesb08/kamailio
C | 198 lines | 89 code | 36 blank | 73 comment | 35 complexity | db7b4b91cfc9b7d708a50f7ba692179b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, 0BSD
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Flowroute LLC (flowroute.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <math.h>
  29. #include "netstring.h"
  30. #include "../../sr_module.h"
  31. #include "../../mem/mem.h"
  32. int netstring_read_fd(int fd, char **netstring) {
  33. int i, bytes;
  34. size_t len = 0;
  35. *netstring = NULL;
  36. char buffer[10]={0};
  37. /* Peek at first 10 bytes, to get length and colon */
  38. bytes = recv(fd,buffer,10,MSG_PEEK);
  39. if (bytes<3) return NETSTRING_ERROR_TOO_SHORT;
  40. /* No leading zeros allowed! */
  41. if (buffer[0] == '0' && isdigit(buffer[1]))
  42. return NETSTRING_ERROR_LEADING_ZERO;
  43. /* The netstring must start with a number */
  44. if (!isdigit(buffer[0])) return NETSTRING_ERROR_NO_LENGTH;
  45. /* Read the number of bytes */
  46. for (i = 0; i < bytes && isdigit(buffer[i]); i++) {
  47. /* Error if more than 9 digits */
  48. if (i >= 9) return NETSTRING_ERROR_TOO_LONG;
  49. /* Accumulate each digit, assuming ASCII. */
  50. len = len*10 + (buffer[i] - '0');
  51. }
  52. /* Read the colon */
  53. if (buffer[i++] != ':') return NETSTRING_ERROR_NO_COLON;
  54. /* Read the whole string from the buffer */
  55. size_t read_len = i+len+1;
  56. char *buffer2 = pkg_malloc(read_len);
  57. if (!buffer2) {
  58. LM_ERR("Out of memory!");
  59. return -1;
  60. }
  61. bytes = recv(fd,buffer2,read_len,0);
  62. /* Make sure we got the whole netstring */
  63. if (read_len > bytes) return NETSTRING_ERROR_TOO_SHORT;
  64. /* Test for the trailing comma */
  65. if (buffer2[read_len-1] != ',') return NETSTRING_ERROR_NO_COMMA;
  66. buffer2[read_len-1] = '\0';
  67. int x;
  68. for(x=0;x<=read_len-i-1;x++) {
  69. buffer2[x]=buffer2[x+i];
  70. }
  71. *netstring = buffer2;
  72. return 0;
  73. }
  74. /* Reads a netstring from a `buffer` of length `buffer_length`. Writes
  75. to `netstring_start` a pointer to the beginning of the string in
  76. the buffer, and to `netstring_length` the length of the
  77. string. Does not allocate any memory. If it reads successfully,
  78. then it returns 0. If there is an error, then the return value will
  79. be negative. The error values are:
  80. NETSTRING_ERROR_TOO_LONG More than 999999999 bytes in a field
  81. NETSTRING_ERROR_NO_COLON No colon was found after the number
  82. NETSTRING_ERROR_TOO_SHORT Number of bytes greater than buffer length
  83. NETSTRING_ERROR_NO_COMMA No comma was found at the end
  84. NETSTRING_ERROR_LEADING_ZERO Leading zeros are not allowed
  85. NETSTRING_ERROR_NO_LENGTH Length not given at start of netstring
  86. If you're sending messages with more than 999999999 bytes -- about
  87. 2 GB -- then you probably should not be doing so in the form of a
  88. single netstring. This restriction is in place partially to protect
  89. from malicious or erroneous input, and partly to be compatible with
  90. D. J. Bernstein's reference implementation.
  91. Example:
  92. if (netstring_read("3:foo,", 6, &str, &len) < 0) explode_and_die();
  93. */
  94. int netstring_read(char *buffer, size_t buffer_length,
  95. char **netstring_start, size_t *netstring_length) {
  96. int i;
  97. size_t len = 0;
  98. /* Write default values for outputs */
  99. *netstring_start = NULL; *netstring_length = 0;
  100. /* Make sure buffer is big enough. Minimum size is 3. */
  101. if (buffer_length < 3) return NETSTRING_ERROR_TOO_SHORT;
  102. /* No leading zeros allowed! */
  103. if (buffer[0] == '0' && isdigit(buffer[1]))
  104. return NETSTRING_ERROR_LEADING_ZERO;
  105. /* The netstring must start with a number */
  106. if (!isdigit(buffer[0])) return NETSTRING_ERROR_NO_LENGTH;
  107. /* Read the number of bytes */
  108. for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) {
  109. /* Error if more than 9 digits */
  110. if (i >= 9) return NETSTRING_ERROR_TOO_LONG;
  111. /* Accumulate each digit, assuming ASCII. */
  112. len = len*10 + (buffer[i] - '0');
  113. }
  114. /* Check buffer length once and for all. Specifically, we make sure
  115. that the buffer is longer than the number we've read, the length
  116. of the string itself, and the colon and comma. */
  117. if (i + len + 1 >= buffer_length) return NETSTRING_ERROR_TOO_SHORT;
  118. /* Read the colon */
  119. if (buffer[i++] != ':') return NETSTRING_ERROR_NO_COLON;
  120. /* Test for the trailing comma, and set the return values */
  121. if (buffer[i + len] != ',') return NETSTRING_ERROR_NO_COMMA;
  122. *netstring_start = &buffer[i]; *netstring_length = len;
  123. return 0;
  124. }
  125. /* Return the length, in ASCII characters, of a netstring containing
  126. `data_length` bytes. */
  127. size_t netstring_buffer_size(size_t data_length) {
  128. if (data_length == 0) return 3;
  129. return (size_t)ceil(log10((double)data_length + 1)) + data_length + 2;
  130. }
  131. /* Allocate and create a netstring containing the first `len` bytes of
  132. `data`. This must be manually freed by the client. If `len` is 0
  133. then no data will be read from `data`, and it may be NULL. */
  134. size_t netstring_encode_new(char **netstring, char *data, size_t len) {
  135. char *ns;
  136. size_t num_len = 1;
  137. if (len == 0) {
  138. ns = pkg_malloc(3);
  139. if (!ns) {
  140. LM_ERR("Out of memory!");
  141. return 0;
  142. }
  143. ns[0] = '0';
  144. ns[1] = ':';
  145. ns[2] = ',';
  146. } else {
  147. num_len = (size_t)ceil(log10((double)len + 1));
  148. ns = pkg_malloc(num_len + len + 2);
  149. if (!ns) {
  150. LM_ERR("Out of memory!");
  151. return 0;
  152. }
  153. sprintf(ns, "%lu:", (unsigned long)len);
  154. memcpy(ns + num_len + 1, data, len);
  155. ns[num_len + len + 1] = ',';
  156. }
  157. *netstring = ns;
  158. return num_len + len + 2;
  159. }