選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

85 行
1.7KB

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <assert.h>
  5. static const uint8_t s_base16_dec[256] = {
  6. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  7. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  8. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0 - 9 */
  10. 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A - F */
  11. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12. 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a - f */
  13. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  14. };
  15. size_t hls_base16_decode(void* target, const char* source, size_t bytes)
  16. {
  17. size_t i;
  18. uint8_t* p;
  19. p = (uint8_t*)target;
  20. assert(0 == bytes % 2);
  21. for (i = 0; i < bytes / 2; i++)
  22. {
  23. p[i] = s_base16_dec[(unsigned char)source[i * 2]] << 4;
  24. p[i] |= s_base16_dec[(unsigned char)source[i * 2 + 1]];
  25. }
  26. return i;
  27. }
  28. const char* hls_strtrim(const char* s, size_t* n, const char* prefix, const char* suffix)
  29. {
  30. while (s && *n > 0 && prefix && strchr(prefix, *s))
  31. {
  32. --* n;
  33. ++s;
  34. }
  35. while (s && *n > 0 && suffix && strchr(suffix, s[*n - 1]))
  36. --* n;
  37. return s;
  38. };
  39. size_t hls_strsplit(const char* ptr, const char* end, const char* delimiters, const char* quotes, const char** ppnext)
  40. {
  41. char q;
  42. const char* p;
  43. assert(end && delimiters);
  44. q = 0;
  45. for (p = ptr; p && *p && p < end; p++)
  46. {
  47. if (q)
  48. {
  49. // find QUOTES first
  50. if (q == *p)
  51. {
  52. q = 0;
  53. continue;
  54. }
  55. }
  56. else
  57. {
  58. if (strchr(delimiters, *p))
  59. {
  60. break;
  61. }
  62. else if (quotes && strchr(quotes, *p))
  63. {
  64. q = *p;
  65. }
  66. }
  67. }
  68. if (ppnext)
  69. {
  70. *ppnext = p;
  71. while (*ppnext && *ppnext < end && strchr(delimiters, **ppnext))
  72. ++* ppnext;
  73. }
  74. return p - ptr;
  75. }