Fix modes.h so that indent doesn't complain
[openssl.git] / crypto / ui / ui_lib.c
index c526426f8b2b73eac5aa88a7990d8d141a4ebd3c..167da002d24c7197b85e0bef250cf84b98ec973a 100644 (file)
@@ -57,6 +57,7 @@
  */
 
 #include <string.h>
+#include "cryptlib.h"
 #include <openssl/e_os2.h>
 #include <openssl/buffer.h>
 #include <openssl/ui.h>
@@ -66,8 +67,6 @@
 IMPLEMENT_STACK_OF(UI_STRING_ST)
 
 static const UI_METHOD *default_UI_meth=NULL;
-static int ui_meth_num=0;
-static STACK_OF(CRYPTO_EX_DATA_FUNCS) *ui_meth=NULL;
 
 UI *UI_new(void)
        {
@@ -91,7 +90,8 @@ UI *UI_new_method(const UI_METHOD *method)
 
        ret->strings=NULL;
        ret->user_data=NULL;
-       CRYPTO_new_ex_data(ui_meth,ret,&ret->ex_data);
+       ret->flags=0;
+       CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
        return ret;
        }
 
@@ -119,6 +119,7 @@ void UI_free(UI *ui)
        if (ui == NULL)
                return;
        sk_UI_STRING_pop_free(ui->strings,free_string);
+       CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
        OPENSSL_free(ui);
        }
 
@@ -145,7 +146,8 @@ static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
                {
                UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,ERR_R_PASSED_NULL_PARAMETER);
                }
-       else if (result_buf == NULL)
+       else if ((type == UIT_PROMPT || type == UIT_VERIFY
+                        || type == UIT_BOOLEAN) && result_buf == NULL)
                {
                UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,UI_R_NO_RESULT_BUFFER);
                }
@@ -236,7 +238,7 @@ static int general_allocate_boolean(UI *ui,
        return ret;
        }
 
-/* Returns the index to the place in the stack or 0 for error.  Uses a
+/* Returns the index to the place in the stack or -1 for error.  Uses a
    direct reference to the prompt.  */
 int UI_add_input_string(UI *ui, const char *prompt, int flags,
        char *result_buf, int minsize, int maxsize)
@@ -429,14 +431,14 @@ char *UI_construct_prompt(UI *ui, const char *object_desc,
                len += sizeof(prompt3) - 1;
 
                prompt = (char *)OPENSSL_malloc(len + 1);
-               strcpy(prompt, prompt1);
-               strcat(prompt, object_desc);
+               BUF_strlcpy(prompt, prompt1, len + 1);
+               BUF_strlcat(prompt, object_desc, len + 1);
                if (object_name)
                        {
-                       strcat(prompt, prompt2);
-                       strcat(prompt, object_name);
+                       BUF_strlcat(prompt, prompt2, len + 1);
+                       BUF_strlcat(prompt, object_name, len + 1);
                        }
-               strcat(prompt, prompt3);
+               BUF_strlcat(prompt, prompt3, len + 1);
                }
        return prompt;
        }
@@ -544,7 +546,7 @@ int UI_process(UI *ui)
        return ok;
        }
 
-int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)())
+int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void))
        {
        if (ui == NULL)
                {
@@ -574,10 +576,8 @@ int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)())
 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
             CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
         {
-       if(CRYPTO_get_ex_new_index(ui_meth_num, &ui_meth, argl, argp,
-                               new_func, dup_func, free_func) < 0)
-               return -1;
-       return (ui_meth_num++);
+       return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
+                               new_func, dup_func, free_func);
         }
 
 int UI_set_ex_data(UI *r, int idx, void *arg)
@@ -621,8 +621,10 @@ UI_METHOD *UI_create_method(char *name)
        UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
 
        if (ui_method)
+               {
                memset(ui_method, 0, sizeof(*ui_method));
-       ui_method->name = BUF_strdup(name);
+               ui_method->name = BUF_strdup(name);
+               }
        return ui_method;
        }
 
@@ -691,6 +693,17 @@ int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
                return -1;
        }
 
+int UI_method_set_prompt_constructor(UI_METHOD *method, char *(*prompt_constructor)(UI* ui, const char* object_desc, const char* object_name))
+       {
+       if (method)
+               {
+               method->ui_construct_prompt = prompt_constructor;
+               return 0;
+               }
+       else
+               return -1;
+       }
+
 int (*UI_method_get_opener(UI_METHOD *method))(UI*)
        {
        if (method)
@@ -731,6 +744,14 @@ int (*UI_method_get_closer(UI_METHOD *method))(UI*)
                return NULL;
        }
 
+char* (*UI_method_get_prompt_constructor(UI_METHOD *method))(UI*, const char*, const char*)
+       {
+       if (method)
+               return method->ui_construct_prompt;
+       else
+               return NULL;
+       }
+
 enum UI_string_types UI_get_string_type(UI_STRING *uis)
        {
        if (!uis)
@@ -834,8 +855,8 @@ int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
        case UIT_PROMPT:
        case UIT_VERIFY:
                {
-               char number1[20];
-               char number2[20];
+               char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize)+1];
+               char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize)+1];
 
                BIO_snprintf(number1, sizeof(number1), "%d",
                        uis->_.string_data.result_minsize);
@@ -866,7 +887,8 @@ int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
                        return -1;
                        }
 
-               strcpy(uis->result_buf, result);
+               BUF_strlcpy(uis->result_buf, result,
+                           uis->_.string_data.result_maxsize + 1);
                break;
        case UIT_BOOLEAN:
                {
@@ -894,9 +916,9 @@ int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
                                break;
                                }
                        }
+               }
        default:
                break;
                }
-               }
        return 0;
        }