Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.4KB

  1. /*
  2. Copyright 2011-2012 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "serd_internal.h"
  15. #include <math.h>
  16. SERD_API
  17. const uint8_t*
  18. serd_strerror(SerdStatus st)
  19. {
  20. switch (st) {
  21. case SERD_SUCCESS: return (const uint8_t*)"Success";
  22. case SERD_FAILURE: return (const uint8_t*)"Non-fatal failure";
  23. case SERD_ERR_UNKNOWN: return (const uint8_t*)"Unknown error";
  24. case SERD_ERR_BAD_SYNTAX: return (const uint8_t*)"Invalid syntax";
  25. case SERD_ERR_BAD_ARG: return (const uint8_t*)"Invalid argument";
  26. case SERD_ERR_NOT_FOUND: return (const uint8_t*)"Not found";
  27. case SERD_ERR_ID_CLASH: return (const uint8_t*)"Blank node ID clash";
  28. case SERD_ERR_BAD_CURIE: return (const uint8_t*)"Invalid CURIE";
  29. case SERD_ERR_INTERNAL: return (const uint8_t*)"Internal error";
  30. }
  31. return (const uint8_t*)"Unknown error"; // never reached
  32. }
  33. SERD_API
  34. size_t
  35. serd_strlen(const uint8_t* str, size_t* n_bytes, SerdNodeFlags* flags)
  36. {
  37. size_t n_chars = 0;
  38. size_t i = 0;
  39. *flags = 0;
  40. for (; str[i]; ++i) {
  41. if ((str[i] & 0xC0) != 0x80) {
  42. // Does not start with `10', start of a new character
  43. ++n_chars;
  44. switch (str[i]) {
  45. case '\r': case '\n':
  46. *flags |= SERD_HAS_NEWLINE;
  47. break;
  48. case '"':
  49. *flags |= SERD_HAS_QUOTE;
  50. }
  51. }
  52. }
  53. if (n_bytes) {
  54. *n_bytes = i;
  55. }
  56. return n_chars;
  57. }
  58. static inline double
  59. read_sign(const char** sptr)
  60. {
  61. double sign = 1.0;
  62. switch (**sptr) {
  63. case '-': sign = -1.0;
  64. case '+': ++(*sptr);
  65. default: return sign;
  66. }
  67. }
  68. SERD_API
  69. double
  70. serd_strtod(const char* str, char** endptr)
  71. {
  72. double result = 0.0;
  73. // Point s at the first non-whitespace character
  74. const char* s = str;
  75. while (is_space(*s)) { ++s; }
  76. // Read leading sign if necessary
  77. const double sign = read_sign(&s);
  78. // Parse integer part
  79. for (; is_digit(*s); ++s) {
  80. result = (result * 10.0) + (*s - '0');
  81. }
  82. // Parse fractional part
  83. if (*s == '.') {
  84. double denom = 10.0;
  85. for (++s; is_digit(*s); ++s) {
  86. result += (*s - '0') / denom;
  87. denom *= 10.0;
  88. }
  89. }
  90. // Parse exponent
  91. if (*s == 'e' || *s == 'E') {
  92. ++s;
  93. double expt = 0.0;
  94. double expt_sign = read_sign(&s);
  95. for (; is_digit(*s); ++s) {
  96. expt = (expt * 10.0) + (*s - '0');
  97. }
  98. result *= pow(10, expt * expt_sign);
  99. }
  100. if (endptr) {
  101. *endptr = (char*)s;
  102. }
  103. return result * sign;
  104. }
  105. /**
  106. Base64 decoding table.
  107. This is indexed by encoded characters and returns the numeric value used
  108. for decoding, shifted up by 47 to be in the range of printable ASCII.
  109. A '$' is a placeholder for characters not in the base64 alphabet.
  110. */
  111. static const char b64_unmap[] =
  112. "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$m$$$ncdefghijkl$$$$$$"
  113. "$/0123456789:;<=>?@ABCDEFGH$$$$$$IJKLMNOPQRSTUVWXYZ[\\]^_`ab$$$$"
  114. "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
  115. "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
  116. static inline uint8_t unmap(const uint8_t in) { return b64_unmap[in] - 47; }
  117. /**
  118. Decode 4 base64 characters to 3 raw bytes.
  119. */
  120. static inline size_t
  121. decode_chunk(const uint8_t in[4], uint8_t out[3])
  122. {
  123. out[0] = (uint8_t)(((unmap(in[0]) << 2)) | unmap(in[1]) >> 4);
  124. out[1] = (uint8_t)(((unmap(in[1]) << 4) & 0xF0) | unmap(in[2]) >> 2);
  125. out[2] = (uint8_t)(((unmap(in[2]) << 6) & 0xC0) | unmap(in[3]));
  126. return 1 + (in[2] != '=') + ((in[2] != '=') && (in[3] != '='));
  127. }
  128. SERD_API
  129. void*
  130. serd_base64_decode(const uint8_t* str, size_t len, size_t* size)
  131. {
  132. void* buf = malloc((len * 3) / 4 + 2);
  133. *size = 0;
  134. for (size_t i = 0, j = 0; i < len; j += 3) {
  135. uint8_t in[] = "====";
  136. size_t n_in = 0;
  137. for (; i < len && n_in < 4; ++n_in) {
  138. for (; i < len && !is_base64(str[i]); ++i) {} // Skip junk
  139. in[n_in] = str[i++];
  140. }
  141. if (n_in > 1) {
  142. *size += decode_chunk(in, (uint8_t*)buf + j);
  143. }
  144. }
  145. return buf;
  146. }