substr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file substr.c
  3. * @author Ambroz Bizjak <[email protected]>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * substr(string str, string start [, string max])
  33. *
  34. * Description:
  35. * Extracts a substring from a string. The result is the longest substring which
  36. * starts at the offset 'start' bytes into 'str', and is no longer than 'max' bytes.
  37. * If 'max' is not provided, the result is the substring from the offset 'start' to
  38. * the end. In any case, 'start' must not be greater than the length of 'str'.
  39. */
  40. #include <stddef.h>
  41. #include <string.h>
  42. #include <limits.h>
  43. #include <ncd/NCDModule.h>
  44. #include <ncd/static_strings.h>
  45. #include <ncd/extra/value_utils.h>
  46. #include <generated/blog_channel_ncd_substr.h>
  47. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  48. struct substr_instance {
  49. NCDModuleInst *i;
  50. NCDRefTarget *ref_target;
  51. const char *data;
  52. size_t length;
  53. };
  54. static void substr_func_new_common (void *vo, NCDModuleInst *i, NCDRefTarget *ref_target, const char *data, size_t length)
  55. {
  56. struct substr_instance *o = vo;
  57. o->i = i;
  58. o->ref_target = ref_target;
  59. o->data = data;
  60. o->length = length;
  61. NCDModuleInst_Backend_Up(i);
  62. }
  63. static int substr_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  64. {
  65. struct substr_instance *o = vo;
  66. if (name == NCD_STRING_EMPTY) {
  67. if (o->ref_target) {
  68. *out = NCDVal_NewExternalString(mem, o->data, o->length, o->ref_target);
  69. } else {
  70. *out = NCDVal_NewStringBin(mem, (const uint8_t *)o->data, o->length);
  71. }
  72. if (NCDVal_IsInvalid(*out)) {
  73. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewExternalString/NCDVal_NewStringBin failed");
  74. }
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static void func_new_substr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  80. {
  81. NCDValRef str_arg;
  82. NCDValRef start_arg;
  83. NCDValRef max_arg = NCDVal_NewInvalid();
  84. if (!NCDVal_ListRead(params->args, 2, &str_arg, &start_arg) &&
  85. !NCDVal_ListRead(params->args, 3, &str_arg, &start_arg, &max_arg)
  86. ) {
  87. ModuleLog(i, BLOG_ERROR, "wrong arity");
  88. goto fail0;
  89. }
  90. if (!NCDVal_IsString(str_arg) || !NCDVal_IsString(start_arg) ||
  91. (!NCDVal_IsInvalid(max_arg) && !NCDVal_IsString(max_arg))
  92. ) {
  93. ModuleLog(i, BLOG_ERROR, "wrong type");
  94. goto fail0;
  95. }
  96. uintmax_t start;
  97. if (!ncd_read_uintmax(start_arg, &start) || start > SIZE_MAX) {
  98. ModuleLog(i, BLOG_ERROR, "wrong size");
  99. goto fail0;
  100. }
  101. uintmax_t max = SIZE_MAX;
  102. if (!NCDVal_IsInvalid(max_arg)) {
  103. if (!ncd_read_uintmax(max_arg, &max) || max > SIZE_MAX) {
  104. ModuleLog(i, BLOG_ERROR, "wrong max");
  105. goto fail0;
  106. }
  107. }
  108. const char *str_data = NCDVal_StringData(str_arg);
  109. size_t str_length = NCDVal_StringLength(str_arg);
  110. if (start > str_length) {
  111. ModuleLog(i, BLOG_ERROR, "start is beyond the end of the string");
  112. goto fail0;
  113. }
  114. const char *sub_data = str_data + start;
  115. size_t sub_length = str_length - start;
  116. if (sub_length > max) {
  117. sub_length = max;
  118. }
  119. NCDRefTarget *ref_target = (!NCDVal_IsExternalString(str_arg) ? NULL : NCDVal_ExternalStringTarget(str_arg));
  120. substr_func_new_common(vo, i, ref_target, sub_data, sub_length);
  121. return;
  122. fail0:
  123. NCDModuleInst_Backend_SetError(i);
  124. NCDModuleInst_Backend_Dead(i);
  125. }
  126. static struct NCDModule modules[] = {
  127. {
  128. .type = "substr",
  129. .func_new2 = func_new_substr,
  130. .func_getvar2 = substr_func_getvar,
  131. .alloc_size = sizeof(struct substr_instance)
  132. }, {
  133. .type = NULL
  134. }
  135. };
  136. const struct NCDModuleGroup ncdmodule_substr = {
  137. .modules = modules
  138. };