Windows global stuff:
[krb5.git] / src / lib / krb5 / krb / unparse.c
index f749bb18dc4adf821ff453d3d16c26ecba78d806..d047a8bb6ef00b8ad64120d12659e36dd3d23ab9 100644 (file)
@@ -1,11 +1,25 @@
 /*
- * $Source$
- * $Author$
+ * lib/krb5/krb/unparse.c
  *
  * Copyright 1990 by the Massachusetts Institute of Technology.
+ * All Rights Reserved.
  *
- * For copying and distribution information, please see the file
- * <krb5/copyright.h>.
+ * Export of this software from the United States of America may
+ *   require a specific license from the United States Government.
+ *   It is the responsibility of any person or organization contemplating
+ *   export to obtain such a license before exporting.
+ * 
+ * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
+ * distribute this software and its documentation for any purpose and
+ * without fee is hereby granted, provided that the above copyright
+ * notice appear in all copies and that both that copyright notice and
+ * this permission notice appear in supporting documentation, and that
+ * the name of M.I.T. not be used in advertising or publicity pertaining
+ * to distribution of the software without specific, written prior
+ * permission.  M.I.T. makes no representations about the suitability of
+ * this software for any purpose.  It is provided "as is" without express
+ * or implied warranty.
+ * 
  *
  * krb5_unparse_name() routine
  *
  * components.
  */
 
-#if !defined(lint) && !defined(SABER)
-static char rcsid_unparse_c[] =
-"$Id$";
-#endif /* !lint & !SABER */
 
 #include <stdio.h>
-#include <krb5/copyright.h>
-#include <krb5/krb5.h>
-#include <krb5/ext-proto.h>
+#include "k5-int.h"
 
 /*
  * converts the multi-part principal format used in the protocols to a
@@ -36,40 +44,58 @@ static char rcsid_unparse_c[] =
  * appear in any the component, they will be representing using
  * backslash encoding.  ("\/", "\@", or '\0', respectively)
  *
- * returns system errors XXX
+ * returns error
+ *     KRB_PARSE_MALFORMED     principal is invalid (does not contain
+ *                             at least 2 components)
+ * also returns system errors
+ *     ENOMEM                  unable to allocate memory for string
  */
 
 #define REALM_SEP      '@'
 #define        COMPONENT_SEP   '/'
 
 krb5_error_code
-krb5_unparse_name_ext(principal, name, size)
-krb5_const_principal principal;
-register char **name;
-int    *size;
+krb5_unparse_name_ext(context, principal, name, size)
+    krb5_context context;
+    krb5_const_principal principal;
+    register char **name;
+    int        *size;
 {
        register char *cp, *q;
        register int i,j;
-       register int totalsize = 0;
        int     length;
+       krb5_int32 nelem;
+       register int totalsize = 0;
+
+       cp = krb5_princ_realm(context, principal)->data;
+       length = krb5_princ_realm(context, principal)->length;
+       totalsize += length;
+       for (j = 0; j < length; j++,cp++)
+               if (*cp == REALM_SEP  || *cp == COMPONENT_SEP ||
+                   *cp == '\0' || *cp == '\\' || *cp == '\t' ||
+                   *cp == '\n' || *cp == '\b')
+                       totalsize++;
+       totalsize++;            /* This is for the separator */
 
-       if (!principal[0] || !principal[1])
-               return KRB5_PARSE_MALFORMED;
-       for (i = 0; principal[i]; i++) {
-               cp = principal[i]->data;
-               length = principal[i]->length;
+       nelem = krb5_princ_size(context, principal);
+       for (i = 0; i < (int) nelem; i++) {
+               cp = krb5_princ_component(context, principal, i)->data;
+               length = krb5_princ_component(context, principal, i)->length;
+               totalsize += length;
                for (j=0; j < length; j++,cp++)
                        if (*cp == REALM_SEP || *cp == COMPONENT_SEP ||
-                           *cp == '\0' || *cp == '\\' || *cp == '\t')
-                               totalsize += 2;
-                       else
+                           *cp == '\0' || *cp == '\\' || *cp == '\t' ||
+                           *cp == '\n' || *cp == '\b')
                                totalsize++;
                totalsize++;    /* This is for the separator */
        }
 
        /*
-        *  we need only n-1 seps for n components, but we need an
-        * extra byte for the NULL at the end
+        * 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
+        * an extra byte for the NULL at the end.
         */
        if (*name) {
                if (*size < (totalsize)) {
@@ -77,7 +103,7 @@ int  *size;
                        *name = realloc(*name, totalsize);
                }
        } else {
-               *name = malloc(totalsize);      /* room for null */
+               *name = malloc(totalsize);
                if (size)
                        *size = totalsize;
        }
@@ -87,25 +113,36 @@ int        *size;
 
        q = *name;
        
-       for (i = 1; principal[i]; i++) {
-               cp = principal[i]->data;
-               length = principal[i]->length;
+       for (i = 0; i < (int) nelem; i++) {
+               cp = krb5_princ_component(context, principal, i)->data;
+               length = krb5_princ_component(context, principal, i)->length;
                for (j=0; j < length; j++,cp++) {
-                       switch (*cp) {
-                       case COMPONENT_SEP:
-                       case REALM_SEP:
-                       case '\t':
-                       case '\\':
-                               *q++ = '\\';
-                               *q++ = *cp;
-                               break;
-                       case '\0':
-                               *q++ = '\\';
-                               *q++ = '0';
-                               break;
-                       default:
-                               *q++ = *cp;
-                       }
+                   switch (*cp) {
+                   case COMPONENT_SEP:
+                   case REALM_SEP:
+                   case '\0':
+                       *q++ = '\\';
+                       *q++ = *cp;
+                       break;
+                   case '\\':
+                       *q++ = '\\';
+                       *q++ = '\\';
+                       break;
+                   case '\t':
+                       *q++ = '\\';
+                       *q++ = 't';
+                       break;
+                   case '\n':
+                       *q++ = '\\';
+                       *q++ = 'n';
+                       break;
+                   case '\b':
+                       *q++ = '\\';
+                       *q++ = 'b';
+                       break;
+                   default:
+                       *q++ = *cp;
+                   }
                }
                *q++ = COMPONENT_SEP;
        }
@@ -113,8 +150,8 @@ int *size;
        q--;                    /* Back up last component separator */
        *q++ = REALM_SEP;
        
-       cp = principal[0]->data;
-       length = principal[0]->length;
+       cp = krb5_princ_realm(context, principal)->data;
+       length = krb5_princ_realm(context, principal)->length;
        for (j=0; j < length; j++,cp++) {
                switch (*cp) {
                case COMPONENT_SEP:
@@ -137,13 +174,14 @@ int       *size;
     return 0;
 }
 
-krb5_error_code
-krb5_unparse_name(principal, name)
-krb5_const_principal principal;
-register char **name;
+krb5_error_code INTERFACE
+krb5_unparse_name(context, principal, name)
+    krb5_context context;
+    krb5_const_principal principal;
+    register char **name;
 {
        *name = NULL;
-       return(krb5_unparse_name_ext(principal, name, NULL));
+       return(krb5_unparse_name_ext(context, principal, name, NULL));
 }