Imported Upstream version 1.20.1
[platform/upstream/krb5.git] / src / lib / krb5 / krb / unparse.c
index d774b7c..76b1894 100644 (file)
@@ -90,7 +90,8 @@ copy_component_quoting(char *dest, const krb5_data *src, int flags)
     int length = src->length;
 
     if (flags & KRB5_PRINCIPAL_UNPARSE_DISPLAY) {
-        memcpy(dest, src->data, src->length);
+        if (src->length > 0)
+            memcpy(dest, src->data, src->length);
         return src->length;
     }
 
@@ -121,13 +122,6 @@ copy_component_quoting(char *dest, const krb5_data *src, int flags)
             *q++ = '\\';
             *q++ = 'b';
             break;
-#if 0
-            /* Heimdal escapes spaces in principal names upon unparsing */
-        case ' ':
-            *q++ = '\\';
-            *q++ = ' ';
-            break;
-#endif
         case '\0':
             *q++ = '\\';
             *q++ = '0';
@@ -144,8 +138,7 @@ k5_unparse_name(krb5_context context, krb5_const_principal principal,
                 int flags, char **name, unsigned int *size)
 {
     char *q;
-    int i;
-    krb5_int32 nelem;
+    krb5_int32 i;
     unsigned int totalsize = 0;
     char *default_realm = NULL;
     krb5_error_code ret = 0;
@@ -161,33 +154,29 @@ k5_unparse_name(krb5_context context, krb5_const_principal principal,
         if (ret != 0)
             goto cleanup;
 
-        krb5_princ_realm(context, &p)->length = strlen(default_realm);
-        krb5_princ_realm(context, &p)->data = default_realm;
+        p.realm = string2data(default_realm);
 
         if (krb5_realm_compare(context, &p, principal))
             flags |= KRB5_PRINCIPAL_UNPARSE_NO_REALM;
     }
 
     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
-        totalsize += component_length_quoted(krb5_princ_realm(context,
-                                                              principal),
-                                             flags);
+        totalsize += component_length_quoted(&principal->realm, flags);
         totalsize++;            /* This is for the separator */
     }
 
-    nelem = krb5_princ_size(context, principal);
-    for (i = 0; i < (int) nelem; i++) {
-        totalsize += component_length_quoted(krb5_princ_component(context, principal, i), flags);
+    for (i = 0; i < principal->length; i++) {
+        totalsize += component_length_quoted(&principal->data[i], flags);
         totalsize++;    /* This is for the separator */
     }
-    if (nelem == 0)
+    if (principal->length == 0)
         totalsize++;
 
     /*
      * Allocate space for the ascii string; if space has been
      * provided, use it, realloc'ing it if necessary.
      *
-     * We need only n-1 seperators for n components, but we need
+     * We need only n-1 separators for n components, but we need
      * an extra byte for the NUL at the end.
      */
     if (size) {
@@ -208,12 +197,8 @@ k5_unparse_name(krb5_context context, krb5_const_principal principal,
 
     q = *name;
 
-    for (i = 0; i < (int) nelem; i++) {
-        q += copy_component_quoting(q,
-                                    krb5_princ_component(context,
-                                                         principal,
-                                                         i),
-                                    flags);
+    for (i = 0; i < principal->length; i++) {
+        q += copy_component_quoting(q, &principal->data[i], flags);
         *q++ = COMPONENT_SEP;
     }
 
@@ -221,7 +206,7 @@ k5_unparse_name(krb5_context context, krb5_const_principal principal,
         q--;                /* Back up last component separator */
     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
         *q++ = REALM_SEP;
-        q += copy_component_quoting(q, krb5_princ_realm(context, principal), flags);
+        q += copy_component_quoting(q, &principal->realm, flags);
     }
     *q++ = '\0';
 
@@ -233,7 +218,8 @@ cleanup:
 }
 
 krb5_error_code KRB5_CALLCONV
-krb5_unparse_name(krb5_context context, krb5_const_principal principal, register char **name)
+krb5_unparse_name(krb5_context context, krb5_const_principal principal,
+                  char **name)
 {
     if (name != NULL)                      /* name == NULL will return error from _ext */
         *name = NULL;