Path: blob/main/crypto/heimdal/appl/gssmask/common.c
34879 views
/*1* Copyright (c) 2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of KTH nor the names of its contributors may be17* used to endorse or promote products derived from this software without18* specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR27* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,28* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR29* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF30* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233#include <common.h>34RCSID("$Id$");3536krb5_error_code37store_string(krb5_storage *sp, const char *str)38{39size_t len = strlen(str) + 1;40krb5_error_code ret;4142ret = krb5_store_int32(sp, len);43if (ret)44return ret;45ret = krb5_storage_write(sp, str, len);46if (ret != len)47return EINVAL;48return 0;49}5051static void52add_list(char ****list, size_t *listlen, char **str, size_t len)53{54size_t i;55*list = erealloc(*list, sizeof(**list) * (*listlen + 1));5657(*list)[*listlen] = ecalloc(len, sizeof(**list));58for (i = 0; i < len; i++)59(*list)[*listlen][i] = str[i];60(*listlen)++;61}6263static void64permute(char ****list, size_t *listlen,65char **str, const int start, const int len)66{67int i, j;6869#define SWAP(s,i,j) { char *t = str[i]; str[i] = str[j]; str[j] = t; }7071for (i = start; i < len - 1; i++) {72for (j = i+1; j < len; j++) {73SWAP(str,i,j);74permute(list, listlen, str, i+1, len);75SWAP(str,i,j);76}77}78add_list(list, listlen, str, len);79}8081char ***82permutate_all(struct getarg_strings *strings, size_t *size)83{84char **list, ***all = NULL;85int i;8687*size = 0;8889list = ecalloc(strings->num_strings, sizeof(*list));90for (i = 0; i < strings->num_strings; i++)91list[i] = strings->strings[i];9293permute(&all, size, list, 0, strings->num_strings);94free(list);95return all;96}979899