11cb0ef41Sopenharmony_ci/* MIT License
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright (c) 2018 John Schember
41cb0ef41Sopenharmony_ci *
51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights
81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
111cb0ef41Sopenharmony_ci *
121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next
131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
141cb0ef41Sopenharmony_ci * Software.
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
221cb0ef41Sopenharmony_ci * SOFTWARE.
231cb0ef41Sopenharmony_ci *
241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT
251cb0ef41Sopenharmony_ci */
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#if defined(__MVS__)
281cb0ef41Sopenharmony_ci#  include <strings.h>
291cb0ef41Sopenharmony_ci#endif
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci#include "ares_setup.h"
321cb0ef41Sopenharmony_ci#include "ares.h"
331cb0ef41Sopenharmony_ci#include "ares_private.h"
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_civoid ares__strsplit_free(char **elms, size_t num_elm)
361cb0ef41Sopenharmony_ci{
371cb0ef41Sopenharmony_ci  size_t i;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  if (elms == NULL) {
401cb0ef41Sopenharmony_ci    return;
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  for (i = 0; i < num_elm; i++) {
441cb0ef41Sopenharmony_ci    ares_free(elms[i]);
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci  ares_free(elms);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cichar **ares__strsplit_duplicate(char **elms, size_t num_elm)
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  size_t i;
521cb0ef41Sopenharmony_ci  char **out;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  if (elms == NULL || num_elm == 0) {
551cb0ef41Sopenharmony_ci    return NULL;
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  out = ares_malloc_zero(sizeof(*elms) * num_elm);
591cb0ef41Sopenharmony_ci  if (out == NULL) {
601cb0ef41Sopenharmony_ci    return NULL;
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  for (i = 0; i < num_elm; i++) {
641cb0ef41Sopenharmony_ci    out[i] = ares_strdup(elms[i]);
651cb0ef41Sopenharmony_ci    if (out[i] == NULL) {
661cb0ef41Sopenharmony_ci      ares__strsplit_free(out, num_elm);
671cb0ef41Sopenharmony_ci      return NULL;
681cb0ef41Sopenharmony_ci    }
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  return out;
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cichar **ares__strsplit(const char *in, const char *delms, size_t *num_elm)
751cb0ef41Sopenharmony_ci{
761cb0ef41Sopenharmony_ci  ares_status_t       status;
771cb0ef41Sopenharmony_ci  ares__buf_t        *buf   = NULL;
781cb0ef41Sopenharmony_ci  ares__llist_t      *llist = NULL;
791cb0ef41Sopenharmony_ci  ares__llist_node_t *node;
801cb0ef41Sopenharmony_ci  char              **out = NULL;
811cb0ef41Sopenharmony_ci  size_t              cnt = 0;
821cb0ef41Sopenharmony_ci  size_t              idx = 0;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  if (in == NULL || delms == NULL || num_elm == NULL) {
851cb0ef41Sopenharmony_ci    return NULL;
861cb0ef41Sopenharmony_ci  }
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  *num_elm = 0;
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  buf = ares__buf_create_const((const unsigned char *)in, ares_strlen(in));
911cb0ef41Sopenharmony_ci  if (buf == NULL) {
921cb0ef41Sopenharmony_ci    return NULL;
931cb0ef41Sopenharmony_ci  }
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  status = ares__buf_split(
961cb0ef41Sopenharmony_ci    buf, (const unsigned char *)delms, ares_strlen(delms),
971cb0ef41Sopenharmony_ci    ARES_BUF_SPLIT_NO_DUPLICATES | ARES_BUF_SPLIT_CASE_INSENSITIVE, &llist);
981cb0ef41Sopenharmony_ci  if (status != ARES_SUCCESS) {
991cb0ef41Sopenharmony_ci    goto done;
1001cb0ef41Sopenharmony_ci  }
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  cnt = ares__llist_len(llist);
1031cb0ef41Sopenharmony_ci  if (cnt == 0) {
1041cb0ef41Sopenharmony_ci    status = ARES_EFORMERR;
1051cb0ef41Sopenharmony_ci    goto done;
1061cb0ef41Sopenharmony_ci  }
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  out = ares_malloc_zero(cnt * sizeof(*out));
1101cb0ef41Sopenharmony_ci  if (out == NULL) {
1111cb0ef41Sopenharmony_ci    status = ARES_ENOMEM;
1121cb0ef41Sopenharmony_ci    goto done;
1131cb0ef41Sopenharmony_ci  }
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  for (node = ares__llist_node_first(llist); node != NULL;
1161cb0ef41Sopenharmony_ci       node = ares__llist_node_next(node)) {
1171cb0ef41Sopenharmony_ci    ares__buf_t *val  = ares__llist_node_val(node);
1181cb0ef41Sopenharmony_ci    char        *temp = NULL;
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci    status = ares__buf_fetch_str_dup(val, ares__buf_len(val), &temp);
1211cb0ef41Sopenharmony_ci    if (status != ARES_SUCCESS) {
1221cb0ef41Sopenharmony_ci      goto done;
1231cb0ef41Sopenharmony_ci    }
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci    out[idx++] = temp;
1261cb0ef41Sopenharmony_ci  }
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  *num_elm = cnt;
1291cb0ef41Sopenharmony_ci  status   = ARES_SUCCESS;
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_cidone:
1321cb0ef41Sopenharmony_ci  ares__llist_destroy(llist);
1331cb0ef41Sopenharmony_ci  ares__buf_destroy(buf);
1341cb0ef41Sopenharmony_ci  if (status != ARES_SUCCESS) {
1351cb0ef41Sopenharmony_ci    ares__strsplit_free(out, cnt);
1361cb0ef41Sopenharmony_ci    out = NULL;
1371cb0ef41Sopenharmony_ci  }
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  return out;
1401cb0ef41Sopenharmony_ci}
141