]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/testipp.c
Support <hex> strings in IPP files.
[thirdparty/cups.git] / cups / testipp.c
index 150abe0680838529319471b8ecd4450a3469fb77..3514b94d5d984b6b5d990761eb311ca918a6525e 100644 (file)
@@ -1,16 +1,10 @@
 /*
  * IPP test program for CUPS.
  *
- * Copyright 2007-2014 by Apple Inc.
- * Copyright 1997-2005 by Easy Software Products.
+ * Copyright © 2007-2018 by Apple Inc.
+ * Copyright © 1997-2005 by Easy Software Products.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * This file is subject to the Apple OS-Developed Software exception.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
 #include "file.h"
 #include "string-private.h"
 #include "ipp-private.h"
-#ifdef WIN32
+#ifdef _WIN32
 #  include <io.h>
 #else
 #  include <unistd.h>
 #  include <fcntl.h>
-#endif /* WIN32 */
+#endif /* _WIN32 */
 
 
 /*
@@ -229,6 +223,7 @@ static ipp_uchar_t mixed[] =                /* Mixed value buffer */
 void   hex_dump(const char *title, ipp_uchar_t *buffer, size_t bytes);
 void   print_attributes(ipp_t *ipp, int indent);
 ssize_t        read_cb(_ippdata_t *data, ipp_uchar_t *buffer, size_t bytes);
+int    token_cb(_ipp_file_t *f, _ipp_vars_t *v, void *user_data, const char *token);
 ssize_t        write_cb(_ippdata_t *data, ipp_uchar_t *buffer, size_t bytes);
 
 
@@ -712,30 +707,53 @@ main(int  argc,                   /* I - Number of command-line arguments */
 
     for (i = 1; i < (size_t)argc; i ++)
     {
-      if ((fp = cupsFileOpen(argv[i], "r")) == NULL)
+      if (strlen(argv[i]) > 5 && !strcmp(argv[i] + strlen(argv[i]) - 5, ".test"))
       {
-       printf("Unable to open \"%s\" - %s\n", argv[i], strerror(errno));
-       status = 1;
-       continue;
-      }
+       /*
+        * Read an ASCII IPP message...
+        */
 
-      request = ippNew();
-      while ((state = ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL,
-                                request)) == IPP_STATE_ATTRIBUTE);
+        _ipp_vars_t v;                 /* IPP variables */
 
-      if (state != IPP_STATE_DATA)
-      {
-       printf("Error reading IPP message from \"%s\"!\n", argv[i]);
-       status = 1;
+        _ippVarsInit(&v, NULL, NULL, token_cb);
+        request = _ippFileParse(&v, argv[i], NULL);
+        _ippVarsDeinit(&v);
       }
       else
+      {
+       /*
+        * Read a raw (binary) IPP message...
+        */
+
+       if ((fp = cupsFileOpen(argv[i], "r")) == NULL)
+       {
+         printf("Unable to open \"%s\" - %s\n", argv[i], strerror(errno));
+         status = 1;
+         continue;
+       }
+
+       request = ippNew();
+       while ((state = ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL,
+                                 request)) == IPP_STATE_ATTRIBUTE);
+
+       if (state != IPP_STATE_DATA)
+       {
+         printf("Error reading IPP message from \"%s\": %s\n", argv[i], cupsLastErrorString());
+         status = 1;
+
+         ippDelete(request);
+         request = NULL;
+       }
+
+        cupsFileClose(fp);
+      }
+
+      if (request)
       {
        printf("\n%s:\n", argv[i]);
        print_attributes(request, 4);
+       ippDelete(request);
       }
-
-      ippDelete(request);
-      cupsFileClose(fp);
     }
   }
 
@@ -810,88 +828,9 @@ void
 print_attributes(ipp_t *ipp,           /* I - IPP request */
                  int   indent)         /* I - Indentation */
 {
-  int                  i;              /* Looping var */
   ipp_tag_t            group;          /* Current group */
   ipp_attribute_t      *attr;          /* Current attribute */
-  _ipp_value_t         *val;           /* Current value */
-  static const char * const tags[] =   /* Value/group tag strings */
-                       {
-                         "reserved-00",
-                         "operation-attributes-tag",
-                         "job-attributes-tag",
-                         "end-of-attributes-tag",
-                         "printer-attributes-tag",
-                         "unsupported-attributes-tag",
-                         "subscription-attributes-tag",
-                         "event-attributes-tag",
-                         "reserved-08",
-                         "reserved-09",
-                         "reserved-0A",
-                         "reserved-0B",
-                         "reserved-0C",
-                         "reserved-0D",
-                         "reserved-0E",
-                         "reserved-0F",
-                         "unsupported",
-                         "default",
-                         "unknown",
-                         "no-value",
-                         "reserved-14",
-                         "not-settable",
-                         "delete-attr",
-                         "admin-define",
-                         "reserved-18",
-                         "reserved-19",
-                         "reserved-1A",
-                         "reserved-1B",
-                         "reserved-1C",
-                         "reserved-1D",
-                         "reserved-1E",
-                         "reserved-1F",
-                         "reserved-20",
-                         "integer",
-                         "boolean",
-                         "enum",
-                         "reserved-24",
-                         "reserved-25",
-                         "reserved-26",
-                         "reserved-27",
-                         "reserved-28",
-                         "reserved-29",
-                         "reserved-2a",
-                         "reserved-2b",
-                         "reserved-2c",
-                         "reserved-2d",
-                         "reserved-2e",
-                         "reserved-2f",
-                         "octetString",
-                         "dateTime",
-                         "resolution",
-                         "rangeOfInteger",
-                         "begCollection",
-                         "textWithLanguage",
-                         "nameWithLanguage",
-                         "endCollection",
-                         "reserved-38",
-                         "reserved-39",
-                         "reserved-3a",
-                         "reserved-3b",
-                         "reserved-3c",
-                         "reserved-3d",
-                         "reserved-3e",
-                         "reserved-3f",
-                         "reserved-40",
-                         "textWithoutLanguage",
-                         "nameWithoutLanguage",
-                         "reserved-43",
-                         "keyword",
-                         "uri",
-                         "uriScheme",
-                         "charset",
-                         "naturalLanguage",
-                         "mimeMediaType",
-                         "memberName"
-                       };
+  char                  buffer[2048];   /* Value string */
 
 
   for (group = IPP_TAG_ZERO, attr = ipp->attrs; attr; attr = attr->next)
@@ -907,83 +846,12 @@ print_attributes(ipp_t *ipp,              /* I - IPP request */
     {
       group = attr->group_tag;
 
-      printf("\n%*s%s:\n\n", indent - 4, "", tags[group]);
+      printf("\n%*s%s:\n\n", indent - 4, "", ippTagString(group));
     }
 
-    printf("%*s%s (", indent, "", attr->name ? attr->name : "(null)");
-    if (attr->num_values > 1)
-      printf("1setOf ");
-    printf("%s):", tags[attr->value_tag]);
+    ippAttributeString(attr, buffer, sizeof(buffer));
 
-    switch (attr->value_tag)
-    {
-      case IPP_TAG_ENUM :
-      case IPP_TAG_INTEGER :
-          for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
-           printf(" %d", val->integer);
-          putchar('\n');
-          break;
-
-      case IPP_TAG_BOOLEAN :
-          for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
-           printf(" %s", val->boolean ? "true" : "false");
-          putchar('\n');
-          break;
-
-      case IPP_TAG_RANGE :
-          for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
-           printf(" %d-%d", val->range.lower, val->range.upper);
-          putchar('\n');
-          break;
-
-      case IPP_TAG_DATE :
-          {
-           char        vstring[256];   /* Formatted time */
-
-           for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
-             printf(" (%s)", _cupsStrDate(vstring, sizeof(vstring), ippDateToTime(val->date)));
-          }
-          putchar('\n');
-          break;
-
-      case IPP_TAG_RESOLUTION :
-          for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
-           printf(" %dx%d%s", val->resolution.xres, val->resolution.yres,
-                  val->resolution.units == IPP_RES_PER_INCH ? "dpi" : "dpcm");
-          putchar('\n');
-          break;
-
-      case IPP_TAG_STRING :
-      case IPP_TAG_TEXTLANG :
-      case IPP_TAG_NAMELANG :
-      case IPP_TAG_TEXT :
-      case IPP_TAG_NAME :
-      case IPP_TAG_KEYWORD :
-      case IPP_TAG_URI :
-      case IPP_TAG_URISCHEME :
-      case IPP_TAG_CHARSET :
-      case IPP_TAG_LANGUAGE :
-      case IPP_TAG_MIMETYPE :
-          for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
-           printf(" \"%s\"", val->string.text);
-          putchar('\n');
-          break;
-
-      case IPP_TAG_BEGIN_COLLECTION :
-          putchar('\n');
-
-          for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
-         {
-           if (i)
-             putchar('\n');
-           print_attributes(val->collection, indent + 4);
-         }
-          break;
-
-      default :
-          printf("UNKNOWN (%d values)\n", attr->num_values);
-          break;
-    }
+    printf("%*s%s (%s%s): %s\n", indent, "", attr->name ? attr->name : "(null)", attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag), buffer);
   }
 }
 
@@ -1018,6 +886,34 @@ read_cb(_ippdata_t   *data,               /* I - Data */
 }
 
 
+/*
+ * 'token_cb()' - Token callback for ASCII IPP data file parser.
+ */
+
+int                                    /* O - 1 on success, 0 on failure */
+token_cb(_ipp_file_t *f,               /* I - IPP file data */
+         _ipp_vars_t *v,               /* I - IPP variables */
+         void        *user_data,       /* I - User data pointer */
+         const char  *token)           /* I - Token string */
+{
+  (void)v;
+  (void)user_data;
+
+  if (!token)
+  {
+    f->attrs     = ippNew();
+    f->group_tag = IPP_TAG_PRINTER;
+  }
+  else
+  {
+    fprintf(stderr, "Unknown directive \"%s\" on line %d of \"%s\".\n", token, f->linenum, f->filename);
+    return (0);
+  }
+
+  return (1);
+}
+
+
 /*
  * 'write_cb()' - Write data into a buffer.
  */