From 52519bfb65c1f1088da251772e79a54a3baab230 Mon Sep 17 00:00:00 2001 From: Glade Diviney Date: Fri, 14 Oct 2016 09:01:09 -0700 Subject: [PATCH] adding CUPS code --- Android.mk | 82 + CHANGES-1.0.txt | 217 ++ CHANGES-1.1.txt | 3462 +++++++++++++++++++ CHANGES-1.2.txt | 1261 +++++++ CHANGES-1.3.txt | 856 +++++ CHANGES-1.4.txt | 843 +++++ CHANGES-1.5.txt | 312 ++ CHANGES.txt | 225 ++ CREDITS.txt | 51 + INSTALL.txt | 213 ++ LICENSE.txt | 971 ++++++ Makedefs.in | 262 ++ Makefile | 336 ++ README.txt | 163 + config.h | 750 +++++ config.h.in | 747 +++++ configure.in | 97 + cups-config.in | 146 + cups/Dependencies | 260 ++ cups/Makefile | 633 ++++ cups/adminutil.c | 2341 +++++++++++++ cups/adminutil.h | 81 + cups/api-array.header | 34 + cups/api-array.shtml | 196 ++ cups/api-cups.header | 40 + cups/api-cups.shtml | 443 +++ cups/api-filedir.header | 36 + cups/api-filedir.shtml | 31 + cups/api-filter.header | 41 + cups/api-filter.shtml | 765 +++++ cups/api-httpipp.header | 37 + cups/api-httpipp.shtml | 317 ++ cups/api-overview.header | 53 + cups/api-overview.shtml | 94 + cups/api-ppd.header | 38 + cups/api-ppd.shtml | 219 ++ cups/array-private.h | 51 + cups/array.c | 1326 ++++++++ cups/array.h | 92 + cups/attr.c | 335 ++ cups/auth.c | 885 +++++ cups/backchannel.c | 199 ++ cups/backend.c | 154 + cups/backend.h | 78 + cups/conflicts.c | 1214 +++++++ cups/cups-private.h | 272 ++ cups/cups.h | 601 ++++ cups/custom.c | 122 + cups/debug-private.h | 117 + cups/debug.c | 658 ++++ cups/dest-job.c | 358 ++ cups/dest-localization.c | 386 +++ cups/dest-options.c | 1763 ++++++++++ cups/dest.c | 3891 +++++++++++++++++++++ cups/dir.c | 472 +++ cups/dir.h | 69 + cups/emit.c | 1229 +++++++ cups/encode.c | 664 ++++ cups/file-private.h | 137 + cups/file.c | 2718 +++++++++++++++ cups/file.h | 116 + cups/getdevices.c | 283 ++ cups/getifaddrs.c | 266 ++ cups/getputfile.c | 502 +++ cups/globals.c | 384 +++ cups/http-addr.c | 703 ++++ cups/http-addrlist.c | 845 +++++ cups/http-private.h | 394 +++ cups/http-support.c | 2364 +++++++++++++ cups/http.c | 4778 ++++++++++++++++++++++++++ cups/http.h | 487 +++ cups/ipp-private.h | 78 + cups/ipp-support.c | 1090 ++++++ cups/ipp.c | 5574 +++++++++++++++++++++++++++++++ cups/ipp.h | 671 ++++ cups/langprintf.c | 352 ++ cups/language-private.h | 86 + cups/language.c | 1567 +++++++++ cups/language.h | 115 + cups/libcups2.def | 382 +++ cups/libcups2.rc | 75 + cups/libcups_s.exp | 85 + cups/localize.c | 779 +++++ cups/mark.c | 1101 ++++++ cups/md5-private.h | 79 + cups/md5.c | 346 ++ cups/md5passwd.c | 142 + cups/notify.c | 202 ++ cups/options.c | 711 ++++ cups/page.c | 396 +++ cups/ppd-cache.c | 2635 +++++++++++++++ cups/ppd-private.h | 215 ++ cups/ppd.c | 3398 +++++++++++++++++++ cups/ppd.h | 480 +++ cups/pwg-media.c | 928 +++++ cups/pwg-private.h | 104 + cups/raster-private.h | 66 + cups/raster.h | 405 +++ cups/request.c | 1168 +++++++ cups/sidechannel.c | 642 ++++ cups/sidechannel.h | 147 + cups/snmp-private.h | 146 + cups/snmp.c | 1737 ++++++++++ cups/snprintf.c | 362 ++ cups/sspi-private.h | 82 + cups/sspi.c | 1485 ++++++++ cups/string-private.h | 200 ++ cups/string.c | 766 +++++ cups/tempfile.c | 233 ++ cups/test.ppd | 262 ++ cups/test2.ppd | 252 ++ cups/testadmin.c | 120 + cups/testarray.c | 480 +++ cups/testconflicts.c | 138 + cups/testcups.c | 570 ++++ cups/testfile.c | 821 +++++ cups/testhttp.c | 604 ++++ cups/testi18n.c | 619 ++++ cups/testipp.c | 1005 ++++++ cups/testlang.c | 114 + cups/testoptions.c | 116 + cups/testppd.c | 1102 ++++++ cups/testpwg.c | 525 +++ cups/testsnmp.c | 304 ++ cups/thread-private.h | 98 + cups/thread.c | 336 ++ cups/transcode.c | 720 ++++ cups/transcode.h | 81 + cups/usersys.c | 1068 ++++++ cups/utf8demo.txt | 213 ++ cups/util.c | 1848 ++++++++++ cups/versioning.h | 90 + filter/Dependencies | 61 + filter/Makefile | 400 +++ filter/api-raster.header | 37 + filter/api-raster.shtml | 160 + filter/commandtops.c | 538 +++ filter/common.c | 535 +++ filter/common.h | 78 + filter/error.c | 286 ++ filter/gziptoany.c | 112 + filter/interpret.c | 1688 ++++++++++ filter/libcupsimage2.def | 14 + filter/libcupsimage_s.exp | 16 + filter/postscript-driver.header | 32 + filter/postscript-driver.shtml | 276 ++ filter/ppd-compiler.header | 40 + filter/ppd-compiler.shtml | 883 +++++ filter/pstops.c | 3433 +++++++++++++++++++ filter/raster-driver.header | 32 + filter/raster-driver.shtml | 194 ++ filter/raster.c | 1479 ++++++++ filter/rasterbench.c | 355 ++ filter/rastertoepson.c | 1157 +++++++ filter/rastertohp.c | 886 +++++ filter/rastertolabel.c | 1312 ++++++++ filter/rastertopwg.c | 461 +++ filter/spec-ppd.header | 32 + filter/spec-ppd.shtml | 1956 +++++++++++ filter/testraster.c | 1078 ++++++ install-sh | 234 ++ 161 files changed, 103847 insertions(+) create mode 100644 Android.mk create mode 100644 CHANGES-1.0.txt create mode 100644 CHANGES-1.1.txt create mode 100644 CHANGES-1.2.txt create mode 100644 CHANGES-1.3.txt create mode 100644 CHANGES-1.4.txt create mode 100644 CHANGES-1.5.txt create mode 100644 CHANGES.txt create mode 100644 CREDITS.txt create mode 100644 INSTALL.txt create mode 100644 LICENSE.txt create mode 100644 Makedefs.in create mode 100644 Makefile create mode 100644 README.txt create mode 100644 config.h create mode 100644 config.h.in create mode 100644 configure.in create mode 100755 cups-config.in create mode 100644 cups/Dependencies create mode 100644 cups/Makefile create mode 100644 cups/adminutil.c create mode 100644 cups/adminutil.h create mode 100644 cups/api-array.header create mode 100644 cups/api-array.shtml create mode 100644 cups/api-cups.header create mode 100644 cups/api-cups.shtml create mode 100644 cups/api-filedir.header create mode 100644 cups/api-filedir.shtml create mode 100644 cups/api-filter.header create mode 100644 cups/api-filter.shtml create mode 100644 cups/api-httpipp.header create mode 100644 cups/api-httpipp.shtml create mode 100644 cups/api-overview.header create mode 100644 cups/api-overview.shtml create mode 100644 cups/api-ppd.header create mode 100644 cups/api-ppd.shtml create mode 100644 cups/array-private.h create mode 100644 cups/array.c create mode 100644 cups/array.h create mode 100644 cups/attr.c create mode 100644 cups/auth.c create mode 100644 cups/backchannel.c create mode 100644 cups/backend.c create mode 100644 cups/backend.h create mode 100644 cups/conflicts.c create mode 100644 cups/cups-private.h create mode 100644 cups/cups.h create mode 100644 cups/custom.c create mode 100644 cups/debug-private.h create mode 100644 cups/debug.c create mode 100644 cups/dest-job.c create mode 100644 cups/dest-localization.c create mode 100644 cups/dest-options.c create mode 100644 cups/dest.c create mode 100644 cups/dir.c create mode 100644 cups/dir.h create mode 100644 cups/emit.c create mode 100644 cups/encode.c create mode 100644 cups/file-private.h create mode 100644 cups/file.c create mode 100644 cups/file.h create mode 100644 cups/getdevices.c create mode 100644 cups/getifaddrs.c create mode 100644 cups/getputfile.c create mode 100644 cups/globals.c create mode 100644 cups/http-addr.c create mode 100644 cups/http-addrlist.c create mode 100644 cups/http-private.h create mode 100644 cups/http-support.c create mode 100644 cups/http.c create mode 100644 cups/http.h create mode 100644 cups/ipp-private.h create mode 100644 cups/ipp-support.c create mode 100644 cups/ipp.c create mode 100644 cups/ipp.h create mode 100644 cups/langprintf.c create mode 100644 cups/language-private.h create mode 100644 cups/language.c create mode 100644 cups/language.h create mode 100644 cups/libcups2.def create mode 100644 cups/libcups2.rc create mode 100644 cups/libcups_s.exp create mode 100644 cups/localize.c create mode 100644 cups/mark.c create mode 100644 cups/md5-private.h create mode 100644 cups/md5.c create mode 100644 cups/md5passwd.c create mode 100644 cups/notify.c create mode 100644 cups/options.c create mode 100644 cups/page.c create mode 100644 cups/ppd-cache.c create mode 100644 cups/ppd-private.h create mode 100644 cups/ppd.c create mode 100644 cups/ppd.h create mode 100644 cups/pwg-media.c create mode 100644 cups/pwg-private.h create mode 100644 cups/raster-private.h create mode 100644 cups/raster.h create mode 100644 cups/request.c create mode 100644 cups/sidechannel.c create mode 100644 cups/sidechannel.h create mode 100644 cups/snmp-private.h create mode 100644 cups/snmp.c create mode 100644 cups/snprintf.c create mode 100644 cups/sspi-private.h create mode 100644 cups/sspi.c create mode 100644 cups/string-private.h create mode 100644 cups/string.c create mode 100644 cups/tempfile.c create mode 100644 cups/test.ppd create mode 100644 cups/test2.ppd create mode 100644 cups/testadmin.c create mode 100644 cups/testarray.c create mode 100644 cups/testconflicts.c create mode 100644 cups/testcups.c create mode 100644 cups/testfile.c create mode 100644 cups/testhttp.c create mode 100644 cups/testi18n.c create mode 100644 cups/testipp.c create mode 100644 cups/testlang.c create mode 100644 cups/testoptions.c create mode 100644 cups/testppd.c create mode 100644 cups/testpwg.c create mode 100644 cups/testsnmp.c create mode 100644 cups/thread-private.h create mode 100644 cups/thread.c create mode 100644 cups/transcode.c create mode 100644 cups/transcode.h create mode 100644 cups/usersys.c create mode 100644 cups/utf8demo.txt create mode 100644 cups/util.c create mode 100644 cups/versioning.h create mode 100644 filter/Dependencies create mode 100644 filter/Makefile create mode 100644 filter/api-raster.header create mode 100644 filter/api-raster.shtml create mode 100644 filter/commandtops.c create mode 100644 filter/common.c create mode 100644 filter/common.h create mode 100644 filter/error.c create mode 100644 filter/gziptoany.c create mode 100644 filter/interpret.c create mode 100644 filter/libcupsimage2.def create mode 100644 filter/libcupsimage_s.exp create mode 100644 filter/postscript-driver.header create mode 100644 filter/postscript-driver.shtml create mode 100644 filter/ppd-compiler.header create mode 100644 filter/ppd-compiler.shtml create mode 100644 filter/pstops.c create mode 100644 filter/raster-driver.header create mode 100644 filter/raster-driver.shtml create mode 100644 filter/raster.c create mode 100644 filter/rasterbench.c create mode 100644 filter/rastertoepson.c create mode 100644 filter/rastertohp.c create mode 100644 filter/rastertolabel.c create mode 100644 filter/rastertopwg.c create mode 100644 filter/spec-ppd.header create mode 100644 filter/spec-ppd.shtml create mode 100644 filter/testraster.c create mode 100755 install-sh diff --git a/Android.mk b/Android.mk new file mode 100644 index 0000000..6e29be6 --- /dev/null +++ b/Android.mk @@ -0,0 +1,82 @@ +# (c) Copyright 2013 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH:= $(call my-dir) + + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + cups/array.c \ + cups/attr.c \ + cups/auth.c \ + cups/backchannel.c \ + cups/backend.c \ + cups/conflicts.c \ + cups/custom.c \ + cups/debug.c \ + cups/dest.c \ + cups/dest-job.c \ + cups/dest-localization.c \ + cups/dest-options.c \ + cups/dir.c \ + cups/emit.c \ + cups/encode.c \ + cups/file.c \ + cups/getdevices.c \ + cups/getifaddrs.c \ + cups/getputfile.c \ + cups/globals.c \ + cups/http.c \ + cups/http-addr.c \ + cups/http-addrlist.c \ + cups/http-support.c \ + cups/ipp.c \ + cups/ipp-support.c \ + cups/langprintf.c \ + cups/language.c \ + cups/localize.c \ + cups/mark.c \ + cups/md5.c \ + cups/md5passwd.c \ + cups/notify.c \ + cups/options.c \ + cups/page.c \ + cups/ppd.c \ + cups/ppd-cache.c \ + cups/pwg-media.c \ + cups/request.c \ + cups/sidechannel.c \ + cups/snmp.c \ + cups/snprintf.c \ + cups/string.c \ + cups/tempfile.c \ + cups/thread.c \ + cups/transcode.c \ + cups/usersys.c \ + cups/util.c \ + filter/error.c \ + filter/raster.c \ + +disabled_src_files:= \ + +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/cups \ + $(LOCAL_PATH)/../openssl/include +LOCAL_CFLAGS := -D_PPD_DEPRECATED= +LOCAL_MODULE := lib$(PRIV_LIB_NAME)cups +LOCAL_MODULE_TAGS := optional +LOCAL_LDLIBS += -lz -llog +LOCAL_STATIC_LIBRARIES := libssl libcrypto +include $(BUILD_SHARED_LIBRARY) \ No newline at end of file diff --git a/CHANGES-1.0.txt b/CHANGES-1.0.txt new file mode 100644 index 0000000..296d89f --- /dev/null +++ b/CHANGES-1.0.txt @@ -0,0 +1,217 @@ +CHANGES-1.0.txt +--------------- + +CHANGES IN CUPS v1.0.5 + + - The HP-GL/2 filter did not correctly set the pen color + for pens other than #1. + - The scheduler would only accept 26 simultaneous jobs + under some OS releases (mkstemp() limitation.) It now + handles up to 2^32 simultaneous jobs. + - The PostScript filter loaded the printer's PPD file + twice. + - The PAM authentication code now uses pam_strerror() to + provide a textual error message in the error_log file. + - The scheduler now copies PPD and interface script + files instead of moving them; this fixes installations + with a separate requests directory. + - The PostScript RIP did not generate correct 6-color + output. + - Several filters were marking PPD options twice when + they didn't need to. + - The scheduler did not save the printer or class state + after an accept-jobs or reject-jobs operation. + - The cupsGetDefault() function now ignores the PRINTER + environment variable if it is set to "lp". + - New ippErrorString() function to get textual error + messages. + - Better error reporting in the System V commands. + - The lpadmin and lpstat commands always tried to + connect to the default server. + - The text filter didn't load the charset files from the + correct location. + - Wasn't sending a WWW-Authenticate: field to HTTP + clients when authentication was required. + - httpSeparate() didn't always set the default port + number for known methods. + - The HP-GL/2 filter now looks for "PSwidth,length" + instead of (the correct) "PSlength,width" as + documented by HP. It appears that many major CAD + applications are broken and this change allows the + auto-rotation to work with them. + - The IPP "printer-resolution" option was not being + translated. + - The charset files did not include the Microsoft + "standard" characters from 128 to 159 (unused by the + ISO-8859-x charsets) + - The scheduler was chunking the Content-Type field from + CGI programs; this problem was most noticeable with + Microsoft Internet Explorer 5. + - By popular demand, the printers, jobs, and classes + CGIs no longer force a reload of the page every 10/30 + seconds. + - The scheduler incorrectly required that the IPP client + provide a document-format attribute for the + validate-job operation. + - Clients that sent bad IPP requests without the + required attributes-natural-language and + attributes-charset attributes would crash the + scheduler. + + +CHANGES IN CUPS v1.0.4 + + - Documentation updates. + - Jobs would get stuck in the queue and wouldn't print + until you enabled the queue. + - The lp and lpr commands now catch SIGHUP and SIGINTR. + - The lp and lpr commands now use sigaction or sigset + when available. + - CUPS library updates for WIN32/OS-2 + + +CHANGES IN CUPS v1.0.3 + + - Documentation updates. + - The lpq man page was missing. + - The configure script was not properly detecting the + image libraries. + - The top-level makefile was calling "make" instead of + "$(MAKE)". + - PostScript filter fixes for number-up, OutputOrder, + and %Trailer. + - The imagetops filter didn't end the base-85 encoding + properly if the image data was not a multiple of 4 + bytes in length. + - The imagetoraster filter didn't generate good banded + RGB or CMY data (was dividing the line width by 4 + instead of 3...) + - The imagetoraster filter now records the bounding + box of the image on the page. + - The CUPS image library cache code wasn't working as + designed; images larger than the maximum RIP cache + would eventually thrash using the same cache tile. + - The CUPS image library TIFF loading code didn't + handle unknown resolution units properly; the fixed + code uses a default resolution of 128 PPI. + - cupsGetClasses() and cupsGetPrinters() did not free + existing strings if they ran out of memory. + - The scheduler logs incorrectly contained 3 digits for + the timezone offset instead of 4. + - The scheduler now does a lookup for the default user + and group ID; the previous hardcoded values caused + problems with the LPD backend. + - The cancel-job operation now allows any user in the + system group to cancel any job. + - The cancel-job operation stopped the print queue if + the job was being printed. + - Now only stop printers if the backend fails. If the + filter fails then the failure is noted in the + error_log and printing continues with the next file in + the queue. + - Now log whether a filter fails because of a signal + or because it returned a non-zero exit status. + - The root user now always passes the system group test. + - Printers with an interface script and remote printers + and classes didn't have a printer-make-and-model + attribute. + - Added logging of lost/timed-out remote printers. + - The HP-GL/2 filter was scaling the pen width twice. + - Updated the HP-GL/2 filter to use a single SP (Set + Pen) procedure. This makes the output smaller and is + more appropriate since the filter keeps track of the + pen states already. + - The scheduler didn't handle passwords with spaces. + - The IPP backend now does multiple copies and retries + if the destination server requires it (e.g. HP + JetDirect.) + - The disable command didn't implement the "-c" option + (cancel all jobs.) + - Changed the CMYK generation function for the image file + and PostScript RIPs. + - The lp command didn't support the "-h" option as + documented. + - The AppSocket, IPP, and LPD backends now retry on all + network errors. This should prevent stopped queues + caused by a printer being disconnected from the + network or powered off. + - The scheduler now restarts a job if the corresponding + printer is modified. + - The image RIPs now rotate the image if needed to fit + on the page. + + +CHANGES IN CUPS v1.0.2 + + - The HP-GL/2 filter didn't always scale the output + correctly. + - The HP-GL/2 filter now supports changing the page size + automatically when the "fitplot" option is not used. + - The cancel-job operation was expecting a resource name + of the form "/job/#" instead of "/jobs/#"; this + prevented the cancel and lprm commands from working. + - The backends didn't log pages when files were printed + using the "-oraw" option. + - The authorization code did not work with the Slackware + long shadow password package because its crypt() can + return NULL. + - The chunking code didn't work for reading the response + of a POST request. + - cupsGetPPD() now does authentication as needed. + - The N-up code in the PostScript filter didn't work + with some printers (grestoreall would restore the + default blank page and device settings). + - The N-up code in the PostScript filter didn't scale + the pages to fit within the imageable area of the + page. + - Wasn't doing an fchown() on the request files. This + caused problems when the default root account group + and CUPS group were not the same. + + +CHANGES IN CUPS v1.0.1 + + - Documentation updates. + - Fixed a bunch of possible buffer-overflow conditions. + - The scheduler now supports authentication using PAM. + - Updated the Italian message file. + - httpEncode64() didn't add an extra "=" if there was + only one byte in the last three-byte group. + - Now drop any trailing character set from the locale + string (e.g. "en_US.ISO_8859-1" becomes "en_US") + - Fixed "timezone" vs "tm_gmtoff" usage for BSD-based + operating systems. + - Updated IPP security so that "get" operations can be + done from any resource name; this allows the CGIs to + work with printer authentication enabled so long as + authentication isn't turned on for the whole "site". + - The IPP code didn't properly handle the "unsupported" + group; this caused problems with the HP JetDirect since + it doesn't seem to support the "copies" attribute. + - The HTTP chunking code was missing a CR LF pair at the + end of a 0-length chunk. + - The httpSeparate() function didn't handle embedded + usernames and passwords in the URI properly. + - Doing "lpadmin -p printer -E" didn't restart printing + if there were pending jobs. + - The cancel-job operation now requires either a + requesting-user-name attribute or an authenticated + username. + - The add-printer code did not report errors if the + interface script or PPD file could not be renamed. + - Request files are now created without world read + permissions. + - Added a cupsLastError() function to the CUPS API to + retrieve the IPP error code from the last request. + - Options are now case-insensitive. + - The lpq command now provides 10 characters for the + username instead of the original (Berkeley standard) + 7. + - The cancel command needed a local CUPS server to work + (or the appropriate ServerName in cupsd.conf) + - The cancel and lprm commands didn't report the IPP + error if the job could not be cancelled. + - The lp and lpr commands didn't intercept SIGTERM to + remove temporary files when printing from stdin. + - The lp and lpr commands didn't report the IPP error if + the job could not be printed. diff --git a/CHANGES-1.1.txt b/CHANGES-1.1.txt new file mode 100644 index 0000000..c641e79 --- /dev/null +++ b/CHANGES-1.1.txt @@ -0,0 +1,3462 @@ +CHANGES-1.1.txt +--------------- + +CHANGES IN CUPS V1.1.23 + + - Updated the Spanish man pages (STR #1041) + - The lpstat man page contained a typo (STR #1040) + - The scheduler's is_path_absolute() code could cause a + DoS (STR #1042) + - The scheduler's device loading code used the wrong + size limits for the make/model and info parameters + (STR #1035) + - The PNG loading code did not use a "long unsigned + integer" format specifier for the width and height + (STR #1032) + - The web interface only showed the first 4 or 8 + characters of "{variable-name}" for undefined template + variables (STR #1031) + - The hpgltops filter did not handle a common PCL + command to enter HP-GL/2 mode (STR #1037) + + +CHANGES IN CUPS V1.1.23rc1 + + - The lpr man page did not document the "-U" option (STR + #998) + - The scheduler no longer sends the page-set option when + printing banner pages (STR #995) + - Fixed a debug message in the imagetops filter (STR + #1012) + - The lprm man page listed the "-" option in the wrong + order (STR #911) + - The hpgltops filter contained two buffer overflows + that could potentially allow remote access to the "lp" + account (STR #1024) + - The lppasswd command did not protect against file + descriptor or ulimit attacks (STR #1023) + - The "lpc status" command used the wrong resource path + when querying the list of printers and jobs, causing + unnecessary authentication requests (STR #1018) + - The httpWait() function did not handle signal + interruptions (STR #1020) + - The USB backend used the wrong size status variable + when checking the printer status (STR #1017) + - The scheduler did not delete classes from other + classes or implicit classes, which could cause a crash + (STR #1015) + - The IPP backend now logs the remote print job ID at + log level NOTICE instead of INFO (so it shows up in + the error_log file...) + + +CHANGES IN CUPS V1.1.22 + + - The lpstat man page incorrectly listed the "-s" option + as using the equivalent of the "-p" option to list the + printers; it uses the "-v" option to list the printers + (STR #986) + - Now allow 0-length reads in the CUPS file API (STR + #985) + - cupsDoFileRequest() now sets cupsLastError() to + IPP_ERROR on network errors (STR #953) + - The pdftops filter didn't scale small pages up to the + output page size when the fitplot option was used (STR + #984) + - Fixed the ipptest program usage message (STR #959) + - Added Spanish man pages (STR #963) + - Fixed the order of comparisons in the client.conf + reading code (STR #971) + - cupsLangGet() incorrectly set the current locale (STR + #970) + + +CHANGES IN CUPS V1.1.22rc2 + + - The pdftops filter didn't check the range of all + integer attributes (STR #972) + - Documentation corrections (STR #944, STR #946) + - Also sanitize device URI in argv[0] (STR #933) + - cupsRasterReadHeader() didn't swap bytes for the + numeric fields properly (STR #930) + + +CHANGES IN CUPS V1.1.22rc1 + + - Now sanitize the device URI that is reported in the + error_log file (STR #920) + - Fixed some memory and file descriptor leaks in the job + dispatch code (STR #921) + - Deleting a printer could cause a crash with browsing + enabled (STR #865, STR #881, STR #928) + - Browsing would turn off if the scheduler got an EAGAIN + error (STR #924) + - The mime.types file didn't recognize PostScript as a + PJL language name (STR #925) + + +CHANGES IN CUPS V1.1.21 + + - The scheduler did not separate Digest authentication + parameters with commas (STR #882) + - Fixed some problems with image printing to custom page + sizes (STR #891) + - Removed the remaining scheduler code that did not use + the "close-on-exec" file descriptor flag to speed up + program invocations (STR #890) + - The "lpr -r" command removed the print file even if it + was not printed. It now only removes the file if the + job is successfully created (STR #886) + - Revamped the custom page size orientation fix (STR + #127) + - The lp, lpq, lpr, and lpstat commands now report when + an environment variable is pointing to a non-existent + printer instead of just saying "no default + destination" (STR #879) + - Queue names with 2 periods (e.g. "printer..2") were + not supported (STR #866) + + +CHANGES IN CUPS V1.1.21rc2 + + - Fixed a denial-of-service bug in the CUPS browse + protocol support (STR #863) + - The scheduler used a select() timeout of INT_MAX + seconds when there was nothing to do, which doesn't + work on IRIX (STR #864) + - Updated the cupsaddsmb program to use the new Windows + 2000 PostScript drivers instead of the Windows NT + printer drivers (STR #390) + - The gziptoany filter did not produce copies for raw + print jobs (STR #808) + - The cupsLangGet() function now uses nl_langinfo(), + when available, to get the current encoding (STR #856) + - Added a ReloadTimeout directive to control how long + the scheduler waits for jobs to complete before + restarting the scheduler (STR #861) + - Added a note to the default cupsd.conf file which + mentions that you must allow connections from + localhost for the command-line and web interfaces to + work (STR #850) + - The IPP backend incorrectly used the local port when + communicating with a remote server; this caused + problems with some custom configurations (STR #852) + - The cups-lpd mini-daemon wasn't using the right + default banner option (STR #851) + - Updated the new httpDecode64_2() and httpEncode64_2() + functions to handle arbitrary binary data, not just + text (STR #860) + - String options with quotes in their values were not + quoted properly by the scheduler (STR #839) + - Configure script changes for GNU/Hurd (STR #838) + - The lppasswd program was not installed properly by GNU + install when the installer was not root (STR #836) + - Updated the cups-lpd man page (STR #843) + - Fixed a typo in the cupsd man page (STR #833) + - The USB backend now defaults to using the newer + /dev/usb/lpN filenames; this helps on systems which + use the devfs filesystem type on Linux (STR #818) + - The config.h file did not define the HAVE_USERSEC_H + constant when the configure script detected the + usersec.h header file. This caused authentication + errors on AIX (STR #832) + - The lp and lpr commands now report the temporary + filename and error if they are unable to create a + temporary file (STR #812) + - Added ServerTokens directive to control the Server + header in HTTP responses (STR #792) + - Added new httpDecode64_2(), httpEncode64_2(), and + httpSeparate2() functions which offer buffer size + arguments (STR #797) + - The cupsGetFile() and cupsPutFile() code did not + support CDSA or GNUTLS (STR #794) + - The httpSeparate() function did not decode all + character escapes (STR #795) + - The cupstestppd program now checks for invalid Duplex + option choices and fails PPD files that use + non-standard values (STR #791) + - Updated the printer name error message to indicate + that spaces are not allowed (STR #675) + - The scheduler didn't handle HTTP GET form data + properly (STR #744) + - The pstops filter now makes sure that the prolog code + is sent before the setup code (STR #776) + - The pstops filter now handles print files that + incorrectly start @PJL commands without a language + escape (STR #734) + - Miscellaneous build fixes for NetBSD (STR #788) + - Added support for quoted system group names (STR #784) + - Added "version" option to IPP backend to workaround + serious bug in Linksys's IPP implementation (STR #767) + - Added Spanish translation of web interface (STR #772, + STR #802) + - The LPD backend now uses geteuid() instead of getuid() + when it is available (STR #752) + - The IPP backend did not report the printer state if + the wait option was set to "no" (STR #761) + - The printer state was not updated for "STATE: foo,bar" + messages (STR #745) + - Added new CUPS API convenience functions which accept + a HTTP connection to eliminate extra username/password + prompts. This resolves a previous authentication + caching issue (STR #729, STR #743) + - The scheduler did not correctly throttle the browse + broadcasts, resulting in missing printers on client + machines (STR #754) + - The scheduler did not pass the correct CUPS_ENCRYPTION + setting to CGI programs which caused problems on + systems which used non-standard encryption settings + (STR #773) + - The lpq command showed 11st, 12nd, and 13rd instead of + 11th, 12th, and 13th for the rank (STR #769) + - "make install" didn't work on some platforms due to an + error in the man page makefiles (STR #775) + - Changed some calls to snprintf() in the scheduler to + SetStringf() (STR #740) + + +CHANGES IN CUPS V1.1.21rc1 + + - Fixed some "type-punned" warnings produced by GCC when + -fstrict-aliasing is specified (STR #679) + - The PDF filter incorrectly calculated the bounding box + of a page (STR #682) + - The IPP backend did not use SSL when printing over a + port other than 443 (STR #730) + - The scheduler could crash when processing a Limit or + LimitExcept directive (STR #728) + - The lpq, lpr, and lp commands did not differentiate + between the server being unresponsive and the lack of + a default printer (STR #728) + - The PAM checks in the configure script did not stop + after the first match (STR #728) + - The cups-config man page was incorrectly placed in + section 3 (STR #728) + - The cupstestppd utility did not show a warning message + when a PPD file indicated BCP protocol support with + PJL (STR #720) + - The scheduler did not return the correct exit code + when startup failed (STR #718) + - The cupsRasterReadPixels() function checked for + EAGAIN, which caused problems on FreeBSD (STR #723) + - The cupsGetDests() function did not use the current + encryption setting (STR #653) + - The scheduler did not properly parse name-based + BrowseRelay directives in the cupsd.conf file (STR + #711) + - The IPP backend now supports the following options in + the device URI: encryption, waitjob, and waitprinter + (STR #699) + - The parallel, serial, socket, and USB backends did not + return a non-zero exit status when a job failed to + print in the middle of sending it (STR #715) + - Location directives in the cupsd.conf file were + case-sensitive for printer and class names, so + queue-specific access control was not reliable (STR + #700) + - cupsDoFileRequest() did not handle HTTP continue + status messages in all cases, causing sporatic + problems with IPP printers from some vendors (STR + #716) + - The rastertodymo driver now supports the Zebra ZPL + language (STR #713) + - The test suite no longer generates a printcap file, + which caused problems when testing as the root user + (STR #693) + - The scheduler now updates the accepting state of an + implicit class based upon the accepting state of its + member printers (STR #697) + - The pstops filter didn't properly skip leading PJL + commands (STR #664) + - The reinterpret_cast keyword was not highlighted when + printing C/C++ source files in prettyprint mode (STR + #694) + - Fixed a segfault problem with some of the client + programs (STR #668) + - When using RunAsUser, the scheduler did not correctly + set the ownership of the log files, preventing log + file rotation (STR #686) + - The image filters did not correctly load 1-bit PNG + files (STR #687) + - The pdftops filter did not show all annotation objects + in a PDF file (STR #674) + - The pdftops filter did not print the contents of + textual form elements, making it impossible to print a + filled-in form (STR #663) + - Integrated the MacOS X/Darwin USB backend into the + CUPS baseline (STR #661) + - The USB backend incorrectly reported "media tray + empty" (STR #660) + - The scheduler did not use a case-insensitive + comparison when checking for group membership, which + caused problems with Win9x clients printing via SAMBA + (STR #647) + - The scheduler did not report the addresses associated + with certain network errors, making troubleshooting + difficult (STR #648, #649) + - The cupstestppd program did not allow a default choice + of "Unknown" as required by the PPD spec (STR #651) + - The select() buffers are now allocated to be at least + as large as sizeof(fd_set) (STR #639) + - The LPD backend now supports overriding the print job + username via the device URI (STR #631) + - The scheduler did not handle an unknown MIME type when + checking for a CGI script (STR #603) + - Added a timeout optimization to the scheduler's main + loop to allow CUPS to sleep more of the time (STR + #629) + - The USB backend now retries printing to devices of the + form "usb://make/model" if any USB port shows up as + "busy" (STR #617) + - The httpGetHostByName() function did not range check + IP address values (STR #608) + - The httpUpdate() function could return HTTP_ERROR + instead of the HTTP status if the server closed the + connection before the client received the whole + response (STR #611) + - The LPD mini-daemon did not allow the administrator to + force banner pages on (STR #605) + - Added PAM support for Darwin/MacOS X (STR #550) + - The web interface now provides a "Set As Default" + button to set the default printer or class on a server + (STR #577) + - The HTTP authentication cache was broken (STR #517) + - The cupstestppd utility now fails PPD files that have + a DefaultOption keyword for a non-existance option + name (STR #476) + - Optimized the scanning of new PPD files on scheduler + startup (STR #424) + - The EPM list file did not include the bin, lib, or + sbin directories (STR #598) + - The web interface did not redirect administration + tasks to the primary server for a class or printer + (STR #491, #652) + - The cups-lpd mini-daemon did not reject print jobs to + queues that were rejecting new print jobs (STR #515) + - Some calls to the ctype functions did not account for + platforms that use a signed char type by default (STR + #518) + - The scheduler could use excess amounts of CPU if a CGI + program was sending data faster than the client could + take it (STR #595) + - Updated the Ghostscript 8.x integration stuff (STR + #484) + - The lpd backend used a source port of 732 by default, + which is outside of the range defined by RFC 1179; + also added a new (default) "reserve=any" option for + any priviledged port from 1 to 1023 (STR #474) + - The scheduler did not check for a valid Listen/Port + configuration (STR #499) + - The cupsPrintFiles() function did not always set the + last IPP error message (STR #538) + - The pstops filter did not write the PostScript header + line if the file began with a PJL escape sequence (STR + #574) + - The printer-is-accepting-jobs status of remote + printers was not sent to clients via browsing or + polling (STR #571) + - Browse packets did not indicate whether a printer + was accepting or rejecting jobs. + - The web interface did not show the printer state + history information (STR #592) + - The rastertoepson filter would crash under certain + cirsumstances (STR #583) + - The USB backend did not handle serial numbers using + the (incorrect) SN keyword and did not terminate the + make and model name strings properly (STR #471, STR + #588) + - The USB backend did not build on Solaris x86 (STR + #585) + - The cupsDoAuthentication() function did not use the + method name for Digest authentication (STR #584) + - The scheduler could crash if a print job could not be + printed and the PreserveJobHistory option was turned + off (STR #535) + - cups-lpd now logs the temporary filenames that could + not be opened in order to make troubleshooting easier + (STR #565) + - cupsGetJobs() now returns -1 on error (STR #569) + - Added localization for Belarusian (STR #575) + - The LPD backend used the full length of the hostname + when creating the data and control filenames, which + causes problems with older systems that can't handle + long filenames (STR #560) + - The scheduler did not refresh the common printer data + after a fast reload; this prevented banner and other + information from being updated (STR #562) + - The scheduler did not send common or history data to + the client when processing a CUPS-Get-Default request + (STR #559) + - The httpFlush() function did not always flush the + remaining response data in requests (STR #558) + - The scheduler could complete a job before it collected + the exit status from all filters and the backend (STR + #448) + - The PPD conformance tests did not catch group + translation strings that exceeded the maximum allowed + size (STR #454) + - Updated the client code in the scheduler to close the + client connection on errors rather than shutting down + the receive end of the socket; this caused resource + problems on some systems (STR #434) + - cups-polld didn't compile on Tru64 5.1B (STR #436) + - "lpc stat" crashed if the device URI was empty (STR + #548) + - The scheduler did not compile without zlib (STR #433) + - std:floor() cast needed on IRIX 6.5 with SGI C++ + compiler (STR #497) + - cupsRasterReadPixels() and cupsRasterWritePixels() did + not handle EAGAIN and EINTR properly (STR #473) + - RequiresPageRegion should not be consulted for Manual + Feed (STR #514) + - International characters were not substituted in + banner files properly (STR #468) + - Updated pdftops to Xpdf 2.03 code to fix printing bugs + (STR #470) + - The Digest authentication code did not include the + (required) "uri" attribute in the Authorization + response, preventing interoperation with Apache + (STR #408) + - The web interface could lockup when displaying certain + URLs (STR #459) + - The PostScript filters now convert underscores ("_") + to spaces for custom classification names (STR #555) + + +CHANGES IN CUPS V1.1.20 + + - The pstops filter didn't properly handle collated, + duplexed copies of documents with an odd number of + pages on printers that did not do their own collated + copies (STR #389) + - Tru64 doesn't define a prototype for hstrerror() (STR + #430) + - Updated the pdftops filter to use the annotation flags + instead of the subtype to determine whether to print + an annotation (STR #425) + - The French web interface localization did not use + absolute paths for the navigation bar (STR #428) + - The CUPS test suite did not undefine the PRINTER and + LPDEST environment variables. This could lead to bogus + test results (STR #380) + - The cupsLangDefault() function now works if you don't + have the base OS localization installed (STR #418) + - The pdftops filter no longer needs to create temporary + files with tmpnam (STR #406) + - The HTTP code did not use a case-insensitive + comparison when checking for the Basic authentication + method (STR #407) + - The httpEncode() function always added a trailing "=" + character, which is not required by the Base64 + encoding specification (STR #407) + - The signal handlers did not need to call sigset(); + this caused a recursion problem on some versions of + IRIX (STR #422) + - Moved the scheduler termination code into the mainline + to be consistent with the way other signals are + handled (STR #423) + - The cupsaddsmb program didn't export the new CUPS + driver for Windows properly (STR #390) + - The ppdOpen() functions did not issue an error when a + translation string exceeded the maximum allowed by the + Adobe PPD specification (STR #399) + - The default landscape orientation was not the same as + that defined in the PPD file (STR #397) + - Updated the pstoraster patch files and CUPS driver to + work with Ghostscript 8 (STR #402) + - The hpgltops filter did not skip PJL commands (STR + #379) + + +CHANGES IN CUPS V1.1.20rc6 + + - "lp -i jobid -H restart" would often return an error + even though the job restarted successfully (STR #362) + - The scheduler did not check for invalid allow/deny + addresses such as "11.22.33.44/24". It now masks off + the extra address bits and logs a warning message in + the error_log file (STR #337) + - The cupstestppd utility now checks for missing + ImageableArea and PaperDimension attributes for each + defined PageSize (STR #365) + - The IPP code did not wait for a reply indefinitely on + HTTP connections in "blocking" mode (STR #377) + - The web interfaces did not rewrite the default printer + URI properly (STR #299 and #369) + - The LPD backend passed the C and L commands in the + wrong order (STR #378) + - The Dymo label printer driver did not set the label + length properly (STR #373) + - The scheduler did not support job IDs higher than + 99999 (STR #371) + - The Visual C++ project files did not work (STR #366) + - The scheduler's cupsLangSeek() function did not reset + the "EOF" flag, preventing compressed files from being + typed properly in some cases (STR #368) + - The cupsLangGet() cache was only used if the locale + name provided an explicit character set name (STR + #354) + - The CUPS API convenience functions did not call + cupsLangFree() when they were done with the + localization data (STR #354) + - The scheduler did not return the + job-hold-until-supported or job-hold-until-default + attributes (STR #356) + - The cupsaddsmb program did not support the new CUPS + driver for Windows (STR #357) + + +CHANGES IN CUPS V1.1.20rc5 + + - The scheduler did not initialize the browse socket + file descriptor properly when only SLP browsing was + enabled (STR #259) + - The scheduler accessed the job attributes before they + were set (STR #347, fix to STR #335) + - The cupsCancelJob() function did not return 0 when the + job could not be canceled (STR #340) + + +CHANGES IN CUPS V1.1.20rc4 + + - The scheduler did not move the incoming job attributes + in the operation group to the job group (STR #335) + - The cupsDoFileRequest() function did not check for an + early HTTP response while sending the file (STR #314) + - The web interfaces did not quote #, ?, or . in printer + names, which caused some problems with the generated + URLs (STR #320) + - CUPS couldn't be completely compiled with the -dDEBUG + option (STR #331) + + +CHANGES IN CUPS V1.1.20rc3 + + - More SLP changes (STR #259) + - Revamped the child signal handling code to completely + avoid deadlock issues on Solaris (STR #325) + - The lpadmin command displayed an incorrect error + message when the "-u" option was provided with no + arguments (STR #313) + - The web admin interface did not display an error + message if the PPD file could not be loaded (STR #308) + - The ppdEmit() functions did not use the correct + orientation value position for custom page sizes (STR + #292) + + +CHANGES IN CUPS V1.1.20rc2 + + - The serial backend set the IXANY option on the port + for XON/XOFF flow control; this caused problems with + printers that returned status info but were not ready + for more print data (STR #287) + - The scheduler didn't support scripted index files + (index.php, index.pl, etc. - STR #290) + - The scheduler did not correctly localize script files + with "GET" variables (STR #268) + - Changes in job classification are now logged (STR + #289) + - Fixed a few more SLP-related bugs (STR #259) + - Updated the user/group configure checks for MacOS X + 10.3 (STR #270) + - Fixed an offset bug in the PDF filter (STR #284) + - The cupsDoRequest() and cupsDoFileRequest() functions + did not map several HTTP status codes to their IPP + counterparts. This made detecting certain conditions + very difficult (STR #277) + - Config, spool, and status files are now owned by the + scheduler user (usually root) with read permission for + the filter group (STR #283) + - The HP-GL/2 filter did not support the SI command, + some values for the AD and SD commands, and did not + rotate labels properly via the DI command (STR #282) + - The fax support did not update/set the job-hold-until + attribute when a fax job fails (STR #269) + - The cupsLangGet() function didn't support locales of + the form "ll.charset" (STR #271) + - The scheduler did not use the charset when getting the + language localization for a request; this caused extra + disk IO for every request (STR #271) + - The scheduler did not support requests with more than + one language specified (STR #267) + + +CHANGES IN CUPS V1.1.20rc1 + + - The scheduler now waits up to 60 seconds before + restarting to allow active jobs to complete printing + and pending requests to be processed (STR #226) + - The web interface did not work on systems where time_t + is 64 bits (STR #262) + - Added backend tweeks and content-length check from Red + Hat (STR #253) + - The USB backend now uses the 8255 constants instead of + the standard constants when reporting printer status + bits on Linux (STR #254) + - Added new cupsDoAuthentication(), cupsGetFd(), + cupsGetFile(), cupsPutFd(), and cupsPutFile() functions + to the CUPS API (STR #112) + - The PDF filter always scaled and offset pages; this + caused problems under MacOS X, so now the "fitplot" + option controls whether PDF files are scaled to fit + within the printable area of the page (STR #250) + - The LPD backend did not support the port number in a + URI (STR #247) + - Some filters didn't properly support boolean options + (STR #249) + - Landscape PDF files were not always offset by the + correct amount when rotating (STR #243) + - The scheduler could hang in a call to localtime() when + logging messages from the signal handler (STR #242) + - The PDF filter no longer prints form widgets; this + duplicates the behavior of Acrobat Reader (STR #241) + - cupsGetPPD() didn't handle a late termination of a + HTTP connection with the server (STR #220) + - ppdOpen() did not correctly check for "*PPD-Adobe-4." + on the first line of a PPD file. This caused incorrect + PASS results for some PPD files (STR #233) + - cupsEncodeOptions() did not allow boolean options to + use "yes" and "on" for true values (STR #227) + - The pstops filter only sent the TBCP exit sequence if + it was defined in the JCLEnd attribute in the PPD file + (STR #224) + - Support for more than 1024 files was broken on Solaris + 9 (STR #217) + - The setgroups() calls now pass in 1 group (the + configured group) instead of 0 for compatibility with + BSD and Darwin (STR #213) + - The scheduler's built-in broadcast throttling was + ineffective since incoming packets would cause the + next group of outgoing packets to be sent immediately + rather than waiting for the next time slot (STR #211) + - Added a new ppdSetConformance() function to set the + conformance requirements for PPD files. Currently only + two levels are defined, PPD_CONFORM_RELAXED and + PPD_CONFORM_STRICT, and the default is the relaxed + level (STR #212) + - The IPP backend did not correctly execute the + pictwpstops filter on OSX (STR #210) + - The LPD backend did not set the banner class when the + "banner=yes" option was specified in the device URI + (STR #209) + - The imagetoraster filter did not support all of the + page device attributes (STR #208) + - The pdftops filter incorrectly auto-rotated pages when + the user already had specified the proper orientation + (STR #207) + - Fixed AIX shared library support (STR #201) + - Added support for live testing with Valgrind (STR + #193) + - The CGI programs now collect the list of needed + attributes for the class, job, and printer template + files (STR #192) + - The scheduler now passes the first port that is bound + to the local loopback or "any" addresses to the CGI + programs rather than the port that the browser + connected to (STR #103) + - The cupstestppd program now checks for bad + JobPatchFile attributes and incorrect versions of the + Manufacturer attribute for HP printers (STR #155) + - The filter makefile incorrectly installed + libcupsimage.a in the filter directory (STR #180) + - The scheduler did not verify that the job history + files define the job-priority and + job-originating-user-name attributes (STR #178) + - The pstops filter didn't handle poorly-formed binary + PostScript files that had CTRL-D's in them (STR #156) + - The ppdOpen*() and cupsLangGet() functions did not + make a copy of the old locale strings when using the + POSIX locale when reading files, which apparently + caused problems with some implementations of the + standard C library. (STR #159) + - The pdftops filter did not work properly with some + embedded Type1C fonts (STR #177) + - Updated the pdftops filter to be based upon Xpdf + 2.02pl1 (STR #191) + - The scheduler did not reset the group list when + running CGI and filter processes (STR #185) + - The scheduler no longer calls malloc and free from the + signal handlers (STR #190) + - The USB backend now uses the manufacturer and model + strings if the description string is not available + (STR #174) + - The ppdOpen functions still supported the + VariablePaperSize attribute, which was removed in v4.0 + of the PPD spec. This caused problems with PPD files + that relocated the PageSize option to a non-standard + group (STR #158) + - The cups.list file referenced MAN1EXT, MAN3EXT, and + MAN5EXT, but none of those were actually defined (STR + #147) + - Chunked requests could cause a Denial of Service if + the connection is terminated before the first byte of + chunk data is sent/received (STR #143) + - Printers with special characters in their names were + not accessible from the web interface (STR #120) + - The lpstat command now shows the correct interface + script or PPD file, if any, for a print queue (STR #89) + - The lpstat command now shows the printer-state-message + and printer-state-reasons attributes whenever they are + not blank (STR #152) + - The French and German option-conflict.tmpl template + files did not get installed (STR #148) + - The cups.list.in file did not work when compiling + without shared libraries (STR #149) + - The DSOFLAGS included the LDFLAGS, which causes + problems on at least HP-UX (STR #150) + - The fax printer support did not keep track of the fax + capability bit (STR #144) + - The appleLangDefault() function could leak a small + amount of memory (STR #145) + - The ppdOpen() functions now mirror all normal + attributes to the attribute list; previously only + certain unassigned attributes would be added (STR + #139) + - The ppdEmitJCL() function wrote JCL commands to stdout + instead of the passed file pointer (STR #142) + - The httpGets() function could, in certain states, + block waiting for data (STR #132) + - The cupsEmitJCL() function not outputs an empty @PJL + command after the PJL language escape to work around + bugs in certain PJL implementations (STR #131) + - The cupsEmit*() functions didn't set the orientation + value properly (STR #127) + - The cups.spec file didn't list the rc2.d init + directory or the cupstestppd file (STR #134) + + +CHANGES IN CUPS V1.1.19 + + - The GNU TLS code incorrectly used + gnutls_check_pending() instead of + gnutls_record_check_pending() (STR #128) + - The ppdEmit() functions output "PageSize Custom" + instead of "CustomPageSize True" in the DSC comments. + Also, the custom page size code did not use the + ParamCustomPageSize attributes (STR #127) + - The cupstestppd command did not list the conflicting + options (STR #123) + - The lpq command did not ensure that there was + whitespace between the fields in the job listing (STR + #117) + - The German web templates had errors (STR #119) + - The configure script didn't specify the static + libraries properly when configuring with the + --disable-shared option (STR #104) + - The cups.list file used file dependencies for package + formats other than portable, RPM, and Debian (STR #98) + - cupsLangGet() didn't use its language cache (STR #97) + - "lpq -P" would segfault instead of showing a usage + message (STR #94) + - Fixed compiler warnings in pdftops filter (STR #96) + + +CHANGES IN CUPS V1.1.19rc5 + + - Jobs with banner pages that were printed to implicit + classes would get double banner pages for each + file/banner in the job (STR #68) + - The mime.convs file was missing the filter definition + for Windows BMP (image/x-bitmap) files (STR #85) + - The scheduler allowed some READ-ONLY job attributes to + be set, which could cause the scheduler to fail on the + next restart (STR #82) + - The lp and lpr commands did not report when the + scheduler was not responding; instead, the user would + incorrectly see a "no default destination" error (STR + #70) + - cupsLangGet() could fail on OSX due to a corrupt + language preference (STR #78) + - Added more checks for HTTP request timeouts. + - The scheduler dropped the first non-alpha character + after an open brace when doing attribute substitutions + in banner pages (STR #77) + - The scheduler child might send SIGUSR1 to the parent + before the signal handler was installed; this didn't + prevent the scheduler from starting but produced an + annoying error message (STR #45) + + +CHANGES IN CUPS V1.1.19rc4 + + - The lp command did not accept "-" for printing from + the standard input as required by POSIX 1003.1 (STR + #59) + - Added the job-originating-host-name information for + the page_log file documentation in the SAM (STR #31) + - The German web interface templates did not use the + right paths for job operations (STR #54) + - The scheduler would consume all available CPU if + started with a pending job in the queue (STR #35) + - The polling daemon allocated an extra localization + buffer but did not free it, causing cups-polld to + eventually use all available memory (STR #40) + + +CHANGES IN CUPS V1.1.19rc3 + + - The scheduler could get in an infinite loop cancelling + jobs using "cancel -u user dest" (STR #48) + - The "cancel -u user" command did nothing (it should + cancel all jobs on all printers owned by the named + user - STR #48) + - The scheduler would write 0-length job control files + (STR #46) + - Updated the French man pages (translation provided by + Gilles QUERRET) + - The scheduler would delete all printers from + printers.conf if a job was active when a HUP signal + was handled (STR #47) + - The cups-polld program would leak memory if it was + unable to send browse packets to the loopback + interface (STR #40) + - The scheduler did not put the + job-originating-host-name attribute in the job + attributes group. + - The text filter did not default to wrapping text as + defined by the IPP implementation document. + - Scan backends first, PPDs second (STR #37) + - Updated the Netatalk documentation in the SAM (STR #38 + and #39) + - The test suite sent text files to a non-PS print queue, + which requires ESP Ghostscript (provided separately). + Now send the JPEG test file (STR #33) + - The test suite did not show the estimated disk space + requirements (STR #33) + - The test suite did not set the MaxLogSize directive to + 0 to prevent log file rotation (STR #33) + - The test suite still setup the old CUPS Ghostscript + symlinks (STR #33) + - The pstops filter did not report the correct number of + copies for the page_log file when printing collated + copies to a printer that doesn't support them in + hardware (STR #32) + - cupsLangGet() needs to set the CTYPE locale to "C" + (POSIX) to avoid erroneous tolower/toupper values (fix + suggested by Bjoern Jacke) + - Fixed a typo in the cups.list.in file. + - Updated all of the Western European locales to default + to ISO-8859-15 (for Euro support, suggested by Bjoern + Jacke) + - Updated the German message catalog (update provided by + Bjoern Jacke) + + +CHANGES IN CUPS V1.1.19rc2 + + - cupsLangGet() now sets the encoding field based on the + trailing charset in the locale name, and doesn't look + for a message catalog in a specific locale.charset + directory. This fixes STR #26 and is more in line + with the CUPS 1.2 implementation. + - The configure script now aborts if the "ar" command or + compilers cannot be found. + - The static cupsimage library was not built by default. + - The path for the "ln" command was hardcoded in + Makedefs.in instead of being checked at configure time + (STR #28). + - Banner pages containing unescaped { characters would + not work. + - The printer-state-time collection attribute was + encoded as an enumeration instead of an integer. + - The printer-is-accepting-jobs collection attribute was + was not added to the collection value. + - The printer-state-sequence-number collection attribute + was not added to the collection value. + - Fixed typo and const mismatch in IPP backend. + - Updated the man pages for the new configuration + directives. + - Updated the SAM for MacOS 10.2, the CUPS drivers for + windows, the available LPD backend options, and the + new configuration directives. + - The imagetops filter didn't position images properly + on the page (STR #18) + - The configure script didn't add CPPFLAGS to the + compiler options or LDFLAGS to the DSO options (STR + #13) + - The scheduler would try to write a debug log message + when starting a job that contained a NULL string. + Since not all versions of snprintf() support NULL + string pointers this caused some problems (STR #20) + - The testipp program now supports reading of IPP + message files such as those used for the job history + in /var/spool/cups. + + +CHANGES IN CUPS V1.1.19rc1 + + - Added CUPS support files for Java, Perl, and PHP + (located in the "scripting" subdirectory...) + - The scheduler now supports fast-reloads of the + cupsd.conf file when it is updated via HTTP. + - The scheduler always changed the ownership of log + files; it now only does so if they are not in the /dev + directory (i.e. don't want to change the ownership and + permissions of /dev/null...) + - Added libpaper support (patch from Jeff Licquia) + - Added a new istring() rule for MIME types files that + does a case-insensitive comparison of strings. + - The cups-lpd mini-daemon now sends jobs to the default + queue when an empty queue name (or "lp" and there is + no "lp" queue) is sent. + - The scheduler now supports fax queues identified by a + "*cupsFax: True" attribute in the PPD file. When a job + can't be sent, it is held for 5 minutes by default + while other jobs are attempted. The FaxRetryLimit and + FaxRetryInterval directives control the number of + retries and the time between retries. + - The scheduler now preserves the default options of PPD + files when modifying/upgrading an existing PPD file. + When installing a new printer, the scheduler sets the + default media size to Letter or A4 as appropriate for + your locale. + - The scheduler no longer limits the number of + BrowseAddress, BrowsePoll, BrowseRelay, Listen, Port, + SSLListen, and SSLPort directives to 10. + - The scheduler now supports print files that have been + compressed using gzip. + - The scheduler used the stdio functions to read any job + ticket information in a PostScript print job. Since + some platforms limit the number of stdio files to 256, + job ticket information was ignored when the server had + a large number of clients connected to the system. + - Filters and backends may now report the total number + of pages ("PAGE: total NNN") to the scheduler. + - The LPD backend now supports timeout and + sanitize_title options (default to 300 and yes, + respectively) and has some additional changes to + reduce the chances of multiple copies being printed + when only one copy was requested. + - Fixed a polygon drawing bug in the HP-GL/2 filter. + - Added a robots.txt file to the standard install to + prevent search engines from indexing the CUPS server. + - Added support for STATE: messages + (printer-state-reasons), printer-state-history, and + printer-state-time to the scheduler. + - When using RunAsUser, the scheduler would initially + start any previously queued (pending) jobs with + RunAsUser disabled - all backends would be running as + root. + - If a backend failed for a printer, CUPS would + incorrectly requeue the job for printing again. + - Added support for IPP collections and files. + - Added experimental support for generic CGI scripts and + programs, Java, Perl, PHP, and Python to the + scheduler. See the file "CGI.txt" for more + information. + - The CUPS API now supports HTTP cookies and the Expect: + field. + - The cancel command now correctly supports the "-u + user" option to cancel all jobs for the named user. + - The Purge-Jobs operation now supports the my-jobs + boolean attribute and a new purge-jobs boolean + attribute to control whether job history data is + purged from the scheduler; the default is false for + my-jobs and true for purge-jobs to match the original + implementation. + - The scheduler would not timeout printers when only + using SLP browsing. + - If the scheduler was unable to execute a filter, it + would try to restart the job indefinitely until the + filter could be executed. + - When writing BSD printcap files, the scheduler now + includes the rm and rp attributes, allowing the file + to be exported to LPD clients. [Patch from Dominic + Kubla] + - The scheduler optimization to reference IPP attribute + data instead of performing a full copy caused problems + when the referenced data was deleted before it was + sent. It now only references attributes that change + only when the scheduler is restarted. The change also + reduced the memory footprint of a printer object to + 2k. + - The scheduler now holds signals while logging messages + to avoid potential deadlock issues when handling + signals on Solaris 8. + - The lpadmin command now allows printer access control + by group name as well as user name. + - "lpoptions -l" got in an infinite loop if no default + printer was available. + - The scheduler now logs the job-originating-host-name + attribute in the page_log file, and uses "-" for any + empty fields (patch from Dominik Kubla). + - The pdftops filter now scales PDF pages within the + printable area of the page. + - The pstops filter didn't include the page-label and + classification boxes when printing EPS or non- + conformant PS files. + - The imagetops filter didn't always correctly position + the image on the page when printing in landscape + orientation. + - The ppdEmit() functions now support the + RequiresPageRegion attribute when sending InputSlot + and ManualFeed commands. + - The PPD loading code now supports standard options + outside of OpenUI/CloseUI as required by the PPD spec. + - The cupstestppd program has been upgraded to provide a + concise PASS/FAIL report, additional detailed + conformance testing, and support for gzip'd PPD files. + - The PPD loading code is now much more strict when + loading a PPD file, and tracks more format errors. + - The scheduler ignored child signals when gathering the + list of available devices, when it should have been + using the default signal handler. + - The cupsEncodeOptions() function could encode an + option with a NULL last string. + - The socket backend could report the wrong number of + backchannel bytes if an error occurred on the link. + - The cups-polld program now only sleeps after getting + all printers and classes. This allows for longer + intervals without excessive delays before classes show + up... + - Added a new httpWait() function to support waiting for + data for a specific number of milliseconds. + - httpGets() now times out after 1 second on + non-blocking HTTP connections. + - The scheduler no longer accepts rangeOfInteger values + that are out of order (e.g. 5-1) + - The sides attribute was incorrectly sent as a name + value; it is a keyword value. + - The IPP backend now detects if the destination queue + has gone away and reports an error. + - The scheduler and HTTP API now allocate their select() + sets to support larger numbers of clients on systems + that support it. + - The scheduler now sets the CFProcessPath environment + variable under MacOS X. + - The cupsLangDefault() function now uses the + CoreFoundation localization API under MacOS X. + - The httpSeparate() function didn't handle file URIs of + the form "file:///path" properly. + - The lpadmin command now supports a "protocol" option + for specifying the binary communications protocol to + use when printing binary PostScript data. + - The scheduler did not properly parse the SystemGroup + directive, so only the first group would be used. + - Revamped how strings are stored in the scheduler, + providing a substantial improvement in memory usage + for systems with large numbers of printers. + - The PostScript filter now supports binary PostScript + files and files beginning with the PJL language escape + sequence. + - The PPD API now provides additional information from + the PPD file. + - The USB backend didn't compile on Solaris Intel. + - The cupstestppd utility now supports the "-q" option + (quiet) for use in scripts, etc. + - Merged several weight-reducing changes into the CUPS + baseline donated by Apple. + - Added preliminary support for CDSA; patch provided by + Apple. + - Implicit classes are now created from identical + printer classes on the network. + - The lp command now supports a "-H restart" option to + restart previously printed jobs. This functionality + only works if you have enabled the PreserveJobFiles + option. + - The scheduler now supports URIs in HTTP request lines + to conform to the HTTP/1.1 specification. + - The time-at-xyz attributes were not recognized in + banner files if prefixed by a question mark, e.g. + "{?time-at-creation}". + - Added support for pre-filtering application/pictwps + files on MacOS clients before sending them to a server + via IPP. + - The scheduler now allows file:/dev/null device URIs + even if FileDevices is set to No. + - CUPS uses strerror() for hostname resolution errors, + when it should have used hstrerror(). + - The USB backend no longer tries to guess the serial + number of a device from the USB devices file; this + means that printers that don't report their serial + numbers in the device ID string will not be + individually selectable. + - The pstops filter didn't handle page ranges properly + when a page contained an embedded document. + - Added a translation of the web interface to German. + - When printing using the OutputOrder=Reverse option + with duplexing, the output order is now truly + reversed; the order of sub-pages when printing N-up is + the same. + - The pstops filter did not always output the extra + blank page when printing a document with an odd number + of pages with duplexing enabled. + - The ippAddXYZ functions no longer allow the + application to add less than 1 value. + - Fixed a URL rewrite bug in the web interface - local + access was sometimes redirected away from localhost... + - The ppdOpen() functions could get in an infinite loop + if the PPD file contained a keyword or text that was + too large for the buffer. + - Added preliminary support for GNU TLS; patch provided + by Jeff Licquia. + - Now timeout IPP attribute reads after 1 second inside + an attribute definition. + - Now timeout connections that have been shutdown (due + to errors) after 30 seconds instead of the Timeout + setting (300 seconds by default). This provides + faster recovery from DoS attacks. + - A denial-of-service attack warning message was being + written to the log files by the scheduler for every + detection. This caused a DoS of its own in some + situations. The warning message is now written no more + than once per minute. + - Fixed the CIE colorspace support code in the image and + PS RIPs. + - The job-quota-period, job-page-limit, and job-k-limit + attributes were not flagged as integers, so setting + quotas would not work. + - Added an additional response check in the scheduler to + more quickly recover from denial-of-service attacks. + - The cupstestppd file was incorrectly installed in the + /usr/sbin directory instead of /usr/bin. + - The EPM list file did not include the cupstestppd + program or man page files. + + +CHANGES IN CUPS V1.1.18 + + - Fixed a bug in the Set-Job-Attributes code in the + scheduler that would cause it to crash or continuously + write a job control file. + - SECURITY FIX: The scheduler now provides a FileDevice + directive to control whether new printers can be added + using device URIs of the form "file:/filename". The + default is to not allow printers with these device + URIs. + - The scheduler did not compute the cost of filters + properly, nor did it choose a multi-filter solution + with a lower cost than a single filter solution. + - Now install CUPS PPD file test utility (cupstestppd) + to support basic conformance testing of PPD files. + - The scheduler now logs an error message when it sees a + non-conforming PPD file. + - Upgraded pdftops filter to Xpdf 2.01 with fixes for + TrueType fonts. + - Added a MaxClientsPerHost configuration directive to + provide limited protection against Denial of Service + attacks. + - SECURITY FIX: Potential underflow/overflow bug in web + interface. + - SECURITY FIX: Race condition in certificate creation. + - SECURITY FIX: Bad URIs in browse packets could be used + to exploint the web interface underflow/overflow bug. + - SECURITY FIX: Some types of Denial of Service attacks + were not handled properly, so once the attack was over + the scheduler did not close the connections + immediately on all platforms. + - SECURITY FIXES: Added integer overflow/underflow + checks for all image formats. + - The pstops filter didn't reset the showpage operator + back to its original at the end of a job; this + prevented the concatenation of documents (used + primarily for CUPS 1.2...) + - The cupsGetPPD() function didn't always set the + cupsLastError() value when an error occurred. + - The IPP media, output-bin, and sides attributes took + precedence over the corresponding PPD options, which + caused inconsistent behavior under MacOS X with some + PPD files. + - The cupsaddsmb utility specified the wrong number of + arguments to the adddriver command when adding the + Win9x PostScript drivers. + - The web interface did not always report the correct + error message. + - The scheduler did not clear the POSIX signal action + structure when waiting for the child to send it a + SIGUSR1 signal; this could cause the signal handler + not to be called properly, preventing the parent + process from returning. + + +CHANGES IN CUPS V1.1.17 + + - The "manual_copies" option did not work when the LPD + backend had to retry a print job. + - The image filters did not convert GIF images properly. + - The RunAsUser option was incompatible with the new + daemon-mode code in 1.1.16. + - Fixed a problem with the Set-Job-Attributes and + PostScript job ticket code in the scheduler - the + "last" attribute pointer was never updated, which + could cause the scheduler to crash when applying job + ticket data. + - Fixed a problem in the scheduler that caused it to + continue processing HTTP requests on a connection + after it was shutdown. + - The scheduler now allows accounts authenticated via + PAM to not have a corresponding UNIX account, but + group membership still requires the account name to be + listed in the UNIX group file(s)... + - The scheduler used a fixed-size (16k) buffer for + encoding job options for filters; it now dynamically + allocates and expands the buffer as needed depending + on the options that are sent in a job. + - The pdftops filter didn't support all of the MacOS + characters for MacRoman encoded fonts. + - The cupsEncodeOptions() and cupsParseOptions() + functions now conform to the grammer defined by the + current draft of the PAPI specification. The main + difference is that option=yes and option=no are no + longer treated as boolean options. + - The IPP backend didn't honor the encryption settings + in /etc/cups/client.conf. + - Fixed a potential bug in the HTTP code which was + caused by servers sending the status line and + newline(s) in separate packets. + - User-defined classification strings are now printed + verbatim - previously the classification box would be + empty. + - Re-added Spanish to the list of PPD languages that + CUPS supports. + - CUPS API library user and temp file updates for + Windows. + - The image filters did not properly handle grayscale + printing of Sun Raster images. + - The scheduler never reset the NumJobs variable before + loading the job list (previously this only happened on + a full start, so the problem was never apparent...) + - The HTTP and IPP read/write code didn't handle EINTR + (interrupted system call) errors. + - When under high load, the scheduler could abort due to + the wrong errno value after a select() call. This was + caused by the child signal handler. + - Added new load tests to the test target to verify that + cupsd can handle hundreds of simultaneous jobs without + error. + - The Solaris USB backend now supports the new device + URI syntax. + - The ppdOpen*() functions now reset the numeric locale + settings while loading a PPD file. + - Fixed the libtool build rules. + - The manpage make rules didn't use $(MAKE) and + $(MFLAGS) for the language subdirectories. + - Now set the LC_TIME locale category to get the + properly localized time string. + - Fixed a problem in the scheduler that would cause the + web interface problems when adding, modifying, or + configuring a printer or class. + - The backends now ignore SIGPIPE so that failed job + filters will not stop a print queue. + - The lpstat command did not allow for destination lists + ("lpstat -v printer1,printer2") + - Fixed parsing of long filter status messages in the + scheduler. + - Added some startup performance enhancements to the + scheduler so that the printer object information is + regenerated fewer times and the MIME type database is + not filled with lots of empty filters for raw/direct + queues. + - The LPD backend now sends the job title as the print + filename. + - Added support for variable sizes in the EPSON dot + matrix printer drivers. This allows for pages as + short as 1/2" (1 row of labels) and does not do an + automatic form feed. + - French translation updates. + - The filters did not quote the page label string when + embedding it in PostScript output. + - The serial backend now enumerates serial ports under + MacOS X. + - The pdftops filter contained font rasterizer code that + wasn't being used and that depended on X11. This code + has been removed. + + +CHANGES IN CUPS V1.1.16 + + - The cancel and lprm commands now both display an error + message and return a non-zero exit status if an + attempt is made to cancel a job on a non-existent + printer. + - The lpoptions command incorrectly complained if a + request to delete a non-existent printer was made. + - If the client.conf file defines an alternate server + name, the "configure printer" action in the web + interface might not work. + - The lpstat command now supports a "-W" option so that + you can display completed jobs as well as + not-completed (pending) jobs. + - The lp and lpr commands did not return an error when + one or more files in a set of files for printing could + not be printed. + - The lp, lpadmin, and lpstat commands now consistently + return with a non-zero exit status when an error + occurs. + - The scheduler would not accept print jobs sent to a + stopped remote printer. + - The texttops filter incorrectly converted the page + numbers in the prettyprint header to double-byte + characters when printing a non-Unicode text file. This + caused an extra space to appear between each digit in + the page number. + - The scheduler did not use a case-insensitive + comparison when adding filters for a printer. + - Upgraded the pdftops filter to Xpdf 1.01. + - The scheduler no longer passes the page-border and + number-up-layout attributes to filters when printing + banner pages. + - The LPD backend now uses a 30-second timeout when + sending commands and control files, and a 30-second + timeout when retrieving responses from an LPD server. + If a timeout occurs, it retries indefinitely. This + helps to make LPD printing over VPNs work more + reliably. + - The USB backend now supports device URIs based on the + printer serial number and/or model number under Linux. + This avoids the "wrong device filename" problem when + using more than one USB printer. + - Now just shutdown the receiving end of a client + connection when sending an error that requires the + server to disconnect from the client afterwards. This + fixes a problem when doing remote administration with + encryption enabled. + - The scheduler did not send a printer-state-message + attribute if the string was empty; it now always sends + this attribute. This caused the printer message to be + displayed for other printers in the web interface. + - The LPD backend now supports a "manual_copies" option, + e.g.: "lpd://server/queue?manual_copies=no", in order + to handle copies for raw jobs to printers that don't + implement the LPD protocol properly... + - The "mirror" option was not being handled by the + PostScript or image filters. + - Updated the cupsaddsmb command to support the new CUPS + driver for Windows NT/2k/XP. + - Filter status lines longer than 1023 characters could + cause the scheduler to get into an infinite loop. + - The scheduler didn't reset the job state to pending + when modifying an active printer. + - Now limit the maximum number of recursion steps when + searching for a filter for a job, in case a user + defines a circular filter rule. + - The PostScript filter would embed an invalid + requirements comment in some cases. + - Added support for embedded job tickets in PostScript + files. + - The PostScript filter now detects EPS files and should + better handle printing EPS files. + - The cancel command now ignores a trailing destination + name when cancelling a specific job ID (Solaris + compatibility). + - The scheduler now rejects jobs with copies outside the + range of 1 to MaxCopies, inclusive. + - Added new MaxCopies directive to set the maximum + number of copies that a user can request. + - The scheduler didn't block signals while it processed + others and when it forked processes. + - The scheduler checked for new jobs to print when + stopping a job. This caused jobs to restart before a + shutdown. + - Updated the CUPS startup script to better support + different timezones and to support the RedHat/Mandrake + init script functions, if available. + - The scheduler did not properly handle backslashes in + banner files; it incorrectly assumed that "\c" should + always be replaced by "c", instead of only looking for + "\{" and replacing it by "{". + - The texttops filter didn't handle prettyprint=no. + - The text and HP-GL/2 filters didn't check for other + common duplex option names like cupsMarkOptions() did. + - "lpoptions -x printer" no longer clears the "default + printer" status of the printer. + - cupsTempFd() now stops trying to create a temporary + file after 1000 tries, and aborts on any error other + than EEXIST. This should prevent lp/lpr hangs due to + a bad or missing temporary directory. + - The lpadmin command did not send the right URI to the + scheduler when setting options on classes. This + caused a client-error-bad-request error. + - The CUPS API convenience functions would attempt to + connect to the remote server name in a + "printer@server" printer name instead of dealing with + the default (usually local) server. Aside from + causing user confusion, the remote server name might + not be resolved properly, causing further problems. + - "lp -q" would cause the "lp" command to segfault, as + the program would try to print the option letter that + caused the error using the wrong index into the + command-line; bugfix from Debian. + - Fixed a minor inconsistancy in the encoding of boolean + attributes from printer options in + cupsEncodeOptions(). + - Added a FilterNice directive which sets the priority + of job filter processes that are run by the scheduler. + - Added Solaris x86 USB printer support. + - The USB backend now reports both the ulpt and unlpt + devices under *BSD. + - The "lpstat -o" command would truncate the + "printer-jobid" string if it was longer than 21 + characters. + - The PJL-based MIME type rules now look in the first + 1024 bytes instead of just the first 512 bytes to find + the language mode. + - The image file types are now listed explicitly in the + mime.convs file so that additional image file formats + do not use the standard CUPS image filters by default. + - Updated the Software Programmers Manual to include + all of the CUPS API functions. + - ppdOpen*() no longer sorts choices for an option. + - The web interface now enforces constraints in PPD + files when configuring a printer. + - When stopping a printer, the scheduler didn't set the + printer state before stopping the current job. + - The cupsaddsmb utility now lists all data files for + Win9x and WinMe clients when installing that Windows + driver. + - Jobs submitted to a class now bounce immediately to + the next available printer rather than waiting until + that printer is available. + - Filters and backends now also get the CLASS + environment variable set when a job is printed to a + printer class instead of a normal printer. + - Added French translations of the web interface, CUPS + Overview, Software Administrators Manual, and Software + Users Manual contributed by Marian REYT-LLABRES. + - Added several "hint" messages for common configuration + problems that are stored in the error_log file. + - httpSeparate() now unquotes %xx characters in the + username:password field of a URI. + - When starting the scheduler in daemon mode, the parent + process now waits for the child to signal it is ready + to accept connections. + - Added -F option to cupsd to run cupsd in the + foreground but detach from the controlling terminal + and current directory. + - The scheduler did not reload jobs when receiving a HUP + signal; this would cause problems since the pointers + into the file type database would no longer be valid + for existing jobs. + - The scheduler did not save the network interface list + update time, thus no caching of the network data was + actually provided. + - Updated the SuSE PAM configuration file. + - The LPD backend now supports a "reserve" option and no + longer reserves a priviledged port by default. + - The cupsaddsmb command now continues past printers + that do not have a PPD file to export. + - The lpstat command didn't treat printer names as + case-insensitive. + - The lpstat command now reports the printer location + attribute with "lpstat -l -p". + - Fixed a bug in the vsnprintf() emulation function, + which was used on old versions of HP-UX, IRIX, and + Solaris. + - The number-up option was incorrectly being used when + printing banner pages. + - Added support for Greek and Slovak PPD files. + - CUPS now supports printer names containing any + printable character, e.g. "123-abc", "foo-bar", etc. + - The null filter was not supported in mime.convs due to + a bug in the filter validation code. + - Changes in the default printer and printer attributes + were not always reflected in the generated printcap + file. + - Implicit classes did not inherit the location or + description from member printers. + - The httpGetHostByName() function did not handle + hostnames that started with a number. + - Updated the filters to use the %cupsRotation comment + instead of %%Orientation to auto-rotate pages, since + the use of %%Orientation is inconsistent. + - Added the RootCertDuration directive to control how + often the root authentication certificate is updated. + - Increased the size of the IPP write buffer to 32k to + allow for larger attribute values and to provide more + efficient output of large numbers of attributes. + - The polling daemon now retries the initial connection + to the remote server; this fixes a problem when the + remote server is unavailable when the scheduler starts + up... + - The scheduler didn't validate Digest users against the + system group(s), so Digest and BasicDigest + authentication didn't work for administration + operations. + - The scheduler now passes the SHLIB_PATH environment + variable to child processes (HP-UX shared libraries) + - The scheduler now maps accesses from the loopback + interface to "localhost". + - The cups-lpd mini-daemon sent a status code byte in + response to queue state commands, but those commands + only return textual data. + + +CHANGES IN CUPS V1.1.15-1 + + - The lpc and lprm sources didn't include the CUPS + string function header, which is required on systems + that don't have their own snprintf() function. + - The French manpage Makefile tried to install the + language subdirectories when it (obviously) didn't + have to. + + +CHANGES IN CUPS V1.1.15 + + - Updated the CUPS license agreement for the new MacOS + license exception. + - The printer-info attribute now defaults to the printer + name if no value has been set. + - ppdOpen() and friends now add an "Auto" InputSlot + option if none is provided to automatically select the + correct tray. + - Updated the ppdEmit() and ppdEmitFd() functions to + (re)mark the correct PageSize or PageRegion option + depending on the selected ManualFeed or InputSlot + options. + - ppdEmitFd() didn't handle custom page sizes. + - Darwin uses instead of + . + - The jobs.cgi web interface now handles all job + operations, allowing the administrator to allow "job + administrators" or operators to manage jobs (but not + queues) on the server. + - The cupsDoFileRequest() function now checks if the + filename passed into the function is a directory, and + returns the IPP_NOT_POSSIBLE error if so. + - New SCSI printer backend. + - Cleaned up handling of locales with trailing character + set definitions. + - Fixed handling of invalid PPD attributes inside + OpenUI/CloseUI. + - Fixed a problem with SSL and the job, printer, and + admin CGIs on ports other than 443. + - The scheduler didn't handle AuthClass properly. + - Added French translation of man pages. + - Updated the text filter to support the const_cast, + dynamic_cast, and static_cast keywords in ISO C++. + - Now use strlcat() and strlcpy() (or emulation + functions) for easier string/buffer protection. + - The auto-generated printcap/printers.conf files now + have a small comment header explaining where the file + comes from... + - The PostScript filter now supports 6, 9, and 16-up + output, as well as new page-border and + number-up-layout options. + - The lpoptions command didn't set options properly when + using the default printer. + - Added ConfigFilePerm and LogFilePerm directives. + - Increased maximum size of MIME types to IPP_MAX_NAME + to allow for longer printer names. + - No longer create remote printers when loading job + history data. + - The printer-make-and-model attribute wasn't set when + the PPD file didn't contain a NickName attribute. + - Now handle PPD files with translation strings longer + than 80 bytes - they are truncated if they go over... + - The scheduler didn't handle signals until after it + loaded the configuration files the first time; this + caused problems on some installations that would + restart the scheduler as the system booted into run + level 3. + - Now throttle broadcasts like we do for polling. + - Fixed a bug in the reading of PPD files using CR's + instead of CR LF's or LF's. + - The scheduler would crash if cupsd.conf contained a + BrowseProtocols line with no protocols listed. + - The HTML job operation templates now link back to the + destination printer or class. + - The serial backend now detects USB serial devices. + - The LPD mini-daemon (cups-lpd) now passes the + job-originating-host-name attribute to the scheduler + (cupsd). + - Updated the IPP backend to reconnect after downgrading + from IPP/1.1 to 1.0, and when sending requests to HP + JetDirect interfaces that don't support HTTP + Keep-Alive like they should. + - Now pass NLSPATH and DYLD_LIBRARY_PATH environment + variables, if defined, to CGI and job processes. + - Removed the pstoraster filter (based on GNU + Ghostscript 5.50) and now provide the raster "driver" + and patch file necessary to use the current GNU + Ghostscript 7.05 release. + - Removed unnecessary fonts and updated the Courier and + Symbol fonts to the latest versions to better support + non-ISOLatin1 text. + - The text filter now always embeds the Courier and + Symbol fonts to ensure that they contain the full set + of glyphs. + - The lp and lpr commands now only override the SIGINT + handler if it is not being ignored (patch from Robert + Ambrose for some interactive software that catches + SIGINT and will gracefully cancel the print...) + - The PostScript image filter (imagetops) now supports + printing CMYK images using the CMYK colorspace. + - The image filters now support CMYK JPEG files, and + correctly handles the inverted files from Photoshop + (which seems to save RGBW data, not CMYK...) + - Added a "check" target to the top-level makefile to + conform with GNU standards (same as "test"). + - The IPP code didn't always map the POSIX locale "C" to + the proper IPP language code. + - The cupsaddsmb program was updated to use the + setdriver command instead of addprinter. + - Banner pages were not handled properly for implicit + classes. + - When tunneling to a remote system using SSH, the + printer URIs for local printers on the remote system + did not reflect the correct port number. + - The Allow, Deny, BrowseAllow, BrowseDeny, and + BrowseAddress directives now support the network + interface names "@LOCAL" and "@IF(name)" for access + control and browsing based on the current interface + addresses instead of fixed names or IP addresses. + - The texttops filter did not properly recognize the + "nowrap" (wrap=false) option. + - The InstallableOptions group name in a PPD file is now + translated separately (CUPS_MSG_OPTIONS_INSTALLED) so + that UIs can accurately detect the presence of this + group. + - The scheduler no longer keeps job history data for + remote printers on the client (just on the server.) + - The parallel and USB backends now retry if the backend + detects that the printer is not connected to the + system (rather than stopping the queue...) + - The network backends now retry if the backend detects + that the printer is not connected to the network or is + unreachable (rather than stopping the queue...) + - The cupsGetDests() function no longer lists options + and instances for printers that no longer exist. + - The scheduler now converts the document language to + the correct LANG string. + - The cupsaddsmb program now supports alternative CUPS + and SAMBA server names. + - The PostScript filter now supports the Orientation + comment and rotates the page as needed automatically. + - Revamped the makefiles slightly to use automatically + generated dependencies. + - Build fixes for OS X. + - The TIFF reading code depended on the newest version + of libtiff; now conditionally compile that portion of + the loader. + - The PPD code now decodes all JCL options in the + JCLSetup group, not just those options that start with + the prefix "JCL". + - The backends now read print data using the read() + system call to ensure that the current page is printed + while the next page is being processed. + - The pdftops filter did not support shading type 3 + (radial fill) for the "sh" operator. + - The cups-polld program now throttles the local + broadcasts of polled printers and classes so that the + local system is not overwhelmed with hundreds of + printers and classes all at once. + - Updated the serial backend to support 230,400 baud for + the Linux PPC port. + - The cupsGetJobs() function wouldn't report completed + jobs that did not have a document-format attribute + value. + - The cupsEncodeOptions() function now maintains a table + of known boolean and numeric options, and encodes all + other options as strings. + - Now add a newline before the end-of-page code in the + PostScript filter; this fixes a problem with files + that don't end with a newline. + - The image filters looked for the "orientation" option + instead of the correctly named "orientation-requested" + option. + - The cupsEncodeOptions() function now handles mixed + integers and ranges. + - New translation guide for developers to provide native + language support for CUPS. + + +CHANGES IN CUPS V1.1.14 + + - The ippRead() function did not verify that the + attribute name length or string with language value + was not larger than the read buffer. + - The scheduler set the signal handlers before loading + the configuration files the first time; this prevented + the RunAsUser directive from blocking server reloads. + - Added Swedish message catalog. + - The parallel backend now recognizes the /dev/printers + device directory under Linux 2.4.x. + - MacOS X fixes. + - The cupsaddsmb utility sent the server name after the + user information when executing the rpcclient program. + This caused problems with some versions of SAMBA + 2.2.x. + - The IPP backend did not pass the requesting user name + when checking on the print job status. This prevented + it from waiting for the job to complete when + communicating with some IPP implementations that + require it. + + +CHANGES IN CUPS V1.1.13 + + - The lpstat command did not report jobs submitted to + regular printer classes. + - The texttops filter didn't use sufficient precision + when positioning text with some values of cpi and lpi. + This could cause the alignment of text to stray. + - cupsGetDests() didn't merge the options from the + /etc/cups/lpoptions file with ~/.lpoptions - options + in ~/.lpoptions overrode them completely. + - Added support for KOI8-R and KOI8-U character sets, + and added several Russian message catalogs. + - The scheduler put the wrong timezone offset in the log + files (e.g. +0500 instead of -0500 for EST...) + - The scheduler did not ignore trailing whitespace in + *.convs files. + - The scheduler now forces all processes to exit (kill + -9) when it is stopped. This prevents parallel and + USB devices from running in the background after cupsd + goes away. + - The cupsParseOptions() function didn't skip trailing + whitespace after quoted values. + - More changes to support CUPS on OS/2. + - Added Simplified Chinese message catalog. + - Added PAM support for IRIX. + - The cupsGetPPD() function didn't remove the @server + portion of the printer name, and since it would + connect immediately to the remote server instead of + the local server, the printer would not be found. + - Classification and page labels were not rotated to + match the page orientation. + - Now set the TCP "no delay" option on network + connections to improve performance/response time. + - Improved the IRIX printing tools support with patches + from Andrea Suatoni. + - Added a new PrintcapGUI directive to specify the GUI + option panel program to use for the IRIX printing + tools support. + - The cupsGetDests() function did not check to see if a + user-defined default printer (set via lpoptions) still + existed. + - The pstops filter no longer assumes that the default + dictionary is writable when doing N-up processing. + - The pstops filter now supports printing N-up with the + page-set option. + - The imagetoraster filter now supports direct printing + of CMYK image data without conversion/correction. + - The IPP backend now reports printer state/error + conditions when possible (toner low, media empty, + etc.) + - The lpstat command now supports the (undocumented) + IRIX -l option ("-lprintername") for a compact job + listing for a printer. + - The lpstat command now includes printer date/time + information in the output (always Jan 01 00:00) to + make third-party tools happy. + - The text filter now supports non-integer cpi and lpi + values. + - The Margins field in the CUPS raster header was not + initialized by the pstoraster filter. + - Added --with-optim="flags" option to configure script. + - Updated the Italian message translations. + - Updated the cups.list file to install the correct + files. + - The pstoraster filter accessed the third element of a + 2 element array. + - The scheduler did not setup a status pipe for polling + processes, so error messages went to whatever file + descriptor 2 was pointing to when they were started. + - The httpMD5Final() function didn't put a colon between + the password and nonce strings. + - The pstops filter did not default to Binary data for + "%%BeginData:". + - The pstops filter did not stop processing when a line + containing a CTRL-D is seen. + - The scheduler no longer replaces the JobSheets values + from the printers.conf and classes.conf files with the + classification level, if set. This way the original + banner settings are preserved when classification + levels are changed or turned off. + - The serial backend didn't drain the output queue, nor + did it restore the original settings. + - Updated the default system group under MacOS X. + - If no SystemGroup was defined in cupsd.conf, the + system default group was not used. + - The cups-lpd mini-daemon now supports LPD clients that + send multiple control files. + - httpConnectEncrypt() now always uses encryption for + connections on port 443, since port 443 is reserved + for the "https" scheme. + - Group authentication via certificates did not work + from the web interface for accounts other than + "root". + - The serial port backend did not clear the OPOST + option, which could cause problems with some printers. + - The cups-lpd mini-daemon didn't lookup the client IP + address properly. + - The parallel backend now identifies the polled and + interrupt-driven devices under *BSD. + - The scheduler allowed the "always" encryption mode + inside a Location, which is not valid. + - The CUPS startup script now checks for the timezone + information under Linux. + - Now also map the sides attribute to the JCLDuplex + option (if present) in PPD files. + - Updated pdftops to Xpdf 0.93a. + - Added support for MD5 passwords under Slackware. + - Added new AuthType BasicDigest that does Basic + authentication using the MD5 password file managed by + the lppasswd command. + - The banner page attribute substitution code now + retains {name} sequences in banner files when the + named attribute is undefined. Use {?name} to + conditionally substitute an IPP attribute. + - The scheduler now ensures that the ServerRoot + directory and configuration files are owned by and + writable by the User and Group in cupsd.conf. + - The USB backend now lists all USB printer devices + regardless of whether a printer is connected or not. + This allows new USB printers to be connected without + restarting cupsd. + - Added some more minor performance tweeks to the IPP + protocol code to reduce copying and array indexing. + - The cupsaddsmb utility now uses the -c option with + smbclient and rpcclient to avoid the read length limit + for commands on the standard input. + - Added an include file to the CRD handling code in + pstoraster so that it would compile properly on 64-bit + pointer platforms... + + +CHANGES IN CUPS V1.1.12 + + - Added "Polish" to the list of known languages for PPD + files. + - Added missing directory definition to cups-config. + - The CUPS-Move-Job operation did not set the + destination type for the new destination. + - The CUPS-Add-Printer operation did not support the + allow=all or deny=none values to clear the per-user + printer ACLs. + - The SetPrinterAttrs() function did not handle invalid + PPD files that were missing the required NickName + attribute. It now looks for NickName, ModelName, and + then substitutes the string "Bad PPD File" for the + printer-make-and-model attribute. + + +CHANGES IN CUPS V1.1.11 + + - Added support for embedded TrueType fonts in PDF + files. + - Added support for PostScript functions in PDF + files. + - Added new "cupsaddsmb" utility for exporting + CUPS printer drivers to SAMBA/Windows clients. + - Added preliminary support for Darwin/MacOS X. + - The CUPS-Add-Printer operation no longer allows + arbitrary scheme names in device URIs to be used - it + now restricts the available schemes to those found in + the device list (lpinfo -m). + - The ippRead() and ipp_read_file() functions could not + handle more than IPP_MAX_VALUES (100) values in a + 1setOf attribute. These functions have been updated + to dynamically allocate more memory as needed, and the + IPP_MAX_VALUES constant now represents the allocation + increment. [this caused some versions of the + GIMP-print drivers to fail since the number of media + options exceeded 100...] + - The scheduler could crash when BrowseShortNames + was set to "No". + - The scheduler did not prevent MaxClients from being + set to 0, which could cause the scheduler to go in an + infinite loop when accepting a request. + - Made some performance optimizations in the ippRead() + functions to make IPP request/response processing + faster. + - The accept/reject/enable/disable command did not + support properly support the "-h" or default + server name. + - The scheduler did not save the quota configuration + when the job-quota-period attribute was set to 0. + - The LPDEST and PRINTER environment variables did not + support printer instances. + - The text filter now handles more types of boldface and + underline formatting. + - The cupsTempFd() function did not fail if the + temporary directory did not exist; this would cause it + to loop indefinitely instead of returning an error + (-1). + - Stopping (disabling) a printer class did not stop jobs + from printing to printers in that class. + - The cupsGetDests() function was sending the + requested-attributes attribute as a name instead of a + keyword; this caused a serious performance problem on + slower systems since more information had to be + transferred from server to client. + - The web interfaces did not always quote < and & in + things like the job title. This had the potential for + browser-based security violations (on the browser's + machine); bug report from SuSE. + - The scheduler now treats unauthenticated usernames as + case-insensitive when doing quota and allow/deny + processing. + - The lp command sent the "request ID is ..." message + to stderr instead of stdout... + - The PostScript filter (pstops) now handles EPS files, + adding a showpage command to the files as needed. + - The configure script checked for the header + file before the JPEG libraries; since the JPEG headers + can define HAVE_STDLIB_H, the configure check would + cause the JPEG check to fail on some systems. + - The scheduler now supports localized banner files, + using the subdirectory approach, e.g. the "es" + subdirectory under /usr/share/cups/banners is used for + the Spanish banner files. + - Updated the scheduler so it knows the correct + language abbreviation to use for all supported + PPD LanguageVersion values. The new code also + supports country codes as well, so "English-GB" + maps to the "en_GB" locale. + - The cups-lpd mini-daemon did not support + anonymous printing (no username specified). + While the username is REQUIRED by RFC-1179, + MacOS clients do not send the REQUIRED username + information when printing via LPD. + - Added many warning and informational messages + to cups-lpd where they were missing. + - Added Czech message file contributed by SuSE. + - The cups-lpd mini-daemon now returns a non-zero + status if an invalid destination or job ID is + provided. + - The scheduler did not honor the KeepAlive setting in + cupsd.conf. + - Increased the size of the file read/write buffers to + 32k. + - *BSD static library creation fixes. + - Use mkstemps() instead of tmpnam() in pdftops whenever + possible. + - Added httpGetHostByName() function as a wrapper around + gethostbyname() - some implementations of this + function do not support IP addresses (e.g. MacOS X.) + - Added casts to all printf's of file lengths, since + there is currently no standard way of formatting long + long values. + - The client filename field was not cleared in all + instances, resulting in old form data being submitted + to CGIs. + - The httpConnect*() functions now try all available + addresses for a host when connecting for the first + time. + - The pstoraster filter would "lose" all drawing + commands when the PageSize was set but the printer + bitmap was not reallocated. This was most noticeable + with the output from StarOffice 6 beta and would + result in a blank page being output... + - The IPP backend was sending a PAGE comment even when + printing the output from a filter (it should only send + page comments when printing files directly...) + - The pdftops filter didn't properly map glyph names of + embedded Asian TrueType fonts. + - Changed the CUPS startup script to look for a program + named "cupsd", not just any program with "cupsd" in + the name (this caused the apcupsd UPS monitoring + daemon to be stopped/restarted...) + - The CUPS-Move-Job operation did not change the + internal destination name for held jobs, so moved (but + held) jobs would still show up as queued on the + original destination. + - The cups-polld program didn't send the + requested-attributes attribute in the + CUPS-Get-Printers and CUPS-Get-Classes requests, which + made it use more CPU and bandwidth than required. + - The scheduler and CUPS API incorrectly added a + job-sheets-default attribute for remote printers. This + caused banner pages to be omitted from client system + prints. + + +CHANGES IN CUPS V1.1.10-1 + + - Minor fixes to the filter, systemv, and template + makefiles to install files properly. + + +CHANGES IN CUPS V1.1.10 + + - Added a driver for DYMO label printers. + - Added new ClassifyOverride directive to allow users + to override the classification of individual jobs. + - Added new BrowseProtocols directive to control which + browse protocols are used (currently CUPS and SLP). + - Added SLPv2 support (thanks to Matt Peterson for + contributing the initial implementation for CUPS.) + - Adding a raw printer on a remote CUPS server now + correctly redirects PPD file requests to the remote + server. + - The serial backend now limits writes to 1/10th + second worth of data to avoid buffer overflows + with some types of flow control. + - The scheduler did not properly process PUT requests, + so configuration files could not be uploaded to the + server. + - The scheduler did not strip trailing whitespace on + lines in the configuration files. + - The httpWrite() function did not transition the PUT + request to the HTTP_STATUS state to get the status + from the server. + - The scheduler did not properly handle trailing null + ("-") filters when testing a driver that sent data + to the file: pseudo-backend. + - The IPP backend now only sends a document-format of + "application/vnd.cups-raw" when printing to another + CUPS server using a local printer driver or interface + script. Previously the job's document format was + used, which was incorrect. + - The lpadmin command didn't use the ppd-name attribute + with the -m option; this prevented the use of the + "raw" model from the command-line. + - The pstoraster filter output draft (1-bit) 6-color + output in the wrong order; this resulted in yellow + being printed instead of black on Stylus Photo + printers. + - The pdftops filter did not have the Japanese and + Chinese text support compiled into it. + - The IPP and AppSocket backends did not clear the + "waiting for print job to complete" status message, + which caused some confusion... :) + - The serial backend now opens the port in "no delay" + mode to avoid DCD detection problems with some OS's. + + +CHANGES IN CUPS V1.1.9-1 + + - The configure script did not substitute the + correct user and group names. + - The configure script did not use the full path + to the install-sh script when it was used. + - The pstoraster filter did not correctly support + DuplexTumble mode for printers that used flip + duplexing. + - The cups.list.in file was missing from the + distribution. + - The New DeskJet series driver did not use the + correct OrderDependency for the Duplex option. + - Use read() instead of fread() to read piped + print files in lpr/lp. This avoids a bug in the + HP-UX 10.20 fread() function. + - Updated the pstoraster filter to use the MIPS_FIXADE + system call under IRIX to fix bus error problems on + R12000 processors (Ghostscript is not 64-bit clean...) + - Some Xerox PPD files (most notably the Phaser 790) + have illegal whitespace in the option keyword in the + OpenUI line. This caused the PageRegion option to not + be recognized properly for the Phaser 790. + + +CHANGES IN CUPS V1.1.9 + + - Revamped the configure script to use a modular + approach for the various tests. + - Added --with-openssl-* options to properly reference + the OpenSSL libraries in DSOs. + - Added --with-cups-user and --with-cups-group + options to specify the default user and group for + CUPS. + - Added AIX shared library support. + - Added AIX device discovery for the serial and + parallel ports. + - Now use install program or script to install + directories, files, and symlinks. + - Updated pstops filter to use strict handling of EPS + files embedded in a PostScript document. The %%EOF + handling in 1.1.8 caused some dvips files not to + print. + - Fixed yet another memory allocation bug in pstoraster + that would cause it to crash. This fix also ensures + that all memory allocations are done on (at least) a + 64-bit boundary. + - Fixed Digest authentication - httpGetSubField() didn't + skip the Digest keyword. + - The scheduler did not properly handle Digest + authentication with the new multiple-group support. + - The scheduler did not allow usernames that were + not in the UNIX password file to be used for Digest + authentication from passwd.md5. + - The scheduler could not scan PPD files that only used + a carriage return (i.e. MacOS PPD files); the new code + is also about 40% faster, so servers with thousands of + PPD files should start much faster now. + - The scheduler now stores the PPD file size and + modification times in the ppds.dat file, so it can now + incrementally update the PPD database from the model + directory, resulting in significantly faster startup + times. + - The lpinfo command did not return a non-zero status + code if an error occurred. + - Fixed a bug in the scheduler's UpdateJob() function. + Basically, all jobs shared the same status buffer, and + the "buffer start" pointer could point to 1 byte + before the beginning of the buffer. The new + implementation uses a separate buffer for each job and + eliminates the buffer start bug. + - The IPP backend would send N copies of a document if + the receiving device didn't support the copies + attribute, even if the upstream driver already added + the necessary commands to generate the copies. This + was most noticeable with HP printers where N * N + copies would come out instead of N. + - The PostScript filter (pstops) did not properly handle + duplex printing on inkjet printers that provide this + option. Copies would be put on the front and back + sides of the duplexed page, and the filter did not + output an even number of pages. + - The backends always caught SIGTERM after they + connected to the printer. This prevented raw jobs + from being cancelled early. + - The cupsSetDests() function now removes any printers, + instances, and options that are not defined by the + user or server. This should prevent old system-wide + options from being used in individual user accounts. + - Updated the EPSON printer driver and added PPDs for + the newer EPSON Stylus printers that only support the + "ESC i" graphics command. + - The lpadmin command didn't allow you to add remote + printers to a local class. + - The lpadmin command didn't allow you to set the + options (quotas, etc.) for a class. + - The scheduler did not load or save the + job-sheets-default attribute for classes. + - The scheduler did not automatically recreate remote + printers that were part of a class. + - It was possible for a printer class to list the same + printer more than once. + - The scheduler now makes a backup copy of classes.conf + and printers.conf before writing the new file. + - The lppasswd program incorrectly asked for a new + password when deleting an existing MD5 password + account. + - The scheduler did not match "/printers/name.ppd" + against a location of "/printers/name". + - The client code did not always handle HTTP encryption + upgrades properly. + - The client code now caches the last Digest password so + it can retry using a new resource path or nonce value, + which are included in the MD5 sum sent to the server. + This should eliminate unnecessary password prompts + when using Digest authentication. + - The lppasswd command didn't have a man page. + - Updated the PJL detection rules to allow the universal + escape to occur anywhere in the first 128 bytes of the + file. + - The cups-polld program would poll servers continuously + with no delay if there was an error contacting the + server. + - The IPP backend would send an empty job-name or + requesting-user-name attribute if the corresponding + job attribute was an empty string. While this is + allowed by the IPP specification, some HP JetDirect + implementations return a client-error-bad-request + error if an empty name attribute value is received. + The new code only sends these attributes if they are + not the empty string. + - At least some versions of the HP JetDirect firmware + do not correctly implement IPP. Added additional + checks to the IPP backend to eliminate extra, + unsupported attributes which should normally be + ignored by a compliant IPP device. + - The scheduler did not copy the complete list of + supported file types into the + document-format-supported attribute. This caused + clients to not send the local file type (such as + application/vnd.cups-raw for raw print files) and the + corresponding bad output in some cases. + - The scheduler did not fully copy attributes from a + set-job-attributes request - string attributes were + only referenced, which could cause cupsd to crash + or behave irratically. + - The lp command didn't send the right value for the + job-hold-until attribute when "-H resume" was + specified. + - The IPP backend now returns as soon as a job is + completed or reported as "pending-held". + - Added new ImplicitAnyClasses and HideImplicitMembers + directives to the cupsd.conf file to make implicit + classes more usable/transparent to the user. + - Clients can now (with the appropriate authentication) + retrieve and update the server configuration files + using HTTP GET and PUT requests. + - The web interface didn't allow you to modify the + location or description of the printer. + - The pdftops filter now uses its own temporary file + function to work with PDF files using LZW compression + (which use the uncompress program or gunzip) + - The SystemGroup directive now supports specification of + multiple groups. + - Added new Include directive to cupsd.conf, a la + Apache. + - Added new pseudo-driver/PPD called "raw" that can be + used to create/convert a raw queue. This also allows + raw queues to be created in the web interface. + - The pdftops filter didn't handle image objects that + used JPEG and Flate compression together. + - The pstops filter counted pages wrong when using the + N-up and even/odd printing options. This prevented + the page-ranges option from working properly. + - Added another fix to pstoraster for a bus error + condition caused by a lack of parenthesis in the + Ghostscript code. + - Added new "natural-scaling" option which scales the + natural size of the image (percent of natural image + size instead of percent of page size.) + - The lppasswd program is now setuid to the CUPS user + instead of root. + - The PPD functions did not allow for PPD files that + defined the page sizes and margins before the page + size options. + - The mime.types file now checks for the PJL "LANGUAGE = + Postscript" command for PostScript files. + - The scheduler did not truncate file: output files. + - The PPD file reading code did not handle options with + raw quotes (") in the human-readable names. + - The pdftops filter now remaps the space character when + (bad) PDF files contain a .notdef glyph for the space + character. + + +CHANGES IN CUPS V1.1.8 + + - Updated spec file to generate separate cups-pstoraster + package for pstoraster. + - The spec file wasn't setting LOGDIR in the install. + - The scheduler might restart a stopped printer after + stopping a print job. Thanks to Florent + Guiliani for finding this bug! + - The init script showed run level 0 for the Red Hat + chkconfig program. This is incorrect because Red Hat + doesn't use run level 0 for shutdown scripts. + - The IPP backend did not handle the + client-error-not-found error when checking the status + of the job that was sent. This caused remote queues + to stop on client machines when the server had job + history disabled. + - Added httpConnectEncrypt() function to avoid + performance penalty for setting up encrypted + connections initially. + - Use httpConnectEncrypt() in all client apps and in the + CUPS API to ensure consistent usage of encryption + throughout. + - Jobs weren't queued to remote classes (fix from + Richard Begg.) + - AIX changes from Richard Begg. + - Fixed the pstops fix for GNOME output - no longer use + the page numbers in the %%Page: comment since GNOME + puts a filename instead (!?@!#?!). There is still an + issue with N-up printing since GNOME defines its fonts + in the first page instead of the document setup section + (pages must be independent according to the DSC spec) + People with GNOME printing problems should consult bug + #54489... + - The imagetops filter produced PAGE: messages when + generating PostScript for a non-PostScript printer + (only affects page-label and Classification + options.) + - The updated pdftops filter was looking for an options + file called xpdf.conf instead of pdftops.conf. + + +CHANGES IN CUPS V1.1.7 + + - Configuration script changes, including new + "--with-docdir=/dir" option to relocate CUPS + documentation and web content according to your + favorite version of the FHS. + - Documentation updates for encryption, SLP, etc. + - New Software Test Plan and automated test script to + test CUPS prior to installation. + - All scheduler configuration files are now case + insensitive to match Apache. + - Added support for Apache ListenBackLog, Require, + Satisfy, , , and LimitRequestSize + directives. + - Added support for all Apache log levels... + - Added support for "double" HostNameLookups. + - Added new "RunAsUser" directive to support non-root + configurations on the standard (priviledged) ports. + - Added support for non-root invocation of the lpd + backend (does no reserve a priviledged port, which + might not work with some LPD servers...) + - Added new PrintcapFormat directive to control the + output format of the printcap file (BSD or Solaris + formats are supported at present.) + - The CUPS directory service routines now handle + ECONNREFUSED errors gracefully rather than shutting + all browsing off. + - ippErrorString() now returns the recommended error + messages from the IPP/1.1 Model and Semantics + document. + - Fixed a minor IPP compliance issue with responses + to requests without the attributes-charset or + attributes-natural-language attributes. + - Sun fix: need httpFlush() call for chunked IPP + requests in cupsDoFileRequest(). + - httpConnect() now looks up "localhost" by name and + by address (127.0.0.1) for users the go to the + trouble of removing the required localhost entry + in /etc/hosts or on their DNS server... + - Added support for Linux 2.4.x devfs parallel port + filenames (/dev/parallel/N). + - cupsDo[File]Request() and cupsGetPPD() no longer + block trying to reconnect to a crashed or inaccessable + server. + - Added new ppdEmitJCL() function to better handle + PJL commands from PPD files. + - A bug in UpdateJob() would cause the scheduler to + consume 100% CPU until another request was submitted. + - The cancel command did not support the "-" option to + cancel all jobs on all printers. + - The cancel and lprm commands did not support cancelling + the next/current job in the queue. + - The pdftops and pstoraster filters were using unsafe + temporary file functions; while this is not a problem + in normal configurations (the CUPS temporary directory + is restricted), they now use the cupsTempFd() function. + - The mime.types file was missing the recognition rule + for Sun Raster images. + - The admin CGI was passing a printer make string to + ippSetCGIVars() that was being replaced in that + function. + - "lpoptions -l" would resave the options... + - The EPSON drivers now send the "end packet mode" + command when printing to USB devices. + - The scheduler initialized certificates before loading + the cupsd.conf file. + - The scheduler used /dev/random to collect random data, + which could block if insufficient entropy information + had been collected by the kernel. Now use + /dev/urandom. + - Fixed a bug in the whitespace skipping code in + httpGetSubField(). + - The LPD backend now supports a new "order" option: + "lpd://server/queue?order=control,data" (default) and + "lpd://server/queue?order=data,control". + - The scheduler enforced a 30 second timeout on all + clients regardless of the Timeout directive and if a + CGI was currently running. + - cupsParseOptions() now sets boolean options to + option=true or option=false. + - The "percent complete" calculations in the LPD backend + could overflow on large files, causing the percentage + to wrap to 0 every 40MB or so. + - Fixed a memory reallocation bug in pstoraster that + could cause it to crash. + - The LPD backend now sanitizes the job title to avoid + potential problems on remote LPD servers. + - The lp command did not send the requesting-user-name + attribute when altering a job. + - The pstops filter did not handle PostScript files with + lines longer than 8191 bytes. + - The scheduler no longer uses inet_addr() to convert IP + addresses in dot format (mmm.nnn.ooo.ppp) to the + 32-bit format, since it will not work for IPv6 + addresses. + - New "Classification" directive to force labeling of + the current classification on each page. + - New "page-label" attribute to add per-page labels + ("For Official Use Only", "Draft", etc.) + - The scheduler now sets the HTTPS environment variable + for CGI programs when a client connects using + encryption. + - Fixed a recursion bug in the scheduler that could + cause cupsd to crash when a printer was removed. + - The LPDEST and PRINTER environment variables didn't + support instances. + - Dropped the "file" backend from the device list that + is reported, since it is only available for *testing* + and should never be used in a production environment. + The file: device can still be used, but it won't show + up in the list of devices from lpinfo or the web + interface. + - Added support for /dev/lpa# parallel ports under *BSD. + - Added META variables to the CGI header template to + prevent caching of the results. + - Fixed an unaligned memory buffer for the pstoraster + clist states; this caused bus errors for some + combinations of printers, drivers, and options. + - Re-added black reduction for colorful colors; this + helps to prevent dark colors from getting desaturated. + (only used when converting RGB to CMYK) + - Added two new directives - MaxJobsPerPrinter and + MaxJobsPerUser - to allow an administrator to set + the maximum number of pending jobs in a queue or + submitted by a user. + - The scheduler no longer stops a printer if it can't + create the status pipe or run the filters or backend. + This will allow heavily loaded servers to service + clients or start print jobs as the load allows. + - Fixed a bug in the Set-Job-Attributes code that could + crash the scheduler (patch from Martin Zielinski) + - cupsSetDests() did not quote option values with + embedded spaces. + - Added support for the Enable-Printer and + Disable-Printer extension operations (same as + CUPS-Accept-Jobs and CUPS-Reject-Jobs.) + - The AppSocket and IPP backends now wait for the print + job to be finished before exiting; this should prevent + the loss of print jobs with older JetDirect firmware + and make consecutive print jobs print faster. + - The BMP loading code did not handle resolution values + of 0. This is a problem with BMP image files produced + by the GIMP. + - The HTTP Upgrade code (upgrade to TLS encryption) + bypassed the authentication checks. + - The HTTP Upgrade code did not send a 426 status code + to the client and end the current request. This caused + a race condition between the client and server for the + upgrade to TLS. + - Fixed a bug in the EOF and Trailer detection code in + the pstops filter. + - The imagetoraster filter did not add the margins to + the custom page size in the raster header. + - The imagetops filter did not adjust the custom page + size to the size of the printed image. + - The imagetops filter did not include DSC comments + which are required by some printers. + - The imagetops filter did not insert newlines in + Base85 encoded output, causing files to contain + lines longer than 255 characters (violation of the + DSC). + - Added support for the DeskJet 900 series duplexer + and CRET color modes in the HP driver. + - Added support for PPD-defined margins in the HP + driver. + - Fixed the debugging output from pstoraster - the + font list was not terminated by a newline. + - Some versions of the HP-UX pam_unix authentication + module apparently do not pass the appdata_ptr argument + to the conversation function, preventing the scheduler + from authenticating users using PAM under HP-UX. A + workaround using a static variable has been added to + address this problem. + - Fixed a bug in the scheduler SortPrinters() function + that could cause printers to disappear or the + scheduler to crash when adding a printer. + - Changed the pstops filter to not do per-page filtering + if the file does not conform to at least version 3.0 + of the document structuring conventions. This seems + to "fix" printing with broken apps. + - The image filters did not handle older TIFF files that + lacked the samples-per-pixel and bits-per-pixel tags. + - Added new cupsGetJobs() and cupsFreeJobs() functions + to manage print jobs. + - cupsEncodeOptions() would encode names of 0 length and + cupsAddOption() and cupsParseOptions() would add names + of 0 length. + - The scheduler might block waiting for status messages + after starting a new print job. Thanks to Florent + Guiliani for finding this bug! + + +CHANGES IN CUPS V1.1.6-3 + + - The configure script put the JPEG library before the + TIFF library; this caused problems in some + configurations since the TIFF library also supports + JPEG compression of TIFF images. + - Updated the configure script and makefiles to handle + admin man pages with the "1m" extension (HP-UX, IRIX, + Solaris, Tru64) and in odd directories (IRIX) + - The updated cupsTempFile() function did not return + the filename when called with a filename buffer of + NULL (previously it used a static buffer.) + - FreeBSD uses /dev/unlptN, but NetBSD and OpenBSD use + /dev/ulptN. + - DeletePrinter() didn't remove the printer from any + classes it was a member of. + - DeletePrinterFromClass() didn't preserve the + implicit status of a class. + - DeletePrinterFromClasses() didn't remove printers + from implicit classes. + - StartJob() didn't send the job-sheets, job-priority, + and job-hold-until attributes to remote printers. + - LoadAllJobs() was looking for job-sheets-completed + instead of job-media-sheets-completed. This would + prevent accumulation of page data after a restart + of the scheduler. + - The pstops and imagetops filters now generate copies + using the appropriate method for a Level 1, 2, or 3 + printer since some Level 2/3 printers don't support + the /#copies variable anymore. + - The man page for cups-lpd did not mention the "-o" + option. + - The IPP backend didn't handle version-not-supported + errors and revert to IPP/1.0 (previously it only checked + for a bad-request error) + - Caldera fix: lpc now reports unimplemented commands as + unimplemented, not invalid. + - Caldera fix: lpq didn't recognize BSD lpq "-a" option. + - Caldera fix: lpr didn't recognize BSD lpr "-1", "-2", + "-3", "-4", "-q", or "-U" options. + - RedHat fixes: patches to GNU Ghostscript + - SuSE fix: temp file creation patch to GNU Ghostscript + (pstoraster). + - SuSE fix: remove cgi-bin/abort.c and cgi-bin/email.c, + which are not used. + - SuSE fix: missing NULL check in cgi_initialize_post(). + - SuSE fix: potential buffer overflows in + cgi_initialize_string(). + - SuSE fix: potential buffer overflows in + ippSetCGIVars() + - SuSE fix: more NULL checks in ppdOpen(); also make + sure that all memory is freed on error to avoid memory + leaks. + - SuSE fix: Exit from child if setgid() or setuid() + fails. + - SuSE fix: Added setgroups() calls after setgid() and + setuid() calls. + - SuSE fix: potential buffer overflows in httpEncode64() + calls. + - SuSE fix: potential buffer overflows in httpSeparate() + - SuSE fix: potential buffer overflows in ippWrite() for + bad input. + - SuSE fix: potential nul skip in ppd_decode() for + missing hex digits. + + +CHANGES IN CUPS V1.1.6-2 + + - Added changes to support NetBSD startup scripts. + - Added separate compiler options for pstoraster + (Ghostscript) to avoid compiler-induced errors + from Ghostscript's twisted code. + - The mime.types file contained syntax errors. + - Updated the *BSD USB device filenames to use + the /dev/unlptN files so that the USB device + is not reset prior to printing (causes print + corruption on many printers) + - Added new cupsTempFd() function to avoid serious + security bug in glibc fopen() function. The glibc + fopen() function unlinks a file before creating it, + which opens up possible symlink attacks. + - Now reject 0-length names in add-printer and add-class + requests. + - Fix for pstoraster when ZLIB is not available. + - cupsGetPPD() didn't reconnect when a HTTP connection + was lost. + - SuSE fix: httpConnect() didn't check that the + value from gethostbyname() was a valid IPv4 address. + - SuSE fix: httpConnect() didn't allow file descriptor 0 + to be used for a socket. + - SuSE fix: ippRead() didn't confirm that all values in + a set were numeric or string types. + - SuSE fix: lppasswd race condition fixes. + - SuSE fix: directive names could overflow buffer when + reading *.conf files. + - SuSE fix: HEAD requests for PPD files did not use the + same logic as GET requests. + - SuSE fix: possible buffer overflow when adding + /index.html to requested directory name. + - SuSE fix: possible buffer overflow when converting + IPP attributes to string options for filters. + - SuSE fix: creating file: device output with mode 0666 + instead of mode 0600. + - SuSE fix: creating job info files with mode 0640 + instead of 0600. + - SuSE fix: don't rely on snprintf() for including + system name in log filenames. + - SuSE fix: add bounds checking when copying quoted + and hex strings. + + +CHANGES IN CUPS V1.1.6-1 + + - Added configure check for getting the correct + strftime() format string; %c is not Y2k safe, + and %KC and NULL are not universally supported. + + +CHANGES IN CUPS V1.1.6 + + - Fixed another possible DoS attack in httpGets() + - Added check for "LANGUAGE = PCL" and "LANGUAGE = + POSTSCRIPT" in mime.types. + - Resolution options were not being passed into the + filter programs properly. + - The default compiler options for GCC no longer include + "-g3", which apparently is deprecated in newer + versions of GCC. + - CheckJobs() could cause cupsd to crash if a job is + cancelled in StartJob(). + - The printers.conf and classes.conf files are now + written with restricted permissions. + - The round-robin algorithm used by FindAvailablePrinter() + had problems; fixes contributed by Joel Fredrikson. + - If LoadAllJobs() is unable to determine the file type + of a print job, assume "application/vnd.cups-raw". + - The web interface now provides a job_printer_name + value for any corresponding job_printer_uri value. + - The cups-lpd mini-daemon now logs the client address + and hostname as well as all commands and errors in the + syslog file. + - The IPP backend now detects the supported file formats + and only specifies the document format if it is + supported. This makes IPP printing to network print + servers and cards more reliable without affecting the + capabilities of CUPS servers. + - The time_at_xyz attributes are now converted to human- + readable dates and times for the web interfaces. + - The HP and EPSON sample drivers now correctly catch + signals and eject the current page when a job is + cancelled. + - Fixed bug in CGI code - did not ignore control + characters (e.g. newlines) in form data. This caused + sporatic web interface problems. + - The file type logging code in the scheduler referenced + the optional document-format attribute; the new code + uses the resolved MIME type instead. + - The client.conf parsing code now removes trailing + whitespace. + - The MaxJobs directive was being treated as a boolean + instead of an integer. + - The scheduler would not timeout remote printers if + BrowseInterval was set to 0. + - The lpadmin command now supports setting of options + and user-level access control. + - Added "-E" option to all printing commands to force + encryption. + - The client code did not consume the response to the + OPTIONS request when switching to secure mode. + - The scheduler did not output a Content-Length field + when responding to an OPTIONS request. + - Added documentation on using cups-lpd with xinetd + to the man page. + - The socket backend now starts retries at 5 seconds and + increases the interval to 30 seconds. This should + provide faster printing when multiple jobs/files are + queued for a printer. + - The filters and backends no longer buffer output to + stderr. This should provide much more accurate status + reporting. + + +CHANGES IN CUPS V1.1.5-2 + + - Fixed configure check for OpenSSL to work with RSA + code. + - Added configure check for , and use this + check in backend/serial.c. + - Updated configure script handling of data, + configuration, and state directories to use datadir, + sysconfdir, and localstatedir variables. + - NetBSD uses different serial port filenames than + FreeBSD and OpenBSD. + - The pdftops filter didn't need some X-specific files. + - The scheduler makefile doesn't do a chown anymore when + installing (cupsd did this automatically on startup + anyways) + + +CHANGES IN CUPS V1.1.5-1 + + - There was a typo in the top-level Makefile + - The top-level Makefile did not install an init script + for run level 5. + - The configure script did not add the "crypto" library + when checking for the OpenSSL library. + - The OKIDATA PPD files were missing. + - The config.h.in file defined the wrong version number. + - The serial backend did not define "funky_hex" under *BSD. + - Updated the Visual C++ project files and some of the + CUPS API sources to compile under Windows again. + + +CHANGES IN CUPS V1.1.5 + + - Security updates - new default configuration does + not broadcast printer information and only allows + access from the local system. + - EXPERIMENTAL encryption support - CUPS now optionally + supports TLS/SSL encryption via the OpenSSL library. + - Documentation updates. + - Makefile/configure script updates. + - The RPM spec file didn't work out-of-the-box under + RedHat or Mandrake. + - Minor code cleanup to remove extraneous compiler + warnings. + - cupsTempFile() was using %p for the temporary + filename; this should have been %08x (just 8 digit + hex) + - Deleting a printer with active print jobs would still + crash the server. + - ippWrite() and ipp_write_file() didn't send the + correct value length for name-with-language and + text-with-language attributes. + - Updated IPP code to support copied strings (that + should not be freed); this provides slightly more + efficient IPP server performance. + - Updated PDF filter to Xpdf 0.91. + - httpGets() could go into an infinite loop if a line + longer than the input buffer size was sent by a + client. This could be used in a Denial-of-Service + attack. + - The lpstat and CUPS API functions now request only the + data required when getting the list of printer or + class information. This should improve performance + with large numbers of printers on slower machines. + - The scheduler was always enforcing the FilterLimit, + even if FilterLimit was set to 0. + - Updated the Linux USB backend to support Mandrake's + /dev/usb/usblp# filenames. + - The PRINTER and LPDEST environment variables did not + override the lpoptions default printer. + - The PPD read functions incorrectly included trailing + characters (usually whitespace) after quoted string + attributes. + - The multiple-document-handling attribute handling code + did not check for the correct value for collated + copies (separate-documents-uncollated-copies). + - The EPSON driver did not work with OKIDATA printers in + EPSON emulation mode (needed change-emulation command) + - The HP-GL/2 filter did not scale the plot properly in + scale mode 2. + - Added PPD files for 9-pin and 24-pin OKIDATA printers. + - The httpSeparate() function didn't handle passwords + that started with a number. + - ippDelete() could free the character set string + multiple times in name-with-language and + text-with-language attributes. + - The scheduler would access freed memory right after + freeing it (for debug messages); these parts of the + code have been reordered to avoid this situation + which was causing sporatic errors and crashes. + - The ppdClose() function didn't free all of the strings + in the ppd_file_t structure. + - The LoadAllJobs() function in the scheduler did not + close the spool directory. + - Changed all sprintf's that use string formats to + snprintf's, even if the destination buffer is + larger than the source string(s); this protects + against buffer overflows caused outside of CUPS... + - Changed all strcpy's to strncpy's between local and + global variables, even if the destination buffer is + larger than the source string; this protects + against buffer overflows caused outside of CUPS... + - The CUPS certificate functions didn't use the + CUPS_SERVERROOT environment variable when set. + - The directory services code was copying instead of + comparing the remote printer info, resulting in + unnecessary updates of the printer attributes for + remote printers. + - Added new mime.types rules to allow automatic raw + printing of PCL and ESC/P files; PJL headers are + parsed to differentiate between PostScript and + PCL job files. This should eliminate a lot of + the reports of SAMBA printing problems due to + the missing "-oraw" or "-l" options. + - The mimeLoadType() function didn't handle the + 3-argument contains() function. + - The LoadPPDs() function in the scheduler didn't + properly set the alloc_ppds variable or handle a PPD + database containing 0 printers. + - The scheduler FindAvailablePrinter() function didn't + use the same queuing logic as the CheckJobs() + function. This caused classes to stall if a remote + printer was always busy. + - Jobs are now assigned to printers in a class + round-robin style. This should prevent the first + server in the class from bearing the brunt of the + jobs. + - The scheduler's LoadAllJobs() function didn't always + restore remote printers for queued jobs on startup. + - The serial backend didn't support the higher baud + rates with the old termios interface. It now supports + 57600 and 115200 baud. + - The serial backend now supports different types of + flow control; previously it ignored the flow=XYZ + option in the device URI. + - The serial backend now supports DTR/DSR flow control, + which is popular on dot-matrix printers (access with + "flow=dtrdsr" in the device URI) + - Added new job-originating-host-name attribute for + jobs. The new attribute provides the hostname or + IP address of the machine that submitted the job. + - The set-job-attributes code no longer allows read-only + job attributes to be changed. + - Expanded the click area for the navigation bar in the + web interface. + - Updated the lp and cancel commands to support all of + the Solaris print options (some are simply ignored + since they do not map) + - Updated the scheduler to limit the number of file + descriptors to the maximum select() set size. This + was causing problems on Solaris systems where the + max FD count was increased beyond 1024. + - The scheduler's LoadDevices() function was getting + interrupted by the SIGCHLD signal handler; now ignore + child signals while loading devices. + - Added quota and allow/deny user support for printers + and classes. + - Removed black/CMY adjustment code from the PS and + image file RIPs; it was interfering with some CUPS + driver dithering code. + - The lpc program stopped listing the queue statuses + after the first active printer. + - The cups-lpd program used an output format that the + Solaris printing system did not understand. + - Updated the lpq program to use the Solaris format + except under Tru64 UNIX. + - Some DEC PPD files incorrectly use "Off" for the null + value in UI constraints. Added "Off" to the list of + accepted null values. + - Changed the *BSD define constants to __*BSD__ in all + of the backends. + - Added support for "lpstat printername", which is an + undocumented feature in Solaris. + - The HP-GL/2 filter now only sets the plot size if it + is set in the plot file. + - The lpmove command wasn't sending the requesting + user name, causing it to always fail. + - Updated the cupsTempFile() code to use GetTempPath() + under Windows. + - The cups-lpd mini-daemon didn't limit the number of + data files accepted, didn't use cupsTempFile(), + didn't handle control file job information in any + order, and didn't free job options after printing + a file. + - The scheduler copy_banner() function did not + explicitly set the owner and permissions of the banner + files, which could prevent the banner pages from + printing on some systems. + - The lpstat program wasn't listing remote classes. + - The scheduler did not verify that the printer-uri + attribute was specified in all requests that required + it. + + +CHANGES IN CUPS v1.1.4 + + - Makefile and configure script fixes. + - **** Changed the default Printcap setting **** to + /etc/printcap. There are just too many people asking + why application XYZ doesn't see their printers! + - The web admin interface now displays an error if it + can't get the list of printer drivers from cupsd. + - The IPP backend was putting the copies option before + the other job options were set. This caused the IPP + request to contain attribute groups in the wrong + order, which prevented remote printing. + - Added checks in scheduler to free memory used for + IPP requests and language information when closing + a client connection. + - Fixed the duplex option in the HP LaserJet driver. It + should now work with all LaserJet printers (and + compatibles) + - The add-printer web interface didn't initialize the + "old info" data pointer, which caused random crashes + on many OS's. + - Fixed many page sizes defined in the Level 1 + compatibility file "gs_statd.ps" to match reality. + - Fixed another bug in the setpagedevice "code" in + Ghostscript. It should now accept all standard + Adobe attributes on all platforms. + - Fixed pstoraster so that it reallocates memory for + color depth changes as well as size/resolution + changes. This removes an ordering constraint on + the color, page size, and resolution options in + PPD files. + - The IPP backend didn't use the job's character set + when the destination printer supported it. This + caused problems when printing text files to other + CUPS servers. + - Updated the logic used to determine when to rebuild + the PPD file database. The scheduler now checks the + dates and the number of PPD files (was just checking + the dates.) + - Updated the ippSetCGIVars() function (used by the + web interfaces) to only filter valid string values. + - The PostScript filter was scaling 2-up pages + incorrectly. This caused the edges of some pages to + be clipped. + + +CHANGES IN CUPS v1.1.3 + + - Makefile fixes. + - RPM spec file changes. + - Documentation updates. + - Enabled pstoraster debug messages for everything + (only logged when LogLevel set to "debug"...) + - Changed the Input/OutputAttributes fix in + pstoraster so that it works on all platforms. + - The HP-GL/2 filter didn't set the right green + color value in encoded polylines or text. + - Updated the "fitplot" code to handle plot sizes + specified as "PSwidth,length" and "PSlength,width". + - Updated the Linux parallel and USB backends to open + the device files prior to looking in /proc for + autoprobe info. This makes sure that loadable device + driver modules are in fact loaded... + - Added new FilterLimit directive to limit the number + of processing jobs/filters on a system. + - set-job-attributes didn't change the job-state to + held/pending when the job-hold-until attribute was + specified. + - set-job-attributes didn't save the new job attributes. + - Now change the "requesting-user-name" attribute in + requests from remote systems to "remroot" when an + unauthenticated "root" user is sent. This can be + changed using the new RemoteRoot directive in + cupsd.conf. + - The cancel-job, hold-job, release-job, and restart-job + operations didn't log the authenticated username. + - The cups-lpd mini-daemon now checks for a + document-format option before forcing raw mode with + filter mode 'l'. + - The cups-lpd mini-daemon now supports "-o" options + on the command-line (passed by inetd) to set global + defaults for all print queues. + - The pstops filter assumed that a file with a Trailer + comment would also have an EOF comment. + - Added new cupsSetPasswordCB(), cupsSetServer(), + cupsSetUser(), and ippSetPort() functions to better + support client applications (especially GUIs...) + - The CUPS-add-class and CUPS-add-printer operations + didn't reset the printer-name attribute on remote + print queues that had to be renamed when a local + printer was defined with the same name. + - The lpoptions command now supports a "-r" option to + remove options for a printer or instance. + - The lpadmin and admin.cgi programs no longer allow + class and printer names to begin with a number; this + caused the command-line utilities to become confused. + - The Linux USB backend now looks for both the parallel + and usblp driver names in the device list. + - Added a new FontPath directive to cupsd.conf, and also + a "--with-fontpath" option for the configure script to + specify alternate font paths for pstoraster. + - The CUPS-move-job operation didn't update the + job-printer-uri attribute. + - The scheduler only looked up printers and classes by + name in IPP requests, instead of using the full URI. + This caused problems with KUPS and friends with + remote printers. + - The scheduler now handles better localization of + hostnames (e.g. server is host.foo.com, remote is + host.subfoo.foo.com, localized is not host.subfoo...) + - The scheduler logging functions now use a common + log file checking/rotation function (courtesy of + Crutcher Dunnavant at Red Hat) + - The scheduler could accept more client connections + than it allocated for if more than one Port or Listen + line was present in cupsd.conf. + - Other minor scheduler performance tweeks. + - The lpq and lprm commands didn't support the default + printer set using lpoptions. + - The lpoptions command now supports a "-l" option to + list the printer-specific options and their current + settings. + - The web printer and class lists now show a link to the + default printer or class at the top of the page. + - The text filter now supports pretty printing of shell + and perl scripts as well as C/C++ source files. + - The top and bottom margins were reversed for landscape + text printing. + - The lpq and lprm commands didn't understand printer + instances. + - The scheduler only selected on the first 100 file + descriptors instead of the maximum file descriptor + limit. + - The scheduler client, listener, and mainline functions + now share code to disable and enable monitoring for + new client connections. + - The imagetoraster filter didn't support all of the + required pagedevice parameters. + - The serial backend now checks for 100 serial ports + under Linux. + - The scheduler used sscanf() to pull out the remote + printer location, description, and make/model strings, + but if any of these options was empty then sscanf() + would stop processing. + - Added "debug2" log level to provide a little less + verbose debugging information at the "debug" level. + - The scheduler would crash if you stopped a printer + that was currently printing a job. + - The scheduler incorrectly allowed jobs in the cancelled, + aborted, or completed state to be cancelled. + - The image filters did not load TIFF images properly + for bottom-to-top and right-to-left orientations. + - Added new cupsEncodeOptions() function to encode + CUPS options as IPP job attributes. + - The IPP backend, LPD mini-daemon, client commands, + and CUPS API did not properly encode multiple + option values separated by commas. + - Added new scheduler malloc logging in debug mode + (provides summary of total arena size, allocated, + and free bytes once a minute) + - The EPM-based distributions didn't install the + correct symlinks for a few man pages. + - Fixed a memory leak in the scheduler - wasn't + freeing old filters when deleting or renaming + printers. + - The scheduler now queries the primary IP address + for the name of the server and maps any incoming + requests from that address to the server name. + This fixes web admin mapping problems from + server.domain.com to localhost. + - The web printer modify interface now remembers + the previous device and driver settings (except + for serial ports.) + - The job-k-octets attribute is now stored as part of + the job attributes; this preserves the information + after a job is completed when job file history is + turned off. + - Dropped option sub-group parsing code for the moment, + since many Xerox PPD files abuse this feature in PPD + files and don't follow the hierarchy rules. + - Added new wrapper code around options so that duplex + options for some HP printers don't prevent prints. + - Added support for Digital UNIX/Tru64 UNIX/OSF/1 format + for "lpstat -v" output. + - Now show the URI for remote printers instead of + /dev/null in "lpstat -v" output. + - Creating classes and adding printers to a class with + the lpadmin command didn't work. + - The banner pages and test page should now format + correctly in both portrait and landscape orientations. + - Updated banner page substitution so that { can appear + by itself without quoting. + + +CHANGES IN CUPS v1.1.2 + + - Makefile/configure fixes + - RPM spec file and EPM list file fixes + - The cupsTempFile() function now uses a different + algorithm for generating temp files and "reserves" + them to avoid possible security exploitation. + - Now use /dev/random (if available) to seed the random + number generator for certificates. + - The /var/spool/cups and /var/spool/cups/tmp directories + were incorrectly owned by root; they are now owned by + the filter user, typically "lp". + - The scheduler now resets the permissions on the spool + and temp directories as needed to match the filter + user. + - Now expose ppdCollect() as an externally callable + function. + - The image filters now support filtering from the + standard input. + - The imagetoraster filter now collects all printer + options and job patch files and applies them to the + page header as needed. + - Added format and banner options to LPD backend. + - The send-document operation didn't start a job + immediately when last-document was true. + - The set-job-attributes operation didn't correctly + replace the current job-hold-until value. + - Removed the option wrapper code from ppdEmit() and + friends since it caused problems with Ghostscript + and many PS printers. + - Was setting TZ environment variable twice for job + filters. + - Added syslog logging in cups-lpd to aide in + debugging problems. + - The HP-UX parallel port backend did not list the + available parallel ports on some systems (printf + calling problem...) + - The lp and lpr commands overrode user options if + -d/-P were specified after -o. + - The scheduler would crash with a */* filter. + - Added support for a "default" filter for unknown file + types. The example provided in the mime.types and + mime.convs file prints unknown files as if "-oraw" was + specified for the job. This functionality is disabled + by default. + - The "compatibility" mode fix for older backends did not + work for smbspool. Added a workaround for it. + - The HP-GL/2 filter didn't perform the right pen scaling + with some files and the "fitplot" option. + - New Software Performance Specification document that + describes the memory, disk, and CPU usage of all the + CUPS software. + + +CHANGES IN CUPS v1.1.1 + + - The pstoraster Makefile still referenced one of the + old PDF filter files. + - The filter Makefile used INSTALL_DATA instead of + INSTALL_LIB to install the CUPS image library. + - The administration CGI didn't work properly with + network devices. + - The BrowseACL variable was not updated after the + cupsd.conf file was loaded. + - The lpd mini-daemon didn't support printer instances. + - Now use a default umask of 077 for child processes. + - Now put temp files in /var/spool/cups/tmp for child + processes and the root user, unless TMPDIR or TempDir + is defined otherwise. + - cupsGetPPD() no longer uses easy-to-guess filenames. + - The CUPS-Delete-Class and CUPS-Delete-Printer + operations now save classes.conf file as needed. + - The lppasswd command wouldn't add a user. + - The ppdOpen() function could cause a segfault if a + 0-length PPD file was read. + - The image filters were not handling images with + different X and Y resolutions properly. + - The imagetoraster filter defaulted to RGB output + instead of black output like pstoraster. + - The pstops filter didn't handle binary data properly. + - The pstops filter didn't handle copies properly for + PS files lacking DSC comments. + - The pstops filter now appends %%EOF to the end of + documents if they don't have it. + - The cupsGetPPD() function didn't work with remote + printers lacking the @server in the name. + - The configure script didn't work right when only + --prefix was specified. + - The ppdEmit() code now wraps all printer commands so + that buggy PostScript printers will still print a file + after receiving an option that isn't available. + - Fixed the DeskJet margin bug, and disabled 600dpi + color mode until it can be fixed. + - The cupsAddDest() function didn't sort instances + correctly in all cases. + - The time-at-xyz attributes now expand to the date and + time in banner files. + + +CHANGES IN CUPS v1.1 + + - Documentation updates. + - Configuration script updates. + - Didn't map charset and language value strings to lowercase + and _ to - as required by SLP and IPP. + - ppdLoadXYZ() didn't add the list of available fonts to the + ppd_file_t structure. + - The text filter common code was freeing the PPD file data + before it was used. + - The text filter now embeds missing fonts. + - The CGI interface now maps local access to the server to + the localhost address. + - The HP-GL/2 filter didn't use the specified (or default) + color ranges, resulting in strange colors. + - The HP-GL/2 filter didn't default to no input window, which + caused unnecessary clipping of plots. + - Integrated Xpdf's pdftops filter into CUPS, which is a + lightweight and reliable replacement for Ghostscript's + PDF support. + - Removed all PDF support from Ghostscript. + - Updated HP driver to set top margin; this seems to fix + the offset problem seen on HP DeskJet printers. + - Fixed dependencies on the ZLIB and JPEG libraries in + pstoraster. + - The lpr command wasn't using the lpoptions defined by + the user. + - The lpr command would segfault if the CUPS server was + not running. + - The top-level makefile was not installing the CUPS + initialization script. It now does so if it sees there + is an init.d directory in /sbin, /etc/rc.d, or /etc. + - "lpstat -v all" didn't work. + - pstoraster would crash on some platforms doing the + setpagedevice operator. + - The web administration interface now allows you to set + the default banner pages. + - Images can now be positioned on the page using the new + "position" option. + - The AccessLog, ErrorLog, and PageLog directives now + support "%s" to insert the server name. + - Added a new BrowseShortNames directive to allow for + short remote printer names ("printer" instead of + "printer@server") when possible. + - The scheduler could crash if given an invalid PPD file + with no PageSize attributes. + - Updated the serial, parallel, and usb backends to do + multiple writes and ignore ioctl() errors as needed; + this should fix problems with serial printing on old + serial drivers and with the UltraSPARC parallel port + driver under Solaris 2.7. + - Now propagate LD_LIBRARY_PATH to child processes from + cupsd. + - New DataDir directive for installing in alternate + locations. + - New CUPS_SERVERROOT and CUPS_DATADIR environment + variables to specify installation directories as + needed. + - Queued remote jobs recreate remote printers as needed + when the scheduler is started. + - Deleting a printer also purges all jobs on that + printer. + - Old job and control files that don't belong to a + printer are automatically deleted. + - Wasn't updating time-at-processing and + time-at-completed attributes in job. + - Didn't send required multiple-operation-time-out + attribute in response to a get-printer-attributes + request. + - cups-lpd now supports options set with lpoptions. + - The job-hold-until attribute is now provided with all + jobs. For jobs that are not currently held the value + is "no-hold". + - The scheduler was not sending "unknown" values in IPP + responses. + - The lpoptions command now accumulates options from + previous runs rather than replacing all options for a + printer. + - The IPP backend now switches to IPP/1.0 if a 1.1 + request fails. + - The lpadmin and admin.cgi programs now validate new + printer and class names. + - The access_log file now includes the number of IPP bytes + received in a POST request. + + +CHANGES IN CUPS v1.1b5 + + - Documentation updates. + - The pstoraster filter didn't compile without the JPEG library. + - The cupsd server didn't support the HTTP OPTIONS request + method. + - Dropped the "CLOSE" method supported by the cupsd server. + (not defined in HTTP specification) + - Makefile/configure script fixes. + - Missing the job-restart template. + - Added IPP test suite for testing. + - Missing IPP documentation from binary distributions. + - Fixed multiple-document handling code when last-document + not specified. + - Added more checks to IPP requests to prevent bad requests + from getting through. + - Not all of the Ghostscript error output was being sent to + stderr. + - The PostScript filter now added PJL commands to set the + job name and display string, if supported. + - The scheduler would crash if the browse socket could not + be bound. Now disables browsing if port 631 (reserved for + IPP) is being used by a misbehaving daemon. + - The USB backend now looks for the older Linux 2.2.x USB + printer device filenames as well as the newer ones. + - The IPP backend now uses the UTF-8 charset exclusively, + since apparently only CUPS handles more than US-ASCII and + UTF-8... + - Wasn't quoting ( in PostScript banners... + - Send-document requests with no document-format attribute + could cause cupsd to crash. + - Old jobs in the spool directory might cause cupsd to + crash. + - CUPS now supports all of the recommended job-hold-until + keywords as well as name values of the form "HH:MM" and + "HH:MM:SS". + - Added placeholder pointer for TLS encryption to the HTTP + connection structure. + - Fixed the "fast poll" bug reported by DISA - the + status pipe wasn't being closed for multi-file jobs. + - Revamped put_params code in pstoraster to fix bitmap + allocation bug with FrameMaker output. + - Ripped out filename, etc. code from pstoraster as it + is a potential security hole. + - Added support for RIP_CACHE environment variable in the + new pstoraster. + - Fixed USB device filenames for Linux; now support new + pre-2.4 devices (/dev/usb/lp#) and 2.2 devices + (/dev/usblp#) + - Fixed accept-jobs crash with classes. + - Didn't include dot-matrix EPSON drivers in previous + release. + + +CHANGES IN CUPS v1.1b4 + + - Documentation updates. + - Many makefile and configuration script fixes (should + now compile better under *BSD.) + - The MediaPosition attribute was being mishandled by + GhostScript, causing the RIP to fail whenever a paper + tray was selected. + - The scheduler now logs the final line of log information + from a filter, even if it doesn't end with a newline; this + primarily affects GhostScript error output. + - The scheduler was saving implicit classes, so after a few + restarts you'll end up with AnyPrinter, AnyAnyPrinter, etc. + - The JPEG autodetection didn't work with some JPEG files that + came from digital cameras (JPEG but not JFIF); the new + magic types should work with all images that the JPEG library + can handle. + - Fixed a bug in the new contains() MIME type rule that could + cause cupsd to crash. + - Switched to using strtol() in the MIME type code so that you + can use hex, octal, or decimal constants as desired in the + mime.types file. + - Banner files are now treated as templates, allowing any type + of file to be used as a banner. + - Added a 30-second timeout to backend device reports so that a + hung backend will not prevent the scheduler from starting. + - Backends are once again terminated when jobs are stopped; the + CUPS-supplied backends will stay alive until the downstream + filters have had a chance to clear out old page data. + - The charset lookup in the CUPS localization support was wrong + (iso8859-x instead of iso-8859-x) + - Changed the "cpNNNN" code page files to "windows-NNNN" to match + the IANA registrations. + - New PostScript banner pages. + - Added Windows BMP and Alias PIX image file support to the image + filter. + - The PNG reading coded didn't free all of its buffers. + - Added Digest authentication support to the client and server + code. + - Added Solaris options to System V commands. + - Now support the output-bin job template attribute. + - Now log the job-billing attribute in the page_log file, and + keep track of the total number of pages in the + job-media-sheets-completed attribute. + - The penwidth option is now in micrometers to support more + accurate width specification. + - The image filters now support interlaced and transparent PNG + files. + - Didn't handle Keep-Alive for HTTP/1.0 clients. + - The BrowsePoll support didn't handle when BrowseInterval + was set to 0 (now uses 30 seconds if BrowseInterval is 0) + - The DeskJet driver now supports 600 DPI color for printers + that support it. + - New lpinfo and lpmove commands. + - The lpq command now supports the Digital UNIX output format. + - The LPD mini-daemon now supports all required LPD operations. + - Implemented timeouts for multi-file documents. + - New cupsPrintFiles() function in the CUPS API library to + print multiple files using create-job and send-document + requests (1 job ID for multiple files) + - The lp command now sends multiple files as a single job, + matching the behavior of the System V command. + - The "cancel -a" command now purges job history files. + + +CHANGES IN CUPS v1.1b3 + + - Documentation updates. + - The startup script redirected stderr before stdout, + which caused problems with some versions of Bourne + shell and Bash. + - Fixed a bug in the scheduler's PPD language reading + code. + - Fixed a bug in the scheduler's check for the + manufacturer in the PPD. + - The pstoraster filter didn't allow some input and + output attributes to be set. + - Added banner page support. + - Added missing PAM configuration file. + - Configuration script fixes for Linux and *BSD. + - The log file code was using the wrong sign for the + timezone offset. + - The default printcap file is now empty (no printcap + file is generated). + - The scheduler did not start jobs destined for remote + printers when they became available. + - The scheduler now sends jobs to remote printers + immediately. (when sending jobs to a class, the remote + printer is only used when it becomes available) + - The scheduler now supports printing of banner pages + via the job-sheets attribute (banner files go in + /usr/share/cups/banners) + - The cupsd process now forks itself into the background + (override with -f) + - Added several *BSD enhancements. + - Added UNSUPPORTED libtool option to configuration + script to allow the use of libtool. Note that this is + UNSUPPORTED by us, but added by request of the *BSD + folks. + - The parallel, serial, and usb backends now retry the + opening of their ports. This allows multiple print + queues to be associated with a single physical port, + and will allow CUPS to support several types of + parallel port auto-switches in the near future. + - Set-Job-Attributes now supports adding, changing, and + deleting job template attributes, and no longer allows + job-printer-uri to be set (see CUPS-Move-Job) + - Added CUPS-Move-Job operation to support moving of jobs. + - The CGI template functionality now supports multiple + languages (still only have templates for English) + - The CUPS-Get-Printers and CUPS-Get-Classes operations + now support filtering as defined in the IDD. + - The Get-Jobs, CUPS-Get-Printers, and CUPS-Get-Classes + operations no longer limit themselves to 1000 jobs, + printers, or classes (believe it or not, this is + needed for some sites) + - The web interfaces now support language-specific + templates. + - The web admin interface now supports class management. + - The web admin interface now shows a list of + manufacturers before selecting the PPD/driver for a + specific printer. + - The web admin interface now supports configuration of + the default printer options in the PPD file. + - The web interface now uses printer/class + authentication for the test page instead of admin + authentication. + - Updated the RPM spec file for the current release. + - Updated language support for Windows code pages. + - 8-bit character set files can now use multiple fonts + (needed for Arabic, Greek, Hebrew, etc.) + - Added basic right-to-left text support in the text + filter. + - The POSIX locale now uses ISO-8859-1 instead of + US-ASCII. + - Fixed PDF printing problems. + - Fixed PostScript RIP page device dictionary elements + that weren't getting passed in cups_get_params(). + - Added a new "contains" rule for the magic file typing. + - The "printable" rule now accepts characters from 128 to 255 + (needed for Microsoft character sets) + - Added support for ~/.cupsrc as well as /etc/cups/client.conf + so that the default server can be configured on a per-user + basis without environment variables. + - Added LPD mini-daemon to support incoming LPD jobs. + + +CHANGES IN CUPS v1.1b2 + + - Documentation updates. + - The lp command didn't always load the user-defined + destinations, preventing it from seeing the default + printer. + - Many configure script and makefile fixes. + - The Microsoft code page files were missing from the + distribution. + - Added a workaround for the HP IPP client (which is sending + an invalid printer-uri in requests) + - Fixed the encoding of text-with-language and name-with-language + to match the IPP spec. + - Added support for unknown value tags in the IPP routines + (previously they would be ignored) + - Integrated GNU GhostScript 5.50 into the pstoraster filter. + - Client hostname resolution was broken on little-endian + machines. + - Now look at client.conf file for client's default server + and printer. + - The cupsServer() function did not close the client.conf file + if it contained a ServerName directive. + - Added BrowseAllow, BrowseDeny, BrowseOrder, BrowsePoll, and + BrowseRelay directives. + - BrowseInterval 0 disables advertising of local printers, but + still receives information on remote printers. + - New browse polling daemon (for polling servers on different + networks) + - New PPD cache file for faster startup times with large numbers + of PPD files. + - The Host: field was incorrectly required for HTTP/1.0 clients. + - New set-job-attributes operation now supported. + - The mime_load_types() and mime_load_convs() functions did not + close their input files. + + +CHANGES IN CUPS v1.1b1 + + - NEW web-based administration interface. + - NEW EPSON printer drivers. + - NEW user-defined printers and options. + - NEW persistent jobs and job history + - NEW IPP/1.1 support + - NEW template-based web interfaces. + - NEW CUPS-get-devices and CUPS-get-ppds operations. + - NEW support for create-job and send-file operations. + - NEW certificate-based authentication for local + administration. + - NEW USB backend. + - The lpr command now produces human-readable error messages. + - The lpq command now produces BSD standard format output + instead of OSF/1 output. This should resolve the SAMBA + print queue problems that have been reported. + - The IPP backend did not always detect when the "raw" option + was being used. + - The "lpstat -p" command would stop after the first active + printer. + - The "lpstat -v" command would stop before the first remote + printer. diff --git a/CHANGES-1.2.txt b/CHANGES-1.2.txt new file mode 100644 index 0000000..7b0b1f2 --- /dev/null +++ b/CHANGES-1.2.txt @@ -0,0 +1,1261 @@ +CHANGES-1.2.txt +--------------- + +CHANGES IN CUPS V1.2.12 + + - The PHP cups_print_file() function crashed if the options + array contained non-string option values (STR #2430) + - The image/tiff file matching rule incorrectly identified + some text files as TIFF files (STR #2431) + - The filter(7) man page incorrectly documented the + "PAGE: total #-pages" message (STR #2427) + - PCL text files were mis-identified as HP-GL/2 and + caused the HP-GL/2 filter to hang (STR #2423) + - When printing to a queue with user ACLs, the scheduler + incorrectly returned a quota error instead of a "not + allowed to print" error (STR #2409) + - cupsaddsmb could get in a loop if no printer drivers + were installed (STR #2407) + - cupsRasterReadHeader() did not byte-swap the header + properly when compiled with certain versions of GCC. + - The IPP backend did not send the document-format + attribute for filtered jobs (STR #2411) + - Some PPD files could cause a crash in ppdOpen2 (STR + #2408) + - The web admin interface incorrectly handled the "share + printers" and "show remote printers" settings (STR + #2393) + - The scheduler's log messages about AuthClass and + AuthGroupName advised using a replacement directive but + had the wrong syntax (STR #2400) + - Updated the PostScript/PJL and HP-GL/2 MIME rules to + look in the first 4k of the file, not just the first 1k + (STR #2386) + - Updated the Italian localization (STR #2382) + + +CHANGES IN CUPS V1.2.11 + + - Fixed the "relaying from" log message (STR #2376) + - Updated the launchd support on Mac OS X to better + support reconfiguration. + - "make distclean" didn't remove all generated files + (STR #2366) + - Fixed a bug in the advertisement of classes (STR + #2373) + - The IPP backend now stays running until the job is + actually printed by the remote server; previously + it would stop monitoring the job if it was held or + temporarily stopped (STR #2352) + - PDF files were not always printed using the correct + orientation (STR #2348) + - The scheduler could crash if you specified a bad file: + URI for a printer (STR #2351) + - The Renew-Subscription operation now returns the + notify-lease-duration value that was used (STR #2346) + - The IPP backend sent job options to IPP printers, + however some printers tried to override the options + embedded in the PS/PCL stream with those job options + (STR #2349) + - ppdLocalize() now also tries a country-specific + localization for when localizing to a generic locale + name. + - The cupstestppd program now allows for partial + localizations to reduce the size of universal PPD + files. + - Chinese PPD files were incorrectly tagged with the + "cn" locale (should have been "zh") + - The backends now manage the printer-state-reasons + attribute more accurately (STR #2345) + - Java, PHP, Perl, and Python scripts did not work + properly (STR #2342) + - The scheduler would take forever to start if the + maximum number of file descriptors was set to + "unlimited" (STR #2329) + - The page-ranges option was incorrectly applied to the + banner pages (STR #2336) + - Fixed some GCC compile warnings (STR #2340) + - The DBUS notification code was broken for older + versions of DBUS (STR #2327) + - The IPv6 code did not compile on HP-UX 11.23 (STR + #2331) + - PPD constraints did not work properly with custom + options. + - Regular PPD options with the name "CustomFoo" did + not work. + - The USB backend did not work on NetBSD (STR #2324) + - The printer-state-reasons attribute was incorrectly + cleared after a job completed (STR #2323) + - The scheduler did not set the printer operation policy + on startup, only on soft reload (STR #2319) + - The AP_FIRSTPAGE_InputSlot option did not clear any + ManualFeed setting that was made, which caused problems + with some PPD files (STR #2318) + - cupsDoFileRequest() and cupsDoRequest() did not abort + when getting an error in the response (STR #2315) + - The scheduler did not schedule jobs properly to remote + or nested classes (STR #2317) + - Updated the mime.types and mime.convs headers to warn + that the files are overwritten when CUPS is installed. + Local changes should go in local.types or local.convs, + respectively (STR #2310) + - The scheduler could get in an infinite loop if a + printer in an implicit class disappeared (STR #2311) + - The pstops filter did not handle %%EndFeature comments + properly (STR #2306) + - Fixed a problem with the Polish web page printer icons + (STR #2305) + - ppdLocalize() now also localizes the cupsICCProfile + attributes. + - The scheduler still had a reference to the incorrect + "notify-recipient" attribute (STR #2307) + - The "make check" and "make test" subscription tests did + not set the locale (STR #2307) + - The "make check" and "make test" subscription tests + incorrectly used the notify-recipient attribute instead + of notify-recipient-uri (STR #2307) + - cupsRasterInterpretPPD() incorrectly limited the + cupsBorderlessScalingFactor when specified in the + job options. + + +CHANGES IN CUPS V1.2.10 + + - ppdLocalize() now supports localizing for Japanese + using the "jp" locale name used by the ppdmerge + program from the CUPS DDK 1.1.0 (STR #2301) + - _cupsAdminSetServerSettings() did not support changing + of top-level directives as designed. + - The init script path check was broken. + - CUPS incorrectly used the attribute "notify-recipient" + instead of "notify-recicpient-uri" in several places + (STR #2297) + - Fixed a configure script bug on MirBSD (STR #2294) + - The pdftops filter did not limit the amount of recursion + of page sets (STR #2293) + - Custom page sizes with fractional point sizes did not + work (STR #2296) + - The lpoptions command would crash when adding or removing + options on a system with no printers (STR #2295) + + +CHANGES IN CUPS V1.2.9 + + - The scheduler did not use the default job-sheets + (banners) for implicit classes (STR #2284) + - The scheduler could crash when listing complete jobs + that had been unloaded from memory (STR #2288) + - The French localization was doubled up (STR #2287) + - Build system fixes for several platforms (STR #2260, + STR #2275) + - The scheduler's openssl certificate generation code was + broken on some platforms (STR #2282) + - The scheduler's log rotation check for devices was + broken (STR #2278) + - The LPD mini-daemon did not handle the document-format + option correctly (STR #2266) + - The pdftops filter ignored the "match" size option in the + pdftops.conf file (STR #2285) + - cupstestppd now validates UTF-8 text strings in + globalized PPD files (STR #2283) + - The outputorder=reverse option did not work with all + printers (STR #2279) + - Classes containing other classes did not always work + (STR #2255) + - Printer location and description information was lost + if the corresponding string contained the "#" character + (STR #2254) + - cupsRemoveOption() did not work properly (STR #2264) + - The USB backend did not work with some USB to parallel + cables on Mac OS X. + - The test page did not print the rulers properly on + large media sizes (STR #2252) + - The text filter could crash when pretty printing certain + types of files (STR #2158) + + +CHANGES IN CUPS V1.2.8 + + - Documentation fixes (STR #2141, STR #2157) + - The HTTP upgrade redirection used by the scheduler did + not work with Internet Explorer (STR #2235) + - Members of a class with Unicode names did not appear + correctly in the web interface (STR #2154) + - Changing the "Save debugging information" setting in + the web interface no longer affects the other server + settings (STR #1993) + - The scheduler did not choose SSL certificates correctly + on Mac OS X (STR #2225) + - The scheduler could get in an infinite loop when + printing to a remote class (STR #2228) + - The jobs web page did not have separating space after + the number of pages column (STR #2230) + - Added French localization (STR #2221) + - Updated Spanish localization (STR #2223) + - Updated Japanese localization (STR #2216) + - cupsBorderlessScalingFacter was limited to a range of + 0.9 to 1.1, but some printers need larger values (STR + #2222) + - Landscape printing of PDF files did not always work + (STR #2149) + - Fixed slow USB printing on Minolta printers (STR #2104, + STR #2219) + - The ZPL label printer driver could produce stretched + output (PR #6448) + - The IPP backend now clears the printer-state-message + when there are no outstanding errors or warnings (STR + #2126) + - The CUPS Java scripting support did not work with + recent versions of Java due to the use of Sun's private + Base64 class (STR #2152) + - The scheduler did not pass HTTP GET form variables to + custom CGI programs (STR #2173) + - The lpoptions command now displays the reason why a PPD + file cannot be found (STR #2184) + - The scheduler did not accept "none" as a browse + protocol name (STR #2200) + - The scheduler still loaded the remote printer cache, + even when browsing was disabled (STR #2198) + - The SNMP backend now shows OfficeJet printers with the + "HP" manufacturer prefix (STR #2151) + - Web interface HTML cleanup (STR #2153) + - The parallel backend consumed 100% CPU on FreeBSD due + to an apparently common parallel port driver bug (STR + #2161) + - ippReadIO() incorrectly returned IPP_IDLE when the + initial IPP message header could not be read (STR + #2179) + - cupsRasterInterpretPPD() did not support custom options + (STR #1960) + - Collated output produced by the PostScript filter could + lose some options (STR #2137) + - job-hold-until with time values for the next day would + be held for 60 days (STR #2144) + - Some types of Sun raster files did not print correctly + (STR #2107) + - Raw PBM files did not print correctly (STR #2106) + - The SNMP backend no longer uses IPP with HP printers, + as some recent firmware versions appear to not work + (STR #2055) + - cupsMarkOptions() did not handle the + multiple-document-handling option (STR #2135) + - lpstat did not show the local job ID of active printers + (STR #2125) + - The backends incorrectly used STATUS: + media-tray-empty-error messages for out-of-paper + conditions (STR #2123, STR #2124) + - cupsGetPPD2() returned the wrong error when the PPD + file did not exist (STR #2122) + - cupsDoAuthentication() did not translate the password + prompt (STR #2121) + - httpGetLength2() did not handle error messages without + content correctly (STR #2133) + - Added support for 32/64-bit libraries on HP-UX Itanium + systems (STR #2115) + - Fixed a configure script problem with the 32/64-bit + library support (STR #2114) + - The PostScript filter did not properly output document + setup commands for reversed output (STR #2111) + - The scheduler did not parse IPv6 netmasks properly (STR + #2117) + + +CHANGES IN CUPS V1.2.7 + + - Documentation updates (STR #2089) + - Added an Italian translation (STR #2105) + - The PostScript filter now rotates the bounding box + values as needed (STR #2079) + - The scheduler no longer loads the remote printer cache + when browsing is disabled (STR #2084) + - The scheduler no longer writes a new launchd + configuration file if it doesn't have to (STR #2083) + - Updated the USB and PAP backends for Mac OS X (STR + #2086) + - The scheduler now picks up on changes to IPv6 and DNS + configuration on Mac OS X (STR #2085) + - The lpstat program could still hang (STR #2098) + - Fixed an inefficiency in the SNMP IPP detection code + (STR #2100) + - The SSL negotiation code did not implement short + timeouts (STR #2091) + + +CHANGES IN CUPS V1.2.6 + + - The web interface was not localized on Mac OS X (STR + #2075) + - "lpc status" did not show the number of queued jobs for + disabled queues (STR #2069) + - The lpstat program could hang (STR #2073) + - The serial backend did not support the new USB serial + filenames on Linux (STR #2061) + - The parallel backend did not support bidirectional I/O + properly (STR #2056) + - The network backends now log the numeric address that + is being used (STR #2046) + - Fixed a compile error when using libpaper. + - Fixed a compile error when compiling on Solaris with + threading enabled (STR #2049, STR #2050) + - Missing printer-state-changed event for + printer-state-message updates (STR #2047) + + +CHANGES IN CUPS V1.2.5 + + - Documentation updates (STR #2038) + - The SNMP backend no longer uses IPP for Epson printers + (STR #2028) + - Updated the configure script for Tru64 UNIX 5.1 (STR + #2033) + - Tru64 5.1B's getaddrinfo() and getnameinfo() functions + leak file descriptors (STR #2034) + - cupsAddDest() didn't add the parent destination's + options and attributes. + - ppdConflicts() did not handle custom option + constraints. + - Raw printing of gzip'd files did not work (STR #2009) + - The scheduler no longer preserves default option + choices when the new PPD no longer provides the old + default choice (STR #1929) + - The Linux SCSI backend is now only built if the SCSI + development headers are installed. + - USB printing to Minolta printers did not work (STR + #2019) + - Windows clients could not monitor the queue status (STR + #2006) + - The scheduler didn't log the operation name in the + access_log file for Create-Job and Print-Job requests. + - The PostScript filter now separates collated copies + with any required JCL commands so that JCL-based + finishing options act on the individual copies and not + all of the copies as a single document. + - The PostScript filter now disables duplex printing when + printing a 1-page document. + - cups-lpd didn't pass the correct + job-originating-host-name value (STR #2023) + - Fixed some speling errors in the German message catalog + (STR #2012) + - cupstestppd did not catch PPD files with bad + UIConstraints values (STR #2016) + - The USB backend did not work with the current udev- + created printers if the first printer was disconnected + (STR #2017) + - Mirrored and rotated printing did not work with some + documents (STR #2004) + - 2-sided printing with banners did not work properly on + some printers (STR #2018) + - Updated the raw type rule to handle PJL within the + first 4k of a print job (STR #1969) + - Added an Estonian translation (STR #1957) + - Clarified the documentation for the cupsd.conf @LOCAL + and @IF(name) allow/deny functionality (STR #1992) + - The PostScript filters did not escape the Title and For + comments in the print job header (STR #1988) + - The scheduler would use 100% CPU if browsing was + disabled and the cupsd.conf file contained BrowsePoll + lines (STR #1994) + - The cupsDirRead() function did not work properly on + non-POSIX-compliant systems (STR #2001) + - The cupsFile functions didn't handle read/write errors + properly (STR #1996) + - The DBUS support now works with older versions of the + DBUS library. + + +CHANGES IN CUPS V1.2.4 + + - The --with-printcap configure option did not work (STR + #1984) + - The character set reported by cupsLangGet() did not + always reflect the default character set of a given + locale (STR #1983) + - Older Lexmark and Tektronix printers did not work with + IPP (STR #1980) + - Failsafe printing did not work (PR #6328) + - Some web interface redirects did not work (STR #1978) + - The web interface change settings button could + introduce a "Port 0" line in cupsd.conf if there was no + loopback connection available (STR #1979) + - The web interface change settings and edit + configuration file buttons would truncate the + cupsd.conf file (STR #1976) + - The German web interface used the wrong printer icon + images (STR #1973) + - The "All Documents" link in the on-line help was + missing a trailing slash (STR #1971) + - The Polish web interface translation used the wrong + URLs for the job history (STR #1963) + - The "reprint job" button did not work (STR #1956) + - The scheduler did not always report printer or job + events properly (STR #1955) + - The scheduler always stopped the queue on error, + regardless of the exit code, if the error policy was + set to "stop-printer" (STR #1959) + - ppdEmitJCL() included UTF-8 characters in the JCL job + name, which caused problems on some printers (STR + #1959) + - Fixed a buffering problem that cause high CPU usage + (STR #1968) + - The command-line applications did not convert + command-line strings to UTF-8 as needed (STR #1958) + - cupsDirRead() incorrectly aborted when reading a + symbolic link that pointed to a file/directory that did + not exist (STR #1953) + - The cupsInterpretRasterPPD() function did not handle + custom page sizes properly. + + +CHANGES IN CUPS V1.2.3 + + - The scheduler did not send job-state or + job-config-changed events when a job was held, + released, or changed (STR #1947) + - The scheduler now aborts if the configuration file and + directory checks fail (STR #1941) + - Fixed a problem with ippPort() not using the port + number that was set via the client.conf file or + CUPS_SERVER environment variable (STR #1945) + - HTTP headers were not buffered (STR #1899) + - Some IPP printers (HP) did not like UTF-8 job names + (STR #1837) + - The CUPS desktop icon is now localized for Polish (STR + #1920) + - Printer options were not always honored when printing + from Windows clients (STR #1839) + - The openssl command would lock up the scheduler when + generating an encryption certificate on some platforms + due to a lack of entropy for the random number + generator (STR #1876) + - The web admin page did not recognize that "Listen 631" + enabled remote access (STR #1908) + - The web admin page did not check whether changes were + made to the Basic Server Settings check boxes (STR + #1908) + - The IPP backend could generate N*N copies in certain + edge cases. + - The scheduler did not restore remote printers properly + when BrowseShortNames was enabled (STR #1893) + - Polling did not handle changes to the network + environment on Mac OS X (STR #1896) + - The "make test" subscription tests used invalid + notify-recipient-uri values (STR #1910) + - Printers could be left in an undefined state on system + sleep (STR #1905) + - The Berkeley and System V commands did not always use + the expected character set (STR #1915) + - Remote printing fixes (STR #1881) + - The cupstestppd utility did not validate translation + strings for custom options properly. + - Multi-language PPD files were not properly localized in + the web interface (STR #1913) + - The admin page's simple settings options did not check + for local domain socket or IPv6 addresses and did not + use "localhost" as the listen address. + - An empty BrowseProtocols, BrowseLocalProtocols, or + BrowseRemoteProtocols line would crash the scheduler + instead of disabling the corresponding browsing options. + - The scheduler now logs IPP operation status as debug + messages instead of info or error. + - cupsFileRewind() didn't clear the end-of-file state. + - cupstestppd didn't report the actual misspelling of the + 1284DeviceID attribute (STR #1849) + - BrowseRelay didn't work on Debian (STR #1887) + - configure --without-languages didn't work (STR #1879) + - Manually added remote printers did not work (STR #1881) + - The header was not installed. + - Updated the build files for Autoconf 2.60 (STR #1853) + - The scheduler incorrectly terminated the polling + processes after receiving a partial log line. + - The cups-lpd mini-daemon reported "No printer-state + attribute found" errors when reporting the queue status + (PR #6250, STR #1821) + - SNMP backend improvements (STR #1737, STR #1742, STR + #1790, STR #1835, STR #1880) + - The scheduler erroneously reported an error with the + CGI pipe (STR #1860) + - Fixed HP-UX compile problems (STR #1858, STR #1859) + - cupstestppd crashed with some PPD files (STR #1864) + - The and header files did not + work with C++. + + +CHANGES IN CUPS V1.2.2 + + - Documentation updates (STR #1765, STR #1780) + - CUPS didn't know about alternate character set names + for Asian text (STR #1819) + - The lpoptions -o and -r options did not work unless you + specified a printer. + - The lpoptions command incorrectly allowed users to set + printer attributes like printer-type (STR #1791) + - httpWait() did not flush the write buffer, causing "bad + request" errors when communicating with CUPS 1.1.x + servers (STR #1717) + - Polling did not sanitize the printer description, + location, or make and model strings like broadcasts + did. + - Polled printers did not show the server's default + job-sheets option value. + - The Samba password prompt was not properly localized + (STR #1814) + - Added a German translation (STR #1842) + - The scheduler now creates self-signed SSL certficates + automatically when using OpenSSL and CDSA for + encryption, just as for GNU TLS. + - The SNMP backend sporatically reported some printers as + "unknown" (STR #1774) + - The scheduler now forces BrowseTimeout to be at least + twice the BrowseInterval value and non-zero to avoid + common configuration errors. + - The scheduler incorrectly returned printer URIs of the + form "ipp://server/printers/classname" for classes (STR + #1813) + - Updated Japanese localization (STR #1805) + - The scheduler's SSL certificate/key directory was not + created on installation (STR #1788) + - Added a mailto.conf man page and help page (STR #1754) + - The parallel and USB backends no longer wait for the + printer to go on-line - this caused problems with + certain printers that don't follow with the IEEE-1284 + standard (STR #1738) + - The scheduler could crash on a reload when implicit + classes were present (STR #1828) + - The IPP backend incorrectly used the CUPS_ENCRYPTION + environment variable to determine the default + encryption mode when printing (STR #1820) + - USB printing did not work on Solaris (STR #1756) + - The scheduler sorted job priorities in the wrong order + (STR #1811) + - The scheduler did not automatically restart notifiers + that exited or crashed (STR #1793) + - IPv6 support did not work on NetBSD (STR #1834) + - The EPM packaging file did not work (STR #1804) + - The scheduler used up the CPU if BrowseRemoteProtocols + was empty (STR #1792) + - Custom page sizes did not work (STR #1787) + - The SNMP backend could crash on some systems when SNMP + logging was enabled (STR #1789) + - Browsing could produce some funny printer names when + ServerName was set to an IP address (STR #1799) + - Fixed the log message for BrowseRelay (STR #1798) + - Fixes to allow CUPS to compile on MirBSD (STR #1796) + - The scheduler incorrectly set the FINAL_CONTENT_TYPE + environment variable (STR #1795) + - The pdftops filter incorrectly embedded a "produced by" + comment, causing PDF printing not to work on some + operating systems (STR #1801) + - Sending raw jobs from a client system could cause the + client's scheduler to eventually crash (STR #1786) + - The scheduler now checks that the notifier exists prior + to accepting a new subscription request. + - The scheduler now reports the supported + notify-recipient schemes based on the contents of the + ServerBin/notifier directory. + - Event notifications did not include the + notify-sequence-number or other required attributes + (STR #1747) + - Allow/Deny addresses of the form "11.22.33.*" did not + work on Linux (STR #1769) + - cupsGetPPD() did not work if the scheduler was only + listening on a domain socket (STR #1766) + - The scheduler could crash advertising a class (STR + #1768) + - The scheduler could crash if the default printer was + deleted (STR #1776) + - Added a new default CUPS raster format (v3) which does + not compress the raster stream in order to provide the + same cupsRasterReadPixels() and cupsRasterWritePixels() + performance as CUPS 1.1.x. + - The cupsaddsmb man page listed the wrong files for the + CUPS driver. + - Some configure --with options did not work (STR #1746) + - "Allow @IF(name)" didn't work if "name" wasn't the + first network interface (STR #1758) + - The lpstat command did not use the correct character + set when reporting the date and time (STR #1751) + - The cupsaddsmb command and web interface did not update + the Windows PPD files properly, resulting in corrupt + PPD files for the Windows client to use (STR #1750) + - The cupsd.conf man page didn't describe the Listen + domain socket syntax (STR #1753) + - The scheduler no longer tries to support more than + FD_SETSIZE file descriptors. + - CDSA (encryption) support fixes for MacOS X. + - The lppasswd program needs to be setuid to root to + create and update the /etc/cups/passwd.md5 file (STR + #1735) + - 32/64-bit library installation was broken (STR #1741) + - The USB backend now reports a "no such device" error + when using the old filename-based USB URIs instead of + the "success" error. + - Increased the HTTP and IPP read timeouts to 10 seconds, + as 1 second was too short on congested networks (STR + #1719) + - The SNMP backend now uses the device description over + the printer-make-and-model attribute when the attribute + contains a generic name (STR #1728) + - Fixed another file descriptor leak when printing raw + files (STR #1736) + - Raw queues were not shared via LDAP (STR #1739) + - The pstops filter didn't always embed PageSetup + commands from the PPD file (STR #1740) + - "make install" didn't work if you disabled all of the + localizations. + - The scheduler didn't always choose the least costly + filter. + - Fixed parsing of IPv6 addresses in Allow, Deny, + BrowseAllow, BrowseDeny, and BrowseRelay directives + (STR #1713) + - Printers that were shared via LDAP did not get added to + the LDAP server properly (STR #1733) + - LDAP browsing would crash the scheduler if a required + value was missing (STR #1731) + - Special cases for the "localhost" hostname did not + work, causing printing to not work when the /etc/hosts + file did not contain a localhost entry (STR #1723) + - Updated the Spanish translation (STR #1720, STR #1770) + - Reverse-order page output was broken when N-up or + landscape orientations were used (STR #1725) + - The parallel, serial, socket, and USB backends needed + print data before they would report back-channel data, + causing problems with several new drivers (STR #1724) + + +CHANGES IN CUPS V1.2.1 + + - "lprm -h hostname" did not work (STR #1800) + - The web interface did not handle reloads properly for + MSIE (STR #1716) + - The configure script no longer adds linker rpath + options when they are unnecessary. + - The scheduler could crash printing a debug message on + Solaris (STR #1714) + - The --enable-32bit and --enable-64bit configure options + did not always work. + - The password prompt showed the domain socket address + instead of "localhost" for local authentication (STR + #1706) + - The web interface filtered the list of printers even if + the user wasn't logged in (STR #1700) + - The IPP backend did not work reliably with some Xerox + printers (STR #1704) + - Trailing banners were not added when printing a single + file (STR #1698) + - The web interface support programs crashed on Solaris + (STR #1699) + - cupstestppd incorrectly reported problems with + *1284DeviceID attributes (STR #1710) + - Browsing could get disabled after a restart (STR #1670) + - Custom page sizes were not parsed properly (STR #1709) + - The -U option wasn't supported by lpadmin (STR #1702) + - The -u option didn't work with lpadmin (STR #1703) + - The scheduler did not create non-blocking back-channel + pipes, which caused problems when the printer driver + did not read the back-channel data (STR #1705) + - The scheduler no longer uses chunking in responses to + clients - this caused problems with older versions of + CUPS like 1.1.17 (PR #6143) + - Automatic raw printing was broken (STR #1667) + - 6-up printing was broken (STR #1697) + - The pstops filter did not disable CTRL-D processing on + the printer/RIP. + - ppdOpen*() did not load custom options properly (STR + #1680) + - "Set Printer Options" in the web interface did not + update the DefaultImageableArea or + DefaultPaperDimension attributes in the PPD file (STR + #1689) + - Fixed compile errors (STR #1682, STR #1684, STR #1685, + STR #1690) + - The lpstat command displayed the wrong error message + for a missing destination (STR #1683) + - Revised and completed the Polish translation (STR + #1669) + - Stopped jobs did not show up in the list of active jobs + (STR #1676) + - The configure script did not use the GNU TLS + "libgnutls-config" script to find the proper compiler + and linker options. + - The imagetoraster filter did not correctly generate + several 1, 2, and 4-bit color modes. + - cupsRasterWritePixels() could lose track of the current + output row. + - cupsRasterReadPixels() did not automatically swap + 12/16-bit chunked pixel data. + - Moved the private _cups_raster_s structure out of the + public header. + - Updated the CUPS raster format specification to include + encoding rules and colorspace definitions. + - The Zebra PPD files had the wrong PostScript code for + the "default" option choices. + - The imagetoraster filter did not generate correct CIE + XYZ or Lab color data. + - The cups-config script did not work when invoked from a + source directory (STR #1673) + - The SNMP backend did not compile on systems that used + the getifaddrs emulation functions (STR #1668) + + +CHANGES IN CUPS V1.2.0 + + - Documentation updates (STR #1618, STR #1620, STR #1622, + STR #1637) + - Static file copy buffers reduced from 64k to 32k to + work around bogus MallocDebug library assumptions (STR + #1660) + - The scheduler did not decode the backend exit code + properly (STR #1648) + - The MacOS X USB backend did not report the 1284 device ID, + nor did it fix device IDs returned by HP printers. + - The scheduler started more slowly than 1.1.x with large + numbers of printers (STR #1653) + - cupsRasterInterpretPPD() didn't support the + cupsPreferredBitsPerColor attribute, and imagetoraster + didn't use the new API. + - The "make test" script did not create all of the necessary + subdirectories for testing (STR #1638) + - The scheduler did not prevent rotation of logs + redirected to /dev/null (STR #1651) + - "make test" did not include the SNMP backend in the + test environment (STR #1625) + - The EPM packaging files did not work (STR #1621) + - "Use Default Configuration" inserted a broken + configuration file (STR #1624) + - Redirects in the web interface did not always preserve + the encrypted status of a connection (STR #1603) + - Added the Apple "pap" backend. + - Added CUPS library to CUPS Image shared library + linkage to support Linux --as-needed linker option + (STR #1606) + - Fixed support for --enable-pie (STR #1609) + - The pdftops filter did not validate the length of the + encryption key (STR #1608) + - Updated the Polish localization. + - "Encryption Required" in the cupsd.conf file now only + requires encryption when the connection is not over the + loopback interface or domain socket. + - Printer names containing "+" were not quoted properly in + the web interface (STR #1600) + - The SNMP backend now reports the make and model in the + information string so that the auto-generated printer + name is more useful than just an IP address. + + +CHANGES IN CUPS V1.2rc3 + + - The cups-lpd program always did reverse lookups on the + client address, which could be a performance problem. + Added a "-n" option to disable lookups. + - When configured with SSL support, require encryption by + default when displaying the /admin location (STR #1592) + - The next job ID was not computed correctly if the job + cache file got out of sync with the spool directory + (STR #1582) + - The PNG image handling code used deprecated functions + from libpng (STR #1587) + - Added a Polish translation (STR #1584, STR #1586) + - More changes to the scheduler to improve battery life + on portable devices (STR #1583) + - Changed the default log level for status messages back + to "DEBUG" to be consistent with CUPS 1.1.x (STR #1579) + - The error string was not set properly when + cupsDoFileRequest() was given the name of a directory + (STR #1578) + - Fixed handling of job-hold-until (STR #1581) + - Added explicit notes to the cupsaddsmb man page + explaining that the driver filenames are case-sensitive + under UNIX and that they must be all lowercase (Windows + 2000) or all UPPERCASE (Windows 95/98/Me) to work (STR + #1568) + - The USB backend incorrectly split the manufacturer name + if it contained spaces (STR #1566) + - The scheduler would hang when listing PPD files for a + manufacturer whose name contained spaces (STR #1567) + - Added the SNMP backend for network printer discovery + (STR #1555) + - cupstestppd now fails PPD files with 1284DeviceId + instead of 1284DeviceID, and cups-driverd uses a + case-insensitive comparison when looking for it (STR + #1573) + - cupsDoFileRequest() and cupsDoRequest() now work + properly with non-blocking HTTP connections. + - Added Swedish translation (STR #1569) + - "make install" now installs the MIME files with world + read permissions (STR #1565) + - More CDSA encryption support fixes (STR #1563) + - Updated the default mime.types file to support printing + of files that do not have a locally-recognized MIME + media type to raw or System V queues. + - Updated the serial port detection code on Linux (STR + #1562) + - Added some more error checking to httpGetHostname() + (STR #1561) + - The title of some administration pages was not + localized (STR #1548) + - The edit-config.tmpl file was not generated or + installed for the Spanish or Japanese localizations + (STR #1547) + - The mimeDelete() function freed the types before the + filters, but the filters needed the type data (STR #1558) + - The scheduler didn't keep track of the status pipes + properly, leading to a bad select() for multi-file jobs + (STR #1559) + - The cupstestdsc program didn't validate the ordinal + page number value for %%Page: comments. + + +CHANGES IN CUPS V1.2rc2 + + - The scheduler was not always using the string pool, + causing random crashes. + - The lpmove and the web interface's Move Job button did + not work with stopped jobs (STR #1534) + - The PostScript filter did not handle the page-set + option properly with number-up printing (STR #1543) + - The scheduler now only warns about unsupported ACLs + once (STR #1532) + - The "fitplot" option did not work with output from + Mozilla (STR #1542) + - The imagetops filter did not work with Level 2 or 3 + printers (STR #1533) + - The scheduler now recognizes PostScript files with PJL + commands that do not include an ENTER LANGUAGE command. + - Added --with-printcap configure option. + - 64-bit SSL fixes for MacOS X. + - The scheduler didn't send some printer state change + events. + - The scheduler didn't send jobs to busy remote printers. + - Fixed some problems with the launchd support. + - Added new USB printer backend for MacOS X. + - The PostScript filter now handles files that start with + an incomplete PJL header (PR #6076) + - The web interface language selection code did not try + the generic language localization (STR #1531) + - The language cache, string pool, and transcoding caches + are now process global instead of per-thread to avoid + problems with GNOME and to allow for data sharing + between threads (STR #1530) + - Fixed a CUPS 1.1.x compatibility bug (STR #1528) + - The web interface redirection after certain printer + administration tasks was broken (STR #1516) + - Web interface authorization could get stuck (STR #1512) + - Localization updates (STR #1513, STR #1518, STR #1520) + - The pstops filter didn't work with some files (STR + #1523) + - "./configure --enable-static" didn't work (STR #1522) + - The scheduler was not using the configured default + Group (STR #1521) + - The web interface still did not show the localized time + and date for some locales and systems (STR #1509) + - httpAddrGetList() would crash on systems without + getaddrinfo(). + - Socket URIs without a trailing slash would cause the + port number to not be accepted (STR #1519) + - Local raw and System V printers were not advertised as + such for printer browsing (STR #1502) + - The RPM spec file incorrectly put duplicate copies of + the Japanese and Spanish web interface templates in the + main cups package (STR #1517) + - cupsSetDests() did not explicitly set the permissions + of the /etc/cups/lpoptions file (STR #1508) + - The lpq command crashed with the -h option (STR #1515) + + +CHANGES IN CUPS V1.2rc1 + + - Documentation updates (STR #1497, STR #1498) + - The scheduler now redirects browsers to https: URLs + when encryption is required. + - The scheduler would crash when printing with a banner + (STR #1500) + - cups-driverd did not use the LanguageEncoding attribute + in PPD files to convert the NickName to UTF-8 (STR + #1503) + - The lpadmin command could not set the + printer-error-policy attribute (STR #1504) + - The web interface did not show the time and date in the + correct format for the locale (STR #1505) + - CUPS no longer accepts print jobs if a printer does not + support the file format (STR #1501) + - Cleaned up the PostScript filter (pstops) so that it + properly supports %%IncludeFeature and page scaling + (STR #1453) + - Fixed the cupsFileRewind() and cupsFileSeek() functions + to work properly with uncompressed files. + - Added cupsFileGetLine(), cupsFileStderr(), + cupsFileStdin(), and cupsFileStdout() functions to the + CUPS library. + - Added a new cupstestdsc program to test the DSC + conformance of PostScript files. + - Added KDE/GNOME icons and a Manage Printers menu item. + - Added --enable-image and --enable-pdftops configure + options to control whether the image and PDF filters + are built and installed (default = yes for all + platforms but MacOS X) + - Fixed a minor memory leak in the PPD API. + - Fixed transcoding issues (STR #1493) + - The scheduler now enforces a minimum job cost of 100 + when doing FilterLimit checks. + - The scheduler would leak file descriptors when printing + to raw queues (STR #1491) + - The IPv6 support did not compile on Tru64 UNIX (STR + #1488) + - ppdOpen2() now converts the NickName and all UI text to + UTF-8 (STR #1475) + - The Set Allowed Users web page did not work (STR #1486) + - When the default policy was not set or set to a non- + existing policy, the scheduler did not set the default + policy name to "default" (STR #1484) + - The Zebra CPCL driver did not use the correct righthand + margin for the 4" wide label sizes. + - Fixed a problem with the parsing of fractional real + numbers in PPD files. + - Added Spanish localization files (STR #1480) + - Fixed localization of a few scheduler messages (STR + #1478) + - Fixed support for HEAD requests in the scheduler (STR + #1481) + + +CHANGES IN CUPS V1.2b2 + + - Updated the CUPS design description. + - Added --enable-32bit and --enable-64bit configure + options to allow building of separate 32/64-bit + libraries on systems that support both environments + (STR #1472) + - Various compiler warning fixes. + - Fixes for Solaris 10 builds against old GNU TLS and + LDAP libraries. + - Added a cupsArrayUserData() function to retrieve the + user data pointer for an array (useful for typing + arrays) + - The ppdEmitString() function did not compute the + required buffer size properly, leading to dropped + characters on the end of the printer commands in pstops + and imagetops (STR #1470) + + +CHANGES IN CUPS V1.2b1 + + - The serial backend now supports Equinox 8-port serial + hubs (STR #526) + - The IPP backend now supports a compression option to + compress print files as they are sent to the remote + server (STR #956) + - The CUPS browse protocol now supports passing of + default options and browse timeout values from the + server to the clients (STR #800) + - Implicit classes that timed out could cause the + scheduler to crash (STR #1439) + - Added DragonFly support in local device backends (STR + #1362) + - Added LDAP printer browsing support (STR #338) + - Added official support for printer maintenance commands + via the CUPS Command file format and hooks in the + printer-type and web interfaces (STR #932) + - The HP-GL/2 filter could get in an infinite loop trying + to convert HP-PCL files (STR #1415) + - CUPS now implements the HTTP/1.1 Expect header (STR + #1407) + - Options in PPD files are no longer automatically put in + an "Extra" group; rather, all options that are not + inside an Open/CloseGroup will be placed in the + "General" group (STR #1385) + - The scheduler now creates a job-uuid attribute that + uniquely identifies a job on a network (STR #1410) + - The init script now unsets the TMPDIR environment + variable to prevent user temporary directories from + being used by cupsd accidentally (STR #1424) + - Added support for launchd on MacOS X. + - Added support for notify_post on MacOS X. + - Added support for DBUS on Linux. + - All of the Berkeley (except for lpc) and System V + commands now support specification of user, host, and + port (STR #1028, STR #1029, STR #1087) + - The lpmove command now allows you to move all jobs for + a given queue (STR #56) + - The web interface now supports moving of a job or jobs + to another queue (STR #56) + - The web interface now provides searching, paging, and + changing of the sort/display order of classes, jobs, + and printers. + - cupsaddsmb now accepts a password on the command-line + and supports passwords with special characters (STR + #822, STR #1236) + - ppdLoad*() no longer tries to "fix" bad characters in + UI text (STR #1101) + - Printer names can now (reliably) contain Unicode + characters (STR #896) + - The lpstat command now shows the time and date of the + last printer state change instead of the hardcoded "Jan + 01 00:00" (STR #659) + - The scheduler now adds a job-actual-printer-uri + attribute to job objects when printing to a class (STR + #116) + - The scheduler now logs log file open errors to the + system log (STR #1289) + - The scheduler now sets the job-originating-user-name to + the authenticated username, if available (STR #1318) + - The scheduler now only updates the permissions of SSL + keys and certificates when they are under the + ServerRoot directory (STR #1324) + - The rastertodymo driver has been renamed to + rastertolabel (a symlink is installed so that existing + queues continue to work) and now also supports Zebra's + CPCL language. + - The lpstat command could show the wrong active job for + a printer (STR #1301) + - Fixed a potential crash problem in the scheduler when + aborting a CGI program (STR #1290) + - Added a "cancel all jobs" button to the class and + printer web interfaces (STR #1140) + - The add-printer web page now shows the + set-printer-options page after the printer has been + added (STR #690) + - The classes web page now provides links to each of the + member printers (STR #307) + - CUPS now handles HTTP request/response lines up to 32k + in length; this is mainly for better cookie support + (STR #1274) + - Added support for the Apache PassEnv and SetEnv + directives to cupsd.conf (STR #853) + - Added large file (64-bit) support (STR #541) + - Fixed a performance issue with the ippReadIO() + implementation (STR #1284) + - Fixed a performance issue with the scheduler's implicit + class implementation (STR #1283) + - The pdftops filter now adds the Title and Creator + fields from the PDF file to the PostScript document + comments section (STR #539, STR #830) + - Added a new cups_array_t and cupsArray*() functions to + the CUPS API to support sorted lists of data. + - Made the CUPS API library thread-safe (STR #1276) + - Added "media" option support for EFI EFMediaType option + (STR #902) + - Added write buffering to the HTTP code to improve + performance (STR #547) + - The scheduler now uses the attributes-natural-language + attribute to localize banner pages (STR #386) + - The scheduler now returns the address that was used to + connect to it (STR #1076) + - Fixed a problem with N-up printing and OpenOffice (STR + #576) + - Added support for the GCC position independent + executable options (STR #1209) + - Added new BrowseLocalProtocols and + BrowseRemoteProtocols directives to cupsd.conf, + allowing for different browse protocols for local and + remote printers (STR #877) + - PPD files can now contain strings up to 256k in length + (STR #1215) + - The pstops filter now supports the IncludeFeature DSC + comment (STR #1212) + - The pstops filter now disables the setpagedevice + procedure when doing N-up printing (STR #1161) + - The serial backend now supports "stop=1", "stop=2", + "parity=space", and "parity=mark" options (STR #1155) + - "make install" no longer overwrites an existing PAM + configuration file (STR #1064) + - The scheduler now closes all files on startup when run + in daemon mode (STR #1009) + - Added a new RGBW colorspace to the CUPS raster format + (STR #1071) + - The pdftops filter now sets the page size based on the + media box when not scaling the output (STR #912) + - The pdftops filter now supports masked images (STR + #281) + - The pdftops filter produced large output when rendering + PDF files containing lot of repeated images (STR #327) + - The pdftops filter now minimizes print processing of + PDF files when using the page-ranges option (STR #273) + - Updated pdftops filter to Xpdf 3.01. + - Added new cupsBackchannelRead() and + cupsBackchannelWrite() functions, as well as + backchannel support to the parallel, serial, socket, + and USB backends (STR #1252) + - The parallel and USB backends now treat a "no space + available" error as an out-of-paper condition (STR + #1225) + - The "lpc" command now supports the "status all" command + (STR #1004) + - ippReadIO() did not read collections properly (STR + #1249) + - The "make test" script now creates the test files in + "/tmp/cups-$USER" instead of "/tmp/$USER" (STR #981) + - All backends now abort on error when printing a job to + a class - this allows the next printer in the class to + print the job (STR #1084) + - The scheduler now verifies that a printer supports + Letter or A4 media sizes before setting them as the + initial default (STR #1250) + - The cupstestppd program now flags bad Resolution + options (STR #1269) + - The USB backend now retries printing when the printer + is disconnected or turned off (STR #1267) + - Added new httpGetHostname() function to CUPS API, and + use it instead of gethostname() so that the web + interface will work correctly on systems whose hostname + is not the FQDN (STR #1266) + - The scheduler now stops printers if the backend for the + queue is missing on startup (STR #1265) + - The configure script now supports "--disable-library" + to disable particular image file format support + libraries, even if they are available on the build + system (STR #1248) + - The IPP backend did not always report on the total + number of pages that were printed (STR #1251) + - The lpstat program could display garbage date and time + values for locales whose date format exceeded 31 + characters (STR #1263) + - The cupstestppd program would segfault when testing + certain broken PPD files (STR #1268) + - Dramatically reduced the overhead of implicit classes. + - Added new cupsDir*() functions to CUPS API. + - Printers can now be published individually for sharing. + - Fixed a bug in the scheduler's startup signalling code + which caused cupsd to send the SIGUSR1 signal to the + init process instead of the original parent process + (STR #1258) + - Added new on-line help CGI to web interface to provide + searchable help. + - Devices are now tracked dynamically, with each query + doing a new device scan. This eliminates a previous + startup delay caused by slow backends and allows new + printers to be seen without restarting the server, + however it limits the amount of device URI checking + that can be done (basically now the scheduler only + requires a URI with a method that is a listed backend) + - Added new printer auto-detection, server configuration, + and log file viewing to the administration web page. + - Added new "set allowed users" web interface to set the + list of allowed users for a printer or class. + - The scheduler, command-line, and web interfaces now + limit the list of printers and classes to those + accessible by a user. + - cupsMarkOptions() now handles more non-standard + duplexing options and choices (STR #915) + - cups-lpd now honors remote banner requests with the + "standard" banner whenever a printer does not have one + defined (STR #1220) + - The scheduler's denial-of-service checks did not work + properly with IPv6 addresses (STR #1134) + - The lp and lpr commands did not error out properly when + they were unable to write to a temporary file (STR + #1129) + - The pstops filter did not handle Adobe-specific + comments in Windows NT driver output (STR #1085) + - "lpstat -l -p" incorrectly reported the printer + interface (STR #936) + - The web interface now operates exclusively with the + UTF-8 encoding, and sends the appropriate character set + and header information to the web browser (STR #919, + STR #1007) + - Added a "set allowed users" interface to the web + interface so that you can set the list of allowed or + denied users/groups for a printer or class. + - Disallow the "#" character in printer names, since it + has special meaning in the shell, config files, and in + URIs (STR #917, STR #1202) + - Added a new application/x-csource MIME type, and + support for it to the texttops filter so that you can + pretty print plain text files without the C/C++ + keywords being highlighted. + - The pdftops filter did not compile with GCC 4.0 (STR + #1226) + - The texttops filter did not highlight preprocessor + directives followed by a tab properly. + - HP PJL output now uses both JOB DISPLAY and RDYMSG + commands to show the current job on the printer's + display (STR #1218) + - Local authentication certificates are now stored in + /var/run/cups/certs by default instead of + /etc/cups/certs (STR #1211) + - Backends now use "&" to separate options in device + URIs; "+" is still recognized but is deprecated (STR + #842) + - The USB backend no longer supports the usb:/dev/foo + format on systems that support device ID queries. + - Forced classification markings did not work when the + job-sheets parameters were "none,none". + - "lpstat -l -p" incorrectly showed all users as allowed, + even if the queue was restricted to certain users (STR + #801) + - The scheduler now automatically detects SSL/TLS clients + without using the SSLPort/SSLListen directives. + - The CUPS API and scheduler no longer support SSLv2- + encrypted connections. + - Updated the cupsaddsmb utility to correctly export the + CUPS driver for Windows. + - Fixed a signal-handling bug in httpRead() which + ultimately caused the server to print multiple copies + when it was busy (STR #1184) + - The cupsFile API now uses the O_APPEND option when + opening files in append mode (STR #990) + - The md5.h header and md5_* functions are now officially + private and have been renamed to avoid conflicts with + other implementations with the same name. + - The pdftops filter incorrectly embedded some Type1 + fonts (STR #1093) + - The scheduler didn't detect a closed connection in the + middle of an IPP request (STR #1153) + - The scheduler could block trying to read the job status + if there was input pending and the job was cancelled in + the same input cycle (STR #1157) + - The scheduler could crash when deleting a class due to + infinite recursion. + - Updated the Zebra ZPL label printer driver to use the + run-length encoding and support more options. + - Updated serial backend to scan for /dev/ttyC* as well + as /dev/ttyc* for Cyclades serial ports (STR #1049) + - The scheduler could hang reading the job status under + certain circumstances (STR #1068) + - The USB backend termination signal code was inverted + (STR #1046) + - Moved enable and disable commands to sbindir to be + consistent. + - Added new cupsRasterInterpretPPD() function for RIP + filters to setup the raster page header from + PostScript commands in a PPD file. + - The CUPS browsing protocol now offers a "delete" bit + to remove printers as soon as they are deleted on the + server or as soon as the server shuts down gracefully + (STR #793) + - The CUPS_SERVER and ServerName directives (client.conf + and ~/.cupsrc) may now contain names of the form + "server:port" and "/path/to/domain/socket". + - The "cancel -u user" command now works for ordinary + users (STR #751) + - Added test run support to "make test" target (STR #64) + - Added domain socket support (STR #656) + - Added BrowseLocalOptions directive to allow the + administrator to add printer URI options to the browse + URI, e.g. "encryption=required" (STR #732) + - Added BrowseRemoteOptions directive to allow the + administrator to add standard URI options to the + remote printer URI, e.g. "encryption=required" (STR + #732) + - Now put "-I.." compiler option in front of all others + to ensure that local CUPS headers are used before + installed headers (STR #437) + - New cupsLangPrintf() and cupsLangPuts() for localized + interfaces. + - Now support custom attributes and extended options in + PPD files. + - Now provide functions to save PPD files. + - New policy mechanism allows per-operation and + per-printer control over what users and groups are + allowed to do various IPP operations. + - New error policy mechanism to control how aborted + backend errors are handled by the scheduler + (abort-job, retry-job, requeue-job, stop-printer) + - Updated the printer test page with a better color + wheel and a separate grayscale ramp. + - A single backend process is now run to send all print + data for a job. + - Backends and filters can now send and receive + backchannel data over file descriptor 3. + - Updated the raster stream format to support more + user-defined attributes and to do compression of the + page data. diff --git a/CHANGES-1.3.txt b/CHANGES-1.3.txt new file mode 100644 index 0000000..9af534f --- /dev/null +++ b/CHANGES-1.3.txt @@ -0,0 +1,856 @@ +CHANGES-1.3.txt +--------------- + +CHANGES IN CUPS V1.3.11 + + - The scheduler did not prevent nested classes (STR #3211) + - The scheduler did not reprint processing jobs that were moved to + another destination (STR #3222) + - The scheduler did not reset the current job file when stopping a + printer (STR #3226) + - The scheduler did not handle POSTs to custom CGIs properly (STR #3221) + - The pdftops filter did not print landscape PDF pages properly + (STR #2881) + - The scheduler did not handle partial header lines properly from CGI + programs (STR #3194) + - The web interface could hang on OpenBSD (STR #3176, STR #3196) + - The scheduler and cupsfilter utility did not handle rules starting + with a negation operator properly (STR #3160) + - The scheduler and cupsfilter utility would crash with certain MIME + .types rules (STR #3159) + - httpSetField wasn't bracketing IPv6 numeric addresses for the Host: + field (STR #3164) + - The ServerName, if specified, was not treated as a valid alias for the + local system (STR #3167) + - "make epm" did not work (STR #3166) + - "lpstat -h server" showed non-shared printers (STR #3147) + - "make check" did not work on Linux (STR #3161) + + +CHANGES IN CUPS V1.3.10 + + - Documentation fixes (STR #2994, STR #2995, STR #3008, STR #3056, + STR #3057) + - SECURITY: The scheduler now protects against DNS rebinding attacks + (STR #3118) + - SECURITY: Fixed TIFF integer overflow in image filters (STR #3031) + - The scheduler did not support the job-hold-until attribute with the + Restart-Job operation (STR #3130) + - SECURITY: The PNG image reading code did not validate the + image size properly, leading to a potential buffer overflow + (STR #2974) + - The rastertohp driver did not set the 1-sided printing mode when + needed (STR #3131) + - Now use a wrapper program instead of our fork of the Xpdf code to + support printing of PDF files. The new wrapper supports using Xpdf, + poppler, or Ghostscript to convert PDF files to PostScript (STR #3129) + - Long job names caused problems with some PJL printers (STR #3125) + - The lpq command did not work when showing all destinations (STR #3117) + - The scheduler used a codeset name of UTF8 which is not supported on + Solaris (STR #3113) + - cupsGetJobs() did not work with a NULL destination (STR #3107) + - Fixed a localization problem for option choices (incorrectly) named + "Custom" (STR #3106) + - The fallback OpenSSL random number seeding would not work (STR #3079) + - The scheduler might miss a child signal, causing high CPU usage. + - The scheduler did not enforce quotas after the job history was + unloaded (STR #3078) + - The job-k-limit, job-page-limit, and job-quota-period attributes + could not be set using the lpadmin command (STR #3077) + - httpSeparateURI() did not error out on URIs with a missing port + number after a colon. + - Fixed a Valgrind-detected initialization error when creating a + missing directory on startup. + - The scheduler did not always read all of the HTTP headers from a + CGI script/program. + - The scheduler did not always set the "air" property in Bonjour/DNS-SD + registrations. + - The scheduler incorrectly compared Mac OS X UUIDs for access + control, preventing access in certain configurations. + - The IPP backend incorrectly reset the required authentication + to Kerberos when authentication failed. + - The scheduler no longer looks up the local hostname by default; + turn on hostname lookups to restore the previous behavior. + - The scheduler did not always load MIME type rules correctly + (STR #3059) + - The test page did not format correctly on A4 paper (STR #3060) + - The web interface sometimes incorrectly redirected users to + 127.0.0.1 (STR #3022) + - cupsPrintFile*() did not send the document filename for single + file submissions (STR #3055) + - The scheduler did not update the member-names attribute when + removing the last printer from a class. + - The scheduler did not report PPD Products with parenthesis + in them properly (STR #3046) + - The wrong italic fonts were listed in the UTF-8 charset file + for the text filter. + - The backends did not return an OK status for the + CUPS_SC_CMD_GET_BIDI side-channel command (STR #3029) + - The scheduler did not purge jobs that were missing a + time-at-creation attribute, indicating a bad job control file + (STR #3030) + - The "-o job-hold-until=week-end" option did not work properly + (STR #3025) + - The Solaris USB printer device does not support select or poll + (STR #3028) + - The scheduler would crash if you exceeded the MaxSubscriptions + limit. + - The lp "-H immediate" option did not specify that the job + should not be held (STR #3013) + - The scheduler did not support the "Connection: close" + HTTP header (STR #3010) + - The mailto notifier didn't terminate messages properly + (STR #3011) + - Backends could spin trying to read back-channel data + (STR #3001) + - The HP-GL/2 filter was using the wrong default colors + (STR #2966) + - The scheduler incorrectly allowed Get-Jobs operations without a + printer-uri (STR #2996) + - The compression option was not being encoded properly + (STR #2997) + - Added a missing character map for JIS-X0213/ShiftJIS. + - The scheduler now rejects ATTR: messages with empty values. + - The scheduler could consume all CPU handling closed connections + (STR #2988) + - Fixed some configure script bugs with rc/xinetd directories + (STR #2970) + - The Epson sample driver PPDs contained errors (STR #2979) + + +CHANGES IN CUPS V1.3.9 + + - SECURITY: The HP-GL/2 filter did not range check pen numbers + (STR #2911) + - SECURITY: The SGI image file reader did not range check + 16-bit run lengths (STR #2918) + - SECURITY: The text filter did not range check cpi, lpi, or + column values (STR #2919) + - Documentation updates (STR #2904, STR #2944) + - The French web admin page was never updated (STR #2963) + - The IPP backend did not retry print jobs when the printer + reported itself as busy or unavailable (STR #2951) + - The "Set Allowed Users" web interface did not handle trailing + whitespace correctly (STR #2956) + - The PostScript filter did not work with Adobe applications + using custom page sizes (STR #2968) + - The Mac OS X USB backend did not work with some printers + that reported a bad 1284 device ID. + - The scheduler incorrectly resolved the client connection + address when HostNameLookups was set to Off (STR #2946) + - The IPP backend incorrectly stopped the local queue if + the remote server reported the "paused" state. + - The cupsGetDests() function did not catch all types of + request errors. + - The scheduler did not always log "job queued" messages + (STR #2943) + - The scheduler did not support destination filtering using + the printer-location attribute properly (STR #2945) + - The scheduler did not send the server-started, + server-restarted, or server-stopped events (STR #2927) + - The scheduler no longer enforces configuration file + permissions on symlinked files (STR #2937) + - CUPS now reinitializes the DNS resolver on failures + (STR #2920) + - The CUPS desktop menu item was broken (STR #2924) + - The PPD parser was too strict about missing keyword + values in "relaxed" mode. + - The PostScript filter incorrectly mirrored landscape + documents. + - The scheduler did not correctly update the + auth-info-required value(s) if the AuthType was Default. + - The scheduler required Kerberos authentication for + all operations on remote Kerberized printers instead + of just for the operations that needed it. + - The socket backend could wait indefinitely for back- + channel data with some devices. + - PJL panel messages were not reset correctly on older + printers (STR #2909) + - cupsfilter used the wrong default path (STR #2908) + - Fixed address matching for "BrowseAddress @IF(name)" + (STR #2910) + - Fixed compiles on AIX. + - Firefox 3 did not work with the CUPS web interface in SSL + mode (STR #2892) + - Custom options with multiple parameters were not emitted + correctly. + - Refined the cupstestppd utility. + - ppdEmit*() did not support custom JCL options (STR #2889) + - The cupstestppd utility incorrectly reported missing + "en" base translations (STR #2887) + + +CHANGES IN CUPS V1.3.8 + + - Documentation updates (STR #2785, STR #2861, STR #2862) + - The scheduler did not add the ending job sheet when the + job was released. + - The IPP backend did not relay marker-* attributes. + - The CUPS GNOME/KDE menu item was not localized for + Chinese (STR #2880) + - The CUPS GNOME/KDE menu item was not localized for + Japanese (STR #2876) + - The cupstestppd utility reported mixed line endings for + Mac OS and Windows PPD files (STR #2874) + - The pdftops filter did not print landscape orientation PDF + pages correctly on all printers (STR #2850) + - The scheduler did not handle expiring of implicit classes + or their members properly, leading to a configuration where + one of the members would have a short name (STR #2766) + - The scheduler and cupstestppd utilities did not support + cupsFilter and cupsPreFilter programs with spaces in their + names (STR #2866) + - Removed unused variables and assignments found by the + LLVM "clang" tool. + - Added NULL checks recommended by the LLVM "clang" tool. + - The scheduler would crash if you started a printer that + pointed to a backend that did not exist (STR #2865) + - The ppdLocalize functions incorrectly mapped all generic + locales to country-specific locales. + - The cups-driverd program did not support Simplified Chinese + or Traditional Chinese language version strings (STR #2851) + - Added an Indonesian translation (STR #2792) + - Fixed a timing issue in the backends that could cause data + corruption with the CUPS_SC_CMD_DRAIN_OUTPUT side-channel + command (STR #2858) + - The scheduler did not support "HostNameLookups" with all of + the boolean names (STR #2861) + - Fixed a compile problem with glibc 2.8 (STR #2860) + - The scheduler incorrectly filtered out queues with ACLs and + authentication. + - The PostScript filter did not support %%IncludeFeature lines + in the page setup section of each page (STR #2831) + - The scheduler did not generate printer-state events when the + default printer was changed (STR #2764) + - cupstestppd incorrectly reported a warning about the PPD format + version in some locales (STR #2854) + - cupsGetPPD() and friends incorrectly returned a PPD file for + a class with no printers. + - The member-uris values for local printers in a class returned + by the scheduler did not reflect the connected hostname or + port. + - The CUPS PHP extension was not thread-safe (STR #2828) + - The scheduler incorrectly added the document-format-default + attribute to the list of "common" printer attributes, which + over time would slow down the printing system (STR #2755, + STR #2836) + - The cups-deviced and cups-driverd helper programs did not set + the CFProcessPath environment variable on Mac OS X (STR #2837) + - "lpstat -p" could report the wrong job as printing (STR #2845) + - The scheduler would crash when some cupsd.conf directives + were missing values (STR #2849) + - The web interface "move jobs" operation redirected users to + the wrong URL (STR #2815) + - The Polish web interface translation contained errors + (STR #2815) + - The scheduler did not report PostScript printer PPDs with + filters as PostScript devices. + - The scheduler did not set the job document-format attribute + for jobs submitted using Create-Job and Send-Document. + - cupsFileTell() did not work for log files opened in append + mode (STR #2810) + - The scheduler did not set QUERY_STRING all of the time + for CGI scripts (STR #2781, STR #2816) + - The scheduler now returns an error for bad job-sheets + values (STR #2775) + - Authenticated remote printing did not work over domain + sockets (STR #2750) + - The scheduler incorrectly logged errors for print filters + when a job was canceled (STR #2806, #2808) + - The scheduler no longer allows multiple RSS subscriptions + with the same URI (STR #2789) + - The scheduler now supports Kerberized printing with + multiple server names (STR #2783) + - "Satisfy any" did not work in IPP policies (STR #2782) + - The CUPS imaging library would crash with very large + images - more than 16Mx16M pixels (STR #2805) + - The PNG image loading code would crash with large images + (STR #2790) + - The scheduler did not limit the total number of filters. + - The scheduler now ensures that the RSS directory has + the correct permissions. + - The RSS notifier did not quote the feed URL in the RSS + file it created (STR #2801) + - The web interface allowed the creation and cancellation + of RSS subscriptions without a username (STR #2774) + - Increased the default MaxCopies value on Mac OS X to + 9999 to match the limit imposed by the print dialog. + - The scheduler did not reject requests with an empty + Content-Length field (STR #2787) + - The scheduler did not log the current date and time and + did not escape special characters in request URIs when + logging bad requests to the access_log file (STR #2788) + + +CHANGES IN CUPS V1.3.7 + + - CVE-2008-0047: cgiCompileSearch buffer overflow (STR #2729) + - CVE-2008-1373: CUPS GIF image filter overflow (STR #2765) + - Updated the "make check" tests to do a more thorough + automated test. + - cups-driverd complained about missing directories (STR + #2777) + - cupsaddsmb would leave the Samba username and password on + disk if no Windows drivers were installed (STR #2779) + - The Linux USB backend used 100% CPU when a printer was + disconnected (STR #2769) + - The sample raster drivers did not properly handle SIGTERM + (STR #2770) + - The scheduler sent notify_post() messages too often on + Mac OS X. + - Kerberos access to the web interface did not work + (STR #2748) + - The scheduler did not support "AuthType Default" in IPP + policies (STR #2749) + - The scheduler did not support the "HideImplicitMembers" + directive as documented (STR #2760) + - "make check" didn't return a non-zero exit code on + error (STR #2758) + - The scheduler incorrectly logged AUTH_foo environment + variables in debug mode (STR #2751) + - The image filters inverted PBM files (STR #2746) + - cupsctl would crash if the scheduler was not running + (STR #2741) + - The scheduler could crash when printing using a port + monitor (STR #2742) + - The scheduler would crash if PAM was broken (STR #2734) + - The image filters did not work with some CMYK JPEG files + produced by Adobe applications (STR #2727) + - The Mac OS X USB backend did not work with printers that + did not report a make or model. + - The job-sheets option was not encoded properly (STR #2715) + - The scheduler incorrectly complained about missing LSB + PPD directories. + + +CHANGES IN CUPS V1.3.6 + + - Documentation updates (STR #2646, STR #2647, STR #2649) + - Fixed a problem with the web interface "Use Kerberos + Authentication" check box (STR #2703) + - The scheduler unconditionally overwrote the printer-state- + message with "process-name failed" when a filter or backend + failed, preventing a useful error message from being shown + to the user. + - Policies on CUPS-Move-Job didn't work as expected (STR + #2699) + - The configure script only supported D-BUS on Linux + (STR #2702) + - The scheduler did not support (STR #2701) + - The scheduler did not reset the job-hold-until attribute + after a job's hold time was reached. + - The scheduler did not support printer supply attributes + (STR #1307) + - The Kerberos credentials provided by some Windows KDCs + were still too large - now use a dynamic buffer to + support credentials up to 64k in size (STR #2695) + - Printing a test page from the web interface incorrectly + defaulted to the "guest" user (STR #2688) + - The cupsEncodeOptions2() function did not parse multiple- + value attribute values properly (STR #2690) + - The scheduler incorrectly sent printer-stopped events for + status updates from the print filters (STR #2680) + - The IPP backend could crash when handling printer errors + (STR #2667) + - Multi-file jobs did not print to remote CUPS servers + (STR #2673) + - The scheduler did not provide the Apple language ID to + job filters. + - Kerberos authentication did not work with the web + interface (STR #2606, STR #2669) + - The requesing-user-name-allowed and -denied functionality + did not work for Kerberos-authenticated usernames (STR + #2670) + - CUPS didn't compile on HP-UX 11i (STR #2679) + - cupsEncodeOptions2() did not handle option values like + "What's up, doc?" properly. + - Added lots of memory allocation checks (Fortify) + - The scheduler would crash if it was unable to add a job + file (Fortify) + - ppdOpen*() did not check all memory allocations (Coverity) + - ippReadIO() did not check all memory allocations (Coverity) + - The PostScript filter did not detect read errors (Coverity) + - The scheduler did not check for a missing job-sheets-completed + attribute when sending an event notification (Coverity) + - "Set Printer Options" might not work with raw queues (Coverity) + - cupsRasterInterpretPPD() could crash on certain PostScript + errors (Coverity) + - The USB backend did not check for back-channel support + properly on all systems (Coverity) + - Fixed memory leaks in the GIF and PNM image loading code + (Coverity) + - Removed some dead code in the CUPS API and scheduler (Coverity) + - Fixed two overflow bugs in the HP-GL/2 filter (Coverity) + - Fixed another ASN1 string parsing bug (STR #2665) + - The RSS notifier directory was not installed with the + correct permissions. + - The standard CUPS backends could use 100% CPU while waiting + for print data (STR #2664) + - Filename-based MIME rules did not work (STR #2659) + - The cups-polld program did not exit if the scheduler crashed + (STR #2640) + - The scheduler would crash if you tried to set the port-monitor + on a raw queue (STR #2639) + - The scheduler could crash if a polled remote printer was + converted to a class (STR #2656) + - The web interface and cupsctl did not correctly reflect + the "allow printing from the Internet" state (STR #2650) + - The scheduler incorrectly treated MIME types as case- + sensitive (STR #2657) + - The Java support classes did not send UTF-8 strings to + the scheduler (STR #2651) + - The CGI code did not handle interrupted POST requests + properly (STR #2652) + - The PostScript filter incorrectly handled number-up when + the number of pages was evenly divisible by the number-up + value. + - The PDF filter incorrectly filtered pages when page-ranges + and number-up were both specified (STR #2643) + - The IPP backend did not handle printing of pictwps files + to a non-Mac CUPS server properly. + - The scheduler did not detect network interface changes + on operating systems other than Mac OS X (STR #2631) + - The scheduler now logs the UNIX error message when it + is unable to create a request file such as a print job. + - Added support for --enable-pie on Mac OS X. + + +CHANGES IN CUPS V1.3.5 + + - The SNMP backend did not check for negative string + lengths (STR #2589) + - The scheduler incorrectly removed auth-info attributes, + potentially leading to a loss of all options for a job. + - The scheduler stopped sending CUPS browse packets on a + restart when using fixed addresses (STR #2618) + - Fixed PDF filter security issues (CVE-2007-4352 + CVE-2007-5392 CVE-2007-5393) + - Changing settings would always change the DefaultAuthType + and Allow lines (STR #2580) + - The scheduler would crash when submitting an undefined + format file from Samba with LogLevel debug2 (STR #2600) + - The scheduler did not use poll() when epoll() was not + supported by the running kernel (STR #2582) + - Fixed a compile problem with Heimdal Kerberos (STR #2592) + - The USB backend now retries connections to a printer + indefinitely rather than stopping the queue. + - Printers with untranslated JCL options were not exported + to Samba correctly (STR #2570) + - The USB backend did not work with some Minolta USB + printers (STR #2604) + - The strcasecmp() emulation code did not compile (STR + #2612) + - The scheduler would crash if a job was sent to an empty + class (STR #2605) + - The lpc command did not work in non-UTF-8 locales (STR + #2595) + - Subscriptions for printer-stopped events also received + other state changes (STR #2572) + - cupstestppd incorrectly reported translation errors for + the "en" locale. + - ppdOpen() did not handle custom options properly when the + Custom attribute appeared before the OpenUI for that + option. + - The scheduler could crash when deleting a printer or + listing old jobs. + - The Mac OS X USB backend did not allow for requeuing of + jobs submitted to a class. + - lpmove didn't accept a job ID by itself. + - The scheduler incorrectly removed job history information + for remote print jobs. + - The scheduler incorrectly sent the + "com.apple.printerListChanged" message for printer state + changes. + - The PostScript filter drew the page borders (when enabled) + outside the imageable area. + - The LPD and IPP backends did not default to the correct + port numbers when using alternate scheme names. + - The scheduler incorrectly deleted hardwired remote + printers on system sleep. + - The scheduler would abort if a bad browse protocol name + was listed in the cupsd.conf file. + - The online cupsd.conf help file incorrectly showed + "dns-sd" instead of "dnssd" for Bonjour sharing. + - The scheduler could crash changing the port-monitor value. + - The scheduler generated CoreFoundation errors when run as + a background process. + - When printing with number-up > 1, it was possible to get + an extra blank page. + + +CHANGES IN CUPS V1.3.4 + + - Documentation updates (STR #2560, STR #2563, STR #2569) + - CUPS now maps the "nb" locale to "no" on all platforms + (STR #2575) + - CUPS did not work with a Windows 2003 R2 KDC (STR #2568) + - ippReadIO() could read past the end of a buffer (STR + #2561) + - The scheduler would crash on shutdown if it was unable + to create a Kerberos context. + - Multiple AuthTypes in cupsd.conf did not work (STR + #2545) + - The snmp.conf file referenced the wrong man page (STR + #2564) + - The cupsaddsmb program didn't handle domain sockets + properly (STR #2556) + - The scheduler now validates device URIs when adding + printers. + - Updated httpSeparateURI() to support hostnames with + the backslash character. + - Updated the Japanese localization (STR #2546) + - The parallel backend now gets the current IEEE-1284 + device ID string on Linux (STR #2553) + - The IPP backend now checks the job status at + variable intervals (from 1 to 10 seconds) instead + of every 10 seconds for faster remote printing + (STR #2548) + - "lpr -p" and "lpr -l" did not work (STR #2544) + - Compilation failed when a previous version of CUPS + was installed and was included in the SSL include + path (STR #2538) + - The scheduler did not reject requests with charsets + other than US-ASCII or UTF-8, and the CUPS API + incorrectly passed the locale charset to the scheduler + instead of UTF-8 (STR #2537) + - cups-deviced did not filter out duplicate devices. + - The AppleTalk backend incorrectly added a scheme + listing when AppleTalk was disabled or no printers + were found. + - The PostScript filter generated N^2 copies when the + printer supported collated copies and user requested + reverse-order output. + - The scheduler did not reprint all of the files in a + job that was held. + - The scheduler did not update the printcap file after + removing stale remote queues. + - The cupsd.conf man page incorrectly referenced + "AuthType Kerberos" instead of "AuthType Negotiate". + + +CHANGES IN CUPS V1.3.3 + + - The scheduler did not use the attributes-natural-language + attribute when passing the LANG environment variable to + cups-deviced or cups-driverd. + - The scheduler did not use the printer-op-policy when + modifying classes or printers (STR #2525) + - The auth-info-required attribute was not always updated + for remote queues that required authentication. + - The German web interface localization contained errors + (STR #2523) + - The Swedish localization contained errors (STR #2522) + + +CHANGES IN CUPS V1.3.2 + + - The 1.3.1 release was incorrectly created from the + 1.4.x source tree (STR #2519) + - Added support for 32/64-bit libraries on HP-UX + (STR #2520) + - The scheduler incorrectly used portrait as the default + orientation (STR #2513) + - The scheduler no longer writes the printcap file for + every remote printer update (STR #2512) + - Remote raw printing with multiple copies did not work + (STR #2518) + - Updated the configure script to require at least autoconf + 2.60 (STR #2515) + - Some gzip'd PPD files were not read in their entirety + (STR #2510) + + +CHANGES IN CUPS V1.3.1 + + - Documentation updates. + - The USB backend on Mac OS X could hang if the driver and + printer did not match. + - Delegated Kerberos credentials were not working. + - "make distclean" incorrectly removed the edit-config.tmpl + files (STR #2508) + - Fix compile problem on HP-UX (STR #2501) + - The cupstestppd utility now tests for resolutions greater + than 99999 DPI to detect a missing "x" between the X and Y + resolutions. + - Fixed many problems in the various translations and added + a new "checkpo" utility to validate them. + - The cupstestppd utility now tests the custom page size code + for CUPS raster drivers. + - cupsLangDefault() did not attempt to return a language that + was supported by the calling application. + - If a remote printer stopped while a job was being sent, the + local queue would also get stopped and the job re-queued, + resulting in duplicate prints in some cases. + - A few Apple-specific job options needed to be omitted when + printing a banner page. + - The new peer credential support did not compile on FreeBSD + (STR #2495) + - Direct links to help files did not set the current section + so the table-of-contents was not shown. + - The configure script did not support --localedir=foo (STR #2488) + - The backends were not displaying their localized messages. + - CUPS-Authenticate-Job did not require Kerberos authentication + on queues protected by Kerberos. + - The Zebra ZPL driver did not work with Brady label printers + (STR #2487) + - Norwegian wasn't localized on Mac OS X. + - getnameinfo() returns an error on some systems when DNS is + not available, leading to numerous problems (STR #2486) + - The cupsfilter command did not work properly on Mac OS X. + - The scheduler makefile contained a typo (STR #2483) + - The TBCP and BCP port monitors did not handle the trailing + CTRL-D in some PostScript output properly. + - Fixed the localization instructions and German template for + the "Find New Printers" button (STR #2478) + - The web interface did not work with the Chinese localization + (STR #2477) + - The web interface home page did not work for languages that + were only partially localized (STR #2472) + - Updated the Spanish web interface localization (STR #2473) + - ppdLocalize() did not work for country-specific localizations. + + +CHANGES IN CUPS V1.3.0 + + - The scheduler did not handle out-of-file conditions + gracefully when accepting new connections, leading to + heavy CPU usage. + - The scheduler did not detect ServerBin misconfigurations + (STR #2470) + - "AuthType Default" did not work as expected when the + "DefaultAuthType foo" line appeared after it in the + cupsd.conf file. + - The on-line help did not describe many common printing + options (STR #1846) + - The IPP backend did not return the "auth required" status + when printing to a Kerberos-protected queue. + - The scheduler was not looking in the correct directories + for LSB PPD files (STR #2464) + - Changed references to ESP Ghostscript to GPL Ghostscript + (STR #2463) + - The PostScript filter did not cleanly terminate when + the job was canceled or stopped. + - Fixed generation of Kerberos credentials for remote + printing. Note that this requires a recent version of + MIT Kerberos with a working krb5_cc_new_unique() + function or Heimdal Kerberos. + - Added Portuguese and updated Italian message catalogs. + + +CHANGES IN CUPS V1.3rc2 + + - Added more range checking to the pdftops filter. + - The scheduler would crash if a remote IPP queue was stopped + (STR #2460) + - The scheduler did not allow "DefaultAuthType None". + + +CHANGES IN CUPS V1.3rc1 + + - Updated the German localization (STR #2443) + - cupsAdminGetServerSettings() did not handle properly. + - When lprm and cancel are run with no job ID, they now will + cancel the first stopped job if no pending or processing + jobs are left in the queue. + - The scheduler now logs successful print jobs, filter + failures, and the job file types at the default log + level (STR #2458) + - The scheduler now logs the usernames it is using for + authorization at LogLevel debug instead of debug2 (STR #2448) + - Added Intellitech Intellibar and Zebra CPCL PPDs to the list + of installed PPDs. + - Added 6" and 8" wide label sizes for the Zebra ZPL Label + Printer driver (STR #2442) + - The cupsaddsmb program and web interface now support + exporting of 64-bit Windows drivers, when available + (STR #2439) + - Moving a job that was printing did not stop the job on the + original printer (STR #2262) + - The cups-lpd mini-daemon did not work on Mac OS X server. + - Added httpGetAuthString() and httpSetAuthString() APIs to get + and set the current (cached) authorization string to use for + HTTP requests. + - Updated the default cupsd.conf policy to list the + "administrative" operations separately from the "printer + control" operations so that it is easier to define a + group of users that are "printer operators". + - The web interface now pulls the default cupsd.conf file + from cupsd.conf.default in the CUPS config directory. + - Added a help file for using Kerberos with CUPS. + - The scheduler now strips the "@KDC" portion of Kerberos + usernames since those usernames typically do not appear in + the group membership lists used by CUPS. + - cupsMarkOptions() could (incorrectly) leave multiple option + choices marked. + - Backends could (incorrectly) run as root during discovery + (STR #2454) + - Avahi is now supported for DNS-SD (Bonjour) printer sharing + (STR #2455) + - The default cupsd.conf file had typos and old operation names + (STR #2450) + - The scheduler now erases authentication cache files using the + 7-pass US DoD algorithm. + - Delegated Kerberos credentials (proxy authentication) did not + work. + - The filter makefile did not optimize the libcupsimage.2.dylib + with a sectorder file. + - The IPP backend incorrectly wrote an empty printer message + when processing the "none" state reason. + - The USB backend could deadlock on Mac OS X while performing + a side-channel command. + - The scheduler did not prevent remote queues from being + shared/published. + - The scheduler did not remove the temporary request file on + authentication errors. + - ppdLocalizeIPPReason() did not handle "scheme:" schemes or + "file" URLs. + - ppdLocalizeIPPReason() was not exported on Mac OS X. + + +CHANGES IN CUPS V1.3b1 + + - Copyright updates - CUPS is now owned by Apple Inc. + - Documentation updates (STR #1775, STR #2027, STR #2130, + STR #2131, STR #2263, STR #2356, STR #2397) + - Added new cupsfilter utility (STR #1734) + - Added new job-printer-state-message and + job-printer-state-reasons attributes to jobs (STR #2418) + - Added LDAP+SSL support (STR #1967) + - CUPS now supports authentication via peer credentials + over domain sockets (STR #2242, STR #2277) + - The CUPS sample driver PPDs are now generated by the PPD + compiler and include all of the localized languages by + default (STR #2164) + - You can now specify "AuthType Default" in the cupsd.conf + file to use the default authentication defined by the + DefaultAuthType directive. + - The SNMP backend no longer adds a default Address line + when none is specified in the snmp.conf file; this allows + the backend to be easily disabled as needed (STR #2434) + - Added a new cupsctl command for doing basic changes to + the cupsd.conf file (STR #1777) + - Added a new ppdLocalizeIPPReason() function to get the + localized text/URI for a given IPP reason keyword for a + driver. + - Removed the deskjet2.ppd driver, as it only worked with + a very small subset of HP DeskJet printers and was + confusing to users. The rastertohp driver still + supports the deskjet2.ppd options for existing queues. + - The scheduler did not add a trailing banner page if a + client did not specify the last document in a job (STR + #1711) + - The scheduler did not report Bonjour shared printers as + remote printers (STR #2384) + - Added new -R and -W options to the cupstestppd program + for greater control over the testing of PPDs. + - Added a new cupsGetServerPPD() function for getting + an available PPD from the server (STR #2334) + - Added a new cupsDoIORequest() function for reading + and writing files via IPP requests (STR #2334) + - Added a new CUPS_GET_PPD operation for getting an + available PPD file on the server (STR #2334) + - CUPS_GET_PPDS now reports multiple ppd-product values + based on the PPD ModelName and Product strings (STR + #2334, STR #2383) + - CUPS_GET_PPDS now reports the PSVersion attributes + from a PPD file in the ppd-psversion attribute + (STR #2334) + - CUPS_GET_PPDS now reports the cupsModelNumber attribute + from a PPD file in the ppd-model-number attribute (STR + #2383) + - CUPS_GET_PPDS now reports a driver type string in the + ppd-type attribute based on the cupsFax and cupsFilter + attributes in a PPD file (STR #2383) + - Added a new printer attribute called "cups-version" + which reports the version of CUPS that is running + (STR #2240) + - backendRunLoop() now aborts immediately on SIGTERM + if no data has been written yet (STR #2103) + - Due to poor IPP support from the vendors, the SNMP + backend no longer tries IPP connections; instead, + it now uses a lookup file with fallback to port 9100 + (socket://address) and 515 (lpd://address) printing + (STR #2035, STR #2354) + - The scheduler now recreates the CUPS log directory as + needed (STR #2353) + - cupsLangDefault() now maps new-style Apple locale names + to the traditional ll_CC form (STR #2357) + - Add new cupsArrayNew2() API to support hashed lookups + of array elements (STR #2358) + - ppdConflicts() optimizations (STR #2358) + - The cupstestppd program now tests for existing filters, + icons, profiles, and dialog extensions (STR #2326) + - The web interface no longer lists new printers on the + main administration page. Instead, a new "List Available + Printers" button is provided that shows a separate page + with the list of printers. + - The web interface now supports setting the banner and + policy options on raw printers and classes (STR #2238) + - The socket backend now reads any pending back-channel + data before shutting down the socket (STR #2325) + - Added a new ErrorPolicy directive in the cupsd.conf + file (STR #1871) + - Printers that use JCL options are now exported to Samba + correctly (STR #1985) + - The IPP backend now relays printer-state-message values + from the server to the client (STR #2109) + - Added support for the PWG printer-alert and + printer-alert-description attributes (STR #2088) + - Added support for LPD "stream" mode (STR #2036) + - The scheduler now reports the PostScript product string + from PPD files in CUPS-Get-PPDs responses (STR #1900) + - Raw printing with queues pointing to the file pseudo- + device and multiple files and/or banners now works (STR + #1933) + - Added new public cupsAdminGetServerSettings() and + cupsAdminSetServerSettings() APIs. + - Added new "makebuttons" script in the "tools" directory + for creating web interface buttons (STR #2231) + - Added support for DNS-SD (aka "Bonjour") printer sharing + (STR #1171) + - Job operations (cancel, hold, release, etc.) from the + web interface now return back to the original page (STR + #2239) + - The classes or printers list is now shown after a + successful deletion from the web interface (STR #1999) + - The default configuration now allows browse packets from + any address (STR #2008) + - The web interface now provides an "allow printing from the + Internet" check box (STR #1897) + - The notify-events-default and + notify-lease-duration-default attributes can now be set + (STR #1671) + - Server-side default options are now sent to clients when + the "printer-defaults" attribute group is requested (STR + #1923) + - Added support for Linux "relro" linker option (STR #1614) + - CUPS now validates the number-up option value (STR #1329) + - The on-line help now provides better search capabilities + (STR #1701) + - The web interface "Add This Printer" button now allows you + to change the printer name, description, and location + (STR #1646) + - Added support for Mac OS X authorization services + (STR #2206) + - Added support for driver-specific pre-filters (STR #2108) + - Added a new side-channel API for drivers and backends + for basic device control and information queries (STR + #1898) + - The scheduler now uses poll(), epoll(), or /dev/kqueue + instead of select() when possible (STR #1261) + - Added new cupsArrayGetIndex() and cupsArrayGetInsert() + functions to get the current index and insertion + positions of an array. + - Added a new --with-max-copies configure option (STR + #2090) + - Added new cupsRemoveDest() and cupsSetDefaultDest() + functions. + - Added support for cupsPJLCharset attribute in PPD files + which specifies the character set that is used in PJL + strings (STR #1969) + - Moved the definition of the (private) _http_s structure + to http-private.h; code that directly accesses the + http_t members will no longer compile! + - Added support for setting the document-format-default + attribute on a per-printer basis. + - Added support for IntelliBar label printers. diff --git a/CHANGES-1.4.txt b/CHANGES-1.4.txt new file mode 100644 index 0000000..09326b2 --- /dev/null +++ b/CHANGES-1.4.txt @@ -0,0 +1,843 @@ +CHANGES-1.4.txt +--------------- + +CHANGES IN CUPS V1.4.8 + + - The scheduler would delete job data files when restarted (STR #3880) + - The network backends could crash if a printer returned a value of 0 + for the maximum capacity for a supply (STR #3875) + + +CHANGES IN CUPS V1.4.7 + + - Documentation changes (STR #3710, STR #3720, STR #3745, STR #3750, + STR #3757, STR #3758, STR #3782, STR #3826, STR #3829, STR #3837) + - Web interface fixes (STR #3412, STR #3345, STR #3455, STR #3707, + STR #3755, STR #3769, STR #3783) + - Configure script fixes (STR #3659, STR #3691) + - Compilation fixes (STR #3718, STR #3771, STR #3774) + - The imageto* filters could crash with bad GIF files (STR #3867) + - The scheduler might leave old job data files in the spool directory + (STR #3795) + - CUPS did not work with locales using the ASCII character set + (STR #3832) + - httpAddrString() did not return a URI-style IPv6 numeric address + (STR #3814) + - Fixed an issue when reading compressed CUPS raster streams (STR #3812) + - Fixed an issue with PostScript printer auto-configuration (STR #3443) + - Fixed some compatibility issues with the libusb-based USB backend + (STR #3799) + - The network backends no longer try to collect SNMP supply and status + information for raw queues (STR #3809) + - The DBUS notifier did not report job state changes (STR #3805) + - The scheduler did not always report that the "normal" print-quality + value was supported (STR #3803) + - The gziptoany filter did not report the correct error if it was unable + to write the uncompressed document to the next filter or backend in + the chain (STR #3797) + - The Epson and Oki 9-pin drivers had a bad resolution option + (STR #3798) + - The scheduler did not always register the correct default ICC profile + on Mac OS X. + - The scheduler did not use the job owner when authorizing access for + the CUPS-Get-Document operation, preventing non-admins from accessing + their own jobs. + - CUPS did not work with some printers that incorrectly implemented the + HTTP/1.1 standard (STR #3778, STR #3791) + - The scheduler did not retry fax jobs properly. + - The scheduler now recognizes an empty cupsCommands PPD keyword as + meaning that CUPS commands are not supported for a printer (STR #3773) + - Fixed a crash bug in the scheduler when the application/octet-stream + MIME type was not defined (STR #3690) + - Polled printers were advertised more slowly than necessary (STR #3574) + - cupsResolveConflicts() did not handle resolving multiple UIConstraints + issues (STR #3705) + - The SetEnv and PassEnv directives had no effect (STR #3664) + - The libusb-based USB backend printed slowly to the LaserJet 1300 and + other printers (STR #3405) + - "lp" and "lpr" failed to print with Kerberos enabled (STR #3768) + - The cupsctl program now displays an error if you try to directly set + the Port or Listen directives (STR #3749) + - PPD files with "*JobPatchFile: bla" no longer fail to load in relaxed + conformance mode (STR #3747) + - The scheduler generated a bad notify-text string for printer state + change notifications (STR #3739) + - The scheduler incorrectly updated printers.conf when it really needed + to update classes.conf or remote.cache (STR #3726) + - Hardwired remote printers with options did not work (STR #3717) + - Accessing the CUPS web interface using a CNAME-based hostname would + sometimes fail due to redirection to the actual hostname (STR #3701) + - Subscription events had a misspelled attribute (STR #3693) + - "make check" failed if LC_MESSAGES was set (STR #3765) + - Fixed the configure script to always look for the pkg-config script + (STR #3761) + - The scheduler now only looks up interface hostnames if HostNameLookups + are enabled (STR #3737) + - Fixed a compilation problem on DragonFly BSD (STR #3738) + - The default PageLogFormat value had the username and job ID swapped + from CUPS 1.3.x (STR #3727) + - The scheduler could crash if a browsed printer times out while a job + is printing (STR #3754) + - The scheduler incorrectly mapped custom page sizes to standard sizes + (STR #3764) + - cupsfilter and pstops did not map IPP attributes to PPD options due to + a change in cupsMarkOptions (STR #3756) + - The scheduler did not always show the most recent status message from + the print filters (STR #3731) + - The PostScript filter did not apply the mirror and number-up options + properly, leading to offset and clipped output (STR #3732) + - The network backends always reported "low toner" or "out of toner" + states, even for inkjet printers (STR #3733) + + +CHANGES IN CUPS V1.4.6 + + - Fixed a "make check" issue on Solaris (STR #3729) + - Regression: The pstops filter did not support landscape printing of + PostScript files (STR #3722) + - The scheduler killed retried (fax) jobs after restarting them + (STR #3697) + - The cupsAdminSetServerSettings() function disabled sharing when + debug logging was enabled (STR #3712) + + +CHANGES IN CUPS V1.4.5 + + - Documentation fixes (STR #3542, STR #3650) + - Localization fixes (STR #3635, STR #3636, STR #3647, STR #3666) + - Security: Fixed a memory corruption bug reported in CVE-2010-2941 + (STR #3648) + - The CUPS API incorrectly mapped the HTTP_UNAUTHORIZED status to the + IPP_NOT_AUTHORIZED status code, when IPP_NOT_AUTHENTICATED would be + the correct mapping (STR #3684) + - The scheduler would restart jobs while shutting down (STR #3679) + - Fixed a PPD loader bug that could cause a crash in cupsd (STR #3680) + - Improved the mapping of non-standard PPD and PWG names (STR #3671) + - The scheduler did not initialize Kerberos in all cases (STR #3662) + - cupsAdminSetServerSettings duplicated Listen and Order lines + (STR #3645) + - Added DeviceN colorspace support to the CUPS Raster format (STR #3419) + - ppdMarkDefaults() did not clear the marked field of the previous + choices (STR #3642) + - The serial backend would not allow a raw job to be canceled + (STR #3649) + - The socket backend could go into an infinite loop with certain + printers (STR #3622) + - Setting the PRINTER or LPDEST environment variables to "name/instance" + did not work (STR #3485) + - The scheduler did not handle the JobRetryLimit setting properly + (STR #3466) + - The lpstat command always showed a remote job ID of 0 for shared + printers (STR #3627) + - Increased the write timeout for the libusb-based USB backend to 5 + minutes (STR #3595) + - The libusb-base USB backend did not check whether the printer has a + serial number (STR #3590) + - The lpadmin command did not support setting of custom option values + (STR #3631) + - The lpadmin command did not support setting of the location or + description of a class (STR #3613) + - The cupsaddsmb command did not give up after too many failed attempts + (STR #3615) + - The CUPS library no longer uses certain problematic ctype macros that + change based on the locale's character set. + - PJL value substitution of more than 9 values was broken (STR #3621) + - Custom options with missing string values caused ppdEmit* to segfault + (STR #3620) + - Fixed an issue with the Italian version of the web interface + (STR #3624) + - Fixed the Solaris SMF configuration file for cups-lpd (STR #3611) + - The scheduler did not set the notify-subscribed-event attribute when + delivering printer-added or printer-modified events (STR #3608) + - The mailto notifier could get into an infinite loop (STR #3609) + - Date/time information was not shown in banner pages. + - Relational operators were broken in #if/#elif/#else/#endif expressions + for the PPD compiler. + - Moving a job via the web interface failed without asking for + authentication (STR #3559) + - The scheduler now clears the printer-state-reasons when the driver is + changed (STR #3570) + - The web interface did not allow a user to change the driver + (STR #3537, STR #3601) + - The scheduler was not setting the PATH_INFO environment variable when + needed (STR #3600) + - The scheduler incorrectly set the CUPSD_AUTH_TYPE environment + variable instead of AUTH_TYPE (STR #3599) + - Fixed a buffer overrun in the PPD compiler (STR #3594) + - Fixed some additional IPP job template attribute mapping issues in the + scheduler. + + +CHANGES IN CUPS V1.4.4 + + - Documentation updates (STR #3453, STR #3527, STR #3528, STR #3529) + - Security: The fix for CVE-2009-3553 was incomplete (STR #3490) + - Security: The texttops filter did not check the results of allocations + (STR #3516) + - Security: The web admin interface could disclose the contents of + memory (STR #3577) + - Security: CUPS could overwrite files as root in directories owned or + writable by non-root users (STR #3510) + - The cups-config utility did not return the correct linker options on + AIX (STR #3587) + - Fixed some IPP conformance issues with the scheduler's + ippget-event-life, operations-supported, output-bin, and sides + attributes (STR #3554) + - The OpenSSL interfaces have been made thread-safe and the GNU TLS + interface is explicitly forbidden when threading is enabled + (STR #3461) + - Fixed an IPP conformance issue with the scheduler's Send-Document + implementation (STR #3514) + - Added additional validation checks for the 1284 device ID (STR #3534) + - Fixed a problem with the RPM spec file (STR #3544) + - The lpstat command did not limit the job list to the specified + printers (STR #3541) + - The cupsfilter command did not set the RIP_MAX_CACHE environment + variable (STR #3531) + - Fixed support for media-col and page size variants (STR #3394) + - The PostScript filter did not support all media selection options for + the first page (STR #3525) + - The scheduler did not always remove job control files (STR #3425) + - The scheduler could crash on restart if classes were defined + (STR #3524) + - The scheduler no longer looks up network interface hostnames by + default on Mac OS X (STR #3523) + - ippWriteIO did not write collection (member) attributes properly in + all cases (STR #3521) + - The "cupsctl --remote-any" and corresponding web interface check box + (allow printing from the Internet) did not work reliably (STR #3520) + - The lpq and lpr commands would sometimes choose different default + printers (STR #3503) + - cupsDo*Request did not flush error text, leading to multiple issues + (STR #3325, STR #3519) + - cupsDoAuthentication did not cancel password authentication after 3 + failures (STR #3518) + - Fixed several LDAP browsing bugs (STR #3392) + - The Dymo driver did not support copies (STR #3457) + - The scheduler did not update the classes.conf file when deleting a + printer belonging to a class (STR #3505) + - The lppasswd command did not use localized password prompts + (STR #3492) + - The socket backend no longer waits for back-channel data on platforms + other than Mac OS X (STR #3495) + - The scheduler didn't send events when a printer started accepting or + rejecting jobs (STR #3480) + - The web interface now includes additional CSRF protection (STR #3498) + + +CHANGES IN CUPS V1.4.3 + + - SECURITY: The scheduler could try responding on a closed client + connection, leading to a crash (STR #3200) + - SECURITY: The lppasswd program allowed the localization files to be + overridden when running in setuid mode (STR #3482) + - Localization updates (STR #3352, STR #3409, STR #3422, STR #3452, + STR #3473, STR #3502) + - Documentation updates (STR #3451, STR #3504) + - The IPP backend now sets the printer-state-message to "Ready to + print." at the end of a successful job (STR #3460) + - The PPD compiler did not correctly add the manufacturer to the output + filename when using the "-m" option (STR #3469) + - The IPP backend did not handle authentication properly for the Get- + Printer-Attributes operation (STR 3458) + - Getting SNMP values larger than 127 bytes did not work. + - IPP conformance: Get-Jobs has a default value for requested-attributes + (STR #3383) + - cupsPrintFiles() did not report all errors (STR #3449) + - cupsAddDest() could read freed memory (STR #3448) + - The DBUS notifier did not build (STR #3447) + - The scheduler would crash when an active printer was deleted. + - The snmp backend did not work with some printers (STR #3413) + - The web interface did not show the conflicting values when setting + options (STR #3440) + - Setting options in the web interface did not always work (STR #3439) + - The scheduler did not use the Get-Job-Attributes policy for a printer + (STR #3431) + - The scheduler added two job-name attributes to each job object + (STR #3428) + - CSS files would not print (STR #3442) + - The scheduler did not clean out completed jobs when PreserveJobHistory + was turned off (STR #3425) + - The web interface did not show completed jobs for a printer + (STR #3436) + - Authenticated printing did not always work when printing directly to + a remote server (STR #3435) + - The USB backend did not work on Solaris (STR #3423) + - cupstestppd didn't catch problems with JobPatchFile definitions + (STR #3421) + - The socket backend could crash if a SNMP string had a negative length. + - Fixed some termination issues with the USB backend on Mac OS X. + - The side-channel APIs did not handle interrupts properly. + - The network backends incorrectly cleared the media-empty-warning + state. + - The web interface did not allow users to successfully add serial + printers (STR #3391) + - cupsTempFd() did not work in some situations (STR #3382) + - Some C API headers were missing C++ wrapper logic. + - The PPD compiler did not localize single-language PPD options properly + (STR #3386) + - Modifying a printer from the web interface sometimes caused the wrong + driver to be selected (STR #3418) + - The scheduler did not handle out-of-memory conditions properly when + loading a job (STR #3407) + - When adding printers from the web interface, the dynamic updates of + the device list made it hard to pick a device (STR #3406) + - Fixed a typo in the web interface admin page template (STR 3403) + - The web interface did not preserve the "printer is shared" state when + modifying a printer (STR #3390) + - The PPD compiler incorrectly inserted translations of empty strings + (STR #3411) + - The scheduler did not reset the SIGPIPE handler of child processes + (STR #3399) + - cupsGetNamedDest() incorrectly returned the default printer if the + named printer did not exist (STR #3397) + - Fixed a GNU TLS error handling bug (STR #3381) + + +CHANGES IN CUPS V1.4.2 + + - SECURITY: The CUPS web interface was vulnerable to several XSS and + HTTP header/body attacks via attribute injection (STR #3367, + STR #3401) + - Fixed localization errors (STR #3359, STR #3372, STR #3380, STR #3387) + - The documentation for classes.conf and printers.conf did not provide + the correct instructions for manual changes (STR #3351) + - The scheduler did not always rebuild printer cache files when the + driver was changed (STR #3356) + - The documentation makefile failed to install localizations when using + newer versions of Bash (STR #3360) + - The configure script did not use the --with-xinetd value for the + default LPD configuration path (STR #3347) + - The configure script incorrectly required glib for DBUS support + (STR #3346) + - The cupstestppd program incorrectly reported filters with bad + permisssions as missing (STR #3363) + - The cups.desktop file used the wrong locale names (STR #3358) + - cupsSideChannelRead() did not return an error for short reads. + - The installed PAM configuration file did not use the correct options + with the pam_unix2 module (STR #3313) + - The scheduler did not preserve default options that contained special + characters (STR #3340) + - The scheduler did not remove old pre-filters when updating a printer + driver (STR #3342) + - The HP/GL-2 filter did not check for early end-of-file (STR #3319) + - The USB backend did not compile on some platforms (STR #3332) + - cupsSideChannelSNMPWalk() could go into an infinite loop with broken + SNMP implementations. + + +CHANGES IN CUPS V1.4.1 + + - Documention fixes (STR #3296) + - SNMP supply levels and states were wrong for some printers. + - The IPP backend did not update the auth-info-required value. + - The libusb-based USB backend would hang at the end of the job + (STR #3315, STR #3318) + - DNS-SD registrations for raw queues had an empty "ty" key (STR #3299) + - The JPEG and BMP MIME type rules were broken (STR #3284) + - cupsGetNamedDest returned the default printer when the named + destination did not exist (STR #3285) + - The JobKillDelay was not triggered for canceled jobs (STR #3292) + - The PPD compiler could get in an infinite loop (STR #3293) + - The configure check for dns-sd.h was broken (STR #3297) + - The "Query Printer for Default Options" page did not go away if the + query job was held (STR #3302) + - Boolean options did not show up as selected in the web interface + (STR #3303) + - The scheduler did not cache or report driver information files + correctly, leading to a variety of issues (STR #3283, STR #3297, + STR #3305) + - cupsDoIORequest() did not abort on permanent errors (STR #3311) + - Modifying a class in the web interface did not work (STR #3312) + - BrowseLocalProtocols could be cleared when changing the sharing + setting (STR #3287) + - The scheduler could return an empty supported document format + (STR #3308) + - The PPD compiler generated invalid PPD files when the locale used + something other than "." for the decimal point (STR #3300) + - The IPP backend did not handle some non-comforming IPP printer + implementations (STR #3262) + - The scheduler leaked three file descriptors to each job filter + (STR #3263) + - The scheduler now uses a default CUPS-Get-Devices timeout of 15 + seconds (STR #3307) + + +CHANGES IN CUPS V1.4.0 + + - Localization updates (STR #3223, STR #3246, STR #3248, STR #3250) + - Documentation updates (STR #3225, STR #3230, STR #3242, STR #3260) + - The --with-pdftops configure option did not accept a full path to the + filter (STR #3278) + - The banner filter did not position the back side image correctly + (STR #3277) + - The dnssd backend could crash (STR #3272) + - The 1284 device ID sometimes contained trailing garbage (STR #3266) + - The USB backend returned different URIs for some printers than in + CUPS 1.3 (STR #3259) + - The scheduler did not do local job-hold-until processing for remote + queues (STR #3258) + - The scheduler did not try all possible SSL certificates on Mac OS X. + - The scheduler did not always remove a file descriptor when using the + kqueue interface (STR #3256) + - The scheduler did not protect against bad job control files in all + cases (STR #3253) + - The scheduler did not encode "+" in model names (STR #3254) + - The web interface didn't show the default options (STR #3244) + - The IPP and LPD backends needed print data before they would do an + SNMP query. + - Fixed a GNU TLS compatibility issue (STR #3231) + - Fixed a HTML error in the add and modify printer web interface + templates (STR #3229) + - The scheduler did not minimize the number of printer state events that + were generated by filter STATE: messages, which could lead to poor + performance. + - The USB backend on Mac OS X did not cleanly cancel a job. + - The network backends now set the connecting-to-device printer-state- + reasons value when looking up the address and copying the print data + for consistency. + - The scheduler now supports the com.apple.print.recoverable-warning + reason on all platforms. + + +CHANGES IN CUPS V1.4rc1 + + - The PPD compiler documentation was missing information on localization + (STR #3212) + - The IPP backend now reconnects after every request when talking to + printers that claim IPP support but only use HTTP/1.0. + - The PPD compiler crashed when both "Resolution" and "Group foo Option + Resolution" were specified in the .drv file. + - The PPD compiler's #if/#elif/#else/#endif did not work for undefined + variables (STR #3210) + - Static libraries could not be installed by a non-root user on systems + needing a ranlib program (STR #3209) + - The scheduler incorrectly always tried to copy Kerberos credentials + for print jobs. + - Updated the Spanish localization (STR #3204) + - The scheduler crashed when getting the default paper size from + libpaper (STR #3205, STR #3206) + - The PPD compiler now defines six variables: CUPS_VERSION, + CUPS_VERSION_MAJOR, CUPS_VERSION_MINOR, CUPS_VERSION_PATCH, + PLATFORM_NAME, and PLATFORM_ARCH (STR #3203) + - Fixed a whitespace skipping bug in cupsRasterInterpretPPD. + - The scheduler did not return HTTP 403 (Forbidden) for authenticated + users that were not authorized to do IPP operations (STR #3193) + - The scheduler did not report more than 8 Product strings from a PPD + file. Some PPD files have as many as 24. + - ppdOpen*() could crash if a keyword had no value string (something + that cupstestppd looks for...) + - cupsLangDefault() did not return the correct language on Mac OS X. + - The Mac OS X USB backend did not handle aborted or stalled pipe + conditions properly, which prevented drivers from ejecting partial + pages when a job was canceled or held. + + +CHANGES IN CUPS V1.4b3 + + - Documentation fixes (STR #3044, STR #3057, STR #3153, STR #3158, + STR #3173) + - Added complete localizations for German, Japanese, Polish, and + Russian and partial localizations for Chinese, Danish, Finnish, + French, Italian, Korean, Norwegian, Portuguese, and Swedish + (STR #3096, STR #3098, STR #3109, STR #3111, STR #3141) + - Updated the configure check for -fstack-protector (STR #3198) + - The network backends now correctly convert SNMP supply descriptions to + UTF-8 encoding as needed. + - The scheduler could crash when deleting an attribute (STR #3197) + - The cups-driverd program did not detect symlink loops (STR #3185) + - The EPSON 24-pin series driver should now feed the correct amount + (STR #2624) + - The scheduler now automatically logs the last N debug messages for + failed print jobs. + - You can now modify a raw print queue (STR #3133) + - Fixed a number of ppdi issues and added a unit test to validate that + ppdc + ppdi can generate and import the same data (STR #3152) + - Moving jobs in the web interface now shows an error if you only have + one printer or class added (STR #3094) + - Since classes have never truly supported the printer-error-policy + stuff added in CUPS 1.2, update the code to reflect the current + reality and support only the retry-current-job policy for now + (STR #3171) + - Revised the password callback support (STR #2953) + - ppdEmit*() did not choose between PageSize and PageRegion properly. + - Make some fairly substantial changes to the Kerberos support code so + that CUPS can work in multi-realm environments and does not require + delegatable credentials. Shared printing still requires delegation, + however "delegation by policy" can be enabled in the KDC to make this + all work. + - "AccessLogLevel actions" did not hide client-error-not-found errors. + - AP_FIRST_InputSlot did not work with number-up. + - cupsBackChannelRead() and cupsBackChannelWrite() could fail due to a + lack of kernel buffers. + - The IPP and LPD backends did not respond to side-channel requests + while copying print data to a temporary file. + - cupsWriteRequestData() flushed the output buffer unnecessarily, + causing reduced performance in some situations. + - If a CGI process died before sending its MIME headers, the request + would hang on the client. + - The printer/class/job search feature on the web interface did not + work (STR #3132) + - The scheduler did not write the printers out for classes. + - CUPS-Get-PPDs did not work properly when filtering by language, + product, or psversion (STR #3136) + - The scheduler now kills job filters when it takes more than 30 seconds + (configurable) to cancel or hold the job. + - The cupstestppd program did not validate the capitalization of + filenames in the PPD file. + - The cupstestppd program did not validate the PageSize and PageRegion + values. + - The cups-deviced helper program could miss reporting some backend + devices (STR #3108) + - The cupsSideChannelSNMP* functions did not work. + - The scheduler could consume 100% CPU when jobs were canceled. + - Clicking on "Select Another Make/Manufacturer" in the web interface + incorrectly added the printer (STR #3095) + - The scheduler no longer uses programs with insecure file + permissions. + - httpAssembleURI*() did not escape backslashes in hostnames. + - The dnssd backend did not unquote "full names" before creating the + device URI. + - The scheduler now supports JobRetryInterval values less than 10 + seconds. + - Updated the Spanish localization (STR #3090) + - The scheduler did not redo Bonjour/DNS-SD registrations when updating + them failed. + - The "authenticated" policy incorrectly required authentication for + status operations. + - ppdOpen*() incorrectly loaded PPDs with multiple JobPatchFile + keywords. + - The network backends no longer report the SNMP "offline" or + maintenance status bits since they are inconsistently implemented and + often unreliable. + - The scheduler no longer logs child processes killed via SIGKILL as + "crashed". + - The printer link shown on the "job moved" template was bad (STR #3085) + - Updated the HTML templates to use the final HTML 4 DOCTYPE (STR #3086) + - The scheduler did not track the "paused" reason properly if a + printer had other reasons associated with it. + - cupsSendRequest() did not clear old local certificate auth data. + - The PPD compiler did not search for localization files properly + (STR #3084) + - cupsGetNamedDest() did not use the fallback default like + cupsGetDests*() (STR #3082) + - The scheduler now provides a LogTimeFormat directive to enable + microseconds in the date and time that are logged. + - The scheduler now provides a MultipleOperationTimeout directive to + control the timeout for multi-file print jobs. + - The configure script incorrectly allowed Avahi to be used for DNS-SD + printer discovery (STR #3065) + - The web interface and scheduler did not support URIs up to 1024 bytes + in length (STR #3072) + - Fixed pdftops issues with page sizes (STR #3063) + - Fixed pdftops issues with Ghostscript (STR #3062) + - The scheduler incorrectly registered default profiles for PostScript + printers with no specified colorspace. + - The scheduler incorrectly created an empty org.cups.printers.plist + file on Mac OS X. + - cupsGetPPD3() did not look for local PPDs in the right directory. + - SNMP lookups via side-channel did not work for NULL-VALUE and + and OCTET-STRING OIDs containing nul characters. + - The libusb-based USB backend did not work. + - The scheduler did not set the printer-commands attribute correctly + for some PPDs. + - The ppdi utility did not work. + - The web interface no longer uses multi-part output with old or broken + web browsers (STR #3049) + - CUPS now conforms to the draft IPP/2.0 and IPP/2.1 specification. + - Added a new cupsGetConflicts() API to get a list of conflicting + options. + - The PPD compiler didn't localize options or choices that did not + have associated translation text (STR #3045) + - Updated the Spanish localization (STR #3043) + - Fixed build problems (STR #3040, STR #3047) + - cupsResolveConflicts() did not resolve using the default option + choice in some cases due to the mirror UIConstraints that are + present in most PPD files. + - The scheduler did not honor MIME type priorities. + - The commandtops filter incorrectly used the JCLBegin code to end + its jobs. + - The default BrowseLocalProtocols value was not set properly. + - Since the commandtops filter does not actually support ReportLevels + all on its own, don't list that printer command by default for PS + printers. + - The scheduler did not give filters a chance to log errors or update + printer attributes when a job was canceled. + - The scheduler did not clear the "connecting-to-device" reason keyword + when a job finished. + + +CHANGES IN CUPS V1.4b2 + + - Documentation updates (STR #2983, STR #2998, STR #3021) + - The cupstestppd utility now validates the FileVersion and + FormatVersion values in PPD files. + - The default cupsd.conf file did not reflect the + --with-local-protocols value set at compile-time (STR #3037) + - The cupsGetPPD* APIs now create symlinks to local PPD files + rather than copying them whenever possible. + - Various performance optimizations in the string pool, dests, and + options implementations. + - The cupsGetDests* APIs now return the marker and printer-commands + attributes. + - Side-channel SNMP lookups would not work when cupsSNMPSupplies + was set to False in the PPD file. + - Localized the device descriptions for the SCSI, serial, + and network backends (STR #3014) + - Added a Spanish localization (STR #3015) + - Added support for marker-low-levels and marker-high-levels + attributes. + - The scheduler could hang writing a long log line. + - The cupsGetDevices() function now has an "include_schemes" + parameter. + - The lpinfo command now supports --include-schemes and + --exclude-schemes options. + - The CUPS-Get-PPDs operation now supports the include-schemes + and exclude-schemes attributes. + - The CUPS-Get-Devices operation now supports the include-schemes + attribute. + - The print filters now support a replacement for the fitplot + option called "fit-to-page". + - The LPD backend no longer tries to collect page accounting + information since the LPD protocol does not allow us to + prevent race conditions. + - The scheduler did not save the last marker-change-time value. + - Fixed a problem with printing to some IPP printers, including + CUPS 1.1.x. + - Fixed a redirection problem with the printer web page (STR #3012) + - Fixed a PPD compiler problem with the loading of message + catalogs (STR #2990) + - Fixed a PPD compiler problem with the loading of .strings files + (STR #2989) + - The cupsfilter utility did not set the CONTENT_TYPE environment + variable when running filters. + - The scheduler now waits to allow system sleep until the jobs + have all stopped. + - The IPP, LPD, and socket backends used different "connecting" + progress messages. + + +CHANGES IN CUPS V1.4b1 + + - Documentation updates (STR #2567) + - The PPD compiler now allows local message catalogs to + override the standard CUPS translations (STR #2642) + - The ppdmerge command did not merge custom option strings + (STR #2863) + - The scheduler now supports the Hold-New-Jobs and + Release-Held-New-Jobs operations; these are exposed via the + cupsdisable and cupsenable commands (STR #2332) + - The lpstat command is now much faster when displaying the + status of a single printer (STR #2843) + - The scheduler now caches information from PPD files to provide + significantly faster startup time with large numbers of PPDs + (STR #1293) + - CUPS-Get-Driver now provides much better driver matching based + on the IEEE-1284 device ID and make/model strings (STR #2707) + - Now support the cupsSNMPSupplies keyword to control whether + the network backends query the SNMP Printer MIB for supply + levels. + - Now support and use a new banner file format for better text + support and easier customization (STR #2490) + - The scheduler now sets the PRINTER_INFO and PRINTER_LOCATION + environment variables from the corresponding IPP attributes. + - The ippRead*() and ippWrite*() functions no longer use a + stack-based buffer (STR #2388) + - The CUPS-Add-Modify-Printer operation now allows you to set + the printer-state-reasons attribute. + - The "set printer options" page now supports auto-configuration + of printer options (STR #1440) + - The web interface now provides an advanced server settings + form. + - The web interface's "modify printer" pages now make it + easier to change just one setting (STR #1919) + - The scheduler now supports a plist PrintcapFormat. + - The scheduler now supports multiple addresses in Allow and + Deny lines, just like Apache (STR #2947) + - Added CUPS_JOBTYPE environment variable for job filters so + they know whether they are printing a banner or document + file (STR #2799) + - Added support for printer filtering by the cupsfilter + command (STR #2562) + - Added a SSLOptions directive to allow Windows clients to + talk to CUPS in FIPS mode (STR #2827) + - Renamed the accept and reject commands to cupsaccept and + cupsreject; the old names are still available (STR #2936) + - The locale/translate utility needed an update to work with + Google (STR #2882) + - The lpstat command now supports a -H option to display the + default server (STR #2833) + - The scheduler now supports a FatalErrors directive to control + which errors should cause the scheduler to exit (STR #2536) + - The scheduler now uses the php-cgi program if it is available + (STR #2923) + - The scheduler now supports a DefaultPaperSize directive + (STR #2848) + - The scheduler now passes the job-originating-host-name + value to filters in the options argument (STR #2558) + - CUPS now supports job tickets in PDF files (STR #2903) + - Added a DBUS notifier (STR #2529) + - The LPD mini-daemon now passes the document name when queuing + print jobs (STR #2482) + - The IPP backend did not relay com.apple.print.recoverable-message + values. + - The scheduler now supports a job-media-progress attribute to + track the progress of individual pages. + - The sample HP driver now supports A5 (STR #2798) + - The CUPS web interface menu item now uses the xdg-open + command, when available (STR #2724) + - The cups-lpd program now supports the -h option (STR #2794) + - The scheduler now sets the PAM_TTY parameter and the + PAM_ESTABLISH_CRED credential flag (STR #2745) + - The scheduler now logs unsuccessful requests to the error_log + file as errors (STR #2616) + - Added support for a "retry-current-job" error policy that + retries the current job immediately when the backend encounters + an error (STR #2555) + - The scheduler now returns a "forbidden" error when a user + correctly authenticates but does not have permission to + continue further (STR #2101) + - The scheduler now loads both the server and CA certificates + (if present) from the ServerCertificate file (STR #2146) + - New RSS subscriptions now create their feed files immediately + (STR #2853) + - Added support for a device-location attribute which provides + the physical location of a printer device. + - Added a cupsBackendReport() API which handles quoting of the + device data by a backend. + - Added support for custom options in the web interface + (STR #1729) + - Added support for Mozilla LDAP, reconnection to LDAP servers, + and improved LDAP performance (STR #1962) + - Added Solaris SMF support (STR #1477) + - Added optional support for using TCP wrappers to limit access + to CUPS (STR #263) + - Added ppdPageSizeLimits API. + - Added support for new cupsMediaQualifier2, cupsMediaQualifier3, + cupsMinSize, and cupsMaxSize attributes. + - Added cupsResolveConflicts and ppdInstallableConflict APIs. + - Added support for new cupsUIConstraints and cupsUIResolver + attributes for better option conflict detection and + resolution. + - Increased the maximum size of 1284 device ID strings to + 256 bytes (STR #2877) + - Added an AccessLogLevel directive to cupsd.conf to control + what is logged to the access_log file. + - The default LogLevel is now "warn" instead of "info" to reduce + the amount of logging that is done to disk by default. + - The PPD compiler did not include OID query keywords in PPD + files (STR #2871) + - The cups-driverd helper program now directly supports driver + information files. + - The USB backend now uses libusb when available (STR #1575) + - Added ppdLocalizeAttr function to get the localized version + of an attribute. + - MIME types now support a priority() attribute (STR #2719) + - The standard MIME types are now installed in + DataDir/mime (STR #2719) + - The lpoptions command now describes custom options and + the necessary parameters (STR #2660) + - The ppdmerge program did not support Simplified Chinese + or Traditional Chinese language version strings (STR #2851) + - The PPD compiler now supports localizable attributes + (STR #2738) + - The ppdpo utility now includes cupsIPPReasons values in + the message catalogs it generates (STR #2754) + - The PPD compiler now supports conditional directives + (STR #2636) + - The ppdc utility now supports a "-t" option to test PPD + files (STR #2739) + - The ppdc utility now supports a "-m" option to use the + ModelName value as the output filename. + - The ppdc utility now supports a FileName directive to + set an alternate output filename (STR #2740) + - The side-channel API now supports SNMP queries for the + standard network backends. + - Added a PageLogFormat directive to the cupsd.conf file to + control the format of lines in the page_log file. + - Filters can now send PPD: messages to stderr to set PPD + keywords like DefaultPageSize while a job is printing. + - Added a mdns backend for discovery and printing to printers + that advertise themselves via DNS-SD (Bonjour) + - The ipp, lpd, and socket backends now support DNS-SD service + name resolution. + - The scheduler now uses a single shared file descriptor for + all DNS-SD registrations (STR #2674) + - The ipp, lpd, and socket backends now support SNMP-based + page accounting and supply level monitoring (STR #1655) + - Added support for cupsPJLDisplay attribute to control what + PJL commands are used to display the job information. + - Driver information files can now be installed in + /Library/Printers/PPDs.drv on Mac OS X. + - The CUPS image library now supports reading images larger + than 2GB. + - The scheduler now delays writing config and state files to + reduce disk activity (STR #2684) + - The CUPS-Get-Devices operation now supports the + exclude-schemes and timeout attributes to control which + backends are polled and for how long. + - The cups-deviced helper application now runs backends in + parallel to get the list of devices faster. + - Added --enable-pap configure option. + - The default cupsd.conf file now includes an "authenticated" + policy which requires authentication for remote print jobs. + - Added support for Czech and Hungarian in PPD files + (STR #2735, STR #2736) + - The PPD compiler tools now support Mac OS X .strings files + for localization (STR #2737) + - ppdOpen*() now default the colorspace member to PPD_CS_N + when no DefaultColorSpace attribute is present in the PPD + file. + - The build system has been updated to support separate + installation of data, program, header, and library files. + - All support libraries are now built as shared libraries + by default. + - The scheduler now manages ICC color profiles on Mac OS X. + - The network backends (ipp, lpd, socket) now support + SNMP-based supply and page count monitoring (STR #1655) + - The lppasswd program is no longer installed setuid to + root to make the default installation more secure. + - Added a new ppdLocalizeMarkerName() function to get + the localized version of a marker-names value. + - The scheduler now provides the printer-dns-sd-name + attribute for printers shared via DNS-SD/Bonjour. + - The pdftops filter now executes the Xpdf or poppler + pdftops utility to convert PDF files (STR #1471) + - Bonjour printer registrations now advertise as local or + global based on the current access policies for the + printer. + - cupsGetDests*() and cupsSetDests*() now track the last + used printer preference on Mac OS X. + - Added a new streaming request API (STR #2261) + - Added a new cupsGetNamedDest() function to the CUPS + library for faster printing with lp and lpr (STR #2638) + - The scheduler now sets the PAM RHOST value on systems + that support it (STR #2637) + - The scheduler now sandboxes child processes when + possible. + - The Cancel-Job operation now supports a purge-job + attriibute to purge a specified job. + - ppdEmit* and ppdCollect* now use the NonUIOrderDependency + attributes for custom option selections. + - The web interface now enables/disables the printer + sharing (formerly publishing) controls based on the + server-is-sharing-printers state (STR #2233) + - The scheduler now tracks printer sharing via the + server-is-sharing-printers attribute, and manages LPD + and SMB sharing as well (STR #2233) + - The web interface now allows you to go back to the make/ + manufacturer page if there is no matching printer driver + on the model page (STR #2436) + - The printer list now shows the default media, banner, and + duplex options as well as the color and duplex capabilities + of printers (STR #1175) + - The web interface look-n-feel has been updated (STR #2492) + - The scheduler now supports a CUPS-Get-Document operation + that returns the specified print job document (STR #118) + - The cupsfilter utility now supports a "-J jobid" option + to filter the document from the specified job. + - The scheduler (cupsd) now supports a new option (-t) to + do a syntax check of the cupsd.conf file (STR #2003) + - Added new cupsGetPPD3() API to allow applications to + cache PPDs safely (STR #1473) + - Added generic PostScript and PCL printer driver PPDs. diff --git a/CHANGES-1.5.txt b/CHANGES-1.5.txt new file mode 100644 index 0000000..39edb42 --- /dev/null +++ b/CHANGES-1.5.txt @@ -0,0 +1,312 @@ +CHANGES-1.5.txt +--------------- + +CHANGES IN CUPS V1.5.4 + + - Documentation updates (STR #4112, STR #4130, STR #4134) + - Fixes for libusb-based USB backend (STR #4128) + - The lpq command did not show the owner or title of jobs unless passed + a username on the command-line (STR #4135) + - Localized empty strings contained the message catalog metadata + (STR #4119) + - Fixed a crash in the libusb-based USB backend (STR #4099) + - The cups-lpd mini-daemon no longer handled jobs with multiple copies + (STR #4118) + - Multiple libusb backend fixes (STR #4098, STR #4100) + - The IPP backend no longer tries to get the job status for printers + that do not implement the required operation (STR #4083) + - Sending a document in an unsupported format to an IPP printer now + automatically cancels the job (STR #4093) + - Fix some error reporting issues when printing from /dev/null and + other unusual situations (STR #4015) + - The scheduler now sets the CUPS_MAX_MESSAGE environment variable for + filters (STR #4074) + - Fixed a build issue when using older versions of autoconf (STR #4084) + - The IPP backend now treats the client-error-not-possible status code + as a job history issue, allowing IPP printing to Windows to work + (STR #4047) + - The IPP backend incorrectly included the document-format and + compression attributes in Create-Job requests (STR #4086) + - The libusb-based USB backend did not work on non-Linux platforms + (STR #4088) + + +CHANGES IN CUPS V1.5.3 + + - httpReconnect() did not reset the read/write buffers (STR #4065) + - Compiling without threading support failed (STR #4060) + - Fixed compile problem with old versions of OpenSSL (STR #4036) + - The network backends did not check SNMP supply levels regularly + (STR #4040) + - The online help always included the "help on help" text (STR #4042) + - Fixed a SSL handshake issue on OS X (STR #4045) + - The scheduler could crash if a PPD file contained an invalid paper + size (STR #4049) + - The CUPS polling daemon did not reinitialize its connection to the + remote server on errors in all cases (STR #4031) + - PostScript auto-configuration was slow and unreliable with some + printers (STR #4028) + - Missing localizations caused empty output (STR #4033) + - The cups-driverd program could temporarily "forget" a PPD file if it + was updated in place. + - The dnssd backend now prefers IPPS over IPP. + - The USB backend now uses and requires LIBUSB 1.0 or later (STR #3477) + - The LIBUSB-based USB backend now supports the back-channel (STR #2890) + - Changed how timeouts are implemented in the LPD backend (STR #4013) + - Added more supported color names for SNMP supplies (STR #3981) + - The default InputSlot setting was never used (STR #3957) + - POSIX ACLs are now set properly on certificate files (STR #3970) + - Supplies with commas in their names were not reported correctly + (STR #4020) + - The cupsGetPPD3() function created a temporary file when one was not + needed (STR #4018) + - The scheduler now ensures that job notifications contain a value for + the notify-printer-uri attribute (STR #4014) + - The lp and lpr commands did not cancel jobs queued from stdin on an + error (STR #4015) + - Fixed the IPP backend's handling of HTTP/1.0 compatibility (STR #3988) + - The IPP backend did not always setup username/password authentication + for printers (STR #3985) + - The IPP backend no longer re-queues print jobs that are too large for + the printer/server (STR #3977) + - The RPM spec file did not work (STR #4021, STR #4057) + - Encryption did not work when the server name ended with "." + (STR #4011) + - The multi-purpose tray is now mapped to the IPP "by-pass-tray" + (STR #4009) + - The correct media size was not always passed to IPP printers + (STR #4001) + - Finishing options were not passed to IPP printers (STR #3995) + - Fixed iCloud-based Back to My Mac printing (STR #3996) + + +CHANGES IN CUPS V1.5.2 + + - Reposted what should have been CUPS 1.5.1. + + +CHANGES IN CUPS V1.5.1 + + - Documentation updates (STR #3885, STR #3886, STR #3946, STR #3969) + - Localization updates (STR #3840, STR #3989, STR #3997) + - Build fixes (STR #3956, STR #3999) + - The SNMP backend did not validate the device URIs reported by printers + (STR #4004) + - cupsBackendReport() did not handle newlines in 1284 Device IDs + (STR #4005) + - USB backend fixes for libusb (STR #3965, STR #3978) + - The DBUS notifier did not validate string parameters (STR #3984) + - Group quota ACLs did not work with Kerberos (STR #3972) + - The IPP backend did not retry when a printer responded with + client-error-not-possible (STR #3963) + - PostScript PPDs with filters used the wrong command filter (STR #3973) + - The scheduler incorrectly used free() on a POSIX ACL value, which + could cause a crash (STR #3970) + - PPD files using the MacStandard encoding did not work. + - The web interface did not work on some platforms (STR #3902) + - The lpstat command would crash when then "-u" option was used by a + non-administrator (STR #3953) + - Japanese supply level reporting did not always work. + - The DBUS notifier could crash (STR #3947) + - Relaxed some of the page size checks in cupstestppd. + - The ipptool program now reports attributes that are repeated within + the same attribute group. + - Updated the PWG raster support to match the current draft + specification. + - Fixed some IPP conformance issues in the scheduler. + - Added ipptool support for repeating requests. + - Added IPP/2.2 conformance tests and greatly improved the IPP/1.1, + IPP/2.0, and IPP/2.1 conformance testing. + - IPP messages containing mixed integer/rangeOfInteger values did not + work (STR #3942) + - The ipptool program now provides additional diagnostics for badly- + formatted responses (STR #3857) + - When possible, the IPP backend now stops sending job data early on a + cancel. + - cupsSendRequest and cupsWriteRequestData did not properly read all + HTTP headers, preventing authentication and encryption upgrades from + working in all cases. + - The client.conf Server directive is no longer supported on Mac OS X + 10.7 and later. + - The IPP backend sent the wrong margins in media-col. + - The scheduler did not save or restore large Kerberos credentials for + jobs. + - The dnssd backend did not properly browse for secure IPP printers. + - httpAssembleURI* did not properly escape all special characters in the + username/password field. + - The scheduler now logs config file errors to stderr (STR #3936) + - The configure script incorrectly used bundle-based localizations on + Linux (STR #3938) + - The cups-driverd helper program did not cache .drv files properly, + sometimes leading to a crash (STR #3921) + - CUPS did not build on stock Mac OS X installations. + - Encryption was broken with OpenSSL. + - ipptool's XML output used date/time values with timezone offsets, + which are not supported by Mac OS X's NSDate class. + - Several programs did not support the cupsFilter2 keyword in PPD files. + - The IPP backend incorrectly reported spool-area-full states. + - cupsMarkOptions() did not protect against a bad PPD that was missing + one or more standard Duplex options. + - The PostScript filter did not mirror N-up output properly. + - The ipptool program did not validate UTF-8 strings in XML output. + - Fixed supply level reporting for some printers. + - The scheduler no longer automatically logs debug messages for jobs + that were held or canceled. + - The cupsSendRequest function did not flush remaining response data + from a previous request, leading to apparent chunking issues. + - The scheduler did not report the correct version in the Server: header + (STR #3903) + - The scheduler did not support 1284 device IDs reported by driver + interface programs longer than 127 characters (STR #3871) + - The image filters did not support loading images larger than the + RIPCache setting (STR #3901) + - "PAGE: total NNN" messages did not get logged properly (STR #3887) + - Updated the PWG Raster support to conform to the current draft of the + PWG Raster Format specification. + - The PWG Raster filter did not always write the correct number of + padding lines on the bottom of the page (STR #3904) + - When reporting a denial-of-service attack from the domain socket, the + address reported does not always contain the correct path (STR #3888) + - Badly formed GIF files could cause the image filters to crash + (STR #3914) + - Jobs canceled at the printer were retried by the IPP backend. + - "cupsfilter -u" deleted the input file instead of the PPD file. + - The scheduler did not compute the cost of PPD filters defined using + the cupsFilter2 keyword properly. + - The scheduler did not correctly support the maxsize() attribute for + PPD filters. + + +CHANGES IN CUPS V1.5.0 + + - Documentation updates. + - Localization update (STR #3865) + - Needed to limit TLS to v1.0 on some versions of Mac OS X. + - The snmp backend did not work with some printers. + + +CHANGES IN CUPS V1.5rc1 + + - Compile fixes (STR #3849, STR #3850) + - The scheduler didn't check for empty values for several configuration + directives (STR #3861) + - ipptool didn't generate valid XML when a test was skipped. + - Added additional error checking to the 1284 device ID code (STR #3858) + - Fixed some compatibility issues migrating from the old usblp backend + to the libusb backend (STR #3860) + - Fixed the wake-from-sleep printing behavior on Mac OS X. + - The scheduler incorrectly allowed jobs to be held from a terminating + state. + - The cups-driverd program could crash when a PPD was renamed. + - The dnssd backend took too long to discover printers on large or busy + networks with the new default timeout used by lpinfo and the web + interface. This resulted in "lost" printers. + + +CHANGES IN CUPS V1.5b2 + + - Documentation updates. + - Localization updates (STR #3845) + - Compiler warning cleanup. + - Fixed PIE support for Linux (STR #3846) + - Made httpSetTimeout API public and use it in the IPP backend to avoid + timeout errors. + - The scheduler incorrectly set the "authenticated" printer-type bit for + remote queues using authentication. + + +CHANGES IN CUPS V1.5b1 + + - The CUPS library now supports per-connection HTTP timeouts and + callbacks. + - The CUPS library now supports (limited) SSL/TLS X.509 certificate + validation and revocation (STR #1616) + - Updated the PostScript filter to support IncludeFeature in more + circumstances (STR #3417) + - The schedule did not correctly parse some IPv6 addresses and masks in + the cupsd.conf file (STR #3533) + - Fixed a case-insensitive string comparison issue for locales that do + not treat "I" and "i" as equivalent (STR #3800) + - The scheduler reported an incorrect job-printer-uri value when sharing + was not enabled (STR #3639) + - The scheduler now allows the ServerAlias directive to contain multiple + hostnames separated by spaces or commas (STR #3813) + - The scheduler now sets the process group for child processes and + manages the group (STR #2829) + - Fixed some minor issues discovered by a Coverity scan (STR #3838) + - The scheduler now more carefully creates and removes configuration, + cache, and state files (STR #3715) + - The lpadmin command now allows default option values to be deleted + (STR #2959) + - The lpadmin command now allows the cupsIPPSupplies and + cupsSNMPSupplies keywords to be set in a PPD file (STR #3825) + - Moving a held job no longer releases it (STR #3839) + - Restored support for GNU TLS and OpenSSL with threading enabled + (STR #3605) + - Fixed a confusing error message from cups-polld (STR #3806) + - Increased the default RIPCache value to 128MB (STR #3535) + - MIME errors are now routed to the error_log file (STR #2410) + - Updated PDF filter to support new Ghostscript ps2write device + (STR #3766) + - Updated PDF filter to support new Poppler option to preserve page + sizes in PDF files when the user has not selected a particular media + size (STR #3689) + - Added new PWG Raster filter for IPP Everywhere printer support. + - Added job-uuid, printer-uuid, and subscription-uuid attributes. + - Added support for the cupsSingleFile PPD keyword. + - Dropped support for the printer-state-history attribute (STR #3654) + - Added support for a new cupsIPPSupplies keyword in PPD files to allow + drivers to disable IPP supply level reporting. + - Added support for a new cupsFilter2 keyword in PPD files to allow for + the propagation of the actual MIME media type produced by a filter. + - The scheduler did not always get the correct Kerberos username when + authenticating (STR #3670) + - Added new cupsRasterOpenIO function and CUPS_RASTER_WRITE_PWG to the + CUPS imaging library to support printing to IPP Everywhere raster + printers. + - The scheduler now provides default values for the pages-per-minute and + pages-per-minute-color attributes for PPD files that lack a + Throughput keyword. + - Email notifications did not work on Mac OS X. + - The cupstestppd program now shows an error for files missing a + CloseGroup keyword (STR #3668) + - Name resolution errors no longer cause queues to stop (STR #3719, + STR #3753) + - Added a new cups-exec helper program that applies security profiles + to filters, port monitors, backends, CGI programs, and mini-daemons. + - The web interface can now be disabled using the WebInterface directive + in cupsd.conf (STR #2625) + - The scheduler now provides privacy controls for jobs and subscriptions + (STR #2969) + - Added new cupsArrayNew3 API which offers memory management of array + elements. + - Added several new color spaces to the CUPS raster format (STR #3419) + - The Validate-Job operation now uses the same policy as Print-Job by + default. + - CUPS now uses iconv to implement all of its character encoding + support (STR #3097) + - The scheduler now implements the Cancel-Jobs, Cancel-My-Jobs, and + Close-Job operations along with the job-ids operation attribute from + PWG 5100.11. + - The main CUPS header () no longer includes the PPD header + (). + - The scheduler and CUPS API now support the print-quality job template + attribute. + - The scheduler no longer supports the old Mac OS X Server quota + plugin. + - The scheduler now allows writing to /Users/Shared from print filters + on Mac OS X. + - CUPS no longer supports the old ~/.cupsrc or ~/.lpoptions files from + CUPS 1.1.x. The ~/.cups/client.conf and ~/.cups/lpoptions files that + were introduced in CUPS 1.2 must now be used. + - The ipptest tool is now a first-class user program and has several + improvements along with new documentation (STR #3484) + - The cupstestppd tool now warns about non-unique filenames and + provides a way to ignore all filename warnings. + - Dropped support for the recoverable: and recovered: message prefixes. + - The scheduler now requires that filters and backends have group write + permissions disabled. + - The PPD compiler now checks for overlapping filenames when writing + PPD files. + - The HP-GL/2 filter is no longer included with CUPS (STR #3322) + - The SCSI backend is no longer included with CUPS (STR #3500) diff --git a/CHANGES.txt b/CHANGES.txt new file mode 100644 index 0000000..9bf60a6 --- /dev/null +++ b/CHANGES.txt @@ -0,0 +1,225 @@ +CHANGES.txt - 1.6.3 - 2013-07-11 +-------------------------------- + +CHANGES IN CUPS V1.6.3 + + - The configure script now prefers Clang over GCC. + - Fixed a compile problem on AIX (STR #4307) + - The default IPP version did not always get set before creating a new + IPP request message () + - The lp, lpq, lpr, and lpstat now display an error message advising the + use of the /version=1.1 ServerName option () + - Added documentation about the /version=1.1 option to ServerName in + client.conf () + - httpStatus(HTTP_ERROR) did not return a useful error message + () + - The lp, lpq, lpr, and lpstat commands incorrectly ignored the default + printer set in the lpoptions file () + - Fixed a URI encoding issue for hostnames containing the ` (backquote) + character () + - Added support for RFC 6874's IPv6 link local address format in URIs + () + - The USB backend could crash on libusb-based systems if USB was + disabled in the BIOS () + - Fixed a rounding error in the PWG media size mapping code + () + - Fixed several ipptool test files that used old STATUS names. + - Kerberos credentials could get truncated when printing to a shared + printer. + - Printing using "ipps" URIs was not encrypted. + - Insecure ICC profiles prevented installation of user profiles for a + printer on OS X. + - Added more USB quirks for the libusb-based backend (STR #4311, + ) + - The Russian web interface templates were broken (STR #4310) + - The scheduler no longer tries to do Kerberos authentication over the + loopback interface. + - The IPP backend could fail to pause a job for authentication + (STR #4298) + - Fixed a regression on the handling of auth keys on OS X if the + cups-files.conf was not present or did not contain a SystemAuthKey + value. + - The scheduler incorrectly did a reverse lookup of the server address + when HostNameLookups was turned off (STR #4302) + - The scheduler incorrectly computed the final content type value when + null filters were present. + + +CHANGES IN CUPS V1.6.2 + + - Documentation fixes (STR #4229, STR #4239, STR #4234, STR #4248, + STR #4259) + - Security: All file, directory, user, and group settings are now stored + in a separate cups-files.conf configuration file that cannot be set + through the CUPS web interface or APIs (STR #4223) + - Added a Czech localization (STR #4201) + - Added a French localization (STR #4247) + - Added a Russian localization (STR #4228, STR #4285) + - Updated the Catalan localization (STR #4202) + - Local certificate authentication did not guard against an empty + certification file (STR #4293) + - The scheduler did not reject device URIs with spaces. + - Added USB quirk rule for Epson Stylus Photo 750 (STR #4286) + - The IPP backend could crash if the printer disconnects early + (STR #4284) + - cupsGetPPD did not work with statically-configured CUPS shared + queues (STR #4178) + - The scheduler did not support long MIME media types (STR #4270) + - The cupsfilter command did not set the CHARSET environment variable + for the text filters (STR #4273) + - The lp command did not show errors for unknown "--foo" (STR #4261) + - Bad IPP responses could crash ipptool (STR #4262) + - Updated USB quirk rules for Canon and Xerox printers (STR #4217, + STR #4263) + - Added USB blacklisting for printers that require a custom backend + (STR #4218) + - The PPD compiler did not correctly JCL options (STR #4115, STR #4203) + - The ipptool program now supports DEFINE-MATCH and DEFINE-NO-MATCH + predicates for STATUS directives. + - Fixed a problem with local Kerberos authentication (STR #4140) + - Coverity scan: fixed some minor issues (STR #4242) + - The scheduler did not remove color profiles after deleting a printer + (STR #4232, STR #4276) + - The CUPS library did not always detect a timed out connection to the + server which could cause temporary loss of printing from applications + (STR #4187) + - The ipptool program now supports variable substitution in OPERATION + and DELAY directives (STR #4175) + - The IPP backend now stops queues when the server configuration + prevents successful job submission (STR #4125) + - The XML output of ipptool contained empty dictionaries (STR #4136) + - The scheduler did not delete job control backup files (STR #4244) + - cupsGetPPD3 could return a local PPD instead of the correct remote + PPD. + - The scheduler incorrectly advertised auth-info-required for local + queues needing local authentication (STR #4205) + - CUPS 1.6 clients using the ServerName directive in client.conf did not + work with CUPS 1.3.x or older servers (STR #4231, STR #4291) + - The SNMP backend now tries to work around broken printers that use a + newline to separate key/value pairs. + - The IPP backend did not send a cancel request to printers when a job + was canceled and the printer did not support Create-Job. + - Fixed EPM packaging files (STR #4199) + - OpenBSD build fix (STR #4195, STR #4196, STR #4197) + - The scheduler could crash when using Avahi (STR #4183, STR #4192, + STR #4200, STR #4213) + - The IPP backend could get stuck in an endless loop on certain network + errors (STR #4194) + - 32-bit builds failed on Debian (STR #4133) + - The scheduler no longer accepts or sends job description attributes. + - The IPP backend now works around some conformance issues for broken + printers (STR #4190) + - cupsBackendReport() now filters out all control characters from the + reported 1284 device IDs (STR #4124) + - The scheduler no longer allows job-name values that are not valid + network Unicode strings (STR #4072) + - The web interface did not preserve the order of classes, jobs, or + printers (STR #4170) + - The network backends now support disabling of SNMP supply level + queries via the "snmp" URI option (STR #4106) + - The IPP backend did not specify the compression used (STR #4181) + - ipptool did not support octetString values. + - The scheduler did not recognize dnssd: or ipps: URIs as Bonjour shared + queues (STR #4158) + - Applications could not get the PPD file for statically-configured + Bonjour-shared print queues (STR #4159) + - The cupsd.conf file included obsolete browsing directives (STR #4157) + - Fixed a USB backend compatibility issue on systems using libusb + (STR #4155, STR #4191) + - Some Bonjour features were not available on systems with Avahi + (STR #4156) + - CUPS now includes the port number in the Host: header for HTTP + requests. + - Fixed REPEAT-MATCH for STATUS and EXPECT - was incorrectly erroring + out. + + +CHANGES IN CUPS V1.6.1 + + - Documentation fix (STR #4149) + - RPM packaging fixes (STR #4129, #4145) + - The Japanese and English web interface headers were swapped + (STR #4148) + + +CHANGES IN CUPS V1.6.0 + + - Document changes (STR #4131) + - Added new Catalan (STR #4107) and Spanish (STR #4137) localizations. + + +CHANGES IN CUPS V1.6rc1 + + - Added a new Japanese localization (STR #4122) + - The SNMP backend no longer exits if it is unable to obtain an IPv6 + socket (STR #4109) + - The LPD backend incorrectly used "localhost" in the control file + instead of the current hostname. + + +CHANGES IN CUPS V1.6b1 + + - Documentation updates (STR #3927, STR #3980, STR #4010, STR #4068) + - The scheduler now consolidates all PPD updates from filters at the + end of the job (STR #4075) + - CUPS now supports color management using colord (STR #3808) + - CUPS now supports Bonjour using Avahi (STR #3066) + - The PreserveJobFiles and PreserveJobHistory directives now support + specification of a time interval (STR #3143) + - PPD files can now be archived in (gzip'd) tar files to further reduce + the disk space used by PPD files (STR #3772) + - The network backends now deal with printers that report their levels + in percent but do not specify a maximum capacity of 100 (STR #3551) + - The network backends now report full/almost-full waste bins in + printers along with end-of-life for cleaning pads (STR #4017) + - Added a configure option to set the permissions of the installed + cupsd (STR #3459) + - Added a new WITH-ALL-VALUES directive to ipptool EXPECT predicates + (STR #3949) + - CUPS now supports a User directive in client.conf and the CUPS_USER + environment variable for overriding the default username (STR #3114) + - Now set the PJL USERNAME variable as needed (STR #3100) + - Added support for usernames and passwords longer than 32 characters + (STR #2856) + - Added a new MaxHoldTime directive to automatically cancel jobs that + have been held indefinitely after a specific number of seconds + (STR #2291) + - The LPD backend now uses the originating host name when it is not the + local system (STR #2053) + - CUPS now prefers the suffix "dpcm" when reporting resolution in dots- + per-centimeter (STR #4006) + - The configure script and build system no longer support building of + separate 32-bit and 64-bit libraries. + - The "brightness", "columns", "fitplot", "gamma", "hue", + "natural-scaling", "penwidth", "position", "ppi", "saturation", and + "scaling" options are not longer supported (STR #4010) + - The "page-bottom", "page-left", "page-right", "page-top", + "prettyprint", and "wrap" options have been deprecated (STR #4010) + - The scheduler now reports the standard "number-of-documents" attribute + instead of the CUPS-specific "document-count" attribute in + job objects. + - Added new destination connection and enumeration functions (STR #3924) + - Added new option, localization, and job submission functions that do + not depend on PPD files (STR #3925) + - Added a new MaxJobTime directive for cupsd that specifies the maximum + amount of time allowed for a job to complete before it is canceled. + - The default password callback now supports passwords up to 127 + characters. + - The scheduler now supports a DefaultAuthType of "auto" to + automatically choose between Basic (username/password) and Negotiate + (Kerberos) authentication. + - cupsSideChannelSNMPGet/Walk now support OIDs and values up to 64k in + length. + - CUPS no longer supports automatic remote printers or implicit classes + via the CUPS, LDAP, or SLP protocols (STR #3922, STR #3923) + - The PPD APIs are now deprecated and will be removed in a future + version of CUPS (STR #3927) + - The default IPP version for requests is now 2.0 (STR #3929) + - The IPP APIs no longer expose the ipp_t or ipp_attribute_t structures + and instead provide accessor functions (STR #3928) + - The scheduler will no longer run programs with group write permission. + - The PHP module has been removed (STR #3932) + - The bannertops, commandtoescpx, commandtopclx, imagetops, + imagetoraster, pdftops, rastertoescpx, rastertopclx, and texttops + filters have been removed (STR #3930) + - The serial and parallel backends have been removed (STR 3935) diff --git a/CREDITS.txt b/CREDITS.txt new file mode 100644 index 0000000..b5721c4 --- /dev/null +++ b/CREDITS.txt @@ -0,0 +1,51 @@ +CREDITS.txt - 2012-07-16 +------------------------ + +Few projects are completed by one person, and CUPS is no exception. We'd +like to thank the following individuals for their contributions: + + Nathaniel Barbour - Lots of testing and feedback. + N. Becker - setsid(). + Philippe Combes - French localization and buttons script. + Jean-Eric Cuendet - GhostScript filters for CUPS. + Van Dang - HTTP and IPP policeman. + L. Peter Deutsch - MD5 code. + Dr. ZP Han - setgid()/setuid(). + Guy Harris - *BSD shared libraries and lots of other + fixes. + Bjoern Jacke - I18N stuff. + Wang Jian - CUPS RPM corrections. + Roderick Johnstone - Beta tester of the millenium. + Till Kamppeter - Bug fixes, beta testing, evangelism. + Iñaki Larrañaga - Basque localization. + Kenshi Muto - Japanese localization, patches, and + testing. + Tomohiro Kato - Japanese localization. + Kiko - Bug fixes. + Sergey V. Kovalyov - ESP Print Pro and CUPS beta tester. + Marek Laane - Estonian translation. + Mark Lawrence - Microsoft interoperability testing. + Jeff Licquia - Bug fixes, beta testing, evangelism. + Jason McMullan - Original CUPS RPM distributions. + Àngel Mompó - Catalan localization. + Wes Morgan - *BSD fixes. + Daniel Nylander - Swedish localization. + Niklas 'Nille' Åkerström - Swedish localization. + Naruiko Ogasawara - Japanese localization. + Giulio Orsero - Bug fixes and testing. + Michal Osowiecki - Polish localization. + Citra Paska - Indonesian localization. + Kurt Pfeifle - Bug fixes, beta testing, evangelism. + Vincenzo Reale - Italian localization. + Petter Reinholdtsen - HP-UX compiler stuff. + Juan Pablo González Riopedre - Spanish localization. + Opher Shachar - Hebrew localization. + Stuart Stevens - HP JetDirect IPP information. + Andrea Suatoni - IRIX desktop integration and testing. + Teppo Turliainen - Finnish localization. + Tim Waugh - Lots of patches, testing, and Linux + integration. + Yugami - LDAP browsing support. + +If I've missed someone, please let me know by sending an email to +"msweet@apple.com". diff --git a/INSTALL.txt b/INSTALL.txt new file mode 100644 index 0000000..948433c --- /dev/null +++ b/INSTALL.txt @@ -0,0 +1,213 @@ +INSTALL - CUPS v1.6.3 - 2013-07-11 +---------------------------------- + +This file describes how to compile and install CUPS from source code. For more +information on CUPS see the file called "README.txt". A complete change log can +be found in "CHANGES.txt". + +******************************************************************************* +******************************************************************************* +**** **** +**** USING CUPS REQUIRES ADDITIONAL THIRD-PARTY SUPPORT SOFTWARE AND **** +**** PRINTER DRIVERS. THESE ARE TYPICALLY INCLUDED WITH YOUR OPERATING **** +**** SYSTEM DISTRIBUTION. APPLE DOES NOT ENDORSE OR SUPPORT THIRD-PARTY **** +**** SUPPORT SOFTWARE FOR CUPS. **** +**** **** +******************************************************************************* +******************************************************************************* + + +BEFORE YOU BEGIN + + You'll need ANSI-compliant C and C++ compilers, plus a make program and + POSIX-compliant shell (/bin/sh). The GNU compiler tools and Bash work well + and we have tested the current CUPS code against several versions of GCC + with excellent results. + + The makefiles used by the project should work with most versions of make. + We've tested them with GNU make as well as the make programs shipped by + Compaq, HP, SGI, and Sun. BSD users should use GNU make (gmake) since BSD + make does not support "include". + + Besides these tools you'll want the JPEG, PNG, TIFF, and ZLIB libraries for + image support, the CDSA, GNU TLS, or OpenSSL libraries for encryption + support, the OpenLDAP and OpenSLP libraries for directory services support, + and either MIT (1.6.3 or higher) or Heimdal Kerberos for Kerberos support. + CUPS will compile and run without these, however you'll miss out on many of + the features provided by CUPS. + + Also, please note that CUPS does not include the Ghostscript-based + PostScript filter needed by non-PostScript printers. You *must* download + GPL Ghostscript separately from the CUPS web site if you want to print + PostScript files to non-PostScript printers on operating systems other than + OS X. + + +COMPILING THE SUBVERSION REPOSITORY CODE + + The CUPS Subversion repository doesn't hold a copy of the pre-built + configure script. You'll need to run the GNU autoconf software (2.60 or + higher) to create it: + + autoconf + + +CONFIGURATION + + CUPS uses GNU autoconf, so you should find the usual "configure" script in + the main CUPS source directory. To configure CUPS for your system, type: + + ./configure + + The default installation will put the CUPS software in the "/etc", "/usr", + and "/var" directories on your system, which will overwrite any existing + printing commands on your system. Use the "--prefix" option to install the + CUPS software in another location: + + ./configure --prefix=/some/directory + + To see a complete list of configuration options, use the --help option: + + ./configure --help + + If any of the dependent libraries are not installed in a system default + location (typically "/usr/include" and "/usr/lib") you'll need to set the + CFLAGS, CPPFLAGS, CXXFLAGS, DSOFLAGS, and LDFLAGS environment variables + prior to running configure: + + setenv CFLAGS "-I/some/directory" + setenv CPPFLAGS "-I/some/directory" + setenv CXXFLAGS "-I/some/directory" + setenv DSOFLAGS "-L/some/directory" + setenv LDFLAGS "-L/some/directory" + ./configure ... + + or: + + CFLAGS="-I/some/directory" \ + CPPFLAGS="-I/some/directory" \ + CXXFLAGS="-I/some/directory" \ + DSOFLAGS="-L/some/directory" \ + LDFLAGS="-L/some/directory" \ + ./configure ... + + The "--enable-debug" option compiles CUPS with debugging information + enabled. Additional debug logging support can be enabled using the + "--enable-debug-printfs" option - these debug messages are enabled using the + CUPS_DEBUG_LOG environment variable at run-time. + + CUPS also includes an extensive set of unit tests that can be used to find + and diagnose a variety of common problems - use the "--enable-unit-tests" + configure option to run them at build time. + + Once you have configured things, just type: + + make ENTER + + or if you have FreeBSD, NetBSD, or OpenBSD type: + + gmake ENTER + + to build the software. + + +TESTING THE SOFTWARE + + Aside from the built-in unit tests, CUPS includes an automated test + framework for testing the entire printing system. To run the tests, just + type: + + make check ENTER + + or if you have FreeBSD, NetBSD, or OpenBSD type: + + gmake check ENTER + + The test framework runs a copy of the CUPS scheduler (cupsd) on port 8631 + in /tmp/cups-$USER and produces a nice HTML report of the results. + + +INSTALLING THE SOFTWARE + + Once you have built the software you need to install it. The "install" + target provides a quick way to install the software on your local system: + + make install ENTER + + or for FreeBSD, NetBSD, or OpenBSD: + + gmake install ENTER + + You can also build binary packages that can be installed on other machines + using the RPM spec file ("packaging/cups.spec") or EPM list file + ("packaging/cups.list"). The latter also supports building of binary RPMs, + so it may be more convenient to use. + + You can find the RPM software at: + + http://www.rpm.org/ + + The EPM software is available at: + + http://www.epmhome.org/ + + +CREATING BINARY DISTRIBUTIONS WITH EPM + + The top level makefile supports generation of many types of binary + distributions using EPM. To build a binary distribution type: + + make ENTER + + or + + gmake ENTER + + for FreeBSD, NetBSD, and OpenBSD. The target is one of the + following: + + epm - Builds a script + tarfile package + aix - Builds an AIX package + bsd - Builds a *BSD package + deb - Builds a Debian package + depot - Builds a HP-UX package (also swinstall) + inst - Builds an IRIX package (also tardist) + pkg - Builds a Solaris package + rpm - Builds a RPM package + setld - Build a Tru64 UNIX package + slackware - Build a Slackware package + swinstall - Build a HP-UX package (also depot) + tardist - Builds an IRIX package (also inst) + + +GETTING DEBUG LOGGING FROM CUPS + + When configured with the "--enable-debug-printfs" option, CUPS compiles in + additional debug logging support in the scheduler, CUPS API, and CUPS + Imaging API. The following environment variables are used to enable and + control debug logging: + + CUPS_DEBUG_FILTER Specifies a POSIX regular expression to control + which messages are logged. + CUPS_DEBUG_LEVEL Specifies a number from 0 to 9 to control the + verbosity of the logging. The default level is 1. + CUPS_DEBUG_LOG Specifies a log file to use. Specify the name "-" + to send the messages to stderr. Prefix a filename + with "+" to append to an existing file. + + +REPORTING PROBLEMS + + If you have problems, READ THE DOCUMENTATION FIRST! If the documentation + does not solve your problems, please post a message on the "cups.general" + forum at: + + http://www.cups.org/newsgroups.php + + Include your operating system and version, compiler and version, and any + errors or problems you've run into. The "config.log" file and the output + from the configure script and make should also be sent, as it often helps to + determine the cause of your problem. + + If you are running a version of Linux, be sure to provide the Linux + distribution you have, too. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..75c4a37 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,971 @@ + CUPS License Agreement + + Copyright 2007-2012 by Apple Inc. + 1 Infinite Loop + Cupertino, CA 95014 USA + + WWW: http://www.cups.org/ + + +INTRODUCTION + +CUPS(tm) is provided under the GNU General Public License ("GPL") +and GNU Library General Public License ("LGPL"), Version 2, with +exceptions for Apple operating systems and the OpenSSL toolkit. A +copy of the exceptions and licenses follow this introduction. + +The GNU LGPL applies to the CUPS and CUPS Imaging libraries +located in the "cups" and "filter" subdirectories of the CUPS +source distribution and the files in the "test" subdirectory. The +GNU GPL applies to the remainder of the CUPS distribution. + +For those not familiar with the GNU GPL, the license basically +allows you to: + + - Use the CUPS software at no charge. + - Distribute verbatim copies of the software in source or + binary form. + - Sell verbatim copies of the software for a media fee, or + sell support for the software. + +What this license *does not* allow you to do is make changes or +add features to CUPS and then sell a binary distribution without +source code. You must provide source for any changes or additions +to the software, and all code must be provided under the GPL or +LGPL as appropriate. The only exceptions to this are the portions +of the CUPS software covered by the Apple operating system +license exceptions outlined later in this license agreement. + +The GNU LGPL relaxes the "link-to" restriction, allowing you to +develop applications that use the CUPS and CUPS Imaging libraries +under other licenses and/or conditions as appropriate for your +application, driver, or filter. + + +LICENSE EXCEPTIONS + +In addition, as the copyright holder of CUPS, Apple Inc. grants +the following special exceptions: + + 1. Apple Operating System Development License Exception; + + a. Software that is developed by any person or entity + for an Apple Operating System ("Apple OS-Developed + Software"), including but not limited to Apple and + third party printer drivers, filters, and backends + for an Apple Operating System, that is linked to the + CUPS imaging library or based on any sample filters + or backends provided with CUPS shall not be + considered to be a derivative work or collective work + based on the CUPS program and is exempt from the + mandatory source code release clauses of the GNU GPL. + You may therefore distribute linked combinations of + the CUPS imaging library with Apple OS-Developed + Software without releasing the source code of the + Apple OS-Developed Software. You may also use sample + filters and backends provided with CUPS to develop + Apple OS-Developed Software without releasing the + source code of the Apple OS-Developed Software. + + b. An Apple Operating System means any operating system + software developed and/or marketed by Apple Inc., + including but not limited to all existing releases and + versions of Apple's Darwin, OS X, and OS X Server + products and all follow-on releases and future + versions thereof. + + c. This exception is only available for Apple + OS-Developed Software and does not apply to software + that is distributed for use on other operating + systems. + + d. All CUPS software that falls under this license + exception have the following text at the top of each + source file: + + This file is subject to the Apple OS-Developed + Software exception. + + 2. OpenSSL Toolkit License Exception; + + a. Apple Inc. explicitly allows the compilation and + distribution of the CUPS software with the OpenSSL + Toolkit. + +No developer is required to provide these exceptions in a +derived work. + + +KERBEROS SUPPORT CODE + +The Kerberos support code ("KSC") is copyright 2006 by Jelmer +Vernooij and is provided 'as-is', without any express or implied +warranty. In no event will the author or Apple Inc. be held +liable for any damages arising from the use of the KSC. + +Sources files containing KSC have the following text at the top +of each source file: + + This file contains Kerberos support code, copyright 2006 by + Jelmer Vernooij. + +The KSC copyright and license apply only to Kerberos-related +feature code in CUPS. Such code is typically conditionally +compiled based on the present of the HAVE_GSSAPI preprocessor +definition. + +Permission is granted to anyone to use the KSC for any purpose, +including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + + 1. The origin of the KSC must not be misrepresented; you + must not claim that you wrote the original software. If + you use the KSC in a product, an acknowledgment in the + product documentation would be appreciated but is not + required. + + 2. Altered source versions must be plainly marked as such, + and must not be misrepresented as being the original + software. + + 3. This notice may not be removed or altered from any source + distribution. + + +TRADEMARKS + +CUPS and the CUPS logo (the "CUPS Marks") are trademarks of Apple +Inc. Apple grants you a non-exclusive and non-transferable right +to use the CUPS Marks in any direct port or binary distribution +incorporating CUPS software and in any promotional material +therefor. You agree that your products will meet the highest +levels of quality and integrity for similar goods, not be unlawful, +and be developed, manufactured, and distributed in compliance with +this license. You will not interfere with Apple's rights in the +CUPS Marks, and all use of the CUPS Marks shall inure to the +benefit of Apple. This license does not apply to use of the CUPS +Marks in a derivative products, which requires prior written +permission from Apple Inc. + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + + GNU LIBRARY GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + [This is the first released version of the library GPL. It is + numbered 2 because it goes with version 2 of the ordinary GPL.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Library General Public License, applies to some +specially designated Free Software Foundation software, and to any +other libraries whose authors decide to use it. You can use it for +your libraries, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if +you distribute copies of the library, or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link a program with the library, you must provide +complete object files to the recipients so that they can relink them +with the library, after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + Our method of protecting your rights has two steps: (1) copyright +the library, and (2) offer you this license which gives you legal +permission to copy, distribute and/or modify the library. + + Also, for each distributor's protection, we want to make certain +that everyone understands that there is no warranty for this free +library. If the library is modified by someone else and passed on, we +want its recipients to know that what they have is not the original +version, so that any problems introduced by others will not reflect on +the original authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that companies distributing free +software will individually obtain patent licenses, thus in effect +transforming the program into proprietary software. To prevent this, +we have made it clear that any patent must be licensed for everyone's +free use or not licensed at all. + + Most GNU software, including some libraries, is covered by the ordinary +GNU General Public License, which was designed for utility programs. This +license, the GNU Library General Public License, applies to certain +designated libraries. This license is quite different from the ordinary +one; be sure to read it in full, and don't assume that anything in it is +the same as in the ordinary license. + + The reason we have a separate public license for some libraries is that +they blur the distinction we usually make between modifying or adding to a +program and simply using it. Linking a program with a library, without +changing the library, is in some sense simply using the library, and is +analogous to running a utility program or application program. However, in +a textual and legal sense, the linked executable is a combined work, a +derivative of the original library, and the ordinary General Public License +treats it as such. + + Because of this blurred distinction, using the ordinary General +Public License for libraries did not effectively promote software +sharing, because most developers did not use the libraries. We +concluded that weaker conditions might promote sharing better. + + However, unrestricted linking of non-free programs would deprive the +users of those programs of all benefit from the free status of the +libraries themselves. This Library General Public License is intended to +permit developers of non-free programs to use free libraries, while +preserving your freedom as a user of such programs to change the free +libraries that are incorporated in them. (We have not seen how to achieve +this as regards changes in header files, but we have achieved it as regards +changes in the actual functions of the Library.) The hope is that this +will lead to faster development of free libraries. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, while the latter only +works together with the library. + + Note that it is possible for a library to be covered by the ordinary +General Public License rather than by this special one. + + GNU LIBRARY GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library which +contains a notice placed by the copyright holder or other authorized +party saying it may be distributed under the terms of this Library +General Public License (also called "this License"). Each licensee is +addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also compile or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + c) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + d) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the source code distributed need not include anything that is normally +distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Library General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/Makedefs.in b/Makedefs.in new file mode 100644 index 0000000..41612af --- /dev/null +++ b/Makedefs.in @@ -0,0 +1,262 @@ +# +# "$Id: Makedefs.in 7900 2008-09-03 13:47:57Z mike $" +# +# Common makefile definitions for CUPS. +# +# Copyright 2007-2012 by Apple Inc. +# Copyright 1997-2007 by Easy Software Products, all rights reserved. +# +# 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/". +# + +# +# Programs... +# + +AR = @AR@ +AWK = @AWK@ +CC = @LIBTOOL@ @CC@ +CHMOD = @CHMOD@ +CXX = @LIBTOOL@ @CXX@ +DSO = @DSO@ +DSOXX = @DSOXX@ +GZIP = @GZIP@ +INSTALL = @INSTALL@ +LD = @LD@ +LIBTOOL = @LIBTOOL@ +LN = @LN@ -sf +MV = @MV@ +RANLIB = @RANLIB@ +RM = @RM@ -f +RMDIR = @RMDIR@ +SED = @SED@ +SHELL = /bin/sh + +# +# Installation programs... +# + +INSTALL_BIN = $(LIBTOOL) $(INSTALL) -c -m 555 @INSTALL_STRIP@ +INSTALL_COMPDATA = $(INSTALL) -c -m 444 @INSTALL_GZIP@ +INSTALL_CONFIG = $(INSTALL) -c -m @CUPS_CONFIG_FILE_PERM@ +INSTALL_DATA = $(INSTALL) -c -m 444 +INSTALL_DIR = $(INSTALL) -d +INSTALL_LIB = $(LIBTOOL) $(INSTALL) -c -m 555 @INSTALL_STRIP@ +INSTALL_MAN = $(INSTALL) -c -m 444 +INSTALL_SCRIPT = $(INSTALL) -c -m 555 + +# +# Default user, group, and system groups for the scheduler... +# + +CUPS_USER = @CUPS_USER@ +CUPS_GROUP = @CUPS_GROUP@ +CUPS_SYSTEM_GROUPS = @CUPS_SYSTEM_GROUPS@ +CUPS_PRIMARY_SYSTEM_GROUP = @CUPS_PRIMARY_SYSTEM_GROUP@ + +# +# Default permissions... +# + +CUPS_CONFIG_FILE_PERM = @CUPS_CONFIG_FILE_PERM@ +CUPS_CUPSD_FILE_PERM = @CUPS_CUPSD_FILE_PERM@ +CUPS_LOG_FILE_PERM = @CUPS_LOG_FILE_PERM@ + +# +# Languages to install... +# + +LANGUAGES = @LANGUAGES@ +INSTALL_LANGUAGES = @INSTALL_LANGUAGES@ +UNINSTALL_LANGUAGES = @UNINSTALL_LANGUAGES@ + +# +# Libraries... +# + +LIBCUPS = @LIBCUPS@ +LIBCUPSCGI = @LIBCUPSCGI@ +LIBCUPSIMAGE = @LIBCUPSIMAGE@ +LIBCUPSMIME = @LIBCUPSMIME@ +LIBCUPSPPDC = @LIBCUPSPPDC@ +LIBCUPSSTATIC = @LIBCUPSSTATIC@ +LIBGSSAPI = @LIBGSSAPI@ +LIBMALLOC = @LIBMALLOC@ +LIBMXML = @LIBMXML@ +LIBPAPER = @LIBPAPER@ +LIBUSB = @LIBUSB@ +LIBWRAP = @LIBWRAP@ +LIBZ = @LIBZ@ + +# +# Install static libraries? +# + +INSTALLSTATIC = @INSTALLSTATIC@ + +# +# IPP backend aliases... +# + +IPPALIASES = @IPPALIASES@ + +# +# Install XPC backends? +# + +INSTALLXPC = @INSTALLXPC@ + +# +# Program options... +# +# ARCHFLAGS Defines the default architecture build options. +# OPTIM Defines the common compiler optimization/debugging options +# for all architectures. +# OPTIONS Defines other compile-time options (currently only -DDEBUG +# for extra debug info) +# + +ALL_CFLAGS = -I.. -D_CUPS_SOURCE $(CFLAGS) $(SSLFLAGS) \ + @LARGEFILE@ @PTHREAD_FLAGS@ $(OPTIONS) +ALL_CXXFLAGS = -I.. -D_CUPS_SOURCE $(CXXFLAGS) $(SSLFLAGS) \ + @LARGEFILE@ @PTHREAD_FLAGS@ $(OPTIONS) +ARCHFLAGS = @ARCHFLAGS@ +ARFLAGS = @ARFLAGS@ +BACKLIBS = @BACKLIBS@ +BUILDDIRS = @BUILDDIRS@ +CFLAGS = @CPPFLAGS@ @CFLAGS@ +COMMONLIBS = @LIBS@ +CXXFLAGS = @CPPFLAGS@ @CXXFLAGS@ +CXXLIBS = @CXXLIBS@ +DBUS_NOTIFIER = @DBUS_NOTIFIER@ +DBUS_NOTIFIERLIBS = @DBUS_NOTIFIERLIBS@ +DNSSD_BACKEND = @DNSSD_BACKEND@ +DSOFLAGS = -L../cups @DSOFLAGS@ +DSOLIBS = @DSOLIBS@ $(COMMONLIBS) +DNSSDLIBS = @DNSSDLIBS@ +LAUNCHDLIBS = @LAUNCHDLIBS@ +LDFLAGS = -L../cgi-bin -L../cups -L../filter -L../ppdc \ + -L../scheduler @LDARCHFLAGS@ \ + @LDFLAGS@ @RELROFLAGS@ @PIEFLAGS@ $(OPTIM) +LINKCUPS = @LINKCUPS@ $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(LIBZ) +LINKCUPSIMAGE = @LINKCUPSIMAGE@ +LIBS = $(LINKCUPS) $(COMMONLIBS) +OPTIM = @OPTIM@ +OPTIONS = +PAMLIBS = @PAMLIBS@ +SERVERLIBS = @SERVERLIBS@ +SSLFLAGS = @SSLFLAGS@ +SSLLIBS = @SSLLIBS@ +UNITTESTS = @UNITTESTS@ + + +# +# Directories... +# +# The first section uses the GNU names (which are *extremely* +# difficult to find in a makefile because they are lowercase...) +# We have to define these first because autoconf uses ${prefix} +# and ${exec_prefix} for most of the other directories... +# +# The "datarootdir" variable may not get defined if you are using +# a version of autoconf prior to 2.60. +# +# This is immediately followed by definition in ALL CAPS for the +# needed directories... +# + +bindir = @bindir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +exec_prefix = @exec_prefix@ +includedir = @includedir@ +infodir = @infodir@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +oldincludedir = @oldincludedir@ +prefix = @prefix@ +privateinclude = @privateinclude@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +top_srcdir = @top_srcdir@ + +BUILDROOT = $(DSTROOT) + +AMANDIR = $(BUILDROOT)@AMANDIR@ +BINDIR = $(BUILDROOT)@bindir@ +BUNDLEDIR = @CUPS_BUNDLEDIR@ +CACHEDIR = $(BUILDROOT)@CUPS_CACHEDIR@ +DATADIR = $(BUILDROOT)@CUPS_DATADIR@ +DOCDIR = $(BUILDROOT)@CUPS_DOCROOT@ +ICONDIR = @ICONDIR@ +INCLUDEDIR = $(BUILDROOT)$(includedir) +INITDIR = @INITDIR@ +INITDDIR = @INITDDIR@ +LIBDIR = $(BUILDROOT)$(libdir) +LOCALEDIR = $(BUILDROOT)@CUPS_LOCALEDIR@ +LOGDIR = $(BUILDROOT)@CUPS_LOGDIR@ +MANDIR = $(BUILDROOT)@mandir@ +MENUDIR = @MENUDIR@ +PMANDIR = $(BUILDROOT)@PMANDIR@ +PRIVATEINCLUDE = $(BUILDROOT)@PRIVATEINCLUDE@ +RCLEVELS = @RCLEVELS@ +RCSTART = @RCSTART@ +RCSTOP = @RCSTOP@ +REQUESTS = $(BUILDROOT)@CUPS_REQUESTS@ +SBINDIR = $(BUILDROOT)@sbindir@ +SERVERBIN = $(BUILDROOT)@CUPS_SERVERBIN@ +SERVERROOT = $(BUILDROOT)@CUPS_SERVERROOT@ +SMFMANIFESTDIR = @SMFMANIFESTDIR@ +STATEDIR = $(BUILDROOT)@CUPS_STATEDIR@ +XINETD = @XINETD@ + +MAN1EXT = @MAN1EXT@ +MAN5EXT = @MAN5EXT@ +MAN7EXT = @MAN7EXT@ +MAN8EXT = @MAN8EXT@ +MAN8DIR = @MAN8DIR@ + +PAMDIR = @PAMDIR@ +PAMFILE = @PAMFILE@ + +DEFAULT_LAUNCHD_CONF = @DEFAULT_LAUNCHD_CONF@ +DBUSDIR = @DBUSDIR@ + + +# +# Rules... +# + +.SILENT: +.SUFFIXES: .1 .1.gz .1m .1m.gz .3 .3.gz .5 .5.gz .7 .7.gz .8 .8.gz .a .c .cxx .h .man .o .gz + +.c.o: + echo Compiling $<... + $(CC) $(ARCHFLAGS) $(OPTIM) $(ALL_CFLAGS) -c -o $@ $< + +.cxx.o: + echo Compiling $<... + $(CXX) $(ARCHFLAGS) $(OPTIM) $(ALL_CXXFLAGS) -c -o $@ $< + +.man.1 .man.1m .man.3 .man.5 .man.7 .man.8: + echo Linking $<... + $(RM) $@ + $(LN) $< $@ + +.man.1.gz .man.1m.gz .man.3.gz .man.5.gz .man.7.gz .man.8.gz .man.gz: + echo -n Compressing $<... + $(RM) $@ + gzip -v9 <$< >$@ + + +# +# End of "$Id: Makedefs.in 7900 2008-09-03 13:47:57Z mike $" +# diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d1ef21a --- /dev/null +++ b/Makefile @@ -0,0 +1,336 @@ +# +# "$Id: Makefile 9391 2010-11-30 21:53:04Z mike $" +# +# Top-level Makefile for CUPS. +# +# Copyright 2007-2013 by Apple Inc. +# Copyright 1997-2007 by Easy Software Products, all rights reserved. +# +# 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/". +# + +include Makedefs + + +# +# Directories to make... +# + +DIRS = cups test $(BUILDDIRS) + + +# +# Make all targets... +# + +all: + chmod +x cups-config + echo Using ARCHFLAGS="$(ARCHFLAGS)" + echo Using ALL_CFLAGS="$(ALL_CFLAGS)" + echo Using ALL_CXXFLAGS="$(ALL_CXXFLAGS)" + echo Using CC="$(CC)" + echo Using CXX="$(CC)" + echo Using DSOFLAGS="$(DSOFLAGS)" + echo Using LDFLAGS="$(LDFLAGS)" + echo Using LIBS="$(LIBS)" + for dir in $(DIRS); do\ + echo Making all in $$dir... ;\ + (cd $$dir ; $(MAKE) $(MFLAGS) all $(UNITTESTS)) || exit 1;\ + done + + +# +# Make library targets... +# + +libs: + echo Using ARCHFLAGS="$(ARCHFLAGS)" + echo Using ALL_CFLAGS="$(ALL_CFLAGS)" + echo Using ALL_CXXFLAGS="$(ALL_CXXFLAGS)" + echo Using CC="$(CC)" + echo Using CXX="$(CC)" + echo Using DSOFLAGS="$(DSOFLAGS)" + echo Using LDFLAGS="$(LDFLAGS)" + echo Using LIBS="$(LIBS)" + for dir in $(DIRS); do\ + echo Making libraries in $$dir... ;\ + (cd $$dir ; $(MAKE) $(MFLAGS) libs) || exit 1;\ + done + + +# +# Make unit test targets... +# + +unittests: + echo Using ARCHFLAGS="$(ARCHFLAGS)" + echo Using ALL_CFLAGS="$(ALL_CFLAGS)" + echo Using ALL_CXXFLAGS="$(ALL_CXXFLAGS)" + echo Using CC="$(CC)" + echo Using CXX="$(CC)" + echo Using DSOFLAGS="$(DSOFLAGS)" + echo Using LDFLAGS="$(LDFLAGS)" + echo Using LIBS="$(LIBS)" + for dir in $(DIRS); do\ + echo Making all in $$dir... ;\ + (cd $$dir ; $(MAKE) $(MFLAGS) unittests) || exit 1;\ + done + + +# +# Remove object and target files... +# + +clean: + for dir in $(DIRS); do\ + echo Cleaning in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) clean) || exit 1;\ + done + + +# +# Remove all non-distribution files... +# + +distclean: clean + $(RM) Makedefs config.h config.log config.status + $(RM) cups-config + $(RM) conf/cups-files.conf conf/cupsd.conf + $(RM) conf/mime.convs conf/pam.std conf/snmp.conf + $(RM) doc/help/ref-cups-files-conf.html doc/help/ref-cupsd-conf.html + $(RM) doc/help/standard.html doc/index.html + $(RM) man/client.conf.man + $(RM) man/cups-deviced.man man/cups-driverd.man + $(RM) man/cups-lpd.man man/cupsaddsmb.man man/cupsd.man + $(RM) man/cupsd.conf.man man/drv.man man/lpoptions.man + $(RM) packaging/cups.list + $(RM) packaging/cups-desc.plist packaging/cups-info.plist + $(RM) templates/header.tmpl + $(RM) desktop/cups.desktop + $(RM) scheduler/cups.sh scheduler/cups-lpd.xinetd + $(RM) scheduler/org.cups.cups-lpd.plist scheduler/cups.xml + -$(RM) doc/*/index.html + -$(RM) templates/*/header.tmpl + -$(RM) -r autom4te*.cache clang cups/charmaps cups/locale driver/test + + +# +# Make dependencies +# + +depend: + for dir in $(DIRS); do\ + echo Making dependencies in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) depend) || exit 1;\ + done + + +# +# Run the clang.llvm.org static code analysis tool on the C sources. +# (at least checker-231 is required for scan-build to work this way) +# + +.PHONY: clang clang-changes +clang: + $(RM) -r clang + scan-build -V -k -o `pwd`/clang $(MAKE) $(MFLAGS) clean all +clang-changes: + scan-build -V -k -o `pwd`/clang $(MAKE) $(MFLAGS) all + + +# +# Generate a ctags file... +# + +ctags: + ctags -R . + + +# +# Install everything... +# + +install: install-data install-headers install-libs install-exec + + +# +# Install data files... +# + +install-data: + echo Making all in cups... + (cd cups; $(MAKE) $(MFLAGS) all) + for dir in $(DIRS); do\ + echo Installing data files in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) install-data) || exit 1;\ + done + echo Installing cups-config script... + $(INSTALL_DIR) -m 755 $(BINDIR) + $(INSTALL_SCRIPT) cups-config $(BINDIR)/cups-config + + +# +# Install header files... +# + +install-headers: + for dir in $(DIRS); do\ + echo Installing header files in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) install-headers) || exit 1;\ + done + if test "x$(privateinclude)" != x; then \ + echo Installing config.h into $(PRIVATEINCLUDE)...; \ + $(INSTALL_DIR) -m 755 $(PRIVATEINCLUDE); \ + $(INSTALL_DATA) config.h $(PRIVATEINCLUDE)/config.h; \ + fi + + +# +# Install programs... +# + +install-exec: all + for dir in $(DIRS); do\ + echo Installing programs in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) install-exec) || exit 1;\ + done + + +# +# Install libraries... +# + +install-libs: libs + for dir in $(DIRS); do\ + echo Installing libraries in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) install-libs) || exit 1;\ + done + + +# +# Uninstall object and target files... +# + +uninstall: + for dir in $(DIRS); do\ + echo Uninstalling in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) uninstall) || exit 1;\ + done + echo Uninstalling cups-config script... + $(RM) $(BINDIR)/cups-config + -$(RMDIR) $(BINDIR) + + +# +# Run the test suite... +# + +test: all unittests + echo Running CUPS test suite... + cd test; ./run-stp-tests.sh + + +check: all unittests + echo Running CUPS test suite with defaults... + cd test; ./run-stp-tests.sh 1 0 n n + +debugcheck: all unittests + echo Running CUPS test suite with debug printfs... + cd test; ./run-stp-tests.sh 1 0 n y + + +# +# Create HTML documentation... +# + +apihelp: + for dir in cgi-bin cups filter ppdc scheduler; do\ + echo Generating API help in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) apihelp) || exit 1;\ + done + +framedhelp: + for dir in cgi-bin cups filter ppdc scheduler; do\ + echo Generating framed API help in $$dir... ;\ + (cd $$dir; $(MAKE) $(MFLAGS) framedhelp) || exit 1;\ + done + + +# +# Create an Xcode docset... +# + +docset: apihelp + echo Generating docset directory tree... + $(RM) -r org.cups.docset + mkdir -p org.cups.docset/Contents/Resources/Documentation/help + mkdir -p org.cups.docset/Contents/Resources/Documentation/images + cd man; $(MAKE) $(MFLAGS) html + cd doc; $(MAKE) $(MFLAGS) docset + cd cgi-bin; $(MAKE) $(MFLAGS) makedocset + cgi-bin/makedocset org.cups.docset \ + `svnversion . | sed -e '1,$$s/[a-zA-Z]//g'` \ + doc/help/api-*.tokens + $(RM) doc/help/api-*.tokens + echo Indexing docset... + /Applications/Xcode.app/Contents/Developer/usr/bin/docsetutil index org.cups.docset + echo Generating docset archive and feed... + $(RM) org.cups.docset.atom + /Applications/Xcode.app/Contents/Developer/usr/bin/docsetutil package --output org.cups.docset.xar \ + --atom org.cups.docset.atom \ + --download-url http://www.cups.org/org.cups.docset.xar \ + org.cups.docset + + +# +# Lines of code computation... +# + +sloc: + for dir in cups scheduler; do \ + (cd $$dir; $(MAKE) $(MFLAGS) sloc) || exit 1;\ + done + + +# +# Make software distributions using EPM (http://www.epmhome.org/)... +# + +EPMFLAGS = -v --output-dir dist $(EPMARCH) + +aix bsd deb depot inst pkg setld slackware swinstall tardist: + epm $(EPMFLAGS) -f $@ cups packaging/cups.list + +epm: + epm $(EPMFLAGS) -s packaging/installer.gif cups packaging/cups.list + +rpm: + epm $(EPMFLAGS) -f rpm -s packaging/installer.gif cups packaging/cups.list + +.PHONEY: dist +dist: all + $(RM) -r dist + $(MAKE) $(MFLAGS) epm + case `uname` in \ + *BSD*) $(MAKE) $(MFLAGS) bsd;; \ + Darwin*) $(MAKE) $(MFLAGS) osx;; \ + IRIX*) $(MAKE) $(MFLAGS) tardist;; \ + Linux*) test ! -x /usr/bin/rpm || $(MAKE) $(MFLAGS) rpm;; \ + SunOS*) $(MAKE) $(MFLAGS) pkg;; \ + esac + + +# +# Don't run top-level build targets in parallel... +# + +.NOTPARALLEL: + + +# +# End of "$Id: Makefile 9391 2010-11-30 21:53:04Z mike $". +# diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..353056b --- /dev/null +++ b/README.txt @@ -0,0 +1,163 @@ +README - CUPS v1.6.3 - 2013-07-11 +--------------------------------- + +Looking for compile instructions? Read the file "INSTALL.txt" instead... + + +INTRODUCTION + + CUPS is a standards-based, open source printing system developed by Apple + Inc. for OS® X and other UNIX®-like operating systems. CUPS uses the + Internet Printing Protocol ("IPP") and provides System V and Berkeley + command-line interfaces, a web interface, and a C API to manage printers and + print jobs. It supports printing to both local (parallel, serial, USB) and + networked printers, and printers can be shared from one computer to another, + even over the Internet! + + Internally, CUPS uses PostScript Printer Description ("PPD") files to + describe printer capabilities and features and a wide variety of generic + and device-specific programs to convert and print many types of files. + Sample drivers are included with CUPS to support many Dymo, EPSON, HP, + Intellitech, OKIDATA, and Zebra printers. Many more drivers are available + online and (in some cases) on the driver CD-ROM that came with your printer. + + CUPS is licensed under the GNU General Public License and GNU Library + General Public License versions 2. See the file "LICENSE.txt" for more + information. + + +READING THE DOCUMENTATION + + Once you have installed the software you can access the documentation (and + a bunch of other stuff) online at: + + http://localhost:631/ + + If you're having trouble getting that far, the documentation is located + under the "doc/help" directory. + + Please read the documentation before asking questions. + + +GETTING SUPPORT AND OTHER RESOURCES + + If you have problems, READ THE DOCUMENTATION FIRST! We also provide many + discussion forums which are available at: + + http://www.cups.org/newsgroups.php + + See the CUPS web site at "http://www.cups.org/" for other resources. + + +SETTING UP PRINTER QUEUES USING YOUR WEB BROWSER + + CUPS includes a web-based administration tool that allows you to manage + printers, classes, and jobs on your server. Open the following URL in your + browser to access the printer administration tools: + + http://localhost:631/admin/ + + DO NOT use the hostname for your machine - it will not work with the default + CUPS configuration. To enable administration access on other addresses, + check the "Allow Remote Administration" box and click on the "Change + Settings" button. + + You will be asked for the administration password (root or any other user in + the sys/system/root/admin/lpadmin group on your system) when performing any + administrative function. + + +SETTING UP PRINTER QUEUES FROM THE COMMAND-LINE + + CUPS works best with PPD (PostScript Printer Description) files. In a pinch + you can also use System V style printer interface scripts. + + CUPS includes several sample PPD files you can use: + + Driver PPD Name + ----------------------------- ------------------------------ + Dymo Label Printers drv:///sample.drv/dymo.ppd + Intellitech Intellibar drv:///sample.drv/intelbar.ppd + EPSON Stylus Color Series drv:///sample.drv/stcolor.ppd + EPSON Stylus Photo Series drv:///sample.drv/stphoto.ppd + EPSON Stylus New Color Series drv:///sample.drv/stcolor2.ppd + EPSON Stylus New Photo Series drv:///sample.drv/stphoto2.ppd + EPSON 9-pin Series drv:///sample.drv/epson9.ppd + EPSON 24-pin Series drv:///sample.drv/epson24.ppd + Generic PCL Laser Printer drv:///sample.drv/generpcl.ppd + Generic PostScript Printer drv:///sample.drv/generic.ppd + HP DeskJet Series drv:///sample.drv/deskjet.ppd + HP LaserJet Series drv:///sample.drv/laserjet.ppd + OKIDATA 9-Pin Series drv:///sample.drv/okidata9.ppd + OKIDATA 24-Pin Series drv:///sample.drv/okidat24.ppd + Zebra CPCL Label Printer drv:///sample.drv/zebracpl.ppd + Zebra EPL1 Label Printer drv:///sample.drv/zebraep1.ppd + Zebra EPL2 Label Printer drv:///sample.drv/zebraep2.ppd + Zebra ZPL Label Printer drv:///sample.drv/zebra.ppd + + Run the "lpinfo -m" command to list the available drivers: + + lpinfo -m + + Run the "lpinfo -v" command to list the available printers: + + lpinfo -v + + Then use the correct URI to add the printer using the "lpadmin" command: + + lpadmin -p printername -E -v device-uri -m ppd-name + + Network printers typically use "socket" or "lpd" URIs: + + lpadmin -p printername -E -v socket://11.22.33.44 -m ppd-name + lpadmin -p printername -E -v lpd://11.22.33.44/ -m ppd-name + + The sample drivers provide basic printing capabilities, but generally do not + exercise the full potential of the printers or CUPS. The CUPS web site + provides links and drivers: + + http://www.cups.org/ppd.php PPD files + http://www.cups.org/links.php Links to other drivers + + +PRINTING FILES + + CUPS provides both the System V "lp" and Berkeley "lpr" commands for + printing: + + lp filename + lpr filename + + Both the "lp" and "lpr" commands support printing options for the driver: + + lp -o media=A4 -o resolution=600dpi filename + lpr -o media=A4 -o resolution=600dpi filename + + CUPS recognizes many types of images files as well as PDF, PostScript, + HP-GL/2, and text files, so you can print those files directly rather than + through an application. + + If you have an application that generates output specifically for your + printer then you need to use the "-oraw" or "-l" options: + + lp -o raw filename + lpr -l filename + + This will prevent the filters from misinterpreting your print + file. + + +LEGAL STUFF + + CUPS is Copyright 2007-2013 by Apple Inc. CUPS and the CUPS logo are + trademarks of Apple Inc. + + The MD5 Digest code is Copyright 1999 Aladdin Enterprises. + + This software is based in part on the work of the Independent JPEG Group. + + CUPS is provided under the terms of version 2 of the GNU General Public + License and GNU Library General Public License. This program is distributed + in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the "doc/help/license.html" or "LICENSE.txt" files for more information. diff --git a/config.h b/config.h new file mode 100644 index 0000000..16c520e --- /dev/null +++ b/config.h @@ -0,0 +1,750 @@ +/* config.h. Generated from config.h.in by configure. */ +/* + * "$Id: config.h.in 10776 2012-12-17 22:17:08Z mike $" + * + * Configuration file for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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/". + */ + + /* 07/22/2016 Mopria-notice: some config changes were applied directly to this generated file + * rather than by running the configure script again. + */ + +#ifndef _CUPS_CONFIG_H_ +#define _CUPS_CONFIG_H_ + +/* + * Version of software... + */ + +#define CUPS_SVERSION "CUPS v1.6.3" +#define CUPS_MINIMAL "CUPS/1.6.3" + + +/* + * Default user and groups... + */ + +#define CUPS_DEFAULT_USER "lp" +#define CUPS_DEFAULT_GROUP "lp" +#define CUPS_DEFAULT_SYSTEM_GROUPS "lpadmin sys root" +#define CUPS_DEFAULT_PRINTOPERATOR_AUTH "@SYSTEM" + + +/* + * Default file permissions... + */ + +#define CUPS_DEFAULT_CONFIG_FILE_PERM 0644 +#define CUPS_DEFAULT_LOG_FILE_PERM 0644 + + +/* + * Default logging settings... + */ + +#define CUPS_DEFAULT_LOG_LEVEL "warn" +#define CUPS_DEFAULT_ACCESS_LOG_LEVEL "actions" + + +/* + * Default fatal error settings... + */ + +#define CUPS_DEFAULT_FATAL_ERRORS "config" + + +/* + * Default browsing settings... + */ + +#define CUPS_DEFAULT_BROWSING 1 +#define CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS "dnssd" +#define CUPS_DEFAULT_DEFAULT_SHARED 1 + + +/* + * Default IPP port... + */ + +#define CUPS_DEFAULT_IPP_PORT 631 + + +/* + * Default printcap file... + */ + +#define CUPS_DEFAULT_PRINTCAP "/etc/printcap" + + +/* + * Default Samba and LPD config files... + */ + +#define CUPS_DEFAULT_SMB_CONFIG_FILE "" +#define CUPS_DEFAULT_LPD_CONFIG_FILE "" + + +/* + * Default MaxCopies value... + */ + +#define CUPS_DEFAULT_MAX_COPIES 9999 + + +/* + * Do we have domain socket support, and if so what is the default one? + */ + +#define CUPS_DEFAULT_DOMAINSOCKET "/private/var/run/cupsd" + + +/* + * Default WebInterface value... + */ + +#define CUPS_DEFAULT_WEBIF 0 + + +/* + * Where are files stored? + * + * Note: These are defaults, which can be overridden by environment + * variables at run-time... + */ + +#define CUPS_BINDIR "/usr/bin" +#define CUPS_CACHEDIR "/var/cache/cups" +#define CUPS_DATADIR "/usr/share/cups" +#define CUPS_DOCROOT "/usr/share/doc/cups" +#define CUPS_FONTPATH "/usr/share/cups/fonts" +#define CUPS_LOCALEDIR "/usr/share/locale" +#define CUPS_LOGDIR "/var/log/cups" +#define CUPS_REQUESTS "/var/spool/cups" +#define CUPS_SBINDIR "/usr/sbin" +#define CUPS_SERVERBIN "/usr/lib/cups" +#define CUPS_SERVERROOT "/etc/cups" +#define CUPS_STATEDIR "/var/run/cups" + +/* + * Do we have various image libraries? + */ + +/* #undef HAVE_LIBPNG */ +#define HAVE_LIBZ 1 +/* #undef HAVE_LIBJPEG */ +/* #undef HAVE_LIBTIFF */ + + +/* + * Do we have PAM stuff? + */ + +#ifndef HAVE_LIBPAM +#define HAVE_LIBPAM 0 +#endif /* !HAVE_LIBPAM */ + +/* #undef HAVE_PAM_PAM_APPL_H */ +//#define HAVE_PAM_SET_ITEM 1 +//#define HAVE_PAM_SETCRED 1 + + +/* + * Do we have ? + */ + +#define HAVE_SHADOW_H 1 + + +/* + * Do we have ? + */ + +#define HAVE_CRYPT_H 1 + + +/* + * Do we have ? + */ + +/* #undef HAVE_SCSI_SG_H */ + + +/* + * Use ? + */ + +#define HAVE_STDINT_H 1 + + +/* + * Use , , and/or ? + */ + +#define HAVE_STRING_H 1 +#define HAVE_STRINGS_H 1 +/* #undef HAVE_BSTRING_H */ + + +/* + * Do we have the long long type? + */ + +#define HAVE_LONG_LONG 1 + +#ifdef HAVE_LONG_LONG +# define CUPS_LLFMT "%lld" +# define CUPS_LLCAST (long long) +#else +# define CUPS_LLFMT "%ld" +# define CUPS_LLCAST (long) +#endif /* HAVE_LONG_LONG */ + + +/* + * Do we have the strtoll() function? + */ + +#define HAVE_STRTOLL 1 + +#ifndef HAVE_STRTOLL +# define strtoll(nptr,endptr,base) strtol((nptr), (endptr), (base)) +#endif /* !HAVE_STRTOLL */ + + +/* + * Do we have the strXXX() functions? + */ + +#define HAVE_STRDUP 1 +//#define HAVE_STRLCAT 1 +//#define HAVE_STRLCPY 1 + + +/* + * Do we have the geteuid() function? + */ + +#define HAVE_GETEUID 1 + + +/* + * Do we have the setpgid() function? + */ + +#define HAVE_SETPGID 1 + + +/* + * Do we have the vsyslog() function? + */ + +#define HAVE_VSYSLOG 1 + + +/* + * Do we have the (v)snprintf() functions? + */ + +#define HAVE_SNPRINTF 1 +#define HAVE_VSNPRINTF 1 + + +/* + * What signal functions to use? + */ + +//#define HAVE_SIGSET 1 +#define HAVE_SIGACTION 1 + + +/* + * What wait functions to use? + */ + +#define HAVE_WAITPID 1 +#define HAVE_WAIT3 1 + + +/* + * Do we have the mallinfo function and malloc.h? + */ + +/* #undef HAVE_MALLINFO */ +/* #undef HAVE_MALLOC_H */ + + +/* + * Do we have the POSIX ACL functions? + */ + +#define HAVE_ACL_INIT 1 + + +/* + * Do we have the langinfo.h header file? + */ + +//#define HAVE_LANGINFO_H 1 + + +/* + * Which encryption libraries do we have? + */ + +//#define HAVE_CDSASSL 1 +/* #undef HAVE_GNUTLS */ +#define HAVE_LIBSSL 1 +#define HAVE_SSL 1 + + +/* + * Do we have the SSL_set_tlsext_host_name function? + */ + +/* #undef HAVE_SSL_SET_TLSEXT_HOST_NAME */ + + +/* + * What Security framework headers do we have? + */ + +//#define HAVE_AUTHORIZATION_H 1 +/* #undef HAVE_SECBASEPRIV_H */ +//#define HAVE_SECCERTIFICATE_H 1 +/* #undef HAVE_SECIDENTITYSEARCHPRIV_H */ +//#define HAVE_SECITEM_H 1 +/* #undef HAVE_SECITEMPRIV_H */ +//#define HAVE_SECPOLICY_H 1 +/* #undef HAVE_SECPOLICYPRIV_H */ +/* #undef HAVE_SECURETRANSPORTPRIV_H */ + + +/* + * Do we have the SecCertificateCopyData function? + */ + +#define HAVE_SECCERTIFICATECOPYDATA 1 + + +/* + * Do we have the SecIdentitySearchCreateWithPolicy function? + */ + +#define HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY 1 + + +/* + * Do we have the SecPolicyCreateSSL function? + */ + +#define HAVE_SECPOLICYCREATESSL 1 + + +/* + * Do we have the SecPolicyCreateSSL function? + */ + +#define HAVE_SECPOLICYCREATESSL 1 + + +/* + * Do we have the cssmErrorString function? + */ + +#define HAVE_CSSMERRORSTRING 1 + + +/* + * Do we have libpaper? + */ + +/* #undef HAVE_LIBPAPER */ + + +/* + * Do we have mDNSResponder for DNS Service Discovery (aka Bonjour)? + */ + +//#define HAVE_DNSSD 1 + + +/* + * Do we have Avahi for DNS Service Discovery (aka Bonjour)? + */ + +/* #undef HAVE_AVAHI */ + + +/* + * Do we have ? + */ + +#define HAVE_SYS_IOCTL_H 1 + + +/* + * Does the "stat" structure contain the "st_gen" member? + */ + +//#define HAVE_ST_GEN 1 + + +/* + * Does the "tm" structure contain the "tm_gmtoff" member? + */ + +//#define HAVE_TM_GMTOFF 1 + + +/* + * Do we have rresvport_af()? + */ + +#define HAVE_RRESVPORT_AF 1 + + +/* + * Do we have getaddrinfo()? + */ + +#define HAVE_GETADDRINFO 1 + + +/* + * Do we have getnameinfo()? + */ + +#define HAVE_GETNAMEINFO 1 + + +/* + * Do we have getifaddrs()? + */ + +//#define HAVE_GETIFADDRS 1 + + +/* + * Do we have hstrerror()? + */ + +#define HAVE_HSTRERROR 1 + + +/* + * Do we have res_init()? + */ + +#define HAVE_RES_INIT 1 + + +/* + * Do we have + */ + +#define HAVE_RESOLV_H 1 + + +/* + * Do we have the header file? + */ + +//#define HAVE_SYS_SOCKIO_H 1 + + +/* + * Does the sockaddr structure contain an sa_len parameter? + */ + +/* #undef HAVE_STRUCT_SOCKADDR_SA_LEN */ + + +/* + * Do we have the AIX usersec.h header file? + */ + +/* #undef HAVE_USERSEC_H */ + + +/* + * Do we have pthread support? + */ + +#define HAVE_PTHREAD_H 1 + + +/* + * Do we have launchd support? + */ + +//#define HAVE_LAUNCH_H 1 +//#define HAVE_LAUNCHD 1 + + +/* + * Various scripting languages... + */ + +#define HAVE_JAVA 1 +#define CUPS_JAVA "/usr/bin/java" +#define HAVE_PERL 1 +#define CUPS_PERL "/usr/bin/perl" +//#define HAVE_PHP 1 +#define CUPS_PHP "" +#define HAVE_PYTHON 1 +#define CUPS_PYTHON "/usr/bin/python" + + +/* + * Location of the poppler/Xpdf pdftops program... + */ + +/* #undef HAVE_PDFTOPS */ +/* #undef HAVE_PDFTOPS_WITH_ORIGPAGESIZES */ +#define CUPS_PDFTOPS "/usr/bin/pdftops" + + +/* + * Location of the Ghostscript gs program... + */ + +/* #undef HAVE_GHOSTSCRIPT */ +/* #undef HAVE_GHOSTSCRIPT_PS2WRITE */ +#define CUPS_GHOSTSCRIPT "/usr/bin/gs" + + +/* + * Do we have CoreFoundation public and private headers? + */ + +//#define HAVE_COREFOUNDATION_H 1 +/* #undef HAVE_CFPRIV_H */ +/* #undef HAVE_CFBUNDLEPRIV_H */ + + +/* + * Do we have ApplicationServices public headers? + */ + +#define HAVE_APPLICATIONSERVICES_H 1 + + +/* + * Do we have the SCDynamicStoreCopyComputerName function? + */ + +//#define HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME 1 + + +/* + * Do we have OS X 10.4's mbr_XXX functions? + */ + +#define HAVE_MEMBERSHIP_H 1 +/* #undef HAVE_MEMBERSHIPPRIV_H */ +#define HAVE_MBR_UID_TO_UUID 1 + + +/* + * Do we have Darwin's notify_post header and function? + */ + +//#define HAVE_NOTIFY_H 1 +//#define HAVE_NOTIFY_POST 1 + + +/* + * Do we have Darwin's IOKit private headers? + */ + +/* #undef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */ + + +/* + * Do we have DBUS? + */ + +/* #undef HAVE_DBUS */ +/* #undef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */ + + +/* + * Do we have the GSSAPI support library (for Kerberos support)? + */ + +//#define HAVE_GSS_ACQUIRE_CRED_EX_F 1 +//#define HAVE_GSS_C_NT_HOSTBASED_SERVICE 1 +//#define HAVE_GSS_GSSAPI_H 1 +/* #undef HAVE_GSS_GSSAPI_SPI_H */ +/* #undef HAVE_GSSAPI */ +/* #undef HAVE_GSSAPI_GSSAPI_H */ +/* #undef HAVE_GSSAPI_H */ + + +/* + * Default GSS service name... + */ + +#define CUPS_DEFAULT_GSSSERVICENAME "host" + + +/* + * Select/poll interfaces... + */ + +#define HAVE_POLL 1 +/* #undef HAVE_EPOLL */ +#define HAVE_KQUEUE 1 + + +/* + * Do we have the header? + */ + +#define HAVE_DLFCN_H 1 + + +/* + * Do we have ? + */ + +#define HAVE_SYS_PARAM_H 1 + + +/* + * Do we have ? + */ + +#define HAVE_SYS_UCRED_H 1 + + +/* + * Do we have removefile()? + */ + +#define HAVE_REMOVEFILE 1 + + +/* + * Do we have ? + */ + +#define HAVE_SANDBOX_H 1 + + +/* + * Which random number generator function to use... + */ + +// #define HAVE_ARC4RANDOM 1 +#define HAVE_RANDOM 1 +#define HAVE_LRAND48 1 + +#ifdef HAVE_ARC4RANDOM +# define CUPS_RAND() arc4random() +# define CUPS_SRAND(v) arc4random_stir() +#elif defined(HAVE_RANDOM) +# define CUPS_RAND() random() +# define CUPS_SRAND(v) srandom(v) +#elif defined(HAVE_LRAND48) +# define CUPS_RAND() lrand48() +# define CUPS_SRAND(v) srand48(v) +#else +# define CUPS_RAND() rand() +# define CUPS_SRAND(v) srand(v) +#endif /* HAVE_ARC4RANDOM */ + + +/* + * Do we have vproc_transaction_begin/end? + */ + +#define HAVE_VPROC_TRANSACTION_BEGIN 1 + + +/* + * Do we have libusb? + */ + +/* #undef HAVE_LIBUSB */ + + +/* + * Do we have libwrap and tcpd.h? + */ + +/* #undef HAVE_TCPD_H */ + + +/* + * Do we have ? + */ + +//#define HAVE_ICONV_H 1 + + +/* + * Do we have statfs or statvfs and one of the corresponding headers? + */ + +#define HAVE_STATFS 1 +#define HAVE_STATVFS 1 +#define HAVE_SYS_MOUNT_H 1 +/* #undef HAVE_SYS_STATFS_H */ +#define HAVE_SYS_STATVFS_H 1 +/* #undef HAVE_SYS_VFS_H */ + + +/* + * Location of OS X localization bundle, if any. + */ + +#define CUPS_BUNDLEDIR "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A" + + +/* + * Do we have XPC? + */ + +#define HAVE_XPC 1 +/* #undef HAVE_XPC_PRIVATE_H */ + + +/* + * Do we have Mini-XML? + */ + +/* #undef HAVE_MXML_H */ + + +/* + * Do we have the C99 abs() function? + */ + +#define HAVE_ABS 1 +#if !defined(HAVE_ABS) && !defined(abs) +# if defined(__GNUC__) || __STDC_VERSION__ >= 199901L +# define abs(x) _cups_abs(x) +static inline int _cups_abs(int i) { return (i < 0 ? -i : i); } +# elif defined(_MSC_VER) +# define abs(x) _cups_abs(x) +static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } +# else +# define abs(x) ((x) < 0 ? -(x) : (x)) +# endif /* __GNUC__ || __STDC_VERSION__ */ +#endif /* !HAVE_ABS && !abs */ + +#endif /* !_CUPS_CONFIG_H_ */ + +/* + * End of "$Id: config.h.in 10776 2012-12-17 22:17:08Z mike $". + */ diff --git a/config.h.in b/config.h.in new file mode 100644 index 0000000..83d067e --- /dev/null +++ b/config.h.in @@ -0,0 +1,747 @@ +/* + * "$Id: config.h.in 7918 2008-09-08 22:03:01Z mike $" + * + * Configuration file for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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/". + */ + +#ifndef _CUPS_CONFIG_H_ +#define _CUPS_CONFIG_H_ + +/* + * Version of software... + */ + +#define CUPS_SVERSION "" +#define CUPS_MINIMAL "" + + +/* + * Default user and groups... + */ + +#define CUPS_DEFAULT_USER "lp" +#define CUPS_DEFAULT_GROUP "sys" +#define CUPS_DEFAULT_SYSTEM_GROUPS "sys root system" +#define CUPS_DEFAULT_PRINTOPERATOR_AUTH "@SYSTEM" +#define CUPS_DEFAULT_SYSTEM_AUTHKEY "system.print.admin" + + +/* + * Default file permissions... + */ + +#define CUPS_DEFAULT_CONFIG_FILE_PERM 0640 +#define CUPS_DEFAULT_LOG_FILE_PERM 0644 + + +/* + * Default logging settings... + */ + +#define CUPS_DEFAULT_LOG_LEVEL "warn" +#define CUPS_DEFAULT_ACCESS_LOG_LEVEL "actions" + + +/* + * Default fatal error settings... + */ + +#define CUPS_DEFAULT_FATAL_ERRORS "config" + + +/* + * Default browsing settings... + */ + +#define CUPS_DEFAULT_BROWSING 1 +#define CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS "" +#define CUPS_DEFAULT_DEFAULT_SHARED 1 + + +/* + * Default IPP port... + */ + +#define CUPS_DEFAULT_IPP_PORT 631 + + +/* + * Default printcap file... + */ + +#define CUPS_DEFAULT_PRINTCAP "/etc/printcap" + + +/* + * Default Samba and LPD config files... + */ + +#define CUPS_DEFAULT_SMB_CONFIG_FILE "" +#define CUPS_DEFAULT_LPD_CONFIG_FILE "" + + +/* + * Default MaxCopies value... + */ + +#define CUPS_DEFAULT_MAX_COPIES 100 + + +/* + * Do we have domain socket support, and if so what is the default one? + */ + +#undef CUPS_DEFAULT_DOMAINSOCKET + + +/* + * Default WebInterface value... + */ + +#undef CUPS_DEFAULT_WEBIF + + +/* + * Where are files stored? + * + * Note: These are defaults, which can be overridden by environment + * variables at run-time... + */ + +#define CUPS_BINDIR "/usr/bin" +#define CUPS_CACHEDIR "/var/cache/cups" +#define CUPS_DATADIR "/usr/share/cups" +#define CUPS_DOCROOT "/usr/share/doc/cups" +#define CUPS_FONTPATH "/usr/share/cups/fonts" +#define CUPS_LOCALEDIR "/usr/share/locale" +#define CUPS_LOGDIR "/var/logs/cups" +#define CUPS_REQUESTS "/var/spool/cups" +#define CUPS_SBINDIR "/usr/sbin" +#define CUPS_SERVERBIN "/usr/lib/cups" +#define CUPS_SERVERROOT "/etc/cups" +#define CUPS_STATEDIR "/var/run/cups" + + +/* + * Do we have various image libraries? + */ + +#undef HAVE_LIBPNG +#undef HAVE_LIBZ +#undef HAVE_LIBJPEG +#undef HAVE_LIBTIFF + + +/* + * Do we have PAM stuff? + */ + +#ifndef HAVE_LIBPAM +#define HAVE_LIBPAM 0 +#endif /* !HAVE_LIBPAM */ + +#undef HAVE_PAM_PAM_APPL_H +#undef HAVE_PAM_SET_ITEM +#undef HAVE_PAM_SETCRED + + +/* + * Do we have ? + */ + +#undef HAVE_SHADOW_H + + +/* + * Do we have ? + */ + +#undef HAVE_CRYPT_H + + +/* + * Do we have ? + */ + +#undef HAVE_SCSI_SG_H + + +/* + * Use ? + */ + +#undef HAVE_STDINT_H + + +/* + * Use , , and/or ? + */ + +#undef HAVE_STRING_H +#undef HAVE_STRINGS_H +#undef HAVE_BSTRING_H + + +/* + * Do we have the long long type? + */ + +#undef HAVE_LONG_LONG + +#ifdef HAVE_LONG_LONG +# define CUPS_LLFMT "%lld" +# define CUPS_LLCAST (long long) +#else +# define CUPS_LLFMT "%ld" +# define CUPS_LLCAST (long) +#endif /* HAVE_LONG_LONG */ + + +/* + * Do we have the strtoll() function? + */ + +#undef HAVE_STRTOLL + +#ifndef HAVE_STRTOLL +# define strtoll(nptr,endptr,base) strtol((nptr), (endptr), (base)) +#endif /* !HAVE_STRTOLL */ + + +/* + * Do we have the strXXX() functions? + */ + +#undef HAVE_STRDUP +#undef HAVE_STRLCAT +#undef HAVE_STRLCPY + + +/* + * Do we have the geteuid() function? + */ + +#undef HAVE_GETEUID + + +/* + * Do we have the setpgid() function? + */ + +#undef HAVE_SETPGID + + +/* + * Do we have the vsyslog() function? + */ + +#undef HAVE_VSYSLOG + + +/* + * Do we have the (v)snprintf() functions? + */ + +#undef HAVE_SNPRINTF +#undef HAVE_VSNPRINTF + + +/* + * What signal functions to use? + */ + +#undef HAVE_SIGSET +#undef HAVE_SIGACTION + + +/* + * What wait functions to use? + */ + +#undef HAVE_WAITPID +#undef HAVE_WAIT3 + + +/* + * Do we have the mallinfo function and malloc.h? + */ + +#undef HAVE_MALLINFO +#undef HAVE_MALLOC_H + + +/* + * Do we have the POSIX ACL functions? + */ + +#undef HAVE_ACL_INIT + + +/* + * Do we have the langinfo.h header file? + */ + +#undef HAVE_LANGINFO_H + + +/* + * Which encryption libraries do we have? + */ + +#undef HAVE_CDSASSL +#undef HAVE_GNUTLS +#undef HAVE_LIBSSL +#undef HAVE_SSL + + +/* + * Do we have the SSL_set_tlsext_host_name function? + */ + +#undef HAVE_SSL_SET_TLSEXT_HOST_NAME + + +/* + * What Security framework headers do we have? + */ + +#undef HAVE_AUTHORIZATION_H +#undef HAVE_SECBASEPRIV_H +#undef HAVE_SECCERTIFICATE_H +#undef HAVE_SECIDENTITYSEARCHPRIV_H +#undef HAVE_SECITEM_H +#undef HAVE_SECITEMPRIV_H +#undef HAVE_SECPOLICY_H +#undef HAVE_SECPOLICYPRIV_H +#undef HAVE_SECURETRANSPORTPRIV_H + + +/* + * Do we have the SecCertificateCopyData function? + */ + +#undef HAVE_SECCERTIFICATECOPYDATA + + +/* + * Do we have the SecIdentitySearchCreateWithPolicy function? + */ + +#undef HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY + + +/* + * Do we have the SecPolicyCreateSSL function? + */ + +#undef HAVE_SECPOLICYCREATESSL + + +/* + * Do we have the SecPolicyCreateSSL function? + */ + +#undef HAVE_SECPOLICYCREATESSL + + +/* + * Do we have the cssmErrorString function? + */ + +#undef HAVE_CSSMERRORSTRING + + +/* + * Do we have libpaper? + */ + +#undef HAVE_LIBPAPER + + +/* + * Do we have mDNSResponder for DNS Service Discovery (aka Bonjour)? + */ + +#undef HAVE_DNSSD + + +/* + * Do we have Avahi for DNS Service Discovery (aka Bonjour)? + */ + +#undef HAVE_AVAHI + + +/* + * Do we have ? + */ + +#undef HAVE_SYS_IOCTL_H + + +/* + * Does the "stat" structure contain the "st_gen" member? + */ + +#undef HAVE_ST_GEN + + +/* + * Does the "tm" structure contain the "tm_gmtoff" member? + */ + +#undef HAVE_TM_GMTOFF + + +/* + * Do we have rresvport_af()? + */ + +#undef HAVE_RRESVPORT_AF + + +/* + * Do we have getaddrinfo()? + */ + +#undef HAVE_GETADDRINFO + + +/* + * Do we have getnameinfo()? + */ + +#undef HAVE_GETNAMEINFO + + +/* + * Do we have getifaddrs()? + */ + +#undef HAVE_GETIFADDRS + + +/* + * Do we have hstrerror()? + */ + +#undef HAVE_HSTRERROR + + +/* + * Do we have res_init()? + */ + +#undef HAVE_RES_INIT + + +/* + * Do we have + */ + +#undef HAVE_RESOLV_H + + +/* + * Do we have the header file? + */ + +#undef HAVE_SYS_SOCKIO_H + + +/* + * Does the sockaddr structure contain an sa_len parameter? + */ + +#undef HAVE_STRUCT_SOCKADDR_SA_LEN + + +/* + * Do we have the AIX usersec.h header file? + */ + +#undef HAVE_USERSEC_H + + +/* + * Do we have pthread support? + */ + +#undef HAVE_PTHREAD_H + + +/* + * Do we have launchd support? + */ + +#undef HAVE_LAUNCH_H +#undef HAVE_LAUNCHD + + +/* + * Various scripting languages... + */ + +#undef HAVE_JAVA +#define CUPS_JAVA "/usr/bin/java" +#undef HAVE_PERL +#define CUPS_PERL "/usr/bin/perl" +#undef HAVE_PHP +#define CUPS_PHP "/usr/bin/php" +#undef HAVE_PYTHON +#define CUPS_PYTHON "/usr/bin/python" + + +/* + * Location of the poppler/Xpdf pdftops program... + */ + +#undef HAVE_PDFTOPS +#undef HAVE_PDFTOPS_WITH_ORIGPAGESIZES +#define CUPS_PDFTOPS "/usr/bin/pdftops" + + +/* + * Location of the Ghostscript gs program... + */ + +#undef HAVE_GHOSTSCRIPT +#undef HAVE_GHOSTSCRIPT_PS2WRITE +#define CUPS_GHOSTSCRIPT "/usr/bin/gs" + + +/* + * Do we have CoreFoundation public and private headers? + */ + +#undef HAVE_COREFOUNDATION_H +#undef HAVE_CFPRIV_H +#undef HAVE_CFBUNDLEPRIV_H + + +/* + * Do we have ApplicationServices public headers? + */ + +#undef HAVE_APPLICATIONSERVICES_H + + +/* + * Do we have the SCDynamicStoreCopyComputerName function? + */ + +#undef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME + + +/* + * Do we have OS X 10.4's mbr_XXX functions? + */ + +#undef HAVE_MEMBERSHIP_H +#undef HAVE_MEMBERSHIPPRIV_H +#undef HAVE_MBR_UID_TO_UUID + + +/* + * Do we have Darwin's notify_post header and function? + */ + +#undef HAVE_NOTIFY_H +#undef HAVE_NOTIFY_POST + + +/* + * Do we have Darwin's IOKit private headers? + */ + +#undef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H + + +/* + * Do we have DBUS? + */ + +#undef HAVE_DBUS +#undef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND + + +/* + * Do we have the GSSAPI support library (for Kerberos support)? + */ + +#undef HAVE_GSS_ACQUIRE_CRED_EX_F +#undef HAVE_GSS_C_NT_HOSTBASED_SERVICE +#undef HAVE_GSS_GSSAPI_H +#undef HAVE_GSS_GSSAPI_SPI_H +#undef HAVE_GSSAPI +#undef HAVE_GSSAPI_GSSAPI_H +#undef HAVE_GSSAPI_H + + +/* + * Default GSS service name... + */ + +#define CUPS_DEFAULT_GSSSERVICENAME "" + + +/* + * Select/poll interfaces... + */ + +#undef HAVE_POLL +#undef HAVE_EPOLL +#undef HAVE_KQUEUE + + +/* + * Do we have the header? + */ + +#undef HAVE_DLFCN_H + + +/* + * Do we have ? + */ + +#undef HAVE_SYS_PARAM_H + + +/* + * Do we have ? + */ + +#undef HAVE_SYS_UCRED_H + + +/* + * Do we have removefile()? + */ + +#undef HAVE_REMOVEFILE + + +/* + * Do we have ? + */ + +#undef HAVE_SANDBOX_H + + +/* + * Which random number generator function to use... + */ + +#undef HAVE_ARC4RANDOM +#undef HAVE_RANDOM +#undef HAVE_LRAND48 + +#ifdef HAVE_ARC4RANDOM +# define CUPS_RAND() arc4random() +# define CUPS_SRAND(v) arc4random_stir() +#elif defined(HAVE_RANDOM) +# define CUPS_RAND() random() +# define CUPS_SRAND(v) srandom(v) +#elif defined(HAVE_LRAND48) +# define CUPS_RAND() lrand48() +# define CUPS_SRAND(v) srand48(v) +#else +# define CUPS_RAND() rand() +# define CUPS_SRAND(v) srand(v) +#endif /* HAVE_ARC4RANDOM */ + + +/* + * Do we have vproc_transaction_begin/end? + */ + +#undef HAVE_VPROC_TRANSACTION_BEGIN + + +/* + * Do we have libusb? + */ + +#undef HAVE_LIBUSB + + +/* + * Do we have libwrap and tcpd.h? + */ + +#undef HAVE_TCPD_H + + +/* + * Do we have ? + */ + +#undef HAVE_ICONV_H + + +/* + * Do we have statfs or statvfs and one of the corresponding headers? + */ + +#undef HAVE_STATFS +#undef HAVE_STATVFS +#undef HAVE_SYS_MOUNT_H +#undef HAVE_SYS_STATFS_H +#undef HAVE_SYS_STATVFS_H +#undef HAVE_SYS_VFS_H + + +/* + * Location of OS X localization bundle, if any. + */ + +#undef CUPS_BUNDLEDIR + + +/* + * Do we have XPC? + */ + +#undef HAVE_XPC +#undef HAVE_XPC_PRIVATE_H + + +/* + * Do we have Mini-XML? + */ + +#undef HAVE_MXML_H + + +/* + * Do we have the C99 abs() function? + */ + +#undef HAVE_ABS +#if !defined(HAVE_ABS) && !defined(abs) +# if defined(__GNUC__) || __STDC_VERSION__ >= 199901L +# define abs(x) _cups_abs(x) +static inline int _cups_abs(int i) { return (i < 0 ? -i : i); } +# elif defined(_MSC_VER) +# define abs(x) _cups_abs(x) +static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } +# else +# define abs(x) ((x) < 0 ? -(x) : (x)) +# endif /* __GNUC__ || __STDC_VERSION__ */ +#endif /* !HAVE_ABS && !abs */ + +#endif /* !_CUPS_CONFIG_H_ */ + +/* + * End of "$Id: config.h.in 7918 2008-09-08 22:03:01Z mike $". + */ diff --git a/configure.in b/configure.in new file mode 100644 index 0000000..3951e90 --- /dev/null +++ b/configure.in @@ -0,0 +1,97 @@ +dnl +dnl "$Id: configure.in 7833 2008-08-04 20:55:13Z mike $" +dnl +dnl Configuration script for CUPS. +dnl +dnl Copyright 2007-2013 by Apple Inc. +dnl Copyright 1997-2007 by Easy Software Products, all rights reserved. +dnl +dnl These coded instructions, statements, and computer programs are the +dnl property of Apple Inc. and are protected by Federal copyright +dnl law. Distribution and use rights are outlined in the file "LICENSE.txt" +dnl which should have been included with this file. If this file is +dnl file is missing or damaged, see the license at "http://www.cups.org/". +dnl + +AC_INIT(cups/cups.h) + +sinclude(config-scripts/cups-opsys.m4) +sinclude(config-scripts/cups-common.m4) +sinclude(config-scripts/cups-directories.m4) +sinclude(config-scripts/cups-manpages.m4) + +sinclude(config-scripts/cups-sharedlibs.m4) +sinclude(config-scripts/cups-libtool.m4) +sinclude(config-scripts/cups-compiler.m4) + +sinclude(config-scripts/cups-network.m4) +sinclude(config-scripts/cups-poll.m4) +sinclude(config-scripts/cups-gssapi.m4) +sinclude(config-scripts/cups-threads.m4) +sinclude(config-scripts/cups-ssl.m4) +sinclude(config-scripts/cups-pam.m4) +sinclude(config-scripts/cups-largefile.m4) +sinclude(config-scripts/cups-dnssd.m4) +sinclude(config-scripts/cups-launchd.m4) +sinclude(config-scripts/cups-defaults.m4) +sinclude(config-scripts/cups-scripting.m4) + +INSTALL_LANGUAGES="" +UNINSTALL_LANGUAGES="" +LANGFILES="" +if test "x$LANGUAGES" != x; then + INSTALL_LANGUAGES="install-languages" + UNINSTALL_LANGUAGES="uninstall-languages" + for lang in $LANGUAGES; do + if test -f doc/$lang/index.html.in; then + LANGFILES="$LANGFILES doc/$lang/index.html" + fi + + if test -f templates/$lang/header.tmpl.in; then + LANGFILES="$LANGFILES templates/$lang/header.tmpl" + fi + done +elif test "x$CUPS_BUNDLEDIR" != x; then + INSTALL_LANGUAGES="install-langbundle" + UNINSTALL_LANGUAGES="uninstall-langbundle" +fi + +AC_SUBST(INSTALL_LANGUAGES) +AC_SUBST(UNINSTALL_LANGUAGES) + +AC_OUTPUT(Makedefs + conf/cups-files.conf + conf/cupsd.conf + conf/mime.convs + conf/pam.std + conf/snmp.conf + cups-config + data/testprint + desktop/cups.desktop + doc/help/ref-cups-files-conf.html + doc/help/ref-cupsd-conf.html + doc/help/standard.html + doc/index.html + man/client.conf.man + man/cups-deviced.man + man/cups-driverd.man + man/cups-files.conf.man + man/cups-lpd.man + man/cups-snmp.man + man/cupsaddsmb.man + man/cupsd.conf.man + man/cupsd.man + man/lpoptions.man + scheduler/cups-lpd.xinetd + scheduler/cups.sh + scheduler/cups.xml + scheduler/org.cups.cups-lpd.plist + templates/header.tmpl + packaging/cups.list + $LANGFILES) + +chmod +x cups-config + +dnl +dnl End of "$Id: configure.in 7833 2008-08-04 20:55:13Z mike $". +dnl diff --git a/cups-config.in b/cups-config.in new file mode 100755 index 0000000..7ba1044 --- /dev/null +++ b/cups-config.in @@ -0,0 +1,146 @@ +#! /bin/sh +# +# "$Id: cups-config.in 9926 2011-08-27 09:23:01Z mike $" +# +# CUPS configuration utility. +# +# Copyright 2007-2011 by Apple Inc. +# Copyright 2001-2006 by Easy Software Products, all rights reserved. +# +# 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/". +# + +VERSION="@CUPS_VERSION@" +APIVERSION="1.6" +BUILD="@CUPS_BUILD@" + +prefix=@prefix@ +exec_prefix=@exec_prefix@ +bindir=@bindir@ +includedir=@includedir@ +libdir=@libdir@ +imagelibdir=@libdir@ +datarootdir=@datadir@ +datadir=@datadir@ +sysconfdir=@sysconfdir@ +cups_datadir=@CUPS_DATADIR@ +cups_serverbin=@CUPS_SERVERBIN@ +cups_serverroot=@CUPS_SERVERROOT@ +INSTALLSTATIC=@INSTALLSTATIC@ + +# flags for C++ compiler: +CFLAGS="" +LDFLAGS="@EXPORT_LDFLAGS@" +LIBS="@LIBGSSAPI@ @EXPORT_SSLLIBS@ @LIBZ@ @LIBS@" + +# Check for local invocation... +selfdir=`dirname $0` + +if test -f "$selfdir/cups/cups.h"; then + CFLAGS="-I$selfdir" + LDFLAGS="-L$selfdir/cups -L$selfdir/filter $LDFLAGS" + libdir="$selfdir/cups" + imagelibdir="$selfdir/filter" +else + if test $includedir != /usr/include; then + CFLAGS="$CFLAGS -I$includedir" + fi + + if test $libdir != /usr/lib -a $libdir != /usr/lib32 -a $libdir != /usr/lib64; then + LDFLAGS="$LDFLAGS -L$libdir" + fi +fi + + +usage () +{ + echo "Usage: cups-config --api-version" + echo " cups-config --build" + echo " cups-config --cflags" + echo " cups-config --datadir" + echo " cups-config --help" + echo " cups-config --ldflags" + echo " cups-config [--image] [--static] --libs" + echo " cups-config --serverbin" + echo " cups-config --serverroot" + echo " cups-config --version" + + exit $1 +} + +if test $# -eq 0; then + usage 1 +fi + +# Parse command line options +static=no +image=no + +while test $# -gt 0; do + case $1 in + --api-version) + echo $APIVERSION + ;; + --build) + echo $BUILD + ;; + --cflags) + echo $CFLAGS + ;; + --datadir) + echo $cups_datadir + ;; + --help) + usage 0 + ;; + --image) + image=yes + ;; + --ldflags) + echo $LDFLAGS + ;; + --libs) + if test $static = no; then + libs="@EXTLINKCUPS@ $LIBS"; + if test $image = yes; then + libs="@EXTLINKCUPSIMAGE@ $libs" + fi + else + libs="$libdir/libcups.a $LIBS"; + if test $image = yes; then + libs="$libdir/libcupsimage.a $libs" + fi + fi + echo $libs + ;; + --serverbin) + echo $cups_serverbin + ;; + --serverroot) + echo $cups_serverroot + ;; + --static) + if test -z "$INSTALLSTATIC"; then + echo "WARNING: Static libraries not installed!" >&2 + else + static=yes + fi + ;; + --version) + echo $VERSION + ;; + *) + usage 1 + ;; + esac + + shift +done + +# +# End of "$Id: cups-config.in 9926 2011-08-27 09:23:01Z mike $". +# diff --git a/cups/Dependencies b/cups/Dependencies new file mode 100644 index 0000000..10beea2 --- /dev/null +++ b/cups/Dependencies @@ -0,0 +1,260 @@ +adminutil.o: adminutil.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h adminutil.h +array.o: array.c string-private.h ../config.h debug-private.h \ + ../cups/versioning.h array-private.h ../cups/array.h +attr.o: attr.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +auth.o: auth.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +backchannel.o: backchannel.c cups.h file.h versioning.h ipp.h http.h \ + array.h language.h +backend.o: backend.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h backend.h +conflicts.o: conflicts.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +custom.o: custom.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +debug.o: debug.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +dest.o: dest.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +dest-job.o: dest-job.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +dest-localization.o: dest-localization.c cups-private.h string-private.h \ + ../config.h debug-private.h ../cups/versioning.h ipp-private.h \ + ../cups/ipp.h http.h array.h http-private.h md5-private.h \ + language-private.h ../cups/transcode.h language.h pwg-private.h \ + ../cups/cups.h file.h ppd-private.h ../cups/ppd.h thread-private.h +dest-options.o: dest-options.c cups-private.h string-private.h \ + ../config.h debug-private.h ../cups/versioning.h ipp-private.h \ + ../cups/ipp.h http.h array.h http-private.h md5-private.h \ + language-private.h ../cups/transcode.h language.h pwg-private.h \ + ../cups/cups.h file.h ppd-private.h ../cups/ppd.h thread-private.h +dir.o: dir.c string-private.h ../config.h debug-private.h \ + ../cups/versioning.h dir.h +emit.o: emit.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +encode.o: encode.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +file.o: file.c file-private.h cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +getdevices.o: getdevices.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +getifaddrs.o: getifaddrs.c http-private.h ../config.h ../cups/http.h \ + versioning.h array.h md5-private.h ipp-private.h ../cups/ipp.h +getputfile.o: getputfile.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +globals.o: globals.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +http.o: http.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +http-addr.o: http-addr.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +http-addrlist.o: http-addrlist.c cups-private.h string-private.h \ + ../config.h debug-private.h ../cups/versioning.h ipp-private.h \ + ../cups/ipp.h http.h array.h http-private.h md5-private.h \ + language-private.h ../cups/transcode.h language.h pwg-private.h \ + ../cups/cups.h file.h ppd-private.h ../cups/ppd.h thread-private.h +http-support.o: http-support.c cups-private.h string-private.h \ + ../config.h debug-private.h ../cups/versioning.h ipp-private.h \ + ../cups/ipp.h http.h array.h http-private.h md5-private.h \ + language-private.h ../cups/transcode.h language.h pwg-private.h \ + ../cups/cups.h file.h ppd-private.h ../cups/ppd.h thread-private.h +ipp.o: ipp.c cups-private.h string-private.h ../config.h debug-private.h \ + ../cups/versioning.h ipp-private.h ../cups/ipp.h http.h array.h \ + http-private.h md5-private.h language-private.h ../cups/transcode.h \ + language.h pwg-private.h ../cups/cups.h file.h ppd-private.h \ + ../cups/ppd.h thread-private.h +ipp-support.o: ipp-support.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +langprintf.o: langprintf.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +language.o: language.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +localize.o: localize.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +mark.o: mark.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +md5.o: md5.c md5-private.h string-private.h ../config.h +md5passwd.o: md5passwd.c http-private.h ../config.h ../cups/http.h \ + versioning.h array.h md5-private.h ipp-private.h ../cups/ipp.h \ + string-private.h +notify.o: notify.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +options.o: options.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +page.o: page.c string-private.h ../config.h debug-private.h \ + ../cups/versioning.h ppd.h cups.h file.h ipp.h http.h array.h \ + language.h +ppd.o: ppd.c cups-private.h string-private.h ../config.h debug-private.h \ + ../cups/versioning.h ipp-private.h ../cups/ipp.h http.h array.h \ + http-private.h md5-private.h language-private.h ../cups/transcode.h \ + language.h pwg-private.h ../cups/cups.h file.h ppd-private.h \ + ../cups/ppd.h thread-private.h +ppd-cache.o: ppd-cache.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +pwg-media.o: pwg-media.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +request.o: request.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +sidechannel.o: sidechannel.c sidechannel.h versioning.h cups-private.h \ + string-private.h ../config.h debug-private.h ipp-private.h \ + ../cups/ipp.h http.h array.h http-private.h md5-private.h \ + language-private.h ../cups/transcode.h language.h pwg-private.h \ + ../cups/cups.h file.h ppd-private.h ../cups/ppd.h thread-private.h +snmp.o: snmp.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h snmp-private.h +snprintf.o: snprintf.c string-private.h ../config.h +string.o: string.c string-private.h ../config.h debug-private.h \ + ../cups/versioning.h thread-private.h array.h +tempfile.o: tempfile.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +thread.o: thread.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +transcode.o: transcode.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +usersys.o: usersys.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +util.o: util.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +testadmin.o: testadmin.c adminutil.h cups.h file.h versioning.h ipp.h \ + http.h array.h language.h string-private.h ../config.h +testarray.o: testarray.c string-private.h ../config.h debug-private.h \ + ../cups/versioning.h array.h dir.h +testconflicts.o: testconflicts.c cups.h file.h versioning.h ipp.h http.h \ + array.h language.h ppd.h string-private.h ../config.h +testcups.o: testcups.c string-private.h ../config.h cups.h file.h \ + versioning.h ipp.h http.h array.h language.h ppd.h +testfile.o: testfile.c string-private.h ../config.h debug-private.h \ + ../cups/versioning.h file.h +testhttp.o: testhttp.c string-private.h ../config.h http-private.h \ + ../cups/http.h versioning.h array.h md5-private.h ipp-private.h \ + ../cups/ipp.h +testi18n.o: testi18n.c string-private.h ../config.h language-private.h \ + ../cups/transcode.h language.h array.h versioning.h +testipp.o: testipp.c file.h versioning.h string-private.h ../config.h \ + ipp-private.h ../cups/ipp.h http.h array.h +testoptions.o: testoptions.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +testlang.o: testlang.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +testppd.o: testppd.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h +testpwg.o: testpwg.c ppd-private.h ../cups/cups.h file.h versioning.h \ + ipp.h http.h array.h language.h ../cups/ppd.h pwg-private.h \ + file-private.h cups-private.h string-private.h ../config.h \ + debug-private.h ipp-private.h http-private.h md5-private.h \ + language-private.h ../cups/transcode.h thread-private.h +testsnmp.o: testsnmp.c cups-private.h string-private.h ../config.h \ + debug-private.h ../cups/versioning.h ipp-private.h ../cups/ipp.h \ + http.h array.h http-private.h md5-private.h language-private.h \ + ../cups/transcode.h language.h pwg-private.h ../cups/cups.h file.h \ + ppd-private.h ../cups/ppd.h thread-private.h snmp-private.h diff --git a/cups/Makefile b/cups/Makefile new file mode 100644 index 0000000..22bb191 --- /dev/null +++ b/cups/Makefile @@ -0,0 +1,633 @@ +# +# "$Id: Makefile 7871 2008-08-27 21:12:43Z mike $" +# +# API library Makefile for CUPS. +# +# Copyright 2007-2012 by Apple Inc. +# Copyright 1997-2006 by Easy Software Products, all rights reserved. +# +# 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. +# + +include ../Makedefs + +# +# Object files... +# + +LIBOBJS = \ + adminutil.o \ + array.o \ + attr.o \ + auth.o \ + backchannel.o \ + backend.o \ + conflicts.o \ + custom.o \ + debug.o \ + dest.o \ + dest-job.o \ + dest-localization.o \ + dest-options.o \ + dir.o \ + emit.o \ + encode.o \ + file.o \ + getdevices.o \ + getifaddrs.o \ + getputfile.o \ + globals.o \ + http.o \ + http-addr.o \ + http-addrlist.o \ + http-support.o \ + ipp.o \ + ipp-support.o \ + langprintf.o \ + language.o \ + localize.o \ + mark.o \ + md5.o \ + md5passwd.o \ + notify.o \ + options.o \ + page.o \ + ppd.o \ + ppd-cache.o \ + pwg-media.o \ + request.o \ + sidechannel.o \ + snmp.o \ + snprintf.o \ + string.o \ + tempfile.o \ + thread.o \ + transcode.o \ + usersys.o \ + util.o +TESTOBJS = \ + testadmin.o \ + testarray.o \ + testconflicts.o \ + testcups.o \ + testfile.o \ + testhttp.o \ + testi18n.o \ + testipp.o \ + testoptions.o \ + testlang.o \ + testppd.o \ + testpwg.o \ + testsnmp.o +OBJS = \ + $(LIBOBJS) \ + $(TESTOBJS) + + +# +# Header files to install... +# + +HEADERS = \ + adminutil.h \ + array.h \ + backend.h \ + cups.h \ + dir.h \ + file.h \ + http.h \ + ipp.h \ + language.h \ + ppd.h \ + raster.h \ + sidechannel.h \ + transcode.h \ + versioning.h + +HEADERSPRIV = \ + array-private.h \ + cups-private.h \ + debug-private.h \ + file-private.h \ + http-private.h \ + ipp-private.h \ + language-private.h \ + md5-private.h \ + ppd-private.h \ + pwg-private.h \ + raster-private.h \ + snmp-private.h \ + string-private.h \ + thread-private.h + + +# +# Targets in this directory... +# + +LIBTARGETS = \ + $(LIBCUPSSTATIC) \ + $(LIBCUPS) + +UNITTARGETS = \ + testadmin \ + testarray \ + testconflicts \ + testcups \ + testfile \ + testhttp \ + testi18n \ + testipp \ + testlang \ + testoptions \ + testppd \ + testpwg \ + testsnmp + +TARGETS = \ + $(LIBTARGETS) + + +# +# Make all targets... +# + +all: $(TARGETS) + + +# +# Make library targets... +# + +libs: $(LIBTARGETS) + + +# +# Make unit tests... +# + +unittests: $(UNITTARGETS) + + +# +# Remove object and target files... +# + +clean: + $(RM) $(OBJS) $(TARGETS) $(UNITTARGETS) + $(RM) libcups.so libcups.sl libcups.dylib + + +# +# Update dependencies (without system header dependencies...) +# + +depend: + $(CC) -MM $(ALL_CFLAGS) $(OBJS:.o=.c) >Dependencies + + +# +# Run oclint to check code coverage... +# + +oclint: + oclint -o=oclint.html -html $(LIBOBJS:.o=.c) -- $(ALL_CFLAGS) + + +# +# Install all targets... +# + +install: all install-data install-headers install-libs install-exec + + +# +# Install data files... +# + +install-data: + + +# +# Install programs... +# + +install-exec: + + +# +# Install headers... +# + +install-headers: + echo Installing header files into $(INCLUDEDIR)/cups... + $(INSTALL_DIR) -m 755 $(INCLUDEDIR)/cups + for file in $(HEADERS); do \ + $(INSTALL_DATA) $$file $(INCLUDEDIR)/cups; \ + done + if test "x$(privateinclude)" != x; then \ + echo Installing private header files into $(PRIVATEINCLUDE)...; \ + $(INSTALL_DIR) -m 755 $(PRIVATEINCLUDE); \ + for file in $(HEADERSPRIV); do \ + $(INSTALL_DATA) $$file $(PRIVATEINCLUDE)/$$file; \ + done; \ + fi + + +# +# Install libraries... +# + +install-libs: $(INSTALLSTATIC) + echo Installing libraries in $(LIBDIR)... + $(INSTALL_DIR) -m 755 $(LIBDIR) + $(INSTALL_LIB) $(LIBCUPS) $(LIBDIR) + if test $(LIBCUPS) = "libcups.so.2" -o $(LIBCUPS) = "libcups.sl.2"; then \ + $(RM) $(LIBDIR)/`basename $(LIBCUPS) .2`; \ + $(LN) $(LIBCUPS) $(LIBDIR)/`basename $(LIBCUPS) .2`; \ + fi + if test $(LIBCUPS) = "libcups.2.dylib"; then \ + $(RM) $(LIBDIR)/libcups.dylib; \ + $(LN) $(LIBCUPS) $(LIBDIR)/libcups.dylib; \ + fi + if test "x$(SYMROOT)" != "x"; then \ + $(INSTALL_DIR) $(SYMROOT); \ + cp $(LIBCUPS) $(SYMROOT); \ + fi + +installstatic: + $(INSTALL_DIR) -m 755 $(LIBDIR) + $(INSTALL_LIB) -m 755 $(LIBCUPSSTATIC) $(LIBDIR) + $(RANLIB) $(LIBDIR)/$(LIBCUPSSTATIC) + $(CHMOD) 555 $(LIBDIR)/$(LIBCUPSSTATIC) + + +# +# Uninstall object and target files... +# + +uninstall: + $(RM) $(LIBDIR)/libcups.2.dylib + $(RM) $(LIBDIR)/$(LIBCUPSSTATIC) + $(RM) $(LIBDIR)/libcups.dylib + $(RM) $(LIBDIR)/libcups_s.a + $(RM) $(LIBDIR)/libcups.sl + $(RM) $(LIBDIR)/libcups.sl.2 + $(RM) $(LIBDIR)/libcups.so + $(RM) $(LIBDIR)/libcups.so.2 + -$(RMDIR) $(LIBDIR) + for file in $(HEADERS); do \ + $(RM) $(INCLUDEDIR)/cups/$$file; \ + done + -$(RMDIR) $(INCLUDEDIR)/cups + + +# +# libcups.so.2, libcups.sl.2 +# + +libcups.so.2 libcups.sl.2: $(LIBOBJS) + echo Linking $@... + $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ $(LIBOBJS) $(LIBGSSAPI) \ + $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + $(RM) `basename $@ .2` + $(LN) $@ `basename $@ .2` + + +# +# libcups.2.dylib +# + +libcups.2.dylib: $(LIBOBJS) $(LIBCUPSORDER) + echo Creating export list for $@... + nm $(LIBOBJS) 2>/dev/null | grep "T _" | awk '{print $$3}' | \ + grep -v -e '^(_cupsConnect|_cupsCharset|_cupsEncodingName|_cupsSetDefaults|_cupsSetHTTPError|_cupsUserDefault|_httpWait)$$' | \ + sort >t.exp + echo Linking $@... + $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ \ + -install_name $(libdir)/$@ \ + -current_version 2.10.0 \ + -compatibility_version 2.0.0 \ + -exported_symbols_list t.exp \ + $(LIBOBJS) $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) \ + $(COMMONLIBS) $(LIBZ) + $(RM) libcups.dylib t.exp + $(LN) $@ libcups.dylib + + +# +# libcups_s.a +# + +libcups_s.a: $(LIBOBJS) libcups_s.exp + echo Creating $@... + $(DSO) $(DSOFLAGS) -Wl,-bexport:libcups_s.exp -o libcups_s.o \ + $(LIBOBJS) $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) \ + $(COMMONLIBS) $(LIBZ) + $(RM) $@ + $(AR) $(ARFLAGS) $@ libcups_s.o + + +# +# libcups.la +# + +libcups.la: $(LIBOBJS) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(DSOFLAGS) -o $@ $(LIBOBJS:.o=.lo) \ + -rpath $(LIBDIR) -version-info 2:10 $(LIBGSSAPI) $(SSLLIBS) \ + $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + + +# +# libcups.a +# + +libcups.a: $(LIBOBJS) + echo Archiving $@... + $(RM) $@ + $(AR) $(ARFLAGS) $@ $(LIBOBJS) + $(RANLIB) $@ + + +# +# testadmin (dependency on static CUPS library is intentional) +# + +testadmin: testadmin.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ testadmin.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + + +# +# testarray (dependency on static CUPS library is intentional) +# + +testarray: testarray.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testarray.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running array API tests... + ./testarray + + +# +# testconflicts (dependency on static CUPS library is intentional) +# + +testconflicts: testconflicts.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ testconflicts.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + + +# +# testcups (dependency on static CUPS library is intentional) +# + +testcups: testcups.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ testcups.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + + +# +# testfile (dependency on static CUPS library is intentional) +# + +testfile: testfile.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testfile.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running file API tests... + ./testfile + + +# +# testhttp (dependency on static CUPS library is intentional) +# + +testhttp: testhttp.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testhttp.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running HTTP API tests... + ./testhttp + + +# +# testipp (dependency on static CUPS library is intentional) +# + +testipp: testipp.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testipp.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running IPP API tests... + ./testipp + + +# +# testi18n (dependency on static CUPS library is intentional) +# + +testi18n: testi18n.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testi18n.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running internationalization API tests... + ./testi18n + + +# +# testlang (dependency on static CUPS library is intentional) +# + +testlang: testlang.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testlang.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running language API tests... + ./testlang + + +# +# testoptions (dependency on static CUPS library is intentional) +# + +testoptions: testoptions.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testoptions.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running option API tests... + ./testoptions + + +# +# testppd (dependency on static CUPS library is intentional) +# + +testppd: testppd.o $(LIBCUPSSTATIC) test.ppd test2.ppd + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testppd.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running PPD API tests... + ./testppd + + +# +# testpwg (dependency on static CUPS library is intentional) +# + +testpwg: testpwg.o $(LIBCUPSSTATIC) test.ppd + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testpwg.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + echo Running PWG API tests... + ./testpwg test.ppd + + +# +# testsnmp (dependency on static CUPS library is intentional) +# + +testsnmp: testsnmp.o $(LIBCUPSSTATIC) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ testsnmp.o $(LIBCUPSSTATIC) \ + $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) + + +# +# Automatic API help files... +# + +apihelp: + echo Generating CUPS API help files... + mxmldoc --section "Programming" \ + --title "Introduction to CUPS Programming" \ + --css ../doc/cups-printable.css \ + --header api-overview.header --intro api-overview.shtml \ + >../doc/help/api-overview.html + mxmldoc --section "Programming" --title "Array API" \ + --css ../doc/cups-printable.css \ + --header api-array.header --intro api-array.shtml \ + api-array.xml \ + array.h array.c >../doc/help/api-array.html + mxmldoc --tokens help/api-array.html api-array.xml >../doc/help/api-array.tokens + $(RM) api-array.xml + mxmldoc --section "Programming" --title "CUPS API" \ + --css ../doc/cups-printable.css \ + --header api-cups.header --intro api-cups.shtml \ + api-cups.xml \ + cups.h adminutil.c dest*.c language.c notify.c \ + options.c tempfile.c usersys.c \ + util.c >../doc/help/api-cups.html + mxmldoc --tokens help/api-cups.html api-cups.xml >../doc/help/api-cups.tokens + $(RM) api-cups.xml + mxmldoc --section "Programming" --title "File and Directory APIs" \ + --css ../doc/cups-printable.css \ + --header api-filedir.header --intro api-filedir.shtml \ + api-filedir.xml \ + file.h file.c dir.h dir.c >../doc/help/api-filedir.html + mxmldoc --tokens api-filedir.xml >../doc/help/api-filedir.tokens + $(RM) api-filedir.xml + mxmldoc --section "Programming" --title "PPD API (DEPRECATED)" \ + --css ../doc/cups-printable.css \ + --header api-ppd.header --intro api-ppd.shtml \ + api-ppd.xml \ + ppd.h attr.c conflicts.c custom.c emit.c localize.c mark.c page.c \ + ppd.c >../doc/help/api-ppd.html + mxmldoc --tokens help/api-ppd.html api-ppd.xml >../doc/help/api-ppd.tokens + $(RM) api-ppd.xml + mxmldoc --section "Programming" --title "HTTP and IPP APIs" \ + --css ../doc/cups-printable.css \ + --header api-httpipp.header --intro api-httpipp.shtml \ + api-httpipp.xml \ + http.h ipp.h auth.c getdevices.c getputfile.c encode.c \ + http.c http-addr.c http-support.c ipp.c ipp-support.c \ + md5passwd.c request.c >../doc/help/api-httpipp.html + mxmldoc --tokens help/api-httpipp.html api-httpipp.xml >../doc/help/api-httpipp.tokens + $(RM) api-httpipp.xml + mxmldoc --section "Programming" \ + --title "Filter and Backend Programming" \ + --css ../doc/cups-printable.css \ + --header api-filter.header --intro api-filter.shtml \ + api-filter.xml \ + backchannel.c backend.h backend.c sidechannel.c sidechannel.h \ + >../doc/help/api-filter.html + mxmldoc --tokens help/api-filter.html api-filter.xml >../doc/help/api-filter.tokens + $(RM) api-filter.xml + +framedhelp: + echo Generating CUPS API help files... + mxmldoc --framed api-overview \ + --section "Programming" \ + --title "Introduction to CUPS Programming" \ + --css ../doc/cups-printable.css \ + --header api-overview.header --intro api-overview.shtml + mxmldoc --framed api-array \ + --section "Programming" --title "Array API" \ + --css ../doc/cups-printable.css \ + --header api-array.header --intro api-array.shtml \ + array.h array.c + mxmldoc --framed api-cups \ + --section "Programming" --title "CUPS API" \ + --css ../doc/cups-printable.css \ + --header api-cups.header --intro api-cups.shtml \ + cups.h adminutil.c dest*.c language.c notify.c \ + options.c tempfile.c usersys.c \ + util.c + mxmldoc --framed api-filedir \ + --section "Programming" --title "File and Directory APIs" \ + --css ../doc/cups-printable.css \ + --header api-filedir.header --intro api-filedir.shtml \ + file.h file.c dir.h dir.c + mxmldoc --framed api-ppd \ + --section "Programming" --title "PPD API (DEPRECATED)" \ + --css ../doc/cups-printable.css \ + --header api-ppd.header --intro api-ppd.shtml \ + ppd.h attr.c conflicts.c custom.c emit.c localize.c mark.c \ + page.c ppd.c + mxmldoc --framed api-httpipp \ + --section "Programming" --title "HTTP and IPP APIs" \ + --css ../doc/cups-printable.css \ + --header api-httpipp.header --intro api-httpipp.shtml \ + http.h ipp.h auth.c getdevices.c getputfile.c encode.c \ + http.c http-addr.c http-support.c ipp.c ipp-support.c \ + md5passwd.c request.c + mxmldoc --framed api-filter \ + --section "Programming" \ + --title "Filter and Backend Programming" \ + --css ../doc/cups-printable.css \ + --header api-filter.header --intro api-filter.shtml \ + backchannel.c backend.h backend.c sidechannel.c sidechannel.h + + +# +# Lines of code computation... +# + +sloc: + echo "libcupslite: \c" + sloccount $(LITEOBJS:.o=.c) 2>/dev/null | grep "Total Physical" | awk '{print $$9}' + echo "libcups: \c" + sloccount $(LIBOBJS:.o=.c) 2>/dev/null | grep "Total Physical" | awk '{print $$9}' + + +# +# Dependencies... +# + +include Dependencies + + +# +# End of "$Id: Makefile 7871 2008-08-27 21:12:43Z mike $". +# diff --git a/cups/adminutil.c b/cups/adminutil.c new file mode 100644 index 0000000..440e443 --- /dev/null +++ b/cups/adminutil.c @@ -0,0 +1,2341 @@ +/* + * "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $" + * + * Administration utility API definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2001-2007 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. + * + * Contents: + * + * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer. + * cupsAdminExportSamba() - Export a printer to Samba. + * cupsAdminGetServerSettings() - Get settings from the server. + * cupsAdminSetServerSettings() - Set settings on the server. + * do_samba_command() - Do a SAMBA command. + * get_cupsd_conf() - Get the current cupsd.conf file. + * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings. + * write_option() - Write a CUPS option to a PPD file. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include "adminutil.h" +#include +#include +#ifdef WIN32 +#else +# include +# include +#endif /* WIN32 */ + + +/* + * Local functions... + */ + +static int do_samba_command(const char *command, + const char *address, + const char *subcommand, + const char *authfile, + FILE *logfile); +static http_status_t get_cupsd_conf(http_t *http, _cups_globals_t *cg, + time_t last_update, char *name, + int namelen, int *remote); +static void invalidate_cupsd_cache(_cups_globals_t *cg); +static void write_option(cups_file_t *dstfp, int order, + const char *name, const char *text, + const char *attrname, + ipp_attribute_t *suppattr, + ipp_attribute_t *defattr, int defval, + int valcount); + + +/* + * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer. + * + * @deprecated@ + */ + +char * /* O - PPD file or NULL */ +cupsAdminCreateWindowsPPD( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *dest, /* I - Printer or class */ + char *buffer, /* I - Filename buffer */ + int bufsize) /* I - Size of filename buffer */ +{ + const char *src; /* Source PPD filename */ + cups_file_t *srcfp, /* Source PPD file */ + *dstfp; /* Destination PPD file */ + ipp_t *request, /* IPP request */ + *response; /* IPP response */ + ipp_attribute_t *suppattr, /* IPP -supported attribute */ + *defattr; /* IPP -default attribute */ + cups_lang_t *language; /* Current language */ + char line[256], /* Line from PPD file */ + junk[256], /* Extra junk to throw away */ + *ptr, /* Pointer into line */ + uri[1024], /* Printer URI */ + option[41], /* Option */ + choice[41]; /* Choice */ + int jcloption, /* In a JCL option? */ + jclorder, /* Next JCL order dependency */ + linenum; /* Current line number */ + time_t curtime; /* Current time */ + struct tm *curdate; /* Current date */ + static const char * const pattrs[] = /* Printer attributes we want */ + { + "job-hold-until-supported", + "job-hold-until-default", + "job-sheets-supported", + "job-sheets-default", + "job-priority-supported", + "job-priority-default" + }; + + + /* + * Range check the input... + */ + + if (buffer) + *buffer = '\0'; + + if (!http) + http = _cupsConnect(); + + if (!http || !dest || !buffer || bufsize < 2) + return (NULL); + + /* + * Get the PPD file... + */ + + if ((src = cupsGetPPD2(http, dest)) == NULL) + return (NULL); + + /* + * Get the supported banner pages, etc. for the printer... + */ + + request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); + + httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, + "localhost", 0, "/printers/%s", dest); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, + "printer-uri", NULL, uri); + + ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), + NULL, pattrs); + + /* + * Do the request and get back a response... + */ + + response = cupsDoRequest(http, request, "/"); + if (!response || cupsLastError() > IPP_OK_CONFLICT) + { + unlink(src); + return (NULL); + } + + /* + * Open the original PPD file... + */ + + if ((srcfp = cupsFileOpen(src, "rb")) == NULL) + return (NULL); + + /* + * Create a temporary output file using the destination buffer... + */ + + if ((dstfp = cupsTempFile2(buffer, bufsize)) == NULL) + { + cupsFileClose(srcfp); + + unlink(src); + + return (NULL); + } + + /* + * Write a new header explaining that this isn't the original PPD... + */ + + cupsFilePuts(dstfp, "*PPD-Adobe: \"4.3\"\n"); + + curtime = time(NULL); + curdate = gmtime(&curtime); + + cupsFilePrintf(dstfp, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 " + "for CUPS Windows Driver\n", + curdate->tm_year + 1900, curdate->tm_mon + 1, curdate->tm_mday, + curdate->tm_hour, curdate->tm_min, curdate->tm_sec); + + /* + * Read the existing PPD file, converting all PJL commands to CUPS + * job ticket comments... + */ + + jcloption = 0; + jclorder = 0; + linenum = 0; + language = cupsLangDefault(); + + while (cupsFileGets(srcfp, line, sizeof(line))) + { + linenum ++; + + if (!strncmp(line, "*PPD-Adobe:", 11)) + { + /* + * Already wrote the PPD header... + */ + + continue; + } + else if (!strncmp(line, "*JCLBegin:", 10) || + !strncmp(line, "*JCLToPSInterpreter:", 20) || + !strncmp(line, "*JCLEnd:", 8) || + !strncmp(line, "*Protocols:", 11)) + { + /* + * Don't use existing JCL keywords; we'll create our own, below... + */ + + cupsFilePrintf(dstfp, "*%% Commented out for CUPS Windows Driver...\n" + "*%%%s\n", line + 1); + continue; + } + else if (!strncmp(line, "*JCLOpenUI", 10)) + { + jcloption = 1; + cupsFilePrintf(dstfp, "%s\n", line); + } + else if (!strncmp(line, "*JCLCloseUI", 11)) + { + jcloption = 0; + cupsFilePrintf(dstfp, "%s\n", line); + } + else if (jcloption && !strncmp(line, "*OrderDependency:", 17)) + { + for (ptr = line + 17; _cups_isspace(*ptr); ptr ++); + + ptr = strchr(ptr, ' '); + + if (ptr) + { + cupsFilePrintf(dstfp, "*OrderDependency: %d%s\n", jclorder, ptr); + jclorder ++; + } + else + cupsFilePrintf(dstfp, "%s\n", line); + } + else if (jcloption && + strncmp(line, "*End", 4) && + strncmp(line, "*Default", 8)) + { + if ((ptr = strchr(line, ':')) == NULL) + { + snprintf(line, sizeof(line), + _cupsLangString(language, _("Missing value on line %d.")), + linenum); + _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0); + + cupsFileClose(srcfp); + cupsFileClose(dstfp); + + unlink(src); + unlink(buffer); + + *buffer = '\0'; + + return (NULL); + } + + if ((ptr = strchr(ptr, '\"')) == NULL) + { + snprintf(line, sizeof(line), + _cupsLangString(language, + _("Missing double quote on line %d.")), + linenum); + _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0); + + cupsFileClose(srcfp); + cupsFileClose(dstfp); + + unlink(src); + unlink(buffer); + + *buffer = '\0'; + + return (NULL); + } + + if (sscanf(line, "*%40s%*[ \t]%40[^:/]", option, choice) != 2) + { + snprintf(line, sizeof(line), + _cupsLangString(language, + _("Bad option + choice on line %d.")), + linenum); + _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0); + + cupsFileClose(srcfp); + cupsFileClose(dstfp); + + unlink(src); + unlink(buffer); + + *buffer = '\0'; + + return (NULL); + } + + if (strchr(ptr + 1, '\"') == NULL) + { + /* + * Skip remaining... + */ + + while (cupsFileGets(srcfp, junk, sizeof(junk)) != NULL) + { + linenum ++; + + if (!strncmp(junk, "*End", 4)) + break; + } + } + + snprintf(ptr + 1, sizeof(line) - (ptr - line + 1), + "%%cupsJobTicket: %s=%s\n\"\n*End", option, choice); + + cupsFilePrintf(dstfp, "*%% Changed for CUPS Windows Driver...\n%s\n", + line); + } + else + cupsFilePrintf(dstfp, "%s\n", line); + } + + cupsFileClose(srcfp); + unlink(src); + + if (linenum == 0) + { + _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, _("Empty PPD file."), 1); + + cupsFileClose(dstfp); + unlink(buffer); + + *buffer = '\0'; + + return (NULL); + } + + /* + * Now add the CUPS-specific attributes and options... + */ + + cupsFilePuts(dstfp, "\n*% CUPS Job Ticket support and options...\n"); + cupsFilePuts(dstfp, "*Protocols: PJL\n"); + cupsFilePuts(dstfp, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n"); + cupsFilePuts(dstfp, "*JCLToPSInterpreter: \"\"\n"); + cupsFilePuts(dstfp, "*JCLEnd: \"\"\n"); + + cupsFilePuts(dstfp, "\n*OpenGroup: CUPS/CUPS Options\n\n"); + + if ((defattr = ippFindAttribute(response, "job-hold-until-default", + IPP_TAG_ZERO)) != NULL && + (suppattr = ippFindAttribute(response, "job-hold-until-supported", + IPP_TAG_ZERO)) != NULL) + write_option(dstfp, jclorder ++, "cupsJobHoldUntil", "Hold Until", + "job-hold-until", suppattr, defattr, 0, 1); + + if ((defattr = ippFindAttribute(response, "job-priority-default", + IPP_TAG_INTEGER)) != NULL && + (suppattr = ippFindAttribute(response, "job-priority-supported", + IPP_TAG_RANGE)) != NULL) + write_option(dstfp, jclorder ++, "cupsJobPriority", "Priority", + "job-priority", suppattr, defattr, 0, 1); + + if ((defattr = ippFindAttribute(response, "job-sheets-default", + IPP_TAG_ZERO)) != NULL && + (suppattr = ippFindAttribute(response, "job-sheets-supported", + IPP_TAG_ZERO)) != NULL) + { + write_option(dstfp, jclorder ++, "cupsJobSheetsStart", "Start Banner", + "job-sheets", suppattr, defattr, 0, 2); + write_option(dstfp, jclorder, "cupsJobSheetsEnd", "End Banner", + "job-sheets", suppattr, defattr, 1, 2); + } + + cupsFilePuts(dstfp, "*CloseGroup: CUPS\n"); + cupsFileClose(dstfp); + + ippDelete(response); + + return (buffer); +} + + +/* + * 'cupsAdminExportSamba()' - Export a printer to Samba. + * + * @deprecated@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsAdminExportSamba( + const char *dest, /* I - Destination to export */ + const char *ppd, /* I - PPD file */ + const char *samba_server, /* I - Samba server */ + const char *samba_user, /* I - Samba username */ + const char *samba_password, /* I - Samba password */ + FILE *logfile) /* I - Log file, if any */ +{ + int status; /* Status of Samba commands */ + int have_drivers; /* Have drivers? */ + char file[1024], /* File to test for */ + authfile[1024], /* Temporary authentication file */ + address[1024], /* Address for command */ + subcmd[1024], /* Sub-command */ + message[1024]; /* Error message */ + cups_file_t *fp; /* Authentication file */ + cups_lang_t *language; /* Current language */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + /* + * Range check input... + */ + + if (!dest || !ppd || !samba_server || !samba_user || !samba_password) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (0); + } + + /* + * Create a temporary authentication file for Samba... + */ + + if ((fp = cupsTempFile2(authfile, sizeof(authfile))) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + return (0); + } + + cupsFilePrintf(fp, "username = %s\n", samba_user); + cupsFilePrintf(fp, "password = %s\n", samba_password); + cupsFileClose(fp); + + /* + * See which drivers are available; the new CUPS v6 and Adobe drivers + * depend on the Windows 2k PS driver, so copy that driver first: + * + * Files: + * + * ps5ui.dll + * pscript.hlp + * pscript.ntf + * pscript5.dll + */ + + have_drivers = 0; + language = cupsLangDefault(); + + snprintf(file, sizeof(file), "%s/drivers/pscript5.dll", cg->cups_datadir); + if (!access(file, 0)) + { + have_drivers |= 1; + + /* + * Windows 2k driver is installed; do the smbclient commands needed + * to copy the Win2k drivers over... + */ + + snprintf(address, sizeof(address), "//%s/print$", samba_server); + + snprintf(subcmd, sizeof(subcmd), + "mkdir W32X86;" + "put %s W32X86/%s.ppd;" + "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;" + "put %s/drivers/pscript.hlp W32X86/pscript.hlp;" + "put %s/drivers/pscript.ntf W32X86/pscript.ntf;" + "put %s/drivers/pscript5.dll W32X86/pscript5.dll", + ppd, dest, cg->cups_datadir, cg->cups_datadir, + cg->cups_datadir, cg->cups_datadir); + + if ((status = do_samba_command("smbclient", address, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to copy Windows 2000 printer " + "driver files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + + /* + * See if we also have the CUPS driver files; if so, use them! + */ + + snprintf(file, sizeof(file), "%s/drivers/cupsps6.dll", cg->cups_datadir); + if (!access(file, 0)) + { + /* + * Copy the CUPS driver files over... + */ + + snprintf(subcmd, sizeof(subcmd), + "put %s/drivers/cups6.ini W32X86/cups6.ini;" + "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;" + "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll", + cg->cups_datadir, cg->cups_datadir, cg->cups_datadir); + + if ((status = do_samba_command("smbclient", address, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to copy CUPS printer driver " + "files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + + /* + * Do the rpcclient command needed for the CUPS drivers... + */ + + snprintf(subcmd, sizeof(subcmd), + "adddriver \"Windows NT x86\" \"%s:" + "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:" + "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf," + "cups6.ini,cupsps6.dll,cupsui6.dll\"", + dest, dest, dest); + } + else + { + /* + * Don't have the CUPS drivers, so just use the standard Windows + * drivers... + */ + + snprintf(subcmd, sizeof(subcmd), + "adddriver \"Windows NT x86\" \"%s:" + "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:" + "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"", + dest, dest, dest); + } + + if ((status = do_samba_command("rpcclient", samba_server, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to install Windows 2000 printer " + "driver files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + } + + /* + * See if we have the Win9x PS driver... + */ + + snprintf(file, sizeof(file), "%s/drivers/ADOBEPS4.DRV", cg->cups_datadir); + if (!access(file, 0)) + { + have_drivers |= 2; + + /* + * Do the smbclient commands needed for the Adobe Win9x drivers... + */ + + snprintf(address, sizeof(address), "//%s/print$", samba_server); + + snprintf(subcmd, sizeof(subcmd), + "mkdir WIN40;" + "put %s WIN40/%s.PPD;" + "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;" + "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;" + "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;" + "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;" + "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;", + ppd, dest, cg->cups_datadir, cg->cups_datadir, + cg->cups_datadir, cg->cups_datadir, cg->cups_datadir); + + if ((status = do_samba_command("smbclient", address, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to copy Windows 9x printer " + "driver files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + + /* + * Do the rpcclient commands needed for the Adobe Win9x drivers... + */ + + snprintf(subcmd, sizeof(subcmd), + "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:" + "ADOBEPS4.HLP:PSMON.DLL:RAW:" + "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM," + "ICONLIB.DLL\"", + dest, dest, dest); + + if ((status = do_samba_command("rpcclient", samba_server, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to install Windows 9x printer " + "driver files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + } + + /* + * See if we have the 64-bit Windows PS driver... + * + * Files: + * + * x64/ps5ui.dll + * x64/pscript.hlp + * x64/pscript.ntf + * x64/pscript5.dll + */ + + snprintf(file, sizeof(file), "%s/drivers/x64/pscript5.dll", cg->cups_datadir); + if (!access(file, 0)) + { + have_drivers |= 4; + + /* + * 64-bit Windows driver is installed; do the smbclient commands needed + * to copy the Win64 drivers over... + */ + + snprintf(address, sizeof(address), "//%s/print$", samba_server); + + snprintf(subcmd, sizeof(subcmd), + "mkdir x64;" + "put %s x64/%s.ppd;" + "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;" + "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;" + "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;" + "put %s/drivers/x64/pscript5.dll x64/pscript5.dll", + ppd, dest, cg->cups_datadir, cg->cups_datadir, + cg->cups_datadir, cg->cups_datadir); + + if ((status = do_samba_command("smbclient", address, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to copy 64-bit Windows printer " + "driver files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + + /* + * See if we also have the CUPS driver files; if so, use them! + */ + + snprintf(file, sizeof(file), "%s/drivers/x64/cupsps6.dll", cg->cups_datadir); + if (!access(file, 0)) + { + /* + * Copy the CUPS driver files over... + */ + + snprintf(subcmd, sizeof(subcmd), + "put %s/drivers/x64/cups6.ini x64/cups6.ini;" + "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;" + "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll", + cg->cups_datadir, cg->cups_datadir, cg->cups_datadir); + + if ((status = do_samba_command("smbclient", address, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to copy 64-bit CUPS printer driver " + "files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + + /* + * Do the rpcclient command needed for the CUPS drivers... + */ + + snprintf(subcmd, sizeof(subcmd), + "adddriver \"Windows x64\" \"%s:" + "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:" + "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf," + "cups6.ini,cupsps6.dll,cupsui6.dll\"", + dest, dest, dest); + } + else + { + /* + * Don't have the CUPS drivers, so just use the standard Windows + * drivers... + */ + + snprintf(subcmd, sizeof(subcmd), + "adddriver \"Windows x64\" \"%s:" + "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:" + "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"", + dest, dest, dest); + } + + if ((status = do_samba_command("rpcclient", samba_server, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to install Windows 2000 printer " + "driver files (%d).")), status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + } + + if (logfile && !(have_drivers & 1)) + { + if (!have_drivers) + strlcpy(message, + _cupsLangString(language, + _("No Windows printer drivers are installed.")), + sizeof(message)); + else + strlcpy(message, + _cupsLangString(language, + _("Warning, no Windows 2000 printer drivers " + "are installed.")), + sizeof(message)); + + _cupsSetError(IPP_NOT_FOUND, message, 0); + _cupsLangPuts(logfile, message); + } + + if (have_drivers == 0) + { + _cupsSetError(IPP_NOT_FOUND, message, 0); + + unlink(authfile); + + return (0); + } + + /* + * Finally, associate the drivers we just added with the queue... + */ + + snprintf(subcmd, sizeof(subcmd), "setdriver %s %s", dest, dest); + + if ((status = do_samba_command("rpcclient", samba_server, subcmd, + authfile, logfile)) != 0) + { + snprintf(message, sizeof(message), + _cupsLangString(language, + _("Unable to set Windows printer driver (%d).")), + status); + + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + if (logfile) + _cupsLangPuts(logfile, message); + + unlink(authfile); + + return (0); + } + + unlink(authfile); + + return (1); +} + + +/* + * 'cupsAdminGetServerSettings()' - Get settings from the server. + * + * The returned settings should be freed with cupsFreeOptions() when + * you are done with them. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsAdminGetServerSettings( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + int *num_settings, /* O - Number of settings */ + cups_option_t **settings) /* O - Settings */ +{ + int i; /* Looping var */ + cups_file_t *cupsd; /* cupsd.conf file */ + char cupsdconf[1024]; /* cupsd.conf filename */ + int remote; /* Remote cupsd.conf file? */ + http_status_t status; /* Status of getting cupsd.conf */ + char line[1024], /* Line from cupsd.conf file */ + *value; /* Value on line */ + cups_option_t *setting; /* Current setting */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + /* + * Range check input... + */ + + if (!http) + { + /* + * See if we are connected to the same server... + */ + + if (cg->http) + { + /* + * Compare the connection hostname, port, and encryption settings to + * the cached defaults; these were initialized the first time we + * connected... + */ + + if (strcmp(cg->http->hostname, cg->server) || + cg->ipp_port != _httpAddrPort(cg->http->hostaddr) || + (cg->http->encryption != cg->encryption && + cg->http->encryption == HTTP_ENCRYPT_NEVER)) + { + /* + * Need to close the current connection because something has changed... + */ + + httpClose(cg->http); + cg->http = NULL; + } + } + + /* + * (Re)connect as needed... + */ + + if (!cg->http) + { + if ((cg->http = _httpCreate(cupsServer(), ippPort(), NULL, + cupsEncryption(), AF_UNSPEC)) == NULL) + { + if (errno) + _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); + else + _cupsSetError(IPP_SERVICE_UNAVAILABLE, + _("Unable to connect to host."), 1); + + if (num_settings) + *num_settings = 0; + + if (settings) + *settings = NULL; + + return (0); + } + } + + http = cg->http; + } + + if (!http || !num_settings || !settings) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + if (num_settings) + *num_settings = 0; + + if (settings) + *settings = NULL; + + return (0); + } + + *num_settings = 0; + *settings = NULL; + + /* + * Get the cupsd.conf file... + */ + + if ((status = get_cupsd_conf(http, cg, cg->cupsd_update, cupsdconf, + sizeof(cupsdconf), &remote)) == HTTP_OK) + { + if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL) + { + char message[1024]; /* Message string */ + + + snprintf(message, sizeof(message), + _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")), + cupsdconf, strerror(errno)); + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + } + } + else + cupsd = NULL; + + if (cupsd) + { + /* + * Read the file, keeping track of what settings are enabled... + */ + + int remote_access = 0, /* Remote access allowed? */ + remote_admin = 0, /* Remote administration allowed? */ + remote_any = 0, /* Remote access from anywhere allowed? */ + browsing = 1, /* Browsing enabled? */ + cancel_policy = 1, /* Cancel-job policy set? */ + debug_logging = 0; /* LogLevel debug set? */ + int linenum = 0, /* Line number in file */ + in_location = 0, /* In a location section? */ + in_policy = 0, /* In a policy section? */ + in_cancel_job = 0, /* In a cancel-job section? */ + in_admin_location = 0; /* In the /admin location? */ + + + invalidate_cupsd_cache(cg); + + cg->cupsd_update = time(NULL); + httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname)); + + while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum)) + { + if (!value && strncmp(line, "")) + { + in_policy = 0; + } + else if (!_cups_strcasecmp(line, "")) + { + in_cancel_job = 0; + } + else if (!_cups_strcasecmp(line, "Require") && in_cancel_job) + { + cancel_policy = 0; + } + else if (!_cups_strcasecmp(line, "")) + { + in_admin_location = 0; + in_location = 0; + } + else if (!_cups_strcasecmp(line, "Allow") && value && + _cups_strcasecmp(value, "localhost") && + _cups_strcasecmp(value, "127.0.0.1") +#ifdef AF_LOCAL + && *value != '/' +#endif /* AF_LOCAL */ +#ifdef AF_INET6 + && strcmp(value, "::1") +#endif /* AF_INET6 */ + ) + { + if (in_admin_location) + remote_admin = 1; + else if (!_cups_strcasecmp(value, "all")) + remote_any = 1; + } + else if (line[0] != '<' && !in_location && !in_policy && + _cups_strcasecmp(line, "Allow") && + _cups_strcasecmp(line, "AuthType") && + _cups_strcasecmp(line, "Deny") && + _cups_strcasecmp(line, "Order") && + _cups_strcasecmp(line, "Require") && + _cups_strcasecmp(line, "Satisfy")) + cg->cupsd_num_settings = cupsAddOption(line, value, + cg->cupsd_num_settings, + &(cg->cupsd_settings)); + } + + cupsFileClose(cupsd); + + cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, + debug_logging ? "1" : "0", + cg->cupsd_num_settings, + &(cg->cupsd_settings)); + + cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, + (remote_access && remote_admin) ? + "1" : "0", + cg->cupsd_num_settings, + &(cg->cupsd_settings)); + + cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY, + remote_any ? "1" : "0", + cg->cupsd_num_settings, + &(cg->cupsd_settings)); + + cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, + (remote_access && browsing) ? "1" : + "0", + cg->cupsd_num_settings, + &(cg->cupsd_settings)); + + cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, + cancel_policy ? "1" : "0", + cg->cupsd_num_settings, + &(cg->cupsd_settings)); + } + else if (status != HTTP_NOT_MODIFIED) + invalidate_cupsd_cache(cg); + + /* + * Remove any temporary files and copy the settings array... + */ + + if (remote) + unlink(cupsdconf); + + for (i = cg->cupsd_num_settings, setting = cg->cupsd_settings; + i > 0; + i --, setting ++) + *num_settings = cupsAddOption(setting->name, setting->value, + *num_settings, settings); + + return (cg->cupsd_num_settings > 0); +} + + +/* + * 'cupsAdminSetServerSettings()' - Set settings on the server. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsAdminSetServerSettings( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + int num_settings, /* I - Number of settings */ + cups_option_t *settings) /* I - Settings */ +{ + int i; /* Looping var */ + http_status_t status; /* GET/PUT status */ + const char *server_port_env; /* SERVER_PORT env var */ + int server_port; /* IPP port for server */ + cups_file_t *cupsd; /* cupsd.conf file */ + char cupsdconf[1024]; /* cupsd.conf filename */ + int remote; /* Remote cupsd.conf file? */ + char tempfile[1024]; /* Temporary new cupsd.conf */ + cups_file_t *temp; /* Temporary file */ + char line[1024], /* Line from cupsd.conf file */ + *value; /* Value on line */ + int linenum, /* Line number in file */ + in_location, /* In a location section? */ + in_policy, /* In a policy section? */ + in_default_policy, /* In the default policy section? */ + in_cancel_job, /* In a cancel-job section? */ + in_admin_location, /* In the /admin location? */ + in_conf_location, /* In the /admin/conf location? */ + in_root_location; /* In the / location? */ + const char *val; /* Setting value */ + int share_printers, /* Share local printers */ + remote_admin, /* Remote administration allowed? */ + remote_any, /* Remote access from anywhere? */ + user_cancel_any, /* Cancel-job policy set? */ + debug_logging; /* LogLevel debug set? */ + int wrote_port_listen, /* Wrote the port/listen lines? */ + wrote_browsing, /* Wrote the browsing lines? */ + wrote_policy, /* Wrote the policy? */ + wrote_loglevel, /* Wrote the LogLevel line? */ + wrote_admin_location, /* Wrote the /admin location? */ + wrote_conf_location, /* Wrote the /admin/conf location? */ + wrote_root_location; /* Wrote the / location? */ + int indent; /* Indentation */ + int cupsd_num_settings; /* New number of settings */ + int old_share_printers, /* Share local printers */ + old_remote_admin, /* Remote administration allowed? */ + old_user_cancel_any, /* Cancel-job policy set? */ + old_debug_logging; /* LogLevel debug set? */ + cups_option_t *cupsd_settings, /* New settings */ + *setting; /* Current setting */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + /* + * Range check input... + */ + + if (!http) + http = _cupsConnect(); + + if (!http || !num_settings || !settings) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + return (0); + } + + /* + * Get the cupsd.conf file... + */ + + if (get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf), + &remote) == HTTP_OK) + { + if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + return (0); + } + } + else + return (0); + + /* + * Get current settings... + */ + + if (!cupsAdminGetServerSettings(http, &cupsd_num_settings, + &cupsd_settings)) + return (0); + + if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, cupsd_num_settings, + cupsd_settings)) != NULL) + old_debug_logging = atoi(val); + else + old_debug_logging = 0; + + DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d", + old_debug_logging)); + + if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, cupsd_num_settings, + cupsd_settings)) != NULL) + old_remote_admin = atoi(val); + else + old_remote_admin = 0; + + DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d", + old_remote_admin)); + + if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings, + cupsd_settings)) != NULL) + remote_any = atoi(val); + else + remote_any = 0; + + DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d", + remote_any)); + + if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, cupsd_num_settings, + cupsd_settings)) != NULL) + old_share_printers = atoi(val); + else + old_share_printers = 0; + + DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d", + old_share_printers)); + + if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, cupsd_num_settings, + cupsd_settings)) != NULL) + old_user_cancel_any = atoi(val); + else + old_user_cancel_any = 0; + + DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d", + old_user_cancel_any)); + + cupsFreeOptions(cupsd_num_settings, cupsd_settings); + + /* + * Get basic settings... + */ + + if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings, + settings)) != NULL) + { + debug_logging = atoi(val); + + if (debug_logging == old_debug_logging) + { + /* + * No change to this setting... + */ + + debug_logging = -1; + } + } + else + debug_logging = -1; + + DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d", + debug_logging)); + + if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings, + settings)) != NULL) + remote_any = atoi(val); + + DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d", + remote_any)); + + if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings, + settings)) != NULL) + { + remote_admin = atoi(val); + + if (remote_admin == old_remote_admin) + { + /* + * No change to this setting... + */ + + remote_admin = -1; + } + } + else + remote_admin = -1; + + DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d", + remote_admin)); + + if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings, + settings)) != NULL) + { + share_printers = atoi(val); + + if (share_printers == old_share_printers) + { + /* + * No change to this setting... + */ + + share_printers = -1; + } + } + else + share_printers = -1; + + DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d", + share_printers)); + + if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings, + settings)) != NULL) + { + user_cancel_any = atoi(val); + + if (user_cancel_any == old_user_cancel_any) + { + /* + * No change to this setting... + */ + + user_cancel_any = -1; + } + } + else + user_cancel_any = -1; + + DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d", + user_cancel_any)); + + /* + * Create a temporary file for the new cupsd.conf file... + */ + + if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL) + { + cupsFileClose(cupsd); + + if (remote) + unlink(cupsdconf); + + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + return (0); + } + + /* + * Copy the old file to the new, making changes along the way... + */ + + cupsd_num_settings = 0; + in_admin_location = 0; + in_cancel_job = 0; + in_conf_location = 0; + in_default_policy = 0; + in_location = 0; + in_policy = 0; + in_root_location = 0; + linenum = 0; + wrote_admin_location = 0; + wrote_browsing = 0; + wrote_conf_location = 0; + wrote_loglevel = 0; + wrote_policy = 0; + wrote_port_listen = 0; + wrote_root_location = 0; + indent = 0; + + if ((server_port_env = getenv("SERVER_PORT")) != NULL) + { + if ((server_port = atoi(server_port_env)) <= 0) + server_port = ippPort(); + } + else + server_port = ippPort(); + + if (server_port <= 0) + server_port = IPP_PORT; + + while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum)) + { + if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && + (remote_admin >= 0 || remote_any > 0 || share_printers >= 0)) + { + if (!wrote_port_listen) + { + wrote_port_listen = 1; + + if (remote_admin > 0 || remote_any > 0 || share_printers > 0) + { + cupsFilePuts(temp, "# Allow remote access\n"); + cupsFilePrintf(temp, "Port %d\n", server_port); + } + else + { + cupsFilePuts(temp, "# Only listen for connections from the local " + "machine.\n"); + cupsFilePrintf(temp, "Listen localhost:%d\n", server_port); + } + +#ifdef CUPS_DEFAULT_DOMAINSOCKET + if ((!value || strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)) && + !access(CUPS_DEFAULT_DOMAINSOCKET, 0)) + cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n"); +#endif /* CUPS_DEFAULT_DOMAINSOCKET */ + } + else if (value && value[0] == '/' +#ifdef CUPS_DEFAULT_DOMAINSOCKET + && strcmp(CUPS_DEFAULT_DOMAINSOCKET, value) +#endif /* CUPS_DEFAULT_DOMAINSOCKET */ + ) + cupsFilePrintf(temp, "Listen %s\n", value); + } + else if ((!_cups_strcasecmp(line, "Browsing") || + !_cups_strcasecmp(line, "BrowseLocalProtocols")) && + share_printers >= 0) + { + if (!wrote_browsing) + { + int new_share_printers = (share_printers > 0 || + (share_printers == -1 && + old_share_printers > 0)); + + wrote_browsing = 1; + + if (new_share_printers) + { + const char *localp = cupsGetOption("BrowseLocalProtocols", + num_settings, settings); + + if (!localp || !localp[0]) + localp = cupsGetOption("BrowseLocalProtocols", cupsd_num_settings, + cupsd_settings); + + cupsFilePuts(temp, "# Share local printers on the local network.\n"); + cupsFilePuts(temp, "Browsing On\n"); + + if (!localp) + localp = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS; + + cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp); + + cupsd_num_settings = cupsAddOption("BrowseLocalProtocols", localp, + cupsd_num_settings, + &cupsd_settings); + } + else + { + cupsFilePuts(temp, "# Disable printer sharing.\n"); + cupsFilePuts(temp, "Browsing Off\n"); + } + } + } + else if (!_cups_strcasecmp(line, "LogLevel") && debug_logging >= 0) + { + wrote_loglevel = 1; + + if (debug_logging) + { + cupsFilePuts(temp, + "# Show troubleshooting information in error_log.\n"); + cupsFilePuts(temp, "LogLevel debug\n"); + } + else + { + cupsFilePuts(temp, "# Show general information in error_log.\n"); + cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n"); + } + } + else if (!_cups_strcasecmp(line, "\n", line, value); + indent += 2; + } + else if (!_cups_strcasecmp(line, "")) + { + indent -= 2; + if (!wrote_policy && in_default_policy) + { + wrote_policy = 1; + + if (!user_cancel_any) + cupsFilePuts(temp, " # Only the owner or an administrator can " + "cancel a job...\n" + " \n" + " Order deny,allow\n" + " Require user @OWNER " + CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n" + " \n"); + } + + in_policy = 0; + in_default_policy = 0; + + cupsFilePuts(temp, "\n"); + } + else if (!_cups_strcasecmp(line, "\n", line, value); + } + else if (!_cups_strcasecmp(line, "")) + { + in_location = 0; + indent -= 2; + if (in_admin_location && remote_admin >= 0) + { + wrote_admin_location = 1; + + if (remote_admin) + cupsFilePuts(temp, " # Allow remote administration...\n"); + else if (remote_admin == 0) + cupsFilePuts(temp, " # Restrict access to the admin pages...\n"); + + cupsFilePuts(temp, " Order allow,deny\n"); + + if (remote_admin) + cupsFilePrintf(temp, " Allow %s\n", + remote_any > 0 ? "all" : "@LOCAL"); + } + else if (in_conf_location && remote_admin >= 0) + { + wrote_conf_location = 1; + + if (remote_admin) + cupsFilePuts(temp, " # Allow remote access to the configuration " + "files...\n"); + else + cupsFilePuts(temp, " # Restrict access to the configuration " + "files...\n"); + + cupsFilePuts(temp, " Order allow,deny\n"); + + if (remote_admin) + cupsFilePrintf(temp, " Allow %s\n", + remote_any > 0 ? "all" : "@LOCAL"); + } + else if (in_root_location && + (remote_admin >= 0 || remote_any > 0 || share_printers >= 0)) + { + wrote_root_location = 1; + + if (remote_admin > 0 && share_printers > 0) + cupsFilePuts(temp, " # Allow shared printing and remote " + "administration...\n"); + else if (remote_admin > 0) + cupsFilePuts(temp, " # Allow remote administration...\n"); + else if (share_printers > 0) + cupsFilePuts(temp, " # Allow shared printing...\n"); + else if (remote_any > 0) + cupsFilePuts(temp, " # Allow remote access...\n"); + else + cupsFilePuts(temp, " # Restrict access to the server...\n"); + + cupsFilePuts(temp, " Order allow,deny\n"); + + if (remote_admin > 0 || remote_any > 0 || share_printers > 0) + cupsFilePrintf(temp, " Allow %s\n", + remote_any > 0 ? "all" : "@LOCAL"); + } + + in_admin_location = 0; + in_conf_location = 0; + in_root_location = 0; + + cupsFilePuts(temp, "\n"); + } + else if (!_cups_strcasecmp(line, "= 0) + { + /* + * Don't write anything for this limit section... + */ + + in_cancel_job = 2; + } + else + { + cupsFilePrintf(temp, "%*s%s", indent, "", line); + + while (*value) + { + for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++); + + if (*valptr) + *valptr++ = '\0'; + + if (!_cups_strcasecmp(value, "cancel-job") && user_cancel_any >= 0) + { + /* + * Write everything except for this definition... + */ + + in_cancel_job = 1; + } + else + cupsFilePrintf(temp, " %s", value); + + for (value = valptr; _cups_isspace(*value); value ++); + } + + cupsFilePuts(temp, ">\n"); + } + } + else + cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value); + + indent += 2; + } + else if (!_cups_strcasecmp(line, "") && in_cancel_job) + { + indent -= 2; + + if (in_cancel_job == 1) + cupsFilePuts(temp, " \n"); + + wrote_policy = 1; + + if (!user_cancel_any) + cupsFilePuts(temp, " # Only the owner or an administrator can cancel " + "a job...\n" + " \n" + " Order deny,allow\n" + " Require user @OWNER " + CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n" + " \n"); + + in_cancel_job = 0; + } + else if ((((in_admin_location || in_conf_location || in_root_location) && + (remote_admin >= 0 || remote_any > 0)) || + (in_root_location && share_printers >= 0)) && + (!_cups_strcasecmp(line, "Allow") || !_cups_strcasecmp(line, "Deny") || + !_cups_strcasecmp(line, "Order"))) + continue; + else if (in_cancel_job == 2) + continue; + else if (line[0] == '<') + { + if (value) + { + cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value); + indent += 2; + } + else + { + if (line[1] == '/') + indent -= 2; + + cupsFilePrintf(temp, "%*s%s\n", indent, "", line); + } + } + else if (!in_policy && !in_location && + (val = cupsGetOption(line, num_settings, settings)) != NULL) + { + /* + * Replace this directive's value with the new one... + */ + + cupsd_num_settings = cupsAddOption(line, val, cupsd_num_settings, + &cupsd_settings); + + /* + * Write the new value in its place, without indentation since we + * only support setting root directives, not in sections... + */ + + cupsFilePrintf(temp, "%s %s\n", line, val); + } + else if (value) + { + if (!in_policy && !in_location) + { + /* + * Record the non-policy, non-location directives that we find + * in the server settings, since we cache this info and record it + * in cupsAdminGetServerSettings()... + */ + + cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings, + &cupsd_settings); + } + + cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value); + } + else + cupsFilePrintf(temp, "%*s%s\n", indent, "", line); + } + + /* + * Write any missing info... + */ + + if (!wrote_browsing && share_printers >= 0) + { + if (share_printers > 0) + { + cupsFilePuts(temp, "# Share local printers on the local network.\n"); + cupsFilePuts(temp, "Browsing On\n"); + } + else + { + cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n"); + cupsFilePuts(temp, "Browsing Off\n"); + } + } + + if (!wrote_loglevel && debug_logging >= 0) + { + if (debug_logging) + { + cupsFilePuts(temp, "# Show troubleshooting information in error_log.\n"); + cupsFilePuts(temp, "LogLevel debug\n"); + } + else + { + cupsFilePuts(temp, "# Show general information in error_log.\n"); + cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n"); + } + } + + if (!wrote_port_listen && + (remote_admin >= 0 || remote_any > 0 || share_printers >= 0)) + { + if (remote_admin > 0 || remote_any > 0 || share_printers > 0) + { + cupsFilePuts(temp, "# Allow remote access\n"); + cupsFilePrintf(temp, "Port %d\n", ippPort()); + } + else + { + cupsFilePuts(temp, + "# Only listen for connections from the local machine.\n"); + cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort()); + } + +#ifdef CUPS_DEFAULT_DOMAINSOCKET + if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0)) + cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n"); +#endif /* CUPS_DEFAULT_DOMAINSOCKET */ + } + + if (!wrote_root_location && + (remote_admin >= 0 || remote_any > 0 || share_printers >= 0)) + { + if (remote_admin > 0 && share_printers > 0) + cupsFilePuts(temp, + "# Allow shared printing and remote administration...\n"); + else if (remote_admin > 0) + cupsFilePuts(temp, "# Allow remote administration...\n"); + else if (share_printers > 0) + cupsFilePuts(temp, "# Allow shared printing...\n"); + else if (remote_any > 0) + cupsFilePuts(temp, "# Allow remote access...\n"); + else + cupsFilePuts(temp, "# Restrict access to the server...\n"); + + cupsFilePuts(temp, "\n" + " Order allow,deny\n"); + + if (remote_admin > 0 || remote_any > 0 || share_printers > 0) + cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL"); + + cupsFilePuts(temp, "\n"); + } + + if (!wrote_admin_location && remote_admin >= 0) + { + if (remote_admin) + cupsFilePuts(temp, "# Allow remote administration...\n"); + else + cupsFilePuts(temp, "# Restrict access to the admin pages...\n"); + + cupsFilePuts(temp, "\n" + " Order allow,deny\n"); + + if (remote_admin) + cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL"); + + cupsFilePuts(temp, "\n"); + } + + if (!wrote_conf_location && remote_admin >= 0) + { + if (remote_admin) + cupsFilePuts(temp, + "# Allow remote access to the configuration files...\n"); + else + cupsFilePuts(temp, "# Restrict access to the configuration files...\n"); + + cupsFilePuts(temp, "\n" + " AuthType Default\n" + " Require user @SYSTEM\n" + " Order allow,deny\n"); + + if (remote_admin) + cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL"); + + cupsFilePuts(temp, "\n"); + } + + if (!wrote_policy && user_cancel_any >= 0) + { + cupsFilePuts(temp, "\n" + " # Job-related operations must be done by the owner " + "or an administrator...\n" + " \n" + " Require user @OWNER @SYSTEM\n" + " Order deny,allow\n" + " \n" + " # All administration operations require an " + "administrator to authenticate...\n" + " \n" + " AuthType Default\n" + " Require user @SYSTEM\n" + " Order deny,allow\n" + "\n"); + + if (!user_cancel_any) + cupsFilePuts(temp, " # Only the owner or an administrator can cancel " + "a job...\n" + " \n" + " Order deny,allow\n" + " Require user @OWNER " + CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n" + " \n"); + + cupsFilePuts(temp, " \n" + " Order deny,allow\n" + " \n" + "\n"); + } + + for (i = num_settings, setting = settings; i > 0; i --, setting ++) + if (setting->name[0] != '_' && + _cups_strcasecmp(setting->name, "Listen") && + _cups_strcasecmp(setting->name, "Port") && + !cupsGetOption(setting->name, cupsd_num_settings, cupsd_settings)) + { + /* + * Add this directive to the list of directives we have written... + */ + + cupsd_num_settings = cupsAddOption(setting->name, setting->value, + cupsd_num_settings, &cupsd_settings); + + /* + * Write the new value, without indentation since we only support + * setting root directives, not in sections... + */ + + cupsFilePrintf(temp, "%s %s\n", setting->name, setting->value); + } + + cupsFileClose(cupsd); + cupsFileClose(temp); + + /* + * Upload the configuration file to the server... + */ + + status = cupsPutFile(http, "/admin/conf/cupsd.conf", tempfile); + + if (status == HTTP_CREATED) + { + /* + * Updated OK, add the basic settings... + */ + + if (debug_logging >= 0) + cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, + debug_logging ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + else + cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, + old_debug_logging ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + + if (remote_admin >= 0) + cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, + remote_admin ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + else + cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, + old_remote_admin ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + + cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY, + remote_any ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + + if (share_printers >= 0) + cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, + share_printers ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + else + cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, + old_share_printers ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + + if (user_cancel_any >= 0) + cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, + user_cancel_any ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + else + cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, + old_user_cancel_any ? "1" : "0", + cupsd_num_settings, &cupsd_settings); + + /* + * Save the new values... + */ + + invalidate_cupsd_cache(cg); + + cg->cupsd_num_settings = cupsd_num_settings; + cg->cupsd_settings = cupsd_settings; + cg->cupsd_update = time(NULL); + + httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname)); + } + else + cupsFreeOptions(cupsd_num_settings, cupsd_settings); + + /* + * Remote our temp files and return... + */ + + if (remote) + unlink(cupsdconf); + + unlink(tempfile); + + return (status == HTTP_CREATED); +} + + +/* + * 'do_samba_command()' - Do a SAMBA command. + */ + +static int /* O - Status of command */ +do_samba_command(const char *command, /* I - Command to run */ + const char *address, /* I - Address for command */ + const char *subcmd, /* I - Sub-command */ + const char *authfile, /* I - Samba authentication file */ + FILE *logfile) /* I - Optional log file */ +{ +#ifdef WIN32 + return (1); /* Always fail on Windows... */ + +#else + int status; /* Status of command */ + int pid; /* Process ID of child */ + + + if (logfile) + _cupsLangPrintf(logfile, + _("Running command: %s %s -N -A %s -c \'%s\'"), + command, address, authfile, subcmd); + + if ((pid = fork()) == 0) + { + /* + * Child goes here, redirect stdin/out/err and execute the command... + */ + + int fd = open("/dev/null", O_RDONLY); + + if (fd > 0) + { + dup2(fd, 0); + close(fd); + } + + if (logfile) + dup2(fileno(logfile), 1); + else if ((fd = open("/dev/null", O_WRONLY)) > 1) + { + dup2(fd, 1); + close(fd); + } + + dup2(1, 2); + + execlp(command, command, address, "-N", "-A", authfile, "-c", subcmd, + (char *)0); + exit(errno); + } + else if (pid < 0) + { + status = -1; + + if (logfile) + _cupsLangPrintf(logfile, _("Unable to run \"%s\": %s"), + command, strerror(errno)); + } + else + { + /* + * Wait for the process to complete... + */ + + while (wait(&status) != pid); + } + + if (logfile) + _cupsLangPuts(logfile, ""); + + DEBUG_printf(("9do_samba_command: status=%d", status)); + + if (WIFEXITED(status)) + return (WEXITSTATUS(status)); + else + return (-WTERMSIG(status)); +#endif /* WIN32 */ +} + + +/* + * 'get_cupsd_conf()' - Get the current cupsd.conf file. + */ + +static http_status_t /* O - Status of request */ +get_cupsd_conf( + http_t *http, /* I - Connection to server */ + _cups_globals_t *cg, /* I - Global data */ + time_t last_update, /* I - Last update time for file */ + char *name, /* I - Filename buffer */ + int namesize, /* I - Size of filename buffer */ + int *remote) /* O - Remote file? */ +{ + int fd; /* Temporary file descriptor */ +#ifndef WIN32 + struct stat info; /* cupsd.conf file information */ +#endif /* WIN32 */ + http_status_t status; /* Status of getting cupsd.conf */ + char host[HTTP_MAX_HOST]; /* Hostname for connection */ + + + /* + * See if we already have the data we need... + */ + + httpGetHostname(http, host, sizeof(host)); + + if (_cups_strcasecmp(cg->cupsd_hostname, host)) + invalidate_cupsd_cache(cg); + + snprintf(name, namesize, "%s/cupsd.conf", cg->cups_serverroot); + *remote = 0; + +#ifndef WIN32 + if (!_cups_strcasecmp(host, "localhost") && !access(name, R_OK)) + { + /* + * Read the local file rather than using HTTP... + */ + + if (stat(name, &info)) + { + char message[1024]; /* Message string */ + + + snprintf(message, sizeof(message), + _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")), + name, strerror(errno)); + _cupsSetError(IPP_INTERNAL_ERROR, message, 0); + + *name = '\0'; + + return (HTTP_SERVER_ERROR); + } + else if (last_update && info.st_mtime <= last_update) + status = HTTP_NOT_MODIFIED; + else + status = HTTP_OK; + } + else +#endif /* !WIN32 */ + { + /* + * Read cupsd.conf via a HTTP GET request... + */ + + if ((fd = cupsTempFd(name, namesize)) < 0) + { + *name = '\0'; + + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + + invalidate_cupsd_cache(cg); + + return (HTTP_SERVER_ERROR); + } + + *remote = 1; + + httpClearFields(http); + + if (last_update) + httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE, + httpGetDateString(last_update)); + + status = cupsGetFd(http, "/admin/conf/cupsd.conf", fd); + + close(fd); + + if (status != HTTP_OK) + { + unlink(name); + *name = '\0'; + } + } + + return (status); +} + + +/* + * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings. + */ + +static void +invalidate_cupsd_cache( + _cups_globals_t *cg) /* I - Global data */ +{ + cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings); + + cg->cupsd_hostname[0] = '\0'; + cg->cupsd_update = 0; + cg->cupsd_num_settings = 0; + cg->cupsd_settings = NULL; +} + + +/* + * 'write_option()' - Write a CUPS option to a PPD file. + */ + +static void +write_option(cups_file_t *dstfp, /* I - PPD file */ + int order, /* I - Order dependency */ + const char *name, /* I - Option name */ + const char *text, /* I - Option text */ + const char *attrname, /* I - Attribute name */ + ipp_attribute_t *suppattr, /* I - IPP -supported attribute */ + ipp_attribute_t *defattr, /* I - IPP -default attribute */ + int defval, /* I - Default value number */ + int valcount) /* I - Number of values */ +{ + int i; /* Looping var */ + + + cupsFilePrintf(dstfp, "*JCLOpenUI *%s/%s: PickOne\n" + "*OrderDependency: %d JCLSetup *%s\n", + name, text, order, name); + + if (defattr->value_tag == IPP_TAG_INTEGER) + { + /* + * Do numeric options with a range or list... + */ + + cupsFilePrintf(dstfp, "*Default%s: %d\n", name, + defattr->values[defval].integer); + + if (suppattr->value_tag == IPP_TAG_RANGE) + { + /* + * List each number in the range... + */ + + for (i = suppattr->values[0].range.lower; + i <= suppattr->values[0].range.upper; + i ++) + { + cupsFilePrintf(dstfp, "*%s %d: \"", name, i); + + if (valcount == 1) + cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n", + attrname, i); + else if (defval == 0) + cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname, i); + else if (defval < (valcount - 1)) + cupsFilePrintf(dstfp, ",%d\"\n", i); + else + cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", i); + } + } + else + { + /* + * List explicit numbers... + */ + + for (i = 0; i < suppattr->num_values; i ++) + { + cupsFilePrintf(dstfp, "*%s %d: \"", name, suppattr->values[i].integer); + + if (valcount == 1) + cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname, + suppattr->values[i].integer); + else if (defval == 0) + cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname, + suppattr->values[i].integer); + else if (defval < (valcount - 1)) + cupsFilePrintf(dstfp, ",%d\"\n", suppattr->values[i].integer); + else + cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", suppattr->values[i].integer); + } + } + } + else + { + /* + * Do text options with a list... + */ + + cupsFilePrintf(dstfp, "*Default%s: %s\n", name, + defattr->values[defval].string.text); + + for (i = 0; i < suppattr->num_values; i ++) + { + cupsFilePrintf(dstfp, "*%s %s: \"", name, + suppattr->values[i].string.text); + + if (valcount == 1) + cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname, + suppattr->values[i].string.text); + else if (defval == 0) + cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\"\n", attrname, + suppattr->values[i].string.text); + else if (defval < (valcount - 1)) + cupsFilePrintf(dstfp, ",%s\"\n", suppattr->values[i].string.text); + else + cupsFilePrintf(dstfp, ",%s\n\"\n*End\n", + suppattr->values[i].string.text); + } + } + + cupsFilePrintf(dstfp, "*JCLCloseUI: *%s\n\n", name); +} + + +/* + * End of "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $". + */ diff --git a/cups/adminutil.h b/cups/adminutil.h new file mode 100644 index 0000000..8260615 --- /dev/null +++ b/cups/adminutil.h @@ -0,0 +1,81 @@ +/* + * "$Id: adminutil.h 7026 2007-10-19 00:57:45Z mike $" + * + * Administration utility API definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2001-2007 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. + */ + +#ifndef _CUPS_ADMINUTIL_H_ +# define _CUPS_ADMINUTIL_H_ + +/* + * Include necessary headers... + */ + +# include +# include "cups.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Constants... + */ + +# define CUPS_SERVER_DEBUG_LOGGING "_debug_logging" +# define CUPS_SERVER_REMOTE_ADMIN "_remote_admin" +# define CUPS_SERVER_REMOTE_ANY "_remote_any" +/*# define CUPS_SERVER_REMOTE_PRINTERS "_remote_printers"*/ +# define CUPS_SERVER_SHARE_PRINTERS "_share_printers" +# define CUPS_SERVER_USER_CANCEL_ANY "_user_cancel_any" + + +/* + * Functions... + */ + +extern int cupsAdminExportSamba(const char *dest, const char *ppd, + const char *samba_server, + const char *samba_user, + const char *samba_password, + FILE *logfile) _CUPS_DEPRECATED; +extern char *cupsAdminCreateWindowsPPD(http_t *http, const char *dest, + char *buffer, int bufsize) + _CUPS_DEPRECATED; + +extern int cupsAdminGetServerSettings(http_t *http, + int *num_settings, + cups_option_t **settings) + _CUPS_API_1_3; +extern int cupsAdminSetServerSettings(http_t *http, + int num_settings, + cups_option_t *settings) + _CUPS_API_1_3; + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_ADMINUTIL_H_ */ + +/* + * End of "$Id: adminutil.h 7026 2007-10-19 00:57:45Z mike $". + */ diff --git a/cups/api-array.header b/cups/api-array.header new file mode 100644 index 0000000..4d5acf0 --- /dev/null +++ b/cups/api-array.header @@ -0,0 +1,34 @@ + + +

Array API

+ +
+ + + + + + + + + + + + + + + + +
Headercups/array.h
Library-lcups
See AlsoProgramming: Introduction to CUPS Programming
diff --git a/cups/api-array.shtml b/cups/api-array.shtml new file mode 100644 index 0000000..7246a7b --- /dev/null +++ b/cups/api-array.shtml @@ -0,0 +1,196 @@ + + +

Overview

+ +

The CUPS array API provides a high-performance generic array container. +The contents of the array container can be sorted and the container itself is +designed for optimal speed and memory usage under a wide variety of conditions. +Sorted arrays use a binary search algorithm from the last found or inserted +element to quickly find matching elements in the array. Arrays created with the +optional hash function can often find elements with a single lookup. The +cups_array_t type is used when +referring to a CUPS array.

+ +

The CUPS scheduler (cupsd) and many of the CUPS API +functions use the array API to efficiently manage large lists of +data.

+ +

Managing Arrays

+ +

Arrays are created using either the +cupsArrayNew, +cupsArrayNew2, or +cupsArrayNew3 functions. The +first function creates a new array with the specified callback function +and user data pointer:

+ +
+#include <cups/array.h>
+
+static int compare_func(void *first, void *second, void *user_data);
+
+void *user_data;
+cups_array_t *array = cupsArrayNew(compare_func, user_data);
+
+ +

The comparison function (type +cups_arrayfunc_t) is called +whenever an element is added to the array and can be NULL to +create an unsorted array. The function returns -1 if the first element should +come before the second, 0 if the first and second elements should have the same +ordering, and 1 if the first element should come after the second.

+ +

The "user_data" pointer is passed to your comparison function. Pass +NULL if you do not need to associate the elements in your array +with additional information.

+ +

The cupsArrayNew2 function adds +two more arguments to support hashed lookups, which can potentially provide +instantaneous ("O(1)") lookups in your array:

+ +
+#include <cups/array.h>
+
+#define HASH_SIZE 512 /* Size of hash table */
+
+static int compare_func(void *first, void *second, void *user_data);
+static int hash_func(void *element, void *user_data);
+
+void *user_data;
+cups_array_t *hash_array = cupsArrayNew2(compare_func, user_data, hash_func, HASH_SIZE);
+
+ +

The hash function (type +cups_ahash_func_t) should return a +number from 0 to (hash_size-1) that (hopefully) uniquely identifies the +element and is called whenever you look up an element in the array with +cupsArrayFind. The hash size is +only limited by available memory, but generally should not be larger than +16384 to realize any performance improvement.

+ +

The cupsArrayNew3 function adds +copy and free callbacks to support basic memory management of elements:

+ +
+#include <cups/array.h>
+
+#define HASH_SIZE 512 /* Size of hash table */
+
+static int compare_func(void *first, void *second, void *user_data);
+static void *copy_func(void *element, void *user_data);
+static void free_func(void *element, void *user_data);
+static int hash_func(void *element, void *user_data);
+
+void *user_data;
+cups_array_t *array = cupsArrayNew3(compare_func, user_data, NULL, 0, copy_func, free_func);
+
+cups_array_t *hash_array = cupsArrayNew3(compare_func, user_data, hash_func, HASH_SIZE, copy_func, free_func);
+
+ +

Once you have created the array, you add elements using the +cupsArrayAdd +cupsArrayInsert functions. +The first function adds an element to the array, adding the new element +after any elements that have the same order, while the second inserts the +element before others with the same order. For unsorted arrays, +cupsArrayAdd appends the element to +the end of the array while +cupsArrayInsert inserts the +element at the beginning of the array. For example, the following code +creates a sorted array of character strings:

+ +
+#include <cups/array.h>
+
+/* Use strcmp() to compare strings - it will ignore the user_data pointer */
+cups_array_t *array = cupsArrayNew((cups_array_func_t)strcmp, NULL);
+
+/* Add four strings to the array */
+cupsArrayAdd(array, "One Fish");
+cupsArrayAdd(array, "Two Fish");
+cupsArrayAdd(array, "Red Fish");
+cupsArrayAdd(array, "Blue Fish");
+
+ +

Elements are removed using the +cupsArrayRemove function, for +example:

+ +
+#include <cups/array.h>
+
+/* Use strcmp() to compare strings - it will ignore the user_data pointer */
+cups_array_t *array = cupsArrayNew((cups_array_func_t)strcmp, NULL);
+
+/* Add four strings to the array */
+cupsArrayAdd(array, "One Fish");
+cupsArrayAdd(array, "Two Fish");
+cupsArrayAdd(array, "Red Fish");
+cupsArrayAdd(array, "Blue Fish");
+
+/* Remove "Red Fish" */
+cupsArrayRemove(array, "Red Fish");
+
+ +

Finally, you free the memory used by the array using the +cupsArrayDelete function. All +of the memory for the array and hash table (if any) is freed, however CUPS +does not free the elements unless you provide copy and free functions.

+ +

Finding and Enumerating Elements

+ +

CUPS provides several functions to find and enumerate elements in an +array. Each one sets or updates a "current index" into the array, such that +future lookups will start where the last one left off:

+ +
+
cupsArrayFind
+
Returns the first matching element.
+
cupsArrayFirst
+
Returns the first element in the array.
+
cupsArrayIndex
+
Returns the Nth element in the array, starting at 0.
+
cupsArrayLast
+
Returns the last element in the array.
+
cupsArrayNext
+
Returns the next element in the array.
+
cupsArrayPrev
+
Returns the previous element in the array.
+
+ +

Each of these functions returns NULL when there is no +corresponding element. For example, a simple for loop using the +cupsArrayFirst and +cupsArrayNext functions will +enumerate all of the strings in our previous example:

+ +
+#include <cups/array.h>
+
+/* Use strcmp() to compare strings - it will ignore the user_data pointer */
+cups_array_t *array = cupsArrayNew((cups_array_func_t)strcmp, NULL);
+
+/* Add four strings to the array */
+cupsArrayAdd(array, "One Fish");
+cupsArrayAdd(array, "Two Fish");
+cupsArrayAdd(array, "Red Fish");
+cupsArrayAdd(array, "Blue Fish");
+
+/* Show all of the strings in the array */
+char *s;
+for (s = (char *)cupsArrayFirst(array); s != NULL; s = (char *)cupsArrayNext(array))
+  puts(s);
+
diff --git a/cups/api-cups.header b/cups/api-cups.header new file mode 100644 index 0000000..ac781af --- /dev/null +++ b/cups/api-cups.header @@ -0,0 +1,40 @@ + + +

CUPS API

+ +
+ + + + + + + + + + + + + + + + +
Headercups/cups.h
Library-lcups
See AlsoProgramming: Introduction to CUPS Programming
+ Programming: Array API
+ Programming: File and Directory APIs
+ Programming: Filter and Backend Programming
+ Programming: HTTP and IPP APIs
+ Programming: PPD API
+ Programming: Raster API
diff --git a/cups/api-cups.shtml b/cups/api-cups.shtml new file mode 100644 index 0000000..caa96b9 --- /dev/null +++ b/cups/api-cups.shtml @@ -0,0 +1,443 @@ + + +

Overview

+ +

The CUPS API provides the convenience functions needed to support +applications, filters, printer drivers, and backends that need to interface +with the CUPS scheduler.

+ +

Clients and Servers

+ +

CUPS is based on the Internet Printing Protocol ("IPP"), which allows +clients (applications) to communicate with a server (the scheduler) to get a +list of printers, send print jobs, and so forth. You identify which server +you want to communicate with using a pointer to the opaque structure +http_t. All of the examples in this document use the +CUPS_HTTP_DEFAULT constant, referring to the default connection +to the scheduler. The HTTP and IPP +APIs document provides more information on server connections.

+ +

Printers and Classes

+ +

Printers and classes (collections of printers) are accessed through +the cups_dest_t structure which +includes the name (name), instance (instance - +a way of selecting certain saved options/settings), and the options and +attributes associated with that destination (num_options and +options). Destinations are created using the +cupsGetDests function and freed +using the cupsFreeDests function. +The cupsGetDest function finds a +specific destination for printing:

+ +
+#include <cups/cups.h>
+
+cups_dest_t *dests;
+int num_dests = cupsGetDests(&dests);
+cups_dest_t *dest = cupsGetDest("name", NULL, num_dests, dests);
+
+/* do something with dest */
+
+cupsFreeDests(num_dests, dests);
+
+ +

Passing NULL to +cupsGetDest for the destination name +will return the default destination. Similarly, passing a NULL +instance will return the default instance for that destination.

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 1: Printer Attributes
Attribute NameDescription
"auth-info-required"The type of authentication required for printing to this + destination: "none", "username,password", "domain,username,password", + or "negotiate" (Kerberos)
"printer-info"The human-readable description of the destination such as "My + Laser Printer".
"printer-is-accepting-jobs""true" if the destination is accepting new jobs, "false" if + not.
"printer-is-shared""true" if the destination is being shared with other computers, + "false" if not.
"printer-location"The human-readable location of the destination such as "Lab 4".
"printer-make-and-model"The human-readable make and model of the destination such as "HP + LaserJet 4000 Series".
"printer-state""3" if the destination is idle, "4" if the destination is printing + a job, and "5" if the destination is stopped.
"printer-state-change-time"The UNIX time when the destination entered the current state.
"printer-state-reasons"Additional comma-delimited state keywords for the destination + such as "media-tray-empty-error" and "toner-low-warning".
"printer-type"The cups_printer_t + value associated with the destination.
+ +

Options

+ +

Options are stored in arrays of +cups_option_t structures. Each +option has a name (name) and value (value) +associated with it. The cups_dest_t +num_options and options members contain the +default options for a particular destination, along with several informational +attributes about the destination as shown in Table 1. +The cupsGetOption function gets +the value for the named option. For example, the following code lists the +available destinations and their human-readable descriptions:

+ +
+#include <cups/cups.h>
+
+cups_dest_t *dests;
+int num_dests = cupsGetDests(&dests);
+cups_dest_t *dest;
+int i;
+const char *value;
+
+for (i = num_dests, dest = dests; i > 0; i --, dest ++)
+  if (dest->instance == NULL)
+  {
+    value = cupsGetOption("printer-info", dest->num_options, dest->options);
+    printf("%s (%s)\n", dest->name, value ? value : "no description");
+  }
+
+cupsFreeDests(num_dests, dests);
+
+ +

You can create your own option arrays using the +cupsAddOption function, which +adds a single named option to an array:

+ +
+#include <cups/cups.h>
+
+int num_options = 0;
+cups_option_t *options = NULL;
+
+/* The returned num_options value is updated as needed */
+num_options = cupsAddOption("first", "value", num_options, &options);
+
+/* This adds a second option value */
+num_options = cupsAddOption("second", "value", num_options, &options);
+
+/* This replaces the first option we added */
+num_options = cupsAddOption("first", "new value", num_options, &options);
+
+ +

Use a for loop to copy the options from a destination:

+ +
+#include <cups/cups.h>
+
+int i;
+int num_options = 0;
+cups_option_t *options = NULL;
+cups_dest_t *dest;
+
+for (i = 0; i < dest->num_options; i ++)
+  num_options = cupsAddOption(dest->options[i].name, dest->options[i].value,
+                              num_options, &options);
+
+ +

Use the cupsFreeOptions +function to free the options array when you are done using it:

+ +
+cupsFreeOptions(num_options, options);
+
+ +

Print Jobs

+ +

Print jobs are identified by a locally-unique job ID number from 1 to +231-1 and have options and one or more files for printing to a +single destination. The cupsPrintFile +function creates a new job with one file. The following code prints the CUPS +test page file:

+ +
+#include <cups/cups.h>
+
+cups_dest_t *dest;
+int num_options;
+cups_option_t *options;
+int job_id;
+
+/* Print a single file */
+job_id = cupsPrintFile(dest->name, "/usr/share/cups/data/testprint.ps",
+                        "Test Print", num_options, options);
+
+ +

The cupsPrintFiles function +creates a job with multiple files. The files are provided in a +char * array:

+ +
+#include <cups/cups.h>
+
+cups_dest_t *dest;
+int num_options;
+cups_option_t *options;
+int job_id;
+char *files[3] = { "file1.pdf", "file2.pdf", "file3.pdf" };
+
+/* Print three files */
+job_id = cupsPrintFiles(dest->name, 3, files, "Test Print", num_options, options);
+
+ +

Finally, the cupsCreateJob +function creates a new job with no files in it. Files are added using the +cupsStartDocument, +cupsWriteRequestData, +and cupsFinishDocument functions. +The following example creates a job with 10 text files for printing:

+ +
+#include <cups/cups.h>
+
+cups_dest_t *dest;
+int num_options;
+cups_option_t *options;
+int job_id;
+int i;
+char buffer[1024];
+
+/* Create the job */
+job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, dest->name, "10 Text Files",
+                       num_options, options);
+
+/* If the job is created, add 10 files */
+if (job_id > 0)
+{
+  for (i = 1; i <= 10; i ++)
+  {
+    snprintf(buffer, sizeof(buffer), "file%d.txt", i);
+
+    cupsStartDocument(CUPS_HTTP_DEFAULT, dest->name, job_id, buffer,
+                      CUPS_FORMAT_TEXT, i == 10);
+
+    snprintf(buffer, sizeof(buffer),
+             "File %d\n"
+             "\n"
+             "One fish,\n"
+             "Two fish,\n
+             "Red fish,\n
+             "Blue fish\n", i);
+
+    /* cupsWriteRequestData can be called as many times as needed */
+    cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, strlen(buffer));
+
+    cupsFinishDocument(CUPS_HTTP_DEFAULT, dest->name);
+  }
+}
+
+ +

Once you have created a job, you can monitor its status using the +cupsGetJobs function, which returns +an array of cups_job_t structures. +Each contains the job ID (id), destination name +(dest), title (title), and other information +associated with the job. The job array is freed using the +cupsFreeJobs function. The following +example monitors a specific job ID, showing the current job state once every +5 seconds until the job is completed:

+ +
+#include <cups/cups.h>
+
+cups_dest_t *dest;
+int job_id;
+int num_jobs;
+cups_job_t *jobs;
+int i;
+ipp_jstate_t job_state = IPP_JOB_PENDING;
+
+while (job_state < IPP_JOB_STOPPED)
+{
+  /* Get my jobs (1) with any state (-1) */
+  num_jobs = cupsGetJobs(&jobs, dest->name, 1, -1);
+
+  /* Loop to find my job */
+  job_state = IPP_JOB_COMPLETED;
+
+  for (i = 0; i < num_jobs; i ++)
+    if (jobs[i].id == job_id)
+    {
+      job_state = jobs[i].state;
+      break;
+    }
+
+  /* Free the job array */
+  cupsFreeJobs(num_jobs, jobs);
+
+  /* Show the current state */
+  switch (job_state)
+  {
+    case IPP_JOB_PENDING :
+        printf("Job %d is pending.\n", job_id);
+        break;
+    case IPP_JOB_HELD :
+        printf("Job %d is held.\n", job_id);
+        break;
+    case IPP_JOB_PROCESSING :
+        printf("Job %d is processing.\n", job_id);
+        break;
+    case IPP_JOB_STOPPED :
+        printf("Job %d is stopped.\n", job_id);
+        break;
+    case IPP_JOB_CANCELED :
+        printf("Job %d is canceled.\n", job_id);
+        break;
+    case IPP_JOB_ABORTED :
+        printf("Job %d is aborted.\n", job_id);
+        break;
+    case IPP_JOB_COMPLETED :
+        printf("Job %d is completed.\n", job_id);
+        break;
+  }
+
+  /* Sleep if the job is not finished */
+  if (job_state < IPP_JOB_STOPPED)
+    sleep(5);
+}
+
+ +

To cancel a job, use the +cupsCancelJob function with the +job ID:

+ +
+#include <cups/cups.h>
+
+cups_dest_t *dest;
+int job_id;
+
+cupsCancelJob(dest->name, job_id);
+
+ +

Error Handling

+ +

If any of the CUPS API printing functions returns an error, the reason for +that error can be found by calling the +cupsLastError and +cupsLastErrorString functions. +cupsLastError returns the last IPP +error code +(ipp_status_t) +that was encountered, while +cupsLastErrorString returns +a (localized) human-readable string that can be shown to the user. For example, +if any of the job creation functions returns a job ID of 0, you can use +cupsLastErrorString to show +the reason why the job could not be created:

+ +
+#include <cups/cups.h>
+
+int job_id;
+
+if (job_id == 0)
+  puts(cupsLastErrorString());
+
+ +

Passwords and Authentication

+ +

CUPS supports authentication of any request, including submission of print +jobs. The default mechanism for getting the username and password is to use the +login user and a password from the console.

+ +

To support other types of applications, in particular Graphical User +Interfaces ("GUIs"), the CUPS API provides functions to set the default +username and to register a callback function that returns a password string.

+ +

The cupsSetPasswordCB +function is used to set a password callback in your program. Only one +function can be used at any time.

+ +

The cupsSetUser function sets the +current username for authentication. This function can be called by your +password callback function to change the current username as needed.

+ +

The following example shows a simple password callback that gets a +username and password from the user:

+ +
+#include <cups/cups.h>
+
+const char *
+my_password_cb(const char *prompt)
+{
+  char	user[65];
+
+
+  puts(prompt);
+
+  /* Get a username from the user */
+  printf("Username: ");
+  if (fgets(user, sizeof(user), stdin) == NULL)
+    return (NULL);
+
+  /* Strip the newline from the string and set the user */
+  user[strlen(user) - 1] = '\0';
+
+  cupsSetUser(user);
+
+  /* Use getpass() to ask for the password... */
+  return (getpass("Password: "));
+}
+
+cupsSetPasswordCB(my_password_cb);
+
+ +

Similarly, a GUI could display the prompt string in a window with input +fields for the username and password. The username should default to the +string returned by the cupsUser +function.

diff --git a/cups/api-filedir.header b/cups/api-filedir.header new file mode 100644 index 0000000..f9f5298 --- /dev/null +++ b/cups/api-filedir.header @@ -0,0 +1,36 @@ + + +

File and Directory APIs

+ +
+ + + + + + + + + + + + + + + + +
Headerscups/file.h
+ cups/dir.h
Library-lcups
See AlsoProgramming: Introduction to CUPS Programming
+ Programming: CUPS API
diff --git a/cups/api-filedir.shtml b/cups/api-filedir.shtml new file mode 100644 index 0000000..96bf0fa --- /dev/null +++ b/cups/api-filedir.shtml @@ -0,0 +1,31 @@ + + +

Overview

+ +

The CUPS file and directory APIs provide portable interfaces +for manipulating files and listing files and directories. Unlike +stdio FILE streams, the cupsFile functions +allow you to open more than 256 files at any given time. They +also manage the platform-specific details of locking, large file +support, line endings (CR, LF, or CR LF), and reading and writing +files using Flate ("gzip") compression. Finally, you can also +connect, read from, and write to network connections using the +cupsFile functions.

+ +

The cupsDir functions manage the platform-specific +details of directory access/listing and provide a convenient way +to get both a list of files and the information (permissions, +size, timestamp, etc.) for each of those files.

diff --git a/cups/api-filter.header b/cups/api-filter.header new file mode 100644 index 0000000..5b7ee18 --- /dev/null +++ b/cups/api-filter.header @@ -0,0 +1,41 @@ + + +

Filter and Backend Programming

+ +
+ + + + + + + + + + + + + + + + +
Headerscups/backend.h
+ cups/sidechannel.h
Library-lcups
See AlsoProgramming: Introduction to CUPS Programming
+ Programming: CUPS API
+ Programming: PPD API
+ Programming: Raster API
+ Programming: Developing PostScript Printer Drivers
+ Programming: Developing Raster Printer Drivers
+ Specifications: CUPS Design Description
diff --git a/cups/api-filter.shtml b/cups/api-filter.shtml new file mode 100644 index 0000000..3f912ba --- /dev/null +++ b/cups/api-filter.shtml @@ -0,0 +1,765 @@ + + +

Overview

+ +

Filters (which include printer drivers and port monitors) and backends +are used to convert job files to a printable format and send that data to the +printer itself. All of these programs use a common interface for processing +print jobs and communicating status information to the scheduler. Each is run +with a standard set of command-line arguments:

+ +

+ +
argv[1]
+
The job ID
+ +
argv[2]
+
The user printing the job
+ +
argv[3]
+
The job name/title
+ +
argv[4]
+
The number of copies to print
+ +
argv[5]
+
The options that were provided when the job was submitted
+ +
argv[6]
+
The file to print (first program only)
+
+ +

The scheduler runs one or more of these programs to print any given job. The +first filter reads from the print file and writes to the standard output, while +the remaining filters read from the standard input and write to the standard +output. The backend is the last filter in the chain and writes to the +device.

+ +

Filters are always run as a non-privileged user, typically "lp", with no +connection to the user's desktop. Backends are run either as a non-privileged +user or as root if the file permissions do not allow user or group execution. +The file permissions section talks about this in +more detail.

+ +

Security Considerations

+ +

It is always important to use security programming practices. Filters and +most backends are run as a non-privileged user, so the major security +consideration is resource utilization - filters should not depend on unlimited +amounts of CPU, memory, or disk space, and should protect against conditions +that could lead to excess usage of any resource like infinite loops and +unbounded recursion. In addition, filters must never allow the user to +specify an arbitrary file path to a separator page, template, or other file +used by the filter since that can lead to an unauthorized disclosure of +information. Always treat input as suspect and validate it!

+ +

If you are developing a backend that runs as root, make sure to check for +potential buffer overflows, integer under/overflow conditions, and file +accesses since these can lead to privilege escalations. When writing files, +always validate the file path and never allow a user to determine +where to store a file.

+ +
Note: + +

Never write files to a user's home directory. Aside from the +security implications, CUPS is a network print service and as such the network +user may not be the same as the local user and/or there may not be a local home +directory to write to.

+ +

In addition, some operating systems provide additional security mechanisms +that further limit file system access, even for backends running as root. On +OS X, for example, no backend may write to a user's home directory.

+
+ +

Canceled Jobs and Signal Handling

+ +

The scheduler sends SIGTERM when a printing job is canceled or +held. Filters, backends, and port monitors must catch +SIGTERM and perform any cleanup necessary to produce a valid output +file or return the printer to a known good state. The recommended behavior is to +end the output on the current page, preferably on the current line or object +being printed.

+ +

Filters and backends may also receive SIGPIPE when an upstream or downstream filter/backend exits with a non-zero status. Developers should generally ignore SIGPIPE at the beginning of main() with the following function call:

+ +
+#include <signal.h>>
+
+...
+
+int
+main(int argc, char *argv[])
+{
+  signal(SIGPIPE, SIG_IGN);
+
+  ...
+}
+
+ +

File Permissions

+ +

For security reasons, CUPS will only run filters and backends that are owned +by root and do not have world or group write permissions. The recommended +permissions for filters and backends are 0555 - read and execute but no write. +Backends that must run as root should use permissions of 0500 - read and execute +by root, no access for other users. Write permissions can be enabled for the +root user only.

+ +

To avoid a warning message, the directory containing your filter(s) must also +be owned by root and have world and group write disabled - permissions of 0755 +or 0555 are strongly encouraged.

+ +

Temporary Files

+ +

Temporary files should be created in the directory specified by the +"TMPDIR" environment variable. The +cupsTempFile2 function can be +used to safely create temporary files in this directory.

+ +

Copy Generation

+ +

The argv[4] argument specifies the number of copies to produce +of the input file. In general, you should only generate copies if the +filename argument is supplied. The only exception to this are +filters that produce device-independent PostScript output, since the PostScript +filter pstops is responsible for generating copies of PostScript +files.

+ +

Exit Codes

+ +

Filters must exit with status 0 when they successfully generate print data +or 1 when they encounter an error. Backends can return any of the +cups_backend_t constants.

+ +

Environment Variables

+ +

The following environment variables are defined by the printing system +when running print filters and backends:

+ +
+ +
APPLE_LANGUAGE
+
The Apple language identifier associated with the job + (OS X only).
+ +
CHARSET
+
The job character set, typically "utf-8".
+ +
CLASS
+
When a job is submitted to a printer class, contains the name of + the destination printer class. Otherwise this environment + variable will not be set.
+ +
CONTENT_TYPE
+
The MIME type associated with the file (e.g. + application/postscript).
+ +
CUPS_CACHEDIR
+
The directory where cache files can be stored. Cache files can be + used to retain information between jobs or files in a job.
+ +
CUPS_DATADIR
+
The directory where (read-only) CUPS data files can be found.
+ +
CUPS_FILETYPE
+
The type of file being printed: "job-sheet" for a banner page and + "document" for a regular print file.
+ +
CUPS_SERVERROOT
+
The root directory of the server.
+ +
DEVICE_URI
+
The device-uri associated with the printer.
+ +
FINAL_CONTENT_TYPE
+
The MIME type associated with the printer (e.g. + application/vnd.cups-postscript).
+ +
LANG
+
The language locale associated with the job.
+ +
PPD
+
The full pathname of the PostScript Printer Description (PPD) + file for this printer.
+ +
PRINTER
+
The queue name of the class or printer.
+ +
RIP_CACHE
+
The recommended amount of memory to use for Raster Image + Processors (RIPs).
+ +
TMPDIR
+
The directory where temporary files should be created.
+ +
+ +

Communicating with the Scheduler

+ +

Filters and backends communicate with the scheduler by writing messages +to the standard error file. The scheduler reads messages from all filters in +a job and processes the message based on its prefix. For example, the following +code sets the current printer state message to "Printing page 5":

+ +
+int page = 5;
+
+fprintf(stderr, "INFO: Printing page %d\n", page);
+
+ +

Each message is a single line of text starting with one of the following +prefix strings:

+ +
+ +
ALERT: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "alert" log level.
+ +
ATTR: attribute=value [attribute=value]
+
Sets the named printer or job attribute(s). Typically this is used + to set the marker-colors, marker-high-levels, + marker-levels, marker-low-levels, + marker-message, marker-names, + marker-types, printer-alert, and + printer-alert-description printer attributes. Standard + marker-types values are listed in Table + 1.
+ +
CRIT: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "critical" log + level.
+ +
DEBUG: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "debug" log level.
+ +
DEBUG2: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "debug2" log level.
+ +
EMERG: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "emergency" log + level.
+ +
ERROR: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "error" log level. + Use "ERROR:" messages for non-persistent processing errors.
+ +
INFO: message
+
Sets the printer-state-message attribute. If the current log level + is set to "debug2", also adds the specified message to the current error + log file using the "info" log level.
+ +
NOTICE: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "notice" log level.
+ +
PAGE: page-number #-copies
+
PAGE: total #-pages
+
Adds an entry to the current page log file. The first form adds + #-copies to the job-media-sheets-completed attribute. The second + form sets the job-media-sheets-completed attribute to #-pages.
+ +
PPD: keyword=value [keyword=value ...]
+
Changes or adds keywords to the printer's PPD file. Typically + this is used to update installable options or default media settings + based on the printer configuration.
+ +
STATE: + printer-state-reason [printer-state-reason ...]
+
STATE: - printer-state-reason [printer-state-reason ...]
+
Sets or clears printer-state-reason keywords for the current queue. + Typically this is used to indicate persistent media, ink, toner, and + configuration conditions or errors on a printer. + Table 2 lists the standard state keywords - + use vendor-prefixed ("com.example.foo") keywords for custom states. See + Managing Printer State in a Filter for more + information. + +
WARNING: message
+
Sets the printer-state-message attribute and adds the specified + message to the current error log file using the "warning" log + level.
+ +
+ +

Messages without one of these prefixes are treated as if they began with +the "DEBUG:" prefix string.

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 1: Standard marker-types Values
marker-typeDescription
developerDeveloper unit
fuserFuser unit
fuserCleaningPadFuser cleaning pad
fuserOilFuser oil
inkInk supply
opcPhoto conductor
solidWaxWax supply
staplesStaple supply
tonerToner supply
transferUnitTransfer unit
wasteInkWaste ink tank
wasteTonerWaste toner tank
wasteWaxWaste wax tank
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 2: Standard State Keywords
KeywordDescription
connecting-to-deviceConnecting to printer but not printing yet.
cover-openThe printer's cover is open.
input-tray-missingThe paper tray is missing.
marker-supply-emptyThe printer is out of ink.
marker-supply-lowThe printer is almost out of ink.
marker-waste-almost-fullThe printer's waste bin is almost full.
marker-waste-fullThe printer's waste bin is full.
media-emptyThe paper tray (any paper tray) is empty.
media-jamThere is a paper jam.
media-lowThe paper tray (any paper tray) is almost empty.
media-neededThe paper tray needs to be filled (for a job that is printing).
pausedStop the printer.
timed-outUnable to connect to printer.
toner-emptyThe printer is out of toner.
toner-lowThe printer is low on toner.
+ +

Managing Printer State in a Filter

+ +

Filters are responsible for managing the state keywords they set using +"STATE:" messages. Typically you will update all of the keywords that +are used by the filter at startup, for example:

+ +
+if (foo_condition != 0)
+  fputs("STATE: +com.example.foo\n", stderr);
+else
+  fputs("STATE: -com.example.foo\n", stderr);
+
+if (bar_condition != 0)
+  fputs("STATE: +com.example.bar\n", stderr);
+else
+  fputs("STATE: -com.example.bar\n", stderr);
+
+ +

Then as conditions change, your filter sends "STATE: +keyword" or "STATE: +-keyword" messages as necessary to set or clear the corresponding keyword, +respectively.

+ +

State keywords are often used to notify the user of issues that span across +jobs, for example "media-empty-warning" that indicates one or more paper trays +are empty. These keywords should not be cleared unless the corresponding issue +no longer exists.

+ +

Filters should clear job-related keywords on startup and exit so that they +do not remain set between jobs. For example, "connecting-to-device" is a job +sub-state and not an issue that applies when a job is not printing.

+ +
Note: + +

"STATE:" messages often provide visible alerts to the user. For example, +on OS X setting a printer-state-reason value with an "-error" or +"-warning" suffix will cause the printer's dock item to bounce if the +corresponding reason is localized with a cupsIPPReason keyword in the +printer's PPD file.

+ +

When providing a vendor-prefixed keyword, always provide the +corresponding standard keyword (if any) to allow clients to respond to the +condition correctly. For example, if you provide a vendor-prefixed keyword +for a low cyan ink condition ("com.example.cyan-ink-low") you must also set the +"marker-supply-low-warning" keyword. In such cases you should also refrain +from localizing the vendor-prefixed keyword in the PPD file - otherwise both +the generic and vendor-specific keyword will be shown in the user +interface.

+ +
+ +

Reporting Supply Levels

+ +

CUPS tracks several "marker-*" attributes for ink/toner supply level +reporting. These attributes allow applications to display the current supply +levels for a printer without printer-specific software. Table 3 lists the marker attributes and what they represent.

+ +

Filters set marker attributes by sending "ATTR:" messages to stderr. For +example, a filter supporting an inkjet printer with black and tri-color ink +cartridges would use the following to initialize the supply attributes:

+ +
+fputs("ATTR: marker-colors=#000000,#00FFFF#FF00FF#FFFF00\n", stderr);
+fputs("ATTR: marker-low-levels=5,10\n", stderr);
+fputs("ATTR: marker-names=Black,Tri-Color\n", stderr);
+fputs("ATTR: marker-types=ink,ink\n", stderr);
+
+ +

Then periodically the filter queries the printer for its current supply +levels and updates them with a separate "ATTR:" message:

+ +
+int black_level, tri_level;
+...
+fprintf(stderr, "ATTR: marker-levels=%d,%d\n", black_level, tri_level);
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 3: Supply Level Attributes
AttributeDescription
marker-colorsA list of comma-separated colors; each color is either "none" or one or + more hex-encoded sRGB colors of the form "#RRGGBB".
marker-high-levelsA list of comma-separated "almost full" level values from 0 to 100; a + value of 100 should be used for supplies that are consumed/emptied like ink + cartridges.
marker-levelsA list of comma-separated level values for each supply. A value of -1 + indicates the level is unavailable, -2 indicates unknown, and -3 indicates + the level is unknown but has not yet reached capacity. Values from 0 to 100 + indicate the corresponding percentage.
marker-low-levelsA list of comma-separated "almost empty" level values from 0 to 100; a + value of 0 should be used for supplies that are filled like waste ink + tanks.
marker-messageA human-readable supply status message for the user like "12 pages of + ink remaining."
marker-namesA list of comma-separated supply names like "Cyan Ink", "Fuser", + etc.
marker-typesA list of comma-separated supply types; the types are listed in + Table 1.
+ +

Communicating with the Backend

+ +

Filters can communicate with the backend via the +cupsBackChannelRead and +cupsSideChannelDoRequest +functions. The +cupsBackChannelRead function +reads data that has been sent back from the device and is typically used to +obtain status and configuration information. For example, the following code +polls the backend for back-channel data:

+ +
+#include <cups/cups.h>
+
+char buffer[8192];
+ssize_t bytes;
+
+/* Use a timeout of 0.0 seconds to poll for back-channel data */
+bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
+
+ +

Filters can also use select() or poll() on the +back-channel file descriptor (3 or CUPS_BC_FD) to read data only +when it is available.

+ +

The +cupsSideChannelDoRequest +function allows you to get out-of-band status information and do synchronization +with the device. For example, the following code gets the current IEEE-1284 +device ID string from the backend:

+ +
+#include <cups/sidechannel.h>
+
+char data[2049];
+int datalen;
+cups_sc_status_t status;
+
+/* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
+   nul-termination... */
+datalen = sizeof(data) - 1;
+
+/* Get the IEEE-1284 device ID, waiting for up to 1 second */
+status = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_DEVICE_ID, data, &datalen, 1.0);
+
+/* Use the returned value if OK was returned and the length is non-zero */
+if (status == CUPS_SC_STATUS_OK && datalen > 0)
+  data[datalen] = '\0';
+else
+  data[0] = '\0';
+
+ +

Forcing All Output to a Printer

+ +

The +cupsSideChannelDoRequest +function allows you to tell the backend to send all pending data to the printer. +This is most often needed when sending query commands to the printer. For example:

+ +
+#include <cups/cups.h>
+#include <cups/sidechannel.h>
+
+char data[1024];
+int datalen = sizeof(data);
+cups_sc_status_t status;
+
+/* Flush pending output to stdout */
+fflush(stdout);
+
+/* Drain output to backend, waiting for up to 30 seconds */
+status = cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT, data, &datalen, 30.0);
+
+/* Read the response if the output was sent */
+if (status == CUPS_SC_STATUS_OK)
+{
+  ssize_t bytes;
+
+  /* Wait up to 10.0 seconds for back-channel data */
+  bytes = cupsBackChannelRead(data, sizeof(data), 10.0);
+  /* do something with the data from the printer */
+}
+
+ +

Communicating with Filters

+ +

Backends communicate with filters using the reciprocal functions +cupsBackChannelWrite, +cupsSideChannelRead, and +cupsSideChannelWrite. We +recommend writing back-channel data using a timeout of 1.0 seconds:

+ +
+#include <cups/cups.h>
+
+char buffer[8192];
+ssize_t bytes;
+
+/* Obtain data from printer/device */
+...
+
+/* Use a timeout of 1.0 seconds to give filters a chance to read */
+cupsBackChannelWrite(buffer, bytes, 1.0);
+
+ +

The cupsSideChannelRead +function reads a side-channel command from a filter, driver, or port monitor. +Backends can either poll for commands using a timeout of 0.0, wait +indefinitely for commands using a timeout of -1.0 (probably in a +separate thread for that purpose), or use select or +poll on the CUPS_SC_FD file descriptor (4) to handle +input and output on several file descriptors at the same time.

+ +

Once a command is processed, the backend uses the +cupsSideChannelWrite function +to send its response. For example, the following code shows how to poll for a +side-channel command and respond to it:

+ +
+#include <cups/sidechannel.h>
+
+cups_sc_command_t command;
+cups_sc_status_t status;
+char data[2048];
+int datalen = sizeof(data);
+
+/* Poll for a command... */
+if (!cupsSideChannelRead(&command, &status, data, &datalen, 0.0))
+{
+  switch (command)
+  {
+    /* handle supported commands, fill data/datalen/status with values as needed */
+
+    default :
+        status  = CUPS_SC_STATUS_NOT_IMPLEMENTED;
+	datalen = 0;
+	break;
+  }
+
+  /* Send a response... */
+  cupsSideChannelWrite(command, status, data, datalen, 1.0);
+}
+
+ +

Doing SNMP Queries with Network Printers

+ +

The Simple Network Management Protocol (SNMP) allows you to get the current +status, page counter, and supply levels from most network printers. Every +piece of information is associated with an Object Identifier (OID), and +every printer has a community name associated with it. OIDs can be +queried directly or by "walking" over a range of OIDs with a common prefix.

+ +

The two CUPS SNMP functions provide a simple API for querying network +printers through the side-channel interface. Each accepts a string containing +an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID) +along with a timeout for the query.

+ +

The cupsSideChannelSNMPGet +function queries a single OID and returns the value as a string in a buffer +you supply:

+ +
+#include <cups/sidechannel.h>
+
+char data[512];
+int datalen = sizeof(data);
+
+if (cupsSideChannelSNMPGet(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &datalen, 5.0)
+        == CUPS_SC_STATUS_OK)
+{
+  /* Do something with the value */
+  printf("Page counter is: %s\n", data);
+}
+
+ +

The +cupsSideChannelSNMPWalk +function allows you to query a whole group of OIDs, calling a function of your +choice for each OID that is found:

+ +
+#include <cups/sidechannel.h>
+
+void
+my_callback(const char *oid, const char *data, int datalen, void *context)
+{
+  /* Do something with the value */
+  printf("%s=%s\n", oid, data);
+}
+
+...
+
+void *my_data;
+
+cupsSNMPSideChannelWalk(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);
+
diff --git a/cups/api-httpipp.header b/cups/api-httpipp.header new file mode 100644 index 0000000..5f245d7 --- /dev/null +++ b/cups/api-httpipp.header @@ -0,0 +1,37 @@ + + +

HTTP and IPP APIs

+ +
+ + + + + + + + + + + + + + + + +
Headercups/cups.h
Library-lcups
See AlsoProgramming: Introduction to CUPS Programming
+ Programming: CUPS API
+ References: CUPS Implementation of IPP
diff --git a/cups/api-httpipp.shtml b/cups/api-httpipp.shtml new file mode 100644 index 0000000..cd0fd53 --- /dev/null +++ b/cups/api-httpipp.shtml @@ -0,0 +1,317 @@ + + +

Overview

+ +

The CUPS HTTP and IPP APIs provide low-level access to the HTTP and IPP +protocols and CUPS scheduler. They are typically used by monitoring and +administration programs to perform specific functions not supported by the +high-level CUPS API functions.

+ +

The HTTP APIs use an opaque structure called +http_t to manage connections to +a particular HTTP or IPP server. The +httpConnectEncrypt function is +used to create an instance of this structure for a particular server. +The constant CUPS_HTTP_DEFAULT can be used with all of the +cups functions to refer to the default CUPS server - the functions +create a per-thread http_t as needed.

+ +

The IPP APIs use two opaque structures for requests (messages sent to the CUPS scheduler) and responses (messages sent back to your application from the scheduler). The ipp_t type holds a complete request or response and is allocated using the ippNew or ippNewRequest functions and freed using the ippDelete function.

+ +

The second opaque structure is called ipp_attribute_t and holds a single IPP attribute which consists of a group tag (ippGetGroupTag), a value type tag (ippGetValueTag), the attribute name (ippGetName), and 1 or more values (ippGetCount, ippGetBoolean, ippGetCollection, ippGetDate, ippGetInteger, ippGetRange, ippGetResolution, and ippGetString). Attributes are added to an ipp_t pointer using one of the ippAdd functions. For example, use ippAddString to add the "printer-uri" and "requesting-user-name" string attributes to a request:

+ +
+ipp_t *request = ippNewRequest(IPP_GET_JOBS);
+
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+             NULL, "ipp://localhost/printers/");
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+             NULL, cupsUser());
+
+ +

Once you have created an IPP request, use the cups functions to send the request to and read the response from the server. For example, the cupsDoRequest function can be used for simple query operations that do not involve files:

+ +
+#include <cups/cups.h>
+
+
+ipp_t *get_jobs(void)
+{
+  ipp_t *request = ippNewRequest(IPP_GET_JOBS);
+
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+               NULL, "ipp://localhost/printers/");
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+               NULL, cupsUser());
+
+  return (cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/"));
+}
+
+ +

The cupsDoRequest function frees the request and returns an IPP response or NULL pointer if the request could not be sent to the server. Once you have a response from the server, you can either use the ippFindAttribute and ippFindNextAttribute functions to find specific attributes, for example:

+ +
+ipp_t *response;
+ipp_attribute_t *attr;
+
+attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM);
+
+ +

You can also walk the list of attributes with a simple for loop like this:

+ +
+ipp_t *response;
+ipp_attribute_t *attr;
+
+for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response))
+  if (ippGetName(attr) == NULL)
+    puts("--SEPARATOR--");
+  else
+    puts(ippGetName(attr));
+
+ +

The for loop approach is normally used when collecting attributes for multiple objects (jobs, printers, etc.) in a response. Attributes with NULL names indicate a separator between the attributes of each object. For example, the following code will list the jobs returned from our previous get_jobs example code:

+ +
+ipp_t *response = get_jobs();
+
+if (response != NULL)
+{
+  ipp_attribute_t *attr;
+  const char *attrname;
+  int job_id = 0;
+  const char *job_name = NULL;
+  const char *job_originating_user_name = NULL;
+
+  puts("Job ID  Owner             Title");
+  puts("------  ----------------  ---------------------------------");
+
+  for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response))
+  {
+   /* Attributes without names are separators between jobs */
+    attrname = ippGetName(attr);
+    if (attrname == NULL)
+    {
+      if (job_id > 0)
+      {
+        if (job_name == NULL)
+          job_name = "(withheld)";
+
+        if (job_originating_user_name == NULL)
+          job_originating_user_name = "(withheld)";
+
+        printf("%5d  %-16s  %s\n", job_id, job_originating_user_name, job_name);
+      }
+
+      job_id = 0;
+      job_name = NULL;
+      job_originating_user_name = NULL;
+      continue;
+    }
+    else if (!strcmp(attrname, "job-id") && ippGetValueTag(attr) == IPP_TAG_INTEGER)
+      job_id = ippGetInteger(attr, 0);
+    else if (!strcmp(attrname, "job-name") && ippGetValueTag(attr) == IPP_TAG_NAME)
+      job_name = ippGetString(attr, 0, NULL);
+    else if (!strcmp(attrname, "job-originating-user-name") &&
+             ippGetValueTag(attr) == IPP_TAG_NAME)
+      job_originating_user_name = ippGetString(attr, 0, NULL);
+  }
+
+  if (job_id > 0)
+  {
+    if (job_name == NULL)
+      job_name = "(withheld)";
+
+    if (job_originating_user_name == NULL)
+      job_originating_user_name = "(withheld)";
+
+    printf("%5d  %-16s  %s\n", job_id, job_originating_user_name, job_name);
+  }
+}
+
+ +

Creating URI Strings

+ +

To ensure proper encoding, the +httpAssembleURIf function must be +used to format a "printer-uri" string for all printer-based requests:

+ +
+const char *name = "Foo";
+char uri[1024];
+ipp_t *request;
+
+httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, cupsServer(),
+                 ippPort(), "/printers/%s", name);
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
+
+ +

Sending Requests with Files

+ +

The cupsDoFileRequest and +cupsDoIORequest functions are +used for requests involving files. The +cupsDoFileRequest function +attaches the named file to a request and is typically used when sending a print +file or changing a printer's PPD file:

+ +
+const char *filename = "/usr/share/cups/data/testprint.ps";
+const char *name = "Foo";
+char uri[1024];
+char resource[1024];
+ipp_t *request = ippNewRequest(IPP_PRINT_JOB);
+ipp_t *response;
+
+/* Use httpAssembleURIf for the printer-uri string */
+httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, cupsServer(),
+                 ippPort(), "/printers/%s", name);
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+             NULL, cupsUser());
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
+             NULL, "testprint.ps");
+
+/* Use snprintf for the resource path */
+snprintf(resource, sizeof(resource), "/printers/%s", name);
+
+response = cupsDoFileRequest(CUPS_HTTP_DEFAULT, request, resource, filename);
+
+ +

The cupsDoIORequest function +optionally attaches a file to the request and optionally saves a file in the +response from the server. It is used when using a pipe for the request +attachment or when using a request that returns a file, currently only +CUPS_GET_DOCUMENT and CUPS_GET_PPD. For example, +the following code will download the PPD file for the sample HP LaserJet +printer driver:

+ +
+char tempfile[1024];
+int tempfd;
+ipp_t *request = ippNewRequest(CUPS_GET_PPD);
+ipp_t *response;
+
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name",
+             NULL, "laserjet.ppd");
+
+tempfd = cupsTempFd(tempfile, sizeof(tempfile));
+
+response = cupsDoIORequest(CUPS_HTTP_DEFAULT, request, "/", -1, tempfd);
+
+ +

The example passes -1 for the input file descriptor to specify +that no file is to be attached to the request. The PPD file attached to the +response is written to the temporary file descriptor we created using the +cupsTempFd function.

+ +

Asynchronous Request Processing

+ +

The cupsSendRequest and +cupsGetResponse support +asynchronous communications with the server. Unlike the other request +functions, the IPP request is not automatically freed, so remember to +free your request with the ippDelete +function.

+ +

File data is attached to the request using the +cupsWriteRequestData +function, while file data returned from the server is read using the +cupsReadResponseData +function. We can rewrite the previous CUPS_GET_PPD example +to use the asynchronous functions quite easily:

+ +
+char tempfile[1024];
+int tempfd;
+ipp_t *request = ippNewRequest(CUPS_GET_PPD);
+ipp_t *response;
+
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name",
+             NULL, "laserjet.ppd");
+
+tempfd = cupsTempFd(tempfile, sizeof(tempfile));
+
+if (cupsSendRequest(CUPS_HTTP_DEFAULT, request, "/") == HTTP_CONTINUE)
+{
+  response = cupsGetResponse(CUPS_HTTP_DEFAULT, "/");
+
+  if (response != NULL)
+  {
+    ssize_t bytes;
+    char buffer[8192];
+
+    while ((bytes = cupsReadResponseData(CUPS_HTTP_DEFAULT, buffer, sizeof(buffer))) > 0)
+      write(tempfd, buffer, bytes);
+  }
+}
+
+/* Free the request! */
+ippDelete(request);
+
+ +

The cupsSendRequest function +returns the initial HTTP request status, typically either +HTTP_CONTINUE or HTTP_UNAUTHORIZED. The latter status +is returned when the request requires authentication of some sort. The +cupsDoAuthentication function +must be called when your see HTTP_UNAUTHORIZED and the request +re-sent. We can add authentication support to our example code by using a +do ... while loop:

+ +
+char tempfile[1024];
+int tempfd;
+ipp_t *request = ippNewRequest(CUPS_GET_PPD);
+ipp_t *response;
+http_status_t status;
+
+ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name",
+             NULL, "laserjet.ppd");
+
+tempfd = cupsTempFd(tempfile, sizeof(tempfile));
+
+/* Loop for authentication */
+do
+{
+  status = cupsSendRequest(CUPS_HTTP_DEFAULT, request, "/");
+
+  if (status == HTTP_UNAUTHORIZED)
+  {
+    /* Try to authenticate, break out of the loop if that fails */
+    if (cupsDoAuthentication(CUPS_HTTP_DEFAULT, "POST", "/"))
+      break;
+  }
+}
+while (status != HTTP_CONTINUE && status != HTTP_UNAUTHORIZED);
+
+if (status == HTTP_CONTINUE)
+{
+  response = cupsGetResponse(CUPS_HTTP_DEFAULT, "/");
+
+  if (response != NULL)
+  {
+    ssize_t bytes;
+    char buffer[8192];
+
+    while ((bytes = cupsReadResponseData(CUPS_HTTP_DEFAULT, buffer, sizeof(buffer))) > 0)
+      write(tempfd, buffer, bytes);
+  }
+}
+
+/* Free the request! */
+ippDelete(request);
+
diff --git a/cups/api-overview.header b/cups/api-overview.header new file mode 100644 index 0000000..ecb14b6 --- /dev/null +++ b/cups/api-overview.header @@ -0,0 +1,53 @@ + + +

Introduction to CUPS Programming

+ +
+ + + + + + + + + + + + + + + + +
Headerscups/cups.h
+ cups/array.h
+ cups/backend.h
+ cups/dir.h
+ cups/file.h
+ cups/ppd.h
+ cups/raster.h
+ cups/sidechannel.h
Libraries-lcups
+ -lcupsimage
See AlsoProgramming: Developing Raster Printer Drivers
+ Programming: Developing PostScript Printer Drivers
+ Programming: Filter and Backend Programming
+ Programming: Introduction to the PPD Compiler
+ Programming: Array API
+ Programming: CUPS API
+ Programming: File and Directory APIs
+ Programming: HTTP and IPP APIs
+ Programming: PPD API
+ Programming: Raster API
+ References: PPD Compiler Driver Information File Reference
+ Specifications: CUPS PPD Extensions
diff --git a/cups/api-overview.shtml b/cups/api-overview.shtml new file mode 100644 index 0000000..3ece103 --- /dev/null +++ b/cups/api-overview.shtml @@ -0,0 +1,94 @@ + + +

Overview

+ +

CUPS provides two libraries that interface with the different parts of the +printing system. The "cups" library provides all of the common application and +filter functions while the "cupsimage" library provides all of the imaging +functions used in raster printer drivers. The "cups" library functions are +accessed by including the <cups/cups.h> header, while +"cupsimage" functions are found in the <cups/raster.h> +header.

+ +

Compiling Programs

+ +

The CUPS libraries can be used from any C, C++, or Objective C program. +The method of compiling against the libraries varies depending on the +operating system and installation of CUPS. The following sections show how +to compile a simple program (shown below) in two common environments.

+ +

The following simple program lists the available printers on the system:

+ +
+#include <stdio.h>
+#include <cups/cups.h>
+
+int main(void)
+{
+  int i;
+  cups_dest_t *dests, *dest;
+  int num_dests = cupsGetDests(&dests);
+
+  for (i = num_dests, dest = dests; i > 0; i --, dest ++)
+  {
+    if (dest->instance)
+      printf("%s/%s\n", dest->name, dest->instance);
+    else
+      puts(dest->name);
+  }
+
+  return (0);
+}
+
+ +

Compiling with Xcode

+ +

In Xcode, choose New Project... from the File menu, +then select the Standard Tool project type under Command Line +Utility. Click Next and choose a project directory. Click +Next to create the project.

+ +

In the project window, double-click on the Targets group and +control-click on the simple target to show the context menu. Choose +Existing Framework... from the Add submenu. When the file +chooser sheet appears, press the / key and enter "/usr/lib". Scroll +down the file list and select the libcups.dylib file. Click the +Add button in the file chooser and attributes sheets.

+ +

In the project window, double-click on the main.c source file. +Replace the template source code with the listing above and save it. Click the +Build and Go button to build the sample program and run it.

+ +

Compiling with GCC

+ +

From the command-line, create a file called sample.c using your +favorite editor and then run the following command to compile it with GCC and +run it:

+ +
+gcc -o simple `cups-config --cflags` simple.c `cups-config --libs`
+./simple
+
+ +

The cups-config command provides the compiler flags +("cups-config --cflags") and libraries ("cups-config --libs") needed for the +local system.

+ +

Where to Go Next

+ +

If you are developing a print filter, driver, or backend, see the +Filter and Backend Programming +guide. Raster printer driver developers should also read the +Raster API reference.

diff --git a/cups/api-ppd.header b/cups/api-ppd.header new file mode 100644 index 0000000..ef0d051 --- /dev/null +++ b/cups/api-ppd.header @@ -0,0 +1,38 @@ + + +

PPD API (DEPRECATED)

+ +
The PPD API is deprecated starting in CUPS 1.6/OS X 10.8. Please use the new Job Ticket APIs in the CUPS API documentation. These functions will be removed in a future release of CUPS.
+ +
+ + + + + + + + + + + + + + + + +
Headercups/ppd.h
Library-lcups
See AlsoProgramming: Introduction to CUPS Programming
+ Programming: CUPS API
+ Specifications: CUPS PPD Extensions
diff --git a/cups/api-ppd.shtml b/cups/api-ppd.shtml new file mode 100644 index 0000000..6319f23 --- /dev/null +++ b/cups/api-ppd.shtml @@ -0,0 +1,219 @@ + + +

Overview

+ +
The PPD API is deprecated starting in CUPS 1.6/OS X 10.8. Please use the new Job Ticket APIs in the CUPS API documentation. These functions will be removed in a future release of CUPS.
+ +

The CUPS PPD API provides read-only access the data in PostScript Printer +Description ("PPD") files which are used for all printers with a driver. With +it you can obtain the data necessary to display printer options to users, mark +option choices and check for conflicting choices, and output marked choices in +PostScript output. The ppd_file_t +structure contains all of the information in a PPD file.

+ +
Note: + +

The CUPS PPD API uses the terms "option" and "choice" instead of the Adobe +terms "MainKeyword" and "OptionKeyword" to refer to specific printer options and +features. CUPS also treats option ("MainKeyword") and choice ("OptionKeyword") +values as case-insensitive strings, so option "InputSlot" and choice "Upper" +are equivalent to "inputslot" and "upper", respectively.

+
+ +

Loading a PPD File

+ +

The ppdOpenFile function "opens" a +PPD file and loads it into memory. For example, the following code opens the +current printer's PPD file in a CUPS filter:

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd = ppdOpenFile(getenv("PPD"));
+
+ +

The return value is a pointer to a new +ppd_file_t structure or NULL +if the PPD file does not exist or cannot be loaded. The +ppdClose function frees the memory used +by the structure:

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd;
+
+ppdClose(ppd);
+
+ +

Once closed, pointers to the ppd_file_t +structure and any data in it will no longer be valid.

+ +

Options and Groups

+ +

PPD files support multiple options, which are stored in arrays of +ppd_option_t and +ppd_choice_t structures.

+ +

Each option in turn is associated with a group stored in a +ppd_group_t structure. Groups can be +specified in the PPD file; if an option is not associated with a group +then it is put in an automatically-generated "General" group. Groups can also +have sub-groups, however CUPS currently ignores sub-groups because of past +abuses of this functionality.

+ +

Option choices are selected by marking them using one of three functions. The +first is ppdMarkDefaults which +selects all of the default options in the PPD file:

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd;
+
+ppdMarkDefaults(ppd);
+
+ +

The second is ppdMarkOption +which selects a single option choice in the PPD file. For example, the following +code selects the upper paper tray:

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd;
+
+ppdMarkOption(ppd, "InputSlot", "Upper");
+
+ +

The last function is +cupsMarkOptions which selects +multiple option choices in the PPD file from an array of CUPS options, mapping +IPP attributes like "media" and "sides" to their corresponding PPD options. You +typically use this function in a print filter with +cupsParseOptions and +ppdMarkDefaults to select all of +the option choices needed for the job, for example:

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd = ppdOpenFile(getenv("PPD"));
+cups_option_t *options = NULL;
+int num_options = cupsParseOptions(argv[5], 0, &options);
+
+ppdMarkDefaults(ppd);
+cupsMarkOptions(ppd, num_options, options);
+cupsFreeOptions(num_options, options);
+
+ +

Constraints

+ +

PPD files support specification of conflict conditions, called +constraints, between different options. Constraints are stored in an array of +ppd_const_t structures which specify +the options and choices that conflict with each other. The +ppdConflicts function tells you +how many of the selected options are incompatible. Since constraints are +normally specified in pairs, the returned value is typically an even number.

+ +

Page Sizes

+ +

Page sizes are special options which have physical dimensions and margins +associated with them. The size information is stored in +ppd_size_t structures and is available +by looking up the named size with the +ppdPageSize function. The page size and +margins are returned in units called points; there are 72 points per inch. If +you pass NULL for the size, the currently selected size is +returned:

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd;
+ppd_size_t *size = ppdPageSize(ppd, NULL);
+
+ +

Besides the standard page sizes listed in a PPD file, some printers +support variable or custom page sizes. Custom page sizes are supported if the +variables_sizes member of the +ppd_file_t structure is non-zero. +The custom_min, custom_max, and +custom_margins members of the +ppd_file_t structure define the limits +of the printable area. To get the resulting media size, use a page size string +of the form "Custom.widthxlength", where "width" and "length" are +in points. Custom page size names can also be specified in inches +("Custom.widthxheightin"), centimeters +("Custom.widthxheightcm"), or millimeters +("Custom.widthxheightmm"):

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd;
+
+/* Get an 576x720 point custom page size */
+ppd_size_t *size = ppdPageSize(ppd, "Custom.576x720");
+
+/* Get an 8x10 inch custom page size */
+ppd_size_t *size = ppdPageSize(ppd, "Custom.8x10in");
+
+/* Get a 100x200 millimeter custom page size */
+ppd_size_t *size = ppdPageSize(ppd, "Custom.100x200mm");
+
+/* Get a 12.7x34.5 centimeter custom page size */
+ppd_size_t *size = ppdPageSize(ppd, "Custom.12.7x34.5cm");
+
+ +

If the PPD does not support variable page sizes, the +ppdPageSize function will return +NULL.

+ +

Attributes

+ +

Every PPD file is composed of one or more attributes. Most of these +attributes are used to define groups, options, choices, and page sizes, +however several informational attributes may be present which you can access +in your program or filter. Attributes normally look like one of the following +examples in a PPD file:

+ +
+*name: "value"
+*name spec: "value"
+*name spec/text: "value"
+
+ +

The ppdFindAttr and +ppdFindNextAttr functions find the +first and next instances, respectively, of the named attribute with the given +"spec" string and return a ppd_attr_t +structure. If you provide a NULL specifier string, all attributes with the +given name will be returned. For example, the following code lists all of the +Product attributes in a PPD file:

+ +
+#include <cups/ppd.h>
+
+ppd_file_t *ppd;
+ppd_attr_t *attr;
+
+for (attr = ppdFindAttr(ppd, "Product", NULL);
+     attr != NULL;
+     attr = ppdFindNextAttr(ppd, "Product", NULL))
+  puts(attr->value);
+
diff --git a/cups/array-private.h b/cups/array-private.h new file mode 100644 index 0000000..e529694 --- /dev/null +++ b/cups/array-private.h @@ -0,0 +1,51 @@ +/* + * "$Id: array-private.h 3448 2011-10-04 06:53:26Z msweet $" + * + * Private array definitions for CUPS. + * + * Copyright 2011 by Apple Inc. + * + * 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. + */ + +#ifndef _CUPS_ARRAY_PRIVATE_H_ +# define _CUPS_ARRAY_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Functions... + */ + +extern int _cupsArrayAddStrings(cups_array_t *a, const char *s) + _CUPS_API_1_5; +extern cups_array_t *_cupsArrayNewStrings(const char *s) _CUPS_API_1_5; + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_ARRAY_PRIVATE_H_ */ + +/* + * End of "$Id: array-private.h 3448 2011-10-04 06:53:26Z msweet $". + */ diff --git a/cups/array.c b/cups/array.c new file mode 100644 index 0000000..2fb7701 --- /dev/null +++ b/cups/array.c @@ -0,0 +1,1326 @@ +/* + * "$Id: array.c 7616 2008-05-28 00:34:13Z mike $" + * + * Sorted array routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * cupsArrayAdd() - Add an element to the array. + * _cupsArrayAddStrings() - Add zero or more comma-delimited strings to an + * array. + * cupsArrayClear() - Clear the array. + * cupsArrayCount() - Get the number of elements in the array. + * cupsArrayCurrent() - Return the current element in the array. + * cupsArrayDelete() - Free all memory used by the array. + * cupsArrayDup() - Duplicate the array. + * cupsArrayFind() - Find an element in the array. + * cupsArrayFirst() - Get the first element in the array. + * cupsArrayGetIndex() - Get the index of the current element. + * cupsArrayGetInsert() - Get the index of the last inserted element. + * cupsArrayIndex() - Get the N-th element in the array. + * cupsArrayInsert() - Insert an element in the array. + * cupsArrayLast() - Get the last element in the array. + * cupsArrayNew() - Create a new array. + * cupsArrayNew2() - Create a new array with hash. + * cupsArrayNew3() - Create a new array with hash and/or free function. + * _cupsArrayNewStrings() - Create a new array of comma-delimited strings. + * cupsArrayNext() - Get the next element in the array. + * cupsArrayPrev() - Get the previous element in the array. + * cupsArrayRemove() - Remove an element from the array. + * cupsArrayRestore() - Reset the current element to the last @link + * cupsArraySave@. + * cupsArraySave() - Mark the current element for a later @link + * cupsArrayRestore@. + * cupsArrayUserData() - Return the user data for an array. + * cups_array_add() - Insert or append an element to the array. + * cups_array_find() - Find an element in the array. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "debug-private.h" +#include "array-private.h" + + +/* + * Limits... + */ + +#define _CUPS_MAXSAVE 32 /**** Maximum number of saves ****/ + + +/* + * Types and structures... + */ + +struct _cups_array_s /**** CUPS array structure ****/ +{ + /* + * The current implementation uses an insertion sort into an array of + * sorted pointers. We leave the array type private/opaque so that we + * can change the underlying implementation without affecting the users + * of this API. + */ + + int num_elements, /* Number of array elements */ + alloc_elements, /* Allocated array elements */ + current, /* Current element */ + insert, /* Last inserted element */ + unique, /* Are all elements unique? */ + num_saved, /* Number of saved elements */ + saved[_CUPS_MAXSAVE]; + /* Saved elements */ + void **elements; /* Array elements */ + cups_array_func_t compare; /* Element comparison function */ + void *data; /* User data passed to compare */ + cups_ahash_func_t hashfunc; /* Hash function */ + int hashsize, /* Size of hash */ + *hash; /* Hash array */ + cups_acopy_func_t copyfunc; /* Copy function */ + cups_afree_func_t freefunc; /* Free function */ +}; + + +/* + * Local functions... + */ + +static int cups_array_add(cups_array_t *a, void *e, int insert); +static int cups_array_find(cups_array_t *a, void *e, int prev, int *rdiff); + + +/* + * 'cupsArrayAdd()' - Add an element to the array. + * + * When adding an element to a sorted array, non-unique elements are + * appended at the end of the run of identical elements. For unsorted arrays, + * the element is appended to the end of the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsArrayAdd(cups_array_t *a, /* I - Array */ + void *e) /* I - Element */ +{ + DEBUG_printf(("2cupsArrayAdd(a=%p, e=%p)", a, e)); + + /* + * Range check input... + */ + + if (!a || !e) + { + DEBUG_puts("3cupsArrayAdd: returning 0"); + return (0); + } + + /* + * Append the element... + */ + + return (cups_array_add(a, e, 0)); +} + + +/* + * '_cupsArrayAddStrings()' - Add zero or more comma-delimited strings to an + * array. + * + * Note: The array MUST be created using the @link _cupsArrayNewStrings@ + * function. Duplicate strings are NOT added. If the string pointer "s" is NULL + * or the empty string, no strings are added to the array. + */ + +int /* O - 1 on success, 0 on failure */ +_cupsArrayAddStrings(cups_array_t *a, /* I - Array */ + const char *s) /* I - Comma-delimited strings or NULL */ +{ + char *buffer, /* Copy of string */ + *start, /* Start of string */ + *end; /* End of string */ + int status = 1; /* Status of add */ + + + if (!a || !s || !*s) + return (0); + + if (!strchr(s, ',')) + { + /* + * String doesn't contain a comma, so add it as a single value... + */ + + if (!cupsArrayFind(a, (void *)s)) + status = cupsArrayAdd(a, (void *)s); + } + else if ((buffer = strdup(s)) == NULL) + status = 0; + else + { + for (start = end = buffer; *end; start = end) + { + /* + * Find the end of the current delimited string and see if we need to add + * it... + */ + + if ((end = strchr(start, ',')) != NULL) + *end++ = '\0'; + else + end = start + strlen(start); + + if (!cupsArrayFind(a, start)) + status &= cupsArrayAdd(a, start); + } + + free(buffer); + } + + return (status); +} + + +/* + * 'cupsArrayClear()' - Clear the array. + * + * This function is equivalent to removing all elements in the array. + * The caller is responsible for freeing the memory used by the + * elements themselves. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +cupsArrayClear(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return; + + /* + * Free the existing elements as needed.. + */ + + if (a->freefunc) + { + int i; /* Looping var */ + void **e; /* Current element */ + + for (i = a->num_elements, e = a->elements; i > 0; i --, e ++) + (a->freefunc)(*e, a->data); + } + + /* + * Set the number of elements to 0; we don't actually free the memory + * here - that is done in cupsArrayDelete()... + */ + + a->num_elements = 0; + a->current = -1; + a->insert = -1; + a->unique = 1; + a->num_saved = 0; +} + + +/* + * 'cupsArrayCount()' - Get the number of elements in the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Number of elements */ +cupsArrayCount(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return (0); + + /* + * Return the number of elements... + */ + + return (a->num_elements); +} + + +/* + * 'cupsArrayCurrent()' - Return the current element in the array. + * + * The current element is undefined until you call @link cupsArrayFind@, + * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - Element */ +cupsArrayCurrent(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return (NULL); + + /* + * Return the current element... + */ + + if (a->current >= 0 && a->current < a->num_elements) + return (a->elements[a->current]); + else + return (NULL); +} + + +/* + * 'cupsArrayDelete()' - Free all memory used by the array. + * + * The caller is responsible for freeing the memory used by the + * elements themselves. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +cupsArrayDelete(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return; + + /* + * Free the elements if we have a free function (otherwise the caller is + * responsible for doing the dirty work...) + */ + + if (a->freefunc) + { + int i; /* Looping var */ + void **e; /* Current element */ + + for (i = a->num_elements, e = a->elements; i > 0; i --, e ++) + (a->freefunc)(*e, a->data); + } + + /* + * Free the array of element pointers... + */ + + if (a->alloc_elements) + free(a->elements); + + if (a->hashsize) + free(a->hash); + + free(a); +} + + +/* + * 'cupsArrayDup()' - Duplicate the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_array_t * /* O - Duplicate array */ +cupsArrayDup(cups_array_t *a) /* I - Array */ +{ + cups_array_t *da; /* Duplicate array */ + + + /* + * Range check input... + */ + + if (!a) + return (NULL); + + /* + * Allocate memory for the array... + */ + + da = calloc(1, sizeof(cups_array_t)); + if (!da) + return (NULL); + + da->compare = a->compare; + da->data = a->data; + da->current = a->current; + da->insert = a->insert; + da->unique = a->unique; + da->num_saved = a->num_saved; + + memcpy(da->saved, a->saved, sizeof(a->saved)); + + if (a->num_elements) + { + /* + * Allocate memory for the elements... + */ + + da->elements = malloc(a->num_elements * sizeof(void *)); + if (!da->elements) + { + free(da); + return (NULL); + } + + /* + * Copy the element pointers... + */ + + if (a->copyfunc) + { + /* + * Use the copy function to make a copy of each element... + */ + + int i; /* Looping var */ + + for (i = 0; i < a->num_elements; i ++) + da->elements[i] = (a->copyfunc)(a->elements[i], a->data); + } + else + { + /* + * Just copy raw pointers... + */ + + memcpy(da->elements, a->elements, a->num_elements * sizeof(void *)); + } + + da->num_elements = a->num_elements; + da->alloc_elements = a->num_elements; + } + + /* + * Return the new array... + */ + + return (da); +} + + +/* + * 'cupsArrayFind()' - Find an element in the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - Element found or @code NULL@ */ +cupsArrayFind(cups_array_t *a, /* I - Array */ + void *e) /* I - Element */ +{ + int current, /* Current element */ + diff, /* Difference */ + hash; /* Hash index */ + + + /* + * Range check input... + */ + + if (!a || !e) + return (NULL); + + /* + * See if we have any elements... + */ + + if (!a->num_elements) + return (NULL); + + /* + * Yes, look for a match... + */ + + if (a->hash) + { + hash = (*(a->hashfunc))(e, a->data); + + if (hash < 0 || hash >= a->hashsize) + { + current = a->current; + hash = -1; + } + else + { + current = a->hash[hash]; + + if (current < 0 || current >= a->num_elements) + current = a->current; + } + } + else + { + current = a->current; + hash = -1; + } + + current = cups_array_find(a, e, current, &diff); + if (!diff) + { + /* + * Found a match! If the array does not contain unique values, find + * the first element that is the same... + */ + + if (!a->unique && a->compare) + { + /* + * The array is not unique, find the first match... + */ + + while (current > 0 && !(*(a->compare))(e, a->elements[current - 1], + a->data)) + current --; + } + + a->current = current; + + if (hash >= 0) + a->hash[hash] = current; + + return (a->elements[current]); + } + else + { + /* + * No match... + */ + + a->current = -1; + + return (NULL); + } +} + + +/* + * 'cupsArrayFirst()' - Get the first element in the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - First element or @code NULL@ if the array is empty */ +cupsArrayFirst(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return (NULL); + + /* + * Return the first element... + */ + + a->current = 0; + + return (cupsArrayCurrent(a)); +} + + +/* + * 'cupsArrayGetIndex()' - Get the index of the current element. + * + * The current element is undefined until you call @link cupsArrayFind@, + * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +int /* O - Index of the current element, starting at 0 */ +cupsArrayGetIndex(cups_array_t *a) /* I - Array */ +{ + if (!a) + return (-1); + else + return (a->current); +} + + +/* + * 'cupsArrayGetInsert()' - Get the index of the last inserted element. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +int /* O - Index of the last inserted element, starting at 0 */ +cupsArrayGetInsert(cups_array_t *a) /* I - Array */ +{ + if (!a) + return (-1); + else + return (a->insert); +} + + +/* + * 'cupsArrayIndex()' - Get the N-th element in the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - N-th element or @code NULL@ */ +cupsArrayIndex(cups_array_t *a, /* I - Array */ + int n) /* I - Index into array, starting at 0 */ +{ + if (!a) + return (NULL); + + a->current = n; + + return (cupsArrayCurrent(a)); +} + + +/* + * 'cupsArrayInsert()' - Insert an element in the array. + * + * When inserting an element in a sorted array, non-unique elements are + * inserted at the beginning of the run of identical elements. For unsorted + * arrays, the element is inserted at the beginning of the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on failure, 1 on success */ +cupsArrayInsert(cups_array_t *a, /* I - Array */ + void *e) /* I - Element */ +{ + DEBUG_printf(("2cupsArrayInsert(a=%p, e=%p)", a, e)); + + /* + * Range check input... + */ + + if (!a || !e) + { + DEBUG_puts("3cupsArrayInsert: returning 0"); + return (0); + } + + /* + * Insert the element... + */ + + return (cups_array_add(a, e, 1)); +} + + +/* + * 'cupsArrayLast()' - Get the last element in the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - Last element or @code NULL@ if the array is empty */ +cupsArrayLast(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return (NULL); + + /* + * Return the last element... + */ + + a->current = a->num_elements - 1; + + return (cupsArrayCurrent(a)); +} + + +/* + * 'cupsArrayNew()' - Create a new array. + * + * The comparison function ("f") is used to create a sorted array. The function + * receives pointers to two elements and the user data pointer ("d") - the user + * data pointer argument can safely be omitted when not required so functions + * like @code strcmp@ can be used for sorted string arrays. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_array_t * /* O - Array */ +cupsArrayNew(cups_array_func_t f, /* I - Comparison function or @code NULL@ for an unsorted array */ + void *d) /* I - User data pointer or @code NULL@ */ +{ + return (cupsArrayNew3(f, d, 0, 0, 0, 0)); +} + + +/* + * 'cupsArrayNew2()' - Create a new array with hash. + * + * The comparison function ("f") is used to create a sorted array. The function + * receives pointers to two elements and the user data pointer ("d") - the user + * data pointer argument can safely be omitted when not required so functions + * like @code strcmp@ can be used for sorted string arrays. + * + * The hash function ("h") is used to implement cached lookups with the + * specified hash size ("hsize"). + * + * @since CUPS 1.3/OS X 10.5@ + */ + +cups_array_t * /* O - Array */ +cupsArrayNew2(cups_array_func_t f, /* I - Comparison function or @code NULL@ for an unsorted array */ + void *d, /* I - User data or @code NULL@ */ + cups_ahash_func_t h, /* I - Hash function or @code NULL@ for unhashed lookups */ + int hsize) /* I - Hash size (>= 0) */ +{ + return (cupsArrayNew3(f, d, h, hsize, 0, 0)); +} + + +/* + * 'cupsArrayNew3()' - Create a new array with hash and/or free function. + * + * The comparison function ("f") is used to create a sorted array. The function + * receives pointers to two elements and the user data pointer ("d") - the user + * data pointer argument can safely be omitted when not required so functions + * like @code strcmp@ can be used for sorted string arrays. + * + * The hash function ("h") is used to implement cached lookups with the + * specified hash size ("hsize"). + * + * The copy function ("cf") is used to automatically copy/retain elements when + * added or the array is copied. + * + * The free function ("cf") is used to automatically free/release elements when + * removed or the array is deleted. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +cups_array_t * /* O - Array */ +cupsArrayNew3(cups_array_func_t f, /* I - Comparison function or @code NULL@ for an unsorted array */ + void *d, /* I - User data or @code NULL@ */ + cups_ahash_func_t h, /* I - Hash function or @code NULL@ for unhashed lookups */ + int hsize, /* I - Hash size (>= 0) */ + cups_acopy_func_t cf, /* I - Copy function */ + cups_afree_func_t ff) /* I - Free function */ +{ + cups_array_t *a; /* Array */ + + + /* + * Allocate memory for the array... + */ + + a = calloc(1, sizeof(cups_array_t)); + if (!a) + return (NULL); + + a->compare = f; + a->data = d; + a->current = -1; + a->insert = -1; + a->num_saved = 0; + a->unique = 1; + + if (hsize > 0 && h) + { + a->hashfunc = h; + a->hashsize = hsize; + a->hash = malloc(hsize * sizeof(int)); + + if (!a->hash) + { + free(a); + return (NULL); + } + + memset(a->hash, -1, hsize * sizeof(int)); + } + + a->copyfunc = cf; + a->freefunc = ff; + + return (a); +} + + +/* + * '_cupsArrayNewStrings()' - Create a new array of comma-delimited strings. + * + * Note: The array automatically manages copies of the strings passed. If the + * string pointer "s" is NULL or the empty string, no strings are added to the + * newly created array. + */ + +cups_array_t * /* O - Array */ +_cupsArrayNewStrings(const char *s) /* I - Comma-delimited strings or NULL */ +{ + cups_array_t *a; /* Array */ + + + if ((a = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, + (cups_acopy_func_t)_cupsStrAlloc, + (cups_afree_func_t)_cupsStrFree)) != NULL) + _cupsArrayAddStrings(a, s); + + return (a); +} + + +/* + * 'cupsArrayNext()' - Get the next element in the array. + * + * This function is equivalent to "cupsArrayIndex(a, cupsArrayGetIndex(a) + 1)". + * + * The next element is undefined until you call @link cupsArrayFind@, + * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@ + * to set the current element. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - Next element or @code NULL@ */ +cupsArrayNext(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return (NULL); + + /* + * Return the next element... + */ + + if (a->current < a->num_elements) + a->current ++; + + return (cupsArrayCurrent(a)); +} + + +/* + * 'cupsArrayPrev()' - Get the previous element in the array. + * + * This function is equivalent to "cupsArrayIndex(a, cupsArrayGetIndex(a) - 1)". + * + * The previous element is undefined until you call @link cupsArrayFind@, + * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@ + * to set the current element. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - Previous element or @code NULL@ */ +cupsArrayPrev(cups_array_t *a) /* I - Array */ +{ + /* + * Range check input... + */ + + if (!a) + return (NULL); + + /* + * Return the previous element... + */ + + if (a->current >= 0) + a->current --; + + return (cupsArrayCurrent(a)); +} + + +/* + * 'cupsArrayRemove()' - Remove an element from the array. + * + * If more than one element matches "e", only the first matching element is + * removed. + * + * The caller is responsible for freeing the memory used by the + * removed element. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsArrayRemove(cups_array_t *a, /* I - Array */ + void *e) /* I - Element */ +{ + int i, /* Looping var */ + current, /* Current element */ + diff; /* Difference */ + + + /* + * Range check input... + */ + + if (!a || !e) + return (0); + + /* + * See if the element is in the array... + */ + + if (!a->num_elements) + return (0); + + current = cups_array_find(a, e, a->current, &diff); + if (diff) + return (0); + + /* + * Yes, now remove it... + */ + + a->num_elements --; + + if (a->freefunc) + (a->freefunc)(a->elements[current], a->data); + + if (current < a->num_elements) + memmove(a->elements + current, a->elements + current + 1, + (a->num_elements - current) * sizeof(void *)); + + if (current <= a->current) + a->current --; + + if (current < a->insert) + a->insert --; + else if (current == a->insert) + a->insert = -1; + + for (i = 0; i < a->num_saved; i ++) + if (current <= a->saved[i]) + a->saved[i] --; + + if (a->num_elements <= 1) + a->unique = 1; + + return (1); +} + + +/* + * 'cupsArrayRestore()' - Reset the current element to the last @link cupsArraySave@. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - New current element */ +cupsArrayRestore(cups_array_t *a) /* I - Array */ +{ + if (!a) + return (NULL); + + if (a->num_saved <= 0) + return (NULL); + + a->num_saved --; + a->current = a->saved[a->num_saved]; + + if (a->current >= 0 && a->current < a->num_elements) + return (a->elements[a->current]); + else + return (NULL); +} + + +/* + * 'cupsArraySave()' - Mark the current element for a later @link cupsArrayRestore@. + * + * The current element is undefined until you call @link cupsArrayFind@, + * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@ + * to set the current element. + * + * The save/restore stack is guaranteed to be at least 32 elements deep. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsArraySave(cups_array_t *a) /* I - Array */ +{ + if (!a) + return (0); + + if (a->num_saved >= _CUPS_MAXSAVE) + return (0); + + a->saved[a->num_saved] = a->current; + a->num_saved ++; + + return (1); +} + + +/* + * 'cupsArrayUserData()' - Return the user data for an array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void * /* O - User data */ +cupsArrayUserData(cups_array_t *a) /* I - Array */ +{ + if (a) + return (a->data); + else + return (NULL); +} + + +/* + * 'cups_array_add()' - Insert or append an element to the array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +static int /* O - 1 on success, 0 on failure */ +cups_array_add(cups_array_t *a, /* I - Array */ + void *e, /* I - Element to add */ + int insert) /* I - 1 = insert, 0 = append */ +{ + int i, /* Looping var */ + current, /* Current element */ + diff; /* Comparison with current element */ + + + DEBUG_printf(("7cups_array_add(a=%p, e=%p, insert=%d)", a, e, insert)); + + /* + * Verify we have room for the new element... + */ + + if (a->num_elements >= a->alloc_elements) + { + /* + * Allocate additional elements; start with 16 elements, then + * double the size until 1024 elements, then add 1024 elements + * thereafter... + */ + + void **temp; /* New array elements */ + int count; /* New allocation count */ + + + if (a->alloc_elements == 0) + { + count = 16; + temp = malloc(count * sizeof(void *)); + } + else + { + if (a->alloc_elements < 1024) + count = a->alloc_elements * 2; + else + count = a->alloc_elements + 1024; + + temp = realloc(a->elements, count * sizeof(void *)); + } + + DEBUG_printf(("9cups_array_add: count=%d", count)); + + if (!temp) + { + DEBUG_puts("9cups_array_add: allocation failed, returning 0"); + return (0); + } + + a->alloc_elements = count; + a->elements = temp; + } + + /* + * Find the insertion point for the new element; if there is no + * compare function or elements, just add it to the beginning or end... + */ + + if (!a->num_elements || !a->compare) + { + /* + * No elements or comparison function, insert/append as needed... + */ + + if (insert) + current = 0; /* Insert at beginning */ + else + current = a->num_elements; /* Append to the end */ + } + else + { + /* + * Do a binary search for the insertion point... + */ + + current = cups_array_find(a, e, a->insert, &diff); + + if (diff > 0) + { + /* + * Insert after the current element... + */ + + current ++; + } + else if (!diff) + { + /* + * Compared equal, make sure we add to the begining or end of + * the current run of equal elements... + */ + + a->unique = 0; + + if (insert) + { + /* + * Insert at beginning of run... + */ + + while (current > 0 && !(*(a->compare))(e, a->elements[current - 1], + a->data)) + current --; + } + else + { + /* + * Append at end of run... + */ + + do + { + current ++; + } + while (current < a->num_elements && + !(*(a->compare))(e, a->elements[current], a->data)); + } + } + } + + /* + * Insert or append the element... + */ + + if (current < a->num_elements) + { + /* + * Shift other elements to the right... + */ + + memmove(a->elements + current + 1, a->elements + current, + (a->num_elements - current) * sizeof(void *)); + + if (a->current >= current) + a->current ++; + + for (i = 0; i < a->num_saved; i ++) + if (a->saved[i] >= current) + a->saved[i] ++; + + DEBUG_printf(("9cups_array_add: insert element at index %d...", current)); + } +#ifdef DEBUG + else + DEBUG_printf(("9cups_array_add: append element at %d...", current)); +#endif /* DEBUG */ + + if (a->copyfunc) + { + if ((a->elements[current] = (a->copyfunc)(e, a->data)) == NULL) + { + DEBUG_puts("8cups_array_add: Copy function returned NULL, returning 0"); + return (0); + } + } + else + a->elements[current] = e; + + a->num_elements ++; + a->insert = current; + +#ifdef DEBUG + for (current = 0; current < a->num_elements; current ++) + DEBUG_printf(("9cups_array_add: a->elements[%d]=%p", current, + a->elements[current])); +#endif /* DEBUG */ + + DEBUG_puts("9cups_array_add: returning 1"); + + return (1); +} + + +/* + * 'cups_array_find()' - Find an element in the array. + */ + +static int /* O - Index of match */ +cups_array_find(cups_array_t *a, /* I - Array */ + void *e, /* I - Element */ + int prev, /* I - Previous index */ + int *rdiff) /* O - Difference of match */ +{ + int left, /* Left side of search */ + right, /* Right side of search */ + current, /* Current element */ + diff; /* Comparison with current element */ + + + DEBUG_printf(("7cups_array_find(a=%p, e=%p, prev=%d, rdiff=%p)", a, e, prev, + rdiff)); + + if (a->compare) + { + /* + * Do a binary search for the element... + */ + + DEBUG_puts("9cups_array_find: binary search"); + + if (prev >= 0 && prev < a->num_elements) + { + /* + * Start search on either side of previous... + */ + + if ((diff = (*(a->compare))(e, a->elements[prev], a->data)) == 0 || + (diff < 0 && prev == 0) || + (diff > 0 && prev == (a->num_elements - 1))) + { + /* + * Exact or edge match, return it! + */ + + DEBUG_printf(("9cups_array_find: Returning %d, diff=%d", prev, diff)); + + *rdiff = diff; + + return (prev); + } + else if (diff < 0) + { + /* + * Start with previous on right side... + */ + + left = 0; + right = prev; + } + else + { + /* + * Start wih previous on left side... + */ + + left = prev; + right = a->num_elements - 1; + } + } + else + { + /* + * Start search in the middle... + */ + + left = 0; + right = a->num_elements - 1; + } + + do + { + current = (left + right) / 2; + diff = (*(a->compare))(e, a->elements[current], a->data); + + DEBUG_printf(("9cups_array_find: left=%d, right=%d, current=%d, diff=%d", + left, right, current, diff)); + + if (diff == 0) + break; + else if (diff < 0) + right = current; + else + left = current; + } + while ((right - left) > 1); + + if (diff != 0) + { + /* + * Check the last 1 or 2 elements... + */ + + if ((diff = (*(a->compare))(e, a->elements[left], a->data)) <= 0) + current = left; + else + { + diff = (*(a->compare))(e, a->elements[right], a->data); + current = right; + } + } + } + else + { + /* + * Do a linear pointer search... + */ + + DEBUG_puts("9cups_array_find: linear search"); + + diff = 1; + + for (current = 0; current < a->num_elements; current ++) + if (a->elements[current] == e) + { + diff = 0; + break; + } + } + + /* + * Return the closest element and the difference... + */ + + DEBUG_printf(("8cups_array_find: Returning %d, diff=%d", current, diff)); + + *rdiff = diff; + + return (current); +} + + +/* + * End of "$Id: array.c 7616 2008-05-28 00:34:13Z mike $". + */ diff --git a/cups/array.h b/cups/array.h new file mode 100644 index 0000000..466a272 --- /dev/null +++ b/cups/array.h @@ -0,0 +1,92 @@ +/* + * "$Id: array.h 7266 2008-01-29 02:15:29Z mike $" + * + * Sorted array definitions for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2007 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. + */ + +#ifndef _CUPS_ARRAY_H_ +# define _CUPS_ARRAY_H_ + +/* + * Include necessary headers... + */ + +# include "versioning.h" +# include + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Types and structures... + */ + +typedef struct _cups_array_s cups_array_t; + /**** CUPS array type ****/ +typedef int (*cups_array_func_t)(void *first, void *second, void *data); + /**** Array comparison function ****/ +typedef int (*cups_ahash_func_t)(void *element, void *data); + /**** Array hash function ****/ +typedef void *(*cups_acopy_func_t)(void *element, void *data); + /**** Array element copy function ****/ +typedef void (*cups_afree_func_t)(void *element, void *data); + /**** Array element free function ****/ + + +/* + * Functions... + */ + +extern int cupsArrayAdd(cups_array_t *a, void *e) _CUPS_API_1_2; +extern void cupsArrayClear(cups_array_t *a) _CUPS_API_1_2; +extern int cupsArrayCount(cups_array_t *a) _CUPS_API_1_2; +extern void *cupsArrayCurrent(cups_array_t *a) _CUPS_API_1_2; +extern void cupsArrayDelete(cups_array_t *a) _CUPS_API_1_2; +extern cups_array_t *cupsArrayDup(cups_array_t *a) _CUPS_API_1_2; +extern void *cupsArrayFind(cups_array_t *a, void *e) _CUPS_API_1_2; +extern void *cupsArrayFirst(cups_array_t *a) _CUPS_API_1_2; +extern int cupsArrayGetIndex(cups_array_t *a) _CUPS_API_1_3; +extern int cupsArrayGetInsert(cups_array_t *a) _CUPS_API_1_3; +extern void *cupsArrayIndex(cups_array_t *a, int n) _CUPS_API_1_2; +extern int cupsArrayInsert(cups_array_t *a, void *e) _CUPS_API_1_2; +extern void *cupsArrayLast(cups_array_t *a) _CUPS_API_1_2; +extern cups_array_t *cupsArrayNew(cups_array_func_t f, void *d) _CUPS_API_1_2; +extern cups_array_t *cupsArrayNew2(cups_array_func_t f, void *d, + cups_ahash_func_t h, int hsize) _CUPS_API_1_3; +extern cups_array_t *cupsArrayNew3(cups_array_func_t f, void *d, + cups_ahash_func_t h, int hsize, + cups_acopy_func_t cf, + cups_afree_func_t ff) _CUPS_API_1_5; +extern void *cupsArrayNext(cups_array_t *a) _CUPS_API_1_2; +extern void *cupsArrayPrev(cups_array_t *a) _CUPS_API_1_2; +extern int cupsArrayRemove(cups_array_t *a, void *e) _CUPS_API_1_2; +extern void *cupsArrayRestore(cups_array_t *a) _CUPS_API_1_2; +extern int cupsArraySave(cups_array_t *a) _CUPS_API_1_2; +extern void *cupsArrayUserData(cups_array_t *a) _CUPS_API_1_2; + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_ARRAY_H_ */ + +/* + * End of "$Id: array.h 7266 2008-01-29 02:15:29Z mike $". + */ diff --git a/cups/attr.c b/cups/attr.c new file mode 100644 index 0000000..da7a725 --- /dev/null +++ b/cups/attr.c @@ -0,0 +1,335 @@ +/* + * "$Id: attr.c 7584 2008-05-16 22:55:53Z mike $" + * + * PPD model-specific attribute routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 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/". + * + * Contents: + * + * ppdFindAttr() - Find the first matching attribute. + * ppdFindNextAttr() - Find the next matching attribute. + * _ppdNormalizeMakeAndModel() - Normalize a product/make-and-model string. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include "ppd-private.h" + + +/* + * 'ppdFindAttr()' - Find the first matching attribute. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +ppd_attr_t * /* O - Attribute or @code NULL@ if not found */ +ppdFindAttr(ppd_file_t *ppd, /* I - PPD file data */ + const char *name, /* I - Attribute name */ + const char *spec) /* I - Specifier string or @code NULL@ */ +{ + ppd_attr_t key, /* Search key */ + *attr; /* Current attribute */ + + + DEBUG_printf(("2ppdFindAttr(ppd=%p, name=\"%s\", spec=\"%s\")", ppd, name, + spec)); + + /* + * Range check input... + */ + + if (!ppd || !name || ppd->num_attrs == 0) + return (NULL); + + /* + * Search for a matching attribute... + */ + + memset(&key, 0, sizeof(key)); + strlcpy(key.name, name, sizeof(key.name)); + + /* + * Return the first matching attribute, if any... + */ + + if ((attr = (ppd_attr_t *)cupsArrayFind(ppd->sorted_attrs, &key)) != NULL) + { + if (spec) + { + /* + * Loop until we find the first matching attribute for "spec"... + */ + + while (attr && _cups_strcasecmp(spec, attr->spec)) + { + if ((attr = (ppd_attr_t *)cupsArrayNext(ppd->sorted_attrs)) != NULL && + _cups_strcasecmp(attr->name, name)) + attr = NULL; + } + } + } + + return (attr); +} + + +/* + * 'ppdFindNextAttr()' - Find the next matching attribute. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +ppd_attr_t * /* O - Attribute or @code NULL@ if not found */ +ppdFindNextAttr(ppd_file_t *ppd, /* I - PPD file data */ + const char *name, /* I - Attribute name */ + const char *spec) /* I - Specifier string or @code NULL@ */ +{ + ppd_attr_t *attr; /* Current attribute */ + + + /* + * Range check input... + */ + + if (!ppd || !name || ppd->num_attrs == 0) + return (NULL); + + /* + * See if there are more attributes to return... + */ + + while ((attr = (ppd_attr_t *)cupsArrayNext(ppd->sorted_attrs)) != NULL) + { + /* + * Check the next attribute to see if it is a match... + */ + + if (_cups_strcasecmp(attr->name, name)) + { + /* + * Nope, reset the current pointer to the end of the array... + */ + + cupsArrayIndex(ppd->sorted_attrs, cupsArrayCount(ppd->sorted_attrs)); + + return (NULL); + } + + if (!spec || !_cups_strcasecmp(attr->spec, spec)) + break; + } + + /* + * Return the next attribute's value... + */ + + return (attr); +} + + +/* + * '_ppdNormalizeMakeAndModel()' - Normalize a product/make-and-model string. + * + * This function tries to undo the mistakes made by many printer manufacturers + * to produce a clean make-and-model string we can use. + */ + +char * /* O - Normalized make-and-model string or NULL on error */ +_ppdNormalizeMakeAndModel( + const char *make_and_model, /* I - Original make-and-model string */ + char *buffer, /* I - String buffer */ + size_t bufsize) /* I - Size of string buffer */ +{ + char *bufptr; /* Pointer into buffer */ + + + if (!make_and_model || !buffer || bufsize < 1) + { + if (buffer) + *buffer = '\0'; + + return (NULL); + } + + /* + * Skip leading whitespace... + */ + + while (_cups_isspace(*make_and_model)) + make_and_model ++; + + /* + * Remove parenthesis and add manufacturers as needed... + */ + + if (make_and_model[0] == '(') + { + strlcpy(buffer, make_and_model + 1, bufsize); + + if ((bufptr = strrchr(buffer, ')')) != NULL) + *bufptr = '\0'; + } + else if (!_cups_strncasecmp(make_and_model, "XPrint", 6)) + { + /* + * Xerox XPrint... + */ + + snprintf(buffer, bufsize, "Xerox %s", make_and_model); + } + else if (!_cups_strncasecmp(make_and_model, "Eastman", 7)) + { + /* + * Kodak... + */ + + snprintf(buffer, bufsize, "Kodak %s", make_and_model + 7); + } + else if (!_cups_strncasecmp(make_and_model, "laserwriter", 11)) + { + /* + * Apple LaserWriter... + */ + + snprintf(buffer, bufsize, "Apple LaserWriter%s", make_and_model + 11); + } + else if (!_cups_strncasecmp(make_and_model, "colorpoint", 10)) + { + /* + * Seiko... + */ + + snprintf(buffer, bufsize, "Seiko %s", make_and_model); + } + else if (!_cups_strncasecmp(make_and_model, "fiery", 5)) + { + /* + * EFI... + */ + + snprintf(buffer, bufsize, "EFI %s", make_and_model); + } + else if (!_cups_strncasecmp(make_and_model, "ps ", 3) || + !_cups_strncasecmp(make_and_model, "colorpass", 9)) + { + /* + * Canon... + */ + + snprintf(buffer, bufsize, "Canon %s", make_and_model); + } + else if (!_cups_strncasecmp(make_and_model, "primera", 7)) + { + /* + * Fargo... + */ + + snprintf(buffer, bufsize, "Fargo %s", make_and_model); + } + else if (!_cups_strncasecmp(make_and_model, "designjet", 9) || + !_cups_strncasecmp(make_and_model, "deskjet", 7)) + { + /* + * HP... + */ + + snprintf(buffer, bufsize, "HP %s", make_and_model); + } + else + strlcpy(buffer, make_and_model, bufsize); + + /* + * Clean up the make... + */ + + if (!_cups_strncasecmp(buffer, "agfa", 4)) + { + /* + * Replace with AGFA (all uppercase)... + */ + + buffer[0] = 'A'; + buffer[1] = 'G'; + buffer[2] = 'F'; + buffer[3] = 'A'; + } + else if (!_cups_strncasecmp(buffer, "Hewlett-Packard hp ", 19)) + { + /* + * Just put "HP" on the front... + */ + + buffer[0] = 'H'; + buffer[1] = 'P'; + _cups_strcpy(buffer + 2, buffer + 18); + } + else if (!_cups_strncasecmp(buffer, "Hewlett-Packard ", 16)) + { + /* + * Just put "HP" on the front... + */ + + buffer[0] = 'H'; + buffer[1] = 'P'; + _cups_strcpy(buffer + 2, buffer + 15); + } + else if (!_cups_strncasecmp(buffer, "Lexmark International", 21)) + { + /* + * Strip "International"... + */ + + _cups_strcpy(buffer + 8, buffer + 21); + } + else if (!_cups_strncasecmp(buffer, "herk", 4)) + { + /* + * Replace with LHAG... + */ + + buffer[0] = 'L'; + buffer[1] = 'H'; + buffer[2] = 'A'; + buffer[3] = 'G'; + } + else if (!_cups_strncasecmp(buffer, "linotype", 8)) + { + /* + * Replace with LHAG... + */ + + buffer[0] = 'L'; + buffer[1] = 'H'; + buffer[2] = 'A'; + buffer[3] = 'G'; + _cups_strcpy(buffer + 4, buffer + 8); + } + + /* + * Remove trailing whitespace and return... + */ + + for (bufptr = buffer + strlen(buffer) - 1; + bufptr >= buffer && _cups_isspace(*bufptr); + bufptr --); + + bufptr[1] = '\0'; + + return (buffer[0] ? buffer : NULL); +} + + +/* + * End of "$Id: attr.c 7584 2008-05-16 22:55:53Z mike $". + */ diff --git a/cups/auth.c b/cups/auth.c new file mode 100644 index 0000000..659d918 --- /dev/null +++ b/cups/auth.c @@ -0,0 +1,885 @@ +/* + * "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $" + * + * Authentication functions for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products. + * + * This file contains Kerberos support code, copyright 2006 by + * Jelmer Vernooij. + * + * 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. + * + * Contents: + * + * cupsDoAuthentication() - Authenticate a request. + * _cupsSetNegotiateAuthString() - Set the Kerberos authentication string. + * cups_gss_acquire() - Kerberos credentials callback. + * cups_gss_getname() - Get CUPS service credentials for + * authentication. + * cups_gss_printf() - Show debug error messages from GSSAPI. + * cups_local_auth() - Get the local authorization certificate if + * available/applicable. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#if defined(WIN32) || defined(__EMX__) +# include +#else +# include +#endif /* WIN32 || __EMX__ */ + +#if HAVE_AUTHORIZATION_H +# include +# ifdef HAVE_SECBASEPRIV_H +# include +# else +extern const char *cssmErrorString(int error); +# endif /* HAVE_SECBASEPRIV_H */ +#endif /* HAVE_AUTHORIZATION_H */ + +#if defined(SO_PEERCRED) && defined(AF_LOCAL) +# include +#endif /* SO_PEERCRED && AF_LOCAL */ + + +/* + * Local functions... + */ + +#ifdef HAVE_GSSAPI +# ifdef HAVE_GSS_ACQUIRE_CRED_EX_F +# ifdef HAVE_GSS_GSSAPI_SPI_H +# include +# else +# define GSS_AUTH_IDENTITY_TYPE_1 1 +# define gss_acquire_cred_ex_f __ApplePrivate_gss_acquire_cred_ex_f +typedef struct gss_auth_identity +{ + uint32_t type; + uint32_t flags; + char *username; + char *realm; + char *password; + gss_buffer_t *credentialsRef; +} gss_auth_identity_desc; +extern OM_uint32 gss_acquire_cred_ex_f(gss_status_id_t, const gss_name_t, + OM_uint32, OM_uint32, const gss_OID, + gss_cred_usage_t, gss_auth_identity_t, + void *, void (*)(void *, OM_uint32, + gss_status_id_t, + gss_cred_id_t, + gss_OID_set, + OM_uint32)); +# endif /* HAVE_GSS_GSSAPI_SPI_H */ +# include +typedef struct _cups_gss_acquire_s /* Acquire callback data */ +{ + dispatch_semaphore_t sem; /* Synchronization semaphore */ + OM_uint32 major; /* Returned status code */ + gss_cred_id_t creds; /* Returned credentials */ +} _cups_gss_acquire_t; + +static void cups_gss_acquire(void *ctx, OM_uint32 major, + gss_status_id_t status, + gss_cred_id_t creds, gss_OID_set oids, + OM_uint32 time_rec); +# endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */ +static gss_name_t cups_gss_getname(http_t *http, const char *service_name); +# ifdef DEBUG +static void cups_gss_printf(OM_uint32 major_status, OM_uint32 minor_status, + const char *message); +# else +# define cups_gss_printf(major, minor, message) +# endif /* DEBUG */ +#endif /* HAVE_GSSAPI */ +static int cups_local_auth(http_t *http); + + +/* + * 'cupsDoAuthentication()' - Authenticate a request. + * + * This function should be called in response to a @code HTTP_UNAUTHORIZED@ + * status, prior to resubmitting your request. + * + * @since CUPS 1.1.20/OS X 10.4@ + */ + +int /* O - 0 on success, -1 on error */ +cupsDoAuthentication( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *method, /* I - Request method ("GET", "POST", "PUT") */ + const char *resource) /* I - Resource path */ +{ + const char *password, /* Password string */ + *www_auth; /* WWW-Authenticate header */ + char prompt[1024], /* Prompt for user */ + realm[HTTP_MAX_VALUE], /* realm="xyz" string */ + nonce[HTTP_MAX_VALUE]; /* nonce="xyz" string */ + int localauth; /* Local authentication result */ + _cups_globals_t *cg; /* Global data */ + + + DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")", + http, method, resource)); + + if (!http) + http = _cupsConnect(); + + if (!http || !method || !resource) + return (-1); + + DEBUG_printf(("2cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"", + http->digest_tries, http->userpass)); + DEBUG_printf(("2cupsDoAuthentication: WWW-Authenticate=\"%s\"", + httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE))); + + /* + * Clear the current authentication string... + */ + + httpSetAuthString(http, NULL, NULL); + + /* + * See if we can do local authentication... + */ + + if (http->digest_tries < 3) + { + if ((localauth = cups_local_auth(http)) == 0) + { + DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"", + http->authstring)); + + if (http->status == HTTP_UNAUTHORIZED) + http->digest_tries ++; + + return (0); + } + else if (localauth == -1) + { + http->status = HTTP_AUTHORIZATION_CANCELED; + return (-1); /* Error or canceled */ + } + } + + /* + * Nope, see if we should retry the current username:password... + */ + + www_auth = http->fields[HTTP_FIELD_WWW_AUTHENTICATE]; + + if ((http->digest_tries > 1 || !http->userpass[0]) && + (!_cups_strncasecmp(www_auth, "Basic", 5) || + !_cups_strncasecmp(www_auth, "Digest", 6))) + { + /* + * Nope - get a new password from the user... + */ + + cg = _cupsGlobals(); + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + snprintf(prompt, sizeof(prompt), + _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), + cupsUser(), + http->hostname[0] == '/' ? "localhost" : http->hostname); + + http->digest_tries = _cups_strncasecmp(www_auth, "Digest", 6) != 0; + http->userpass[0] = '\0'; + + if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL) + { + http->status = HTTP_AUTHORIZATION_CANCELED; + return (-1); + } + + snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(), + password); + } + else if (http->status == HTTP_UNAUTHORIZED) + http->digest_tries ++; + + if (http->status == HTTP_UNAUTHORIZED && http->digest_tries >= 3) + { + DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)", + http->digest_tries)); + + http->status = HTTP_AUTHORIZATION_CANCELED; + return (-1); + } + + /* + * Got a password; encode it for the server... + */ + +#ifdef HAVE_GSSAPI + if (!_cups_strncasecmp(www_auth, "Negotiate", 9)) + { + /* + * Kerberos authentication... + */ + + if (_cupsSetNegotiateAuthString(http, method, resource)) + { + http->status = HTTP_AUTHORIZATION_CANCELED; + return (-1); + } + } + else +#endif /* HAVE_GSSAPI */ + if (!_cups_strncasecmp(www_auth, "Basic", 5)) + { + /* + * Basic authentication... + */ + + char encode[256]; /* Base64 buffer */ + + + httpEncode64_2(encode, sizeof(encode), http->userpass, + (int)strlen(http->userpass)); + httpSetAuthString(http, "Basic", encode); + } + else if (!_cups_strncasecmp(www_auth, "Digest", 6)) + { + /* + * Digest authentication... + */ + + char encode[33], /* MD5 buffer */ + digest[1024]; /* Digest auth data */ + + + httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm); + httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce); + + httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode); + httpMD5Final(nonce, method, resource, encode); + snprintf(digest, sizeof(digest), + "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", " + "response=\"%s\"", cupsUser(), realm, nonce, resource, encode); + httpSetAuthString(http, "Digest", digest); + } + else + { + DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"", + www_auth)); + http->status = HTTP_AUTHORIZATION_CANCELED; + return (-1); + } + + DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring)); + + return (0); +} + + +#ifdef HAVE_GSSAPI +/* + * '_cupsSetNegotiateAuthString()' - Set the Kerberos authentication string. + */ + +int /* O - 0 on success, -1 on error */ +_cupsSetNegotiateAuthString( + http_t *http, /* I - Connection to server */ + const char *method, /* I - Request method ("GET", "POST", "PUT") */ + const char *resource) /* I - Resource path */ +{ + OM_uint32 minor_status, /* Minor status code */ + major_status; /* Major status code */ + gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER; + /* Output token */ + + + (void)method; + (void)resource; + +# ifdef __APPLE__ + /* + * If the weak-linked GSSAPI/Kerberos library is not present, don't try + * to use it... + */ + + if (gss_init_sec_context == NULL) + { + DEBUG_puts("1_cupsSetNegotiateAuthString: Weak-linked GSSAPI/Kerberos " + "framework is not present"); + return (-1); + } +# endif /* __APPLE__ */ + + if (http->gssname == GSS_C_NO_NAME) + { + http->gssname = cups_gss_getname(http, _cupsGSSServiceName()); + } + + if (http->gssctx != GSS_C_NO_CONTEXT) + { + gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER); + http->gssctx = GSS_C_NO_CONTEXT; + } + + major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL, + &http->gssctx, + http->gssname, http->gssmech, + GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG, + GSS_C_INDEFINITE, + GSS_C_NO_CHANNEL_BINDINGS, + GSS_C_NO_BUFFER, &http->gssmech, + &output_token, NULL, NULL); + +#ifdef HAVE_GSS_ACQUIRE_CRED_EX_F + if (major_status == GSS_S_NO_CRED) + { + /* + * Ask the user for credentials... + */ + + char prompt[1024], /* Prompt for user */ + userbuf[256]; /* Kerberos username */ + const char *username, /* Username string */ + *password; /* Password string */ + _cups_gss_acquire_t data; /* Callback data */ + gss_auth_identity_desc identity; /* Kerberos user identity */ + _cups_globals_t *cg = _cupsGlobals(); + /* Per-thread global data */ + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + snprintf(prompt, sizeof(prompt), + _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), + cupsUser(), http->gsshost); + + if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL) + return (-1); + + /* + * Try to acquire credentials... + */ + + username = cupsUser(); + if (!strchr(username, '@')) + { + snprintf(userbuf, sizeof(userbuf), "%s@%s", username, http->gsshost); + username = userbuf; + } + + identity.type = GSS_AUTH_IDENTITY_TYPE_1; + identity.flags = 0; + identity.username = (char *)username; + identity.realm = (char *)""; + identity.password = (char *)password; + identity.credentialsRef = NULL; + + data.sem = dispatch_semaphore_create(0); + data.major = 0; + data.creds = NULL; + + if (data.sem) + { + major_status = gss_acquire_cred_ex_f(NULL, GSS_C_NO_NAME, 0, + GSS_C_INDEFINITE, GSS_KRB5_MECHANISM, + GSS_C_INITIATE, &identity, &data, + cups_gss_acquire); + + if (major_status == GSS_S_COMPLETE) + { + dispatch_semaphore_wait(data.sem, DISPATCH_TIME_FOREVER); + major_status = data.major; + } + + dispatch_release(data.sem); + + if (major_status == GSS_S_COMPLETE) + { + OM_uint32 release_minor; /* Minor status from releasing creds */ + + major_status = gss_init_sec_context(&minor_status, data.creds, + &http->gssctx, + http->gssname, http->gssmech, + GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG, + GSS_C_INDEFINITE, + GSS_C_NO_CHANNEL_BINDINGS, + GSS_C_NO_BUFFER, &http->gssmech, + &output_token, NULL, NULL); + gss_release_cred(&release_minor, &data.creds); + } + } + } +#endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */ + + if (GSS_ERROR(major_status)) + { + cups_gss_printf(major_status, minor_status, + "_cupsSetNegotiateAuthString: Unable to initialize " + "security context"); + return (-1); + } + +#ifdef DEBUG + else if (major_status == GSS_S_CONTINUE_NEEDED) + cups_gss_printf(major_status, minor_status, + "_cupsSetNegotiateAuthString: Continuation needed!"); +#endif /* DEBUG */ + + if (output_token.length > 0 && output_token.length <= 65536) + { + /* + * Allocate the authorization string since Windows KDCs can have + * arbitrarily large credentials... + */ + + int authsize = 10 + /* "Negotiate " */ + output_token.length * 4 / 3 + 1 + /* Base64 */ + 1; /* nul */ + + httpSetAuthString(http, NULL, NULL); + + if ((http->authstring = malloc(authsize)) == NULL) + { + http->authstring = http->_authstring; + authsize = sizeof(http->_authstring); + } + + strcpy(http->authstring, "Negotiate "); + httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value, + output_token.length); + + gss_release_buffer(&minor_status, &output_token); + } + else + { + DEBUG_printf(("1_cupsSetNegotiateAuthString: Kerberos credentials too " + "large - %d bytes!", (int)output_token.length)); + gss_release_buffer(&minor_status, &output_token); + + return (-1); + } + + return (0); +} + + +# ifdef HAVE_GSS_ACQUIRE_CRED_EX_F +/* + * 'cups_gss_acquire()' - Kerberos credentials callback. + */ +static void +cups_gss_acquire( + void *ctx, /* I - Caller context */ + OM_uint32 major, /* I - Major error code */ + gss_status_id_t status, /* I - Status (unused) */ + gss_cred_id_t creds, /* I - Credentials (if any) */ + gss_OID_set oids, /* I - Mechanism OIDs (unused) */ + OM_uint32 time_rec) /* I - Timestamp (unused) */ +{ + uint32_t min; /* Minor error code */ + _cups_gss_acquire_t *data; /* Callback data */ + + + (void)status; + (void)time_rec; + + data = (_cups_gss_acquire_t *)ctx; + data->major = major; + data->creds = creds; + + gss_release_oid_set(&min, &oids); + dispatch_semaphore_signal(data->sem); +} +# endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */ + + +/* + * 'cups_gss_getname()' - Get CUPS service credentials for authentication. + */ + +static gss_name_t /* O - Server name */ +cups_gss_getname( + http_t *http, /* I - Connection to server */ + const char *service_name) /* I - Service name */ +{ + gss_buffer_desc token = GSS_C_EMPTY_BUFFER; + /* Service token */ + OM_uint32 major_status, /* Major status code */ + minor_status; /* Minor status code */ + gss_name_t server_name; /* Server name */ + char buf[1024]; /* Name buffer */ + + + DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http, + service_name)); + + + /* + * Get the hostname... + */ + + if (!http->gsshost[0]) + { + httpGetHostname(http, http->gsshost, sizeof(http->gsshost)); + + if (!strcmp(http->gsshost, "localhost")) + { + if (gethostname(http->gsshost, sizeof(http->gsshost)) < 0) + { + DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s", + strerror(errno))); + http->gsshost[0] = '\0'; + return (NULL); + } + + if (!strchr(http->gsshost, '.')) + { + /* + * The hostname is not a FQDN, so look it up... + */ + + struct hostent *host; /* Host entry to get FQDN */ + + if ((host = gethostbyname(http->gsshost)) != NULL && host->h_name) + { + /* + * Use the resolved hostname... + */ + + strlcpy(http->gsshost, host->h_name, sizeof(http->gsshost)); + } + else + { + DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.", + http->gsshost)); + http->gsshost[0] = '\0'; + return (NULL); + } + } + } + } + + /* + * Get a service name we can use for authentication purposes... + */ + + snprintf(buf, sizeof(buf), "%s@%s", service_name, http->gsshost); + + DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf)); + + token.value = buf; + token.length = strlen(buf); + server_name = GSS_C_NO_NAME; + major_status = gss_import_name(&minor_status, &token, + GSS_C_NT_HOSTBASED_SERVICE, + &server_name); + + if (GSS_ERROR(major_status)) + { + cups_gss_printf(major_status, minor_status, + "cups_gss_getname: gss_import_name() failed"); + return (NULL); + } + + return (server_name); +} + + +# ifdef DEBUG +/* + * 'cups_gss_printf()' - Show debug error messages from GSSAPI. + */ + +static void +cups_gss_printf(OM_uint32 major_status,/* I - Major status code */ + OM_uint32 minor_status,/* I - Minor status code */ + const char *message) /* I - Prefix for error message */ +{ + OM_uint32 err_major_status, /* Major status code for display */ + err_minor_status; /* Minor status code for display */ + OM_uint32 msg_ctx; /* Message context */ + gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER, + /* Major status message */ + minor_status_string = GSS_C_EMPTY_BUFFER; + /* Minor status message */ + + + msg_ctx = 0; + err_major_status = gss_display_status(&err_minor_status, + major_status, + GSS_C_GSS_CODE, + GSS_C_NO_OID, + &msg_ctx, + &major_status_string); + + if (!GSS_ERROR(err_major_status)) + gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE, + GSS_C_NULL_OID, &msg_ctx, &minor_status_string); + + DEBUG_printf(("1%s: %s, %s", message, (char *)major_status_string.value, + (char *)minor_status_string.value)); + + gss_release_buffer(&err_minor_status, &major_status_string); + gss_release_buffer(&err_minor_status, &minor_status_string); +} +# endif /* DEBUG */ +#endif /* HAVE_GSSAPI */ + + +/* + * 'cups_local_auth()' - Get the local authorization certificate if + * available/applicable. + */ + +static int /* O - 0 if available */ + /* 1 if not available */ + /* -1 error */ +cups_local_auth(http_t *http) /* I - HTTP connection to server */ +{ +#if defined(WIN32) || defined(__EMX__) + /* + * Currently WIN32 and OS-2 do not support the CUPS server... + */ + + return (1); +#else + int pid; /* Current process ID */ + FILE *fp; /* Certificate file */ + char trc[16], /* Try Root Certificate parameter */ + filename[1024]; /* Certificate filename */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ +# if defined(HAVE_AUTHORIZATION_H) + OSStatus status; /* Status */ + AuthorizationItem auth_right; /* Authorization right */ + AuthorizationRights auth_rights; /* Authorization rights */ + AuthorizationFlags auth_flags; /* Authorization flags */ + AuthorizationExternalForm auth_extrn; /* Authorization ref external */ + char auth_key[1024]; /* Buffer */ + char buffer[1024]; /* Buffer */ +# endif /* HAVE_AUTHORIZATION_H */ + + + DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", + http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname)); + + /* + * See if we are accessing localhost... + */ + + if (!httpAddrLocalhost(http->hostaddr) && + _cups_strcasecmp(http->hostname, "localhost") != 0) + { + DEBUG_puts("8cups_local_auth: Not a local connection!"); + return (1); + } + +# if defined(HAVE_AUTHORIZATION_H) + /* + * Delete any previous authorization reference... + */ + + if (http->auth_ref) + { + AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); + http->auth_ref = NULL; + } + + if (!getenv("GATEWAY_INTERFACE") && + httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", + auth_key, sizeof(auth_key))) + { + status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, + kAuthorizationFlagDefaults, &http->auth_ref); + if (status != errAuthorizationSuccess) + { + DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)", + (int)status, cssmErrorString(status))); + return (-1); + } + + auth_right.name = auth_key; + auth_right.valueLength = 0; + auth_right.value = NULL; + auth_right.flags = 0; + + auth_rights.count = 1; + auth_rights.items = &auth_right; + + auth_flags = kAuthorizationFlagDefaults | + kAuthorizationFlagPreAuthorize | + kAuthorizationFlagInteractionAllowed | + kAuthorizationFlagExtendRights; + + status = AuthorizationCopyRights(http->auth_ref, &auth_rights, + kAuthorizationEmptyEnvironment, + auth_flags, NULL); + if (status == errAuthorizationSuccess) + status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn); + + if (status == errAuthorizationSuccess) + { + /* + * Set the authorization string and return... + */ + + httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn, + sizeof(auth_extrn)); + + httpSetAuthString(http, "AuthRef", buffer); + + DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", + http->authstring)); + return (0); + } + else if (status == errAuthorizationCanceled) + return (-1); + + DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)", + (int)status, cssmErrorString(status))); + + /* + * Fall through to try certificates... + */ + } +# endif /* HAVE_AUTHORIZATION_H */ + +# if defined(SO_PEERCRED) && defined(AF_LOCAL) + /* + * See if we can authenticate using the peer credentials provided over a + * domain socket; if so, specify "PeerCred username" as the authentication + * information... + */ + + if ( +# ifdef HAVE_GSSAPI + strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) && +# endif /* HAVE_GSSAPI */ +# ifdef HAVE_AUTHORIZATION_H + !httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", + auth_key, sizeof(auth_key)) && +# endif /* HAVE_AUTHORIZATION_H */ + http->hostaddr->addr.sa_family == AF_LOCAL && + !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */ + { + /* + * Verify that the current cupsUser() matches the current UID... + */ + + struct passwd *pwd; /* Password information */ + const char *username; /* Current username */ + + username = cupsUser(); + + if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid()) + { + httpSetAuthString(http, "PeerCred", username); + + DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", + http->authstring)); + + return (0); + } + } +# endif /* SO_PEERCRED && AF_LOCAL */ + + /* + * Try opening a certificate file for this PID. If that fails, + * try the root certificate... + */ + + pid = getpid(); + snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid); + if ((fp = fopen(filename, "r")) == NULL && pid > 0) + { + /* + * No certificate for this PID; see if we can get the root certificate... + */ + + DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s", + filename, strerror(errno))); + +# ifdef HAVE_GSSAPI + if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) + { + /* + * Kerberos required, don't try the root certificate... + */ + + return (1); + } +# endif /* HAVE_GSSAPI */ + +# ifdef HAVE_AUTHORIZATION_H + if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", + auth_key, sizeof(auth_key))) + { + /* + * Don't use the root certificate as a replacement for an authkey... + */ + + return (1); + } +# endif /* HAVE_AUTHORIZATION_H */ + if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc, + sizeof(trc))) + { + /* + * Scheduler doesn't want us to use the root certificate... + */ + + return (1); + } + + snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir); + fp = fopen(filename, "r"); + } + + if (fp) + { + /* + * Read the certificate from the file... + */ + + char certificate[33], /* Certificate string */ + *certptr; /* Pointer to certificate string */ + + certptr = fgets(certificate, sizeof(certificate), fp); + fclose(fp); + + if (certptr) + { + /* + * Set the authorization string and return... + */ + + httpSetAuthString(http, "Local", certificate); + + DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", + http->authstring)); + + return (0); + } + } + + return (1); +#endif /* WIN32 || __EMX__ */ +} + + +/* + * End of "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $". + */ diff --git a/cups/backchannel.c b/cups/backchannel.c new file mode 100644 index 0000000..3ee1dbc --- /dev/null +++ b/cups/backchannel.c @@ -0,0 +1,199 @@ +/* + * "$Id: backchannel.c 7616 2008-05-28 00:34:13Z mike $" + * + * Backchannel functions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * cupsBackChannelRead() - Read data from the backchannel. + * cupsBackChannelWrite() - Write data to the backchannel. + * cups_setup() - Setup select() + */ + +/* + * Include necessary headers... + */ + +#include "cups.h" +#include +#ifdef WIN32 +# include +# include +#else +# include +#endif /* WIN32 */ + + +/* + * Local functions... + */ + +static void cups_setup(fd_set *set, struct timeval *tval, + double timeout); + + +/* + * 'cupsBackChannelRead()' - Read data from the backchannel. + * + * Reads up to "bytes" bytes from the backchannel/backend. The "timeout" + * parameter controls how many seconds to wait for the data - use 0.0 to + * return immediately if there is no data, -1.0 to wait for data indefinitely. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ssize_t /* O - Bytes read or -1 on error */ +cupsBackChannelRead(char *buffer, /* I - Buffer to read into */ + size_t bytes, /* I - Bytes to read */ + double timeout) /* I - Timeout in seconds, typically 0.0 to poll */ +{ + fd_set input; /* Input set */ + struct timeval tval; /* Timeout value */ + int status; /* Select status */ + + + /* + * Wait for input ready. + */ + + do + { + cups_setup(&input, &tval, timeout); + + if (timeout < 0.0) + status = select(4, &input, NULL, NULL, NULL); + else + status = select(4, &input, NULL, NULL, &tval); + } + while (status < 0 && errno != EINTR && errno != EAGAIN); + + if (status < 0) + return (-1); /* Timeout! */ + + /* + * Read bytes from the pipe... + */ + +#ifdef WIN32 + return ((ssize_t)_read(3, buffer, (unsigned)bytes)); +#else + return (read(3, buffer, bytes)); +#endif /* WIN32 */ +} + + +/* + * 'cupsBackChannelWrite()' - Write data to the backchannel. + * + * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter + * controls how many seconds to wait for the data to be written - use + * 0.0 to return immediately if the data cannot be written, -1.0 to wait + * indefinitely. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ssize_t /* O - Bytes written or -1 on error */ +cupsBackChannelWrite( + const char *buffer, /* I - Buffer to write */ + size_t bytes, /* I - Bytes to write */ + double timeout) /* I - Timeout in seconds, typically 1.0 */ +{ + fd_set output; /* Output set */ + struct timeval tval; /* Timeout value */ + int status; /* Select status */ + ssize_t count; /* Current bytes */ + size_t total; /* Total bytes */ + + + /* + * Write all bytes... + */ + + total = 0; + + while (total < bytes) + { + /* + * Wait for write-ready... + */ + + do + { + cups_setup(&output, &tval, timeout); + + if (timeout < 0.0) + status = select(4, NULL, &output, NULL, NULL); + else + status = select(4, NULL, &output, NULL, &tval); + } + while (status < 0 && errno != EINTR && errno != EAGAIN); + + if (status <= 0) + return (-1); /* Timeout! */ + + /* + * Write bytes to the pipe... + */ + +#ifdef WIN32 + count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total)); +#else + count = write(3, buffer, bytes - total); +#endif /* WIN32 */ + + if (count < 0) + { + /* + * Write error - abort on fatal errors... + */ + + if (errno != EINTR && errno != EAGAIN) + return (-1); + } + else + { + /* + * Write succeeded, update buffer pointer and total count... + */ + + buffer += count; + total += count; + } + } + + return ((ssize_t)bytes); +} + + +/* + * 'cups_setup()' - Setup select() + */ + +static void +cups_setup(fd_set *set, /* I - Set for select() */ + struct timeval *tval, /* I - Timer value */ + double timeout) /* I - Timeout in seconds */ +{ + tval->tv_sec = (int)timeout; + tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec)); + + FD_ZERO(set); + FD_SET(3, set); +} + + +/* + * End of "$Id: backchannel.c 7616 2008-05-28 00:34:13Z mike $". + */ diff --git a/cups/backend.c b/cups/backend.c new file mode 100644 index 0000000..6f8a6b2 --- /dev/null +++ b/cups/backend.c @@ -0,0 +1,154 @@ +/* + * "$Id: backend.c 7810 2008-07-29 01:11:15Z mike $" + * + * Backend functions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2006 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. + * + * Contents: + * + * cupsBackendDeviceURI() - Get the device URI for a backend. + * cupsBackendReport() - Write a device line from a backend. + * quote_string() - Write a quoted string to stdout, escaping \ and ". + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include "backend.h" + + +/* + * Local functions... + */ + +static void quote_string(const char *s); + + +/* + * 'cupsBackendDeviceURI()' - Get the device URI for a backend. + * + * The "argv" argument is the argv argument passed to main(). This + * function returns the device URI passed in the DEVICE_URI environment + * variable or the device URI passed in argv[0], whichever is found + * first. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +const char * /* O - Device URI or @code NULL@ */ +cupsBackendDeviceURI(char **argv) /* I - Command-line arguments */ +{ + const char *device_uri, /* Device URI */ + *auth_info_required; /* AUTH_INFO_REQUIRED env var */ + _cups_globals_t *cg = _cupsGlobals(); /* Global info */ + int options; /* Resolve options */ + ppd_file_t *ppd; /* PPD file */ + ppd_attr_t *ppdattr; /* PPD attribute */ + + + if ((device_uri = getenv("DEVICE_URI")) == NULL) + { + if (!argv || !argv[0] || !strchr(argv[0], ':')) + return (NULL); + + device_uri = argv[0]; + } + + options = _HTTP_RESOLVE_STDERR; + if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) != NULL && + !strcmp(auth_info_required, "negotiate")) + options |= _HTTP_RESOLVE_FQDN; + + if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL) + { + if ((ppdattr = ppdFindAttr(ppd, "cupsIPPFaxOut", NULL)) != NULL && + !_cups_strcasecmp(ppdattr->value, "true")) + options |= _HTTP_RESOLVE_FAXOUT; + + ppdClose(ppd); + } + + return (_httpResolveURI(device_uri, cg->resolved_uri, + sizeof(cg->resolved_uri), options, NULL, NULL)); +} + + +/* + * 'cupsBackendReport()' - Write a device line from a backend. + * + * This function writes a single device line to stdout for a backend. + * It handles quoting of special characters in the device-make-and-model, + * device-info, device-id, and device-location strings. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +void +cupsBackendReport( + const char *device_scheme, /* I - device-scheme string */ + const char *device_uri, /* I - device-uri string */ + const char *device_make_and_model, /* I - device-make-and-model string or @code NULL@ */ + const char *device_info, /* I - device-info string or @code NULL@ */ + const char *device_id, /* I - device-id string or @code NULL@ */ + const char *device_location) /* I - device-location string or @code NULL@ */ +{ + if (!device_scheme || !device_uri) + return; + + printf("%s %s", device_scheme, device_uri); + if (device_make_and_model && *device_make_and_model) + quote_string(device_make_and_model); + else + quote_string("unknown"); + quote_string(device_info); + quote_string(device_id); + quote_string(device_location); + putchar('\n'); + fflush(stdout); +} + + +/* + * 'quote_string()' - Write a quoted string to stdout, escaping \ and ". + */ + +static void +quote_string(const char *s) /* I - String to write */ +{ + fputs(" \"", stdout); + + if (s) + { + while (*s) + { + if (*s == '\\' || *s == '\"') + putchar('\\'); + + if (((*s & 255) < ' ' && *s != '\t') || *s == 0x7f) + putchar(' '); + else + putchar(*s); + + s ++; + } + } + + putchar('\"'); +} + + +/* + * End of "$Id: backend.c 7810 2008-07-29 01:11:15Z mike $". + */ diff --git a/cups/backend.h b/cups/backend.h new file mode 100644 index 0000000..d2e37ff --- /dev/null +++ b/cups/backend.h @@ -0,0 +1,78 @@ +/* + * "$Id: backend.h 7810 2008-07-29 01:11:15Z mike $" + * + * Backend definitions for CUPS. + * + * Copyright 2007-2011 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. + */ + +#ifndef _CUPS_BACKEND_H_ +# define _CUPS_BACKEND_H_ + + +/* + * Include necessary headers... + */ + +# include "versioning.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + +/* + * Constants... + */ + +enum cups_backend_e /**** Backend exit codes ****/ +{ + CUPS_BACKEND_OK = 0, /* Job completed successfully */ + CUPS_BACKEND_FAILED = 1, /* Job failed, use error-policy */ + CUPS_BACKEND_AUTH_REQUIRED = 2, /* Job failed, authentication required */ + CUPS_BACKEND_HOLD = 3, /* Job failed, hold job */ + CUPS_BACKEND_STOP = 4, /* Job failed, stop queue */ + CUPS_BACKEND_CANCEL = 5, /* Job failed, cancel job */ + CUPS_BACKEND_RETRY = 6, /* Job failed, retry this job later */ + CUPS_BACKEND_RETRY_CURRENT = 7 /* Job failed, retry this job immediately */ +}; +typedef enum cups_backend_e cups_backend_t; + /**** Backend exit codes ****/ + + +/* + * Prototypes... + */ + +extern const char *cupsBackendDeviceURI(char **argv) _CUPS_API_1_2; +extern void cupsBackendReport(const char *device_scheme, + const char *device_uri, + const char *device_make_and_model, + const char *device_info, + const char *device_id, + const char *device_location) + _CUPS_API_1_4; + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_BACKEND_H_ */ + +/* + * End of "$Id: backend.h 7810 2008-07-29 01:11:15Z mike $". + */ diff --git a/cups/conflicts.c b/cups/conflicts.c new file mode 100644 index 0000000..f0d01b0 --- /dev/null +++ b/cups/conflicts.c @@ -0,0 +1,1214 @@ +/* + * "$Id: conflicts.c 3794 2012-04-23 22:44:16Z msweet $" + * + * Option marking routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * cupsGetConflicts() - Get a list of conflicting options in a marked + * PPD. + * cupsResolveConflicts() - Resolve conflicts in a marked PPD. + * ppdConflicts() - Check to see if there are any conflicts among + * the marked option choices. + * ppdInstallableConflict() - Test whether an option choice conflicts with an + * installable option. + * ppd_is_installable() - Determine whether an option is in the + * InstallableOptions group. + * ppd_load_constraints() - Load constraints from a PPD file. + * ppd_test_constraints() - See if any constraints are active. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include "ppd-private.h" + + +/* + * Local constants... + */ + +enum +{ + _PPD_NORMAL_CONSTRAINTS, + _PPD_OPTION_CONSTRAINTS, + _PPD_INSTALLABLE_CONSTRAINTS, + _PPD_ALL_CONSTRAINTS +}; + + +/* + * Local functions... + */ + +static int ppd_is_installable(ppd_group_t *installable, + const char *option); +static void ppd_load_constraints(ppd_file_t *ppd); +static cups_array_t *ppd_test_constraints(ppd_file_t *ppd, + const char *option, + const char *choice, + int num_options, + cups_option_t *options, + int which); + + +/* + * 'cupsGetConflicts()' - Get a list of conflicting options in a marked PPD. + * + * This function gets a list of options that would conflict if "option" and + * "choice" were marked in the PPD. You would typically call this function + * after marking the currently selected options in the PPD in order to + * determine whether a new option selection would cause a conflict. + * + * The number of conflicting options are returned with "options" pointing to + * the conflicting options. The returned option array must be freed using + * @link cupsFreeOptions@. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +int /* O - Number of conflicting options */ +cupsGetConflicts( + ppd_file_t *ppd, /* I - PPD file */ + const char *option, /* I - Option to test */ + const char *choice, /* I - Choice to test */ + cups_option_t **options) /* O - Conflicting options */ +{ + int i, /* Looping var */ + num_options; /* Number of conflicting options */ + cups_array_t *active; /* Active conflicts */ + _ppd_cups_uiconsts_t *c; /* Current constraints */ + _ppd_cups_uiconst_t *cptr; /* Current constraint */ + ppd_choice_t *marked; /* Marked choice */ + + + /* + * Range check input... + */ + + if (options) + *options = NULL; + + if (!ppd || !option || !choice || !options) + return (0); + + /* + * Test for conflicts... + */ + + active = ppd_test_constraints(ppd, option, choice, 0, NULL, + _PPD_ALL_CONSTRAINTS); + + /* + * Loop through all of the UI constraints and add any options that conflict... + */ + + for (num_options = 0, c = (_ppd_cups_uiconsts_t *)cupsArrayFirst(active); + c; + c = (_ppd_cups_uiconsts_t *)cupsArrayNext(active)) + { + for (i = c->num_constraints, cptr = c->constraints; + i > 0; + i --, cptr ++) + if (_cups_strcasecmp(cptr->option->keyword, option)) + { + if (cptr->choice) + num_options = cupsAddOption(cptr->option->keyword, + cptr->choice->choice, num_options, + options); + else if ((marked = ppdFindMarkedChoice(ppd, + cptr->option->keyword)) != NULL) + num_options = cupsAddOption(cptr->option->keyword, marked->choice, + num_options, options); + } + } + + cupsArrayDelete(active); + + return (num_options); +} + + +/* + * 'cupsResolveConflicts()' - Resolve conflicts in a marked PPD. + * + * This function attempts to resolve any conflicts in a marked PPD, returning + * a list of option changes that are required to resolve them. On input, + * "num_options" and "options" contain any pending option changes that have + * not yet been marked, while "option" and "choice" contain the most recent + * selection which may or may not be in "num_options" or "options". + * + * On successful return, "num_options" and "options" are updated to contain + * "option" and "choice" along with any changes required to resolve conflicts + * specified in the PPD file and 1 is returned. + * + * If option conflicts cannot be resolved, "num_options" and "options" are not + * changed and 0 is returned. + * + * When resolving conflicts, @code cupsResolveConflicts@ does not consider + * changes to the current page size (@code media@, @code PageSize@, and + * @code PageRegion@) or to the most recent option specified in "option". + * Thus, if the only way to resolve a conflict is to change the page size + * or the option the user most recently changed, @code cupsResolveConflicts@ + * will return 0 to indicate it was unable to resolve the conflicts. + * + * The @code cupsResolveConflicts@ function uses one of two sources of option + * constraint information. The preferred constraint information is defined by + * @code cupsUIConstraints@ and @code cupsUIResolver@ attributes - in this + * case, the PPD file provides constraint resolution actions. + * + * The backup constraint information is defined by the + * @code UIConstraints@ and @code NonUIConstraints@ attributes. These + * constraints are resolved algorithmically by first selecting the default + * choice for the conflicting option, then iterating over all possible choices + * until a non-conflicting option choice is found. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsResolveConflicts( + ppd_file_t *ppd, /* I - PPD file */ + const char *option, /* I - Newly selected option or @code NULL@ for none */ + const char *choice, /* I - Newly selected choice or @code NULL@ for none */ + int *num_options, /* IO - Number of additional selected options */ + cups_option_t **options) /* IO - Additional selected options */ +{ + int i, /* Looping var */ + tries, /* Number of tries */ + num_newopts; /* Number of new options */ + cups_option_t *newopts; /* New options */ + cups_array_t *active, /* Active constraints */ + *pass, /* Resolvers for this pass */ + *resolvers, /* Resolvers we have used */ + *test; /* Test array for conflicts */ + _ppd_cups_uiconsts_t *consts; /* Current constraints */ + _ppd_cups_uiconst_t *constptr; /* Current constraint */ + ppd_attr_t *resolver; /* Current resolver */ + const char *resval; /* Pointer into resolver value */ + char resoption[PPD_MAX_NAME], + /* Current resolver option */ + reschoice[PPD_MAX_NAME], + /* Current resolver choice */ + *resptr, /* Pointer into option/choice */ + firstpage[255]; /* AP_FIRSTPAGE_Keyword string */ + const char *value; /* Selected option value */ + int changed; /* Did we change anything? */ + ppd_choice_t *marked; /* Marked choice */ + + + /* + * Range check input... + */ + + if (!ppd || !num_options || !options || (option == NULL) != (choice == NULL)) + return (0); + + /* + * Build a shadow option array... + */ + + num_newopts = 0; + newopts = NULL; + + for (i = 0; i < *num_options; i ++) + num_newopts = cupsAddOption((*options)[i].name, (*options)[i].value, + num_newopts, &newopts); + if (option && _cups_strcasecmp(option, "Collate")) + num_newopts = cupsAddOption(option, choice, num_newopts, &newopts); + + /* + * Loop until we have no conflicts... + */ + + cupsArraySave(ppd->sorted_attrs); + + resolvers = NULL; + pass = cupsArrayNew((cups_array_func_t)_cups_strcasecmp, NULL); + tries = 0; + + while (tries < 100 && + (active = ppd_test_constraints(ppd, NULL, NULL, num_newopts, newopts, + _PPD_ALL_CONSTRAINTS)) != NULL) + { + tries ++; + + if (!resolvers) + resolvers = cupsArrayNew((cups_array_func_t)_cups_strcasecmp, NULL); + + for (consts = (_ppd_cups_uiconsts_t *)cupsArrayFirst(active), changed = 0; + consts; + consts = (_ppd_cups_uiconsts_t *)cupsArrayNext(active)) + { + if (consts->resolver[0]) + { + /* + * Look up the resolver... + */ + + if (cupsArrayFind(pass, consts->resolver)) + continue; /* Already applied this resolver... */ + + if (cupsArrayFind(resolvers, consts->resolver)) + { + /* + * Resolver loop! + */ + + DEBUG_printf(("1cupsResolveConflicts: Resolver loop with %s!", + consts->resolver)); + goto error; + } + + if ((resolver = ppdFindAttr(ppd, "cupsUIResolver", + consts->resolver)) == NULL) + { + DEBUG_printf(("1cupsResolveConflicts: Resolver %s not found!", + consts->resolver)); + goto error; + } + + if (!resolver->value) + { + DEBUG_printf(("1cupsResolveConflicts: Resolver %s has no value!", + consts->resolver)); + goto error; + } + + /* + * Add the options from the resolver... + */ + + cupsArrayAdd(pass, consts->resolver); + cupsArrayAdd(resolvers, consts->resolver); + + for (resval = resolver->value; *resval && !changed;) + { + while (_cups_isspace(*resval)) + resval ++; + + if (*resval != '*') + break; + + for (resval ++, resptr = resoption; + *resval && !_cups_isspace(*resval); + resval ++) + if (resptr < (resoption + sizeof(resoption) - 1)) + *resptr++ = *resval; + + *resptr = '\0'; + + while (_cups_isspace(*resval)) + resval ++; + + for (resptr = reschoice; + *resval && !_cups_isspace(*resval); + resval ++) + if (resptr < (reschoice + sizeof(reschoice) - 1)) + *resptr++ = *resval; + + *resptr = '\0'; + + if (!resoption[0] || !reschoice[0]) + break; + + /* + * Is this the option we are changing? + */ + + snprintf(firstpage, sizeof(firstpage), "AP_FIRSTPAGE_%s", resoption); + + if (option && + (!_cups_strcasecmp(resoption, option) || + !_cups_strcasecmp(firstpage, option) || + (!_cups_strcasecmp(option, "PageSize") && + !_cups_strcasecmp(resoption, "PageRegion")) || + (!_cups_strcasecmp(option, "AP_FIRSTPAGE_PageSize") && + !_cups_strcasecmp(resoption, "PageSize")) || + (!_cups_strcasecmp(option, "AP_FIRSTPAGE_PageSize") && + !_cups_strcasecmp(resoption, "PageRegion")) || + (!_cups_strcasecmp(option, "PageRegion") && + !_cups_strcasecmp(resoption, "PageSize")) || + (!_cups_strcasecmp(option, "AP_FIRSTPAGE_PageRegion") && + !_cups_strcasecmp(resoption, "PageSize")) || + (!_cups_strcasecmp(option, "AP_FIRSTPAGE_PageRegion") && + !_cups_strcasecmp(resoption, "PageRegion")))) + continue; + + /* + * Try this choice... + */ + + if ((test = ppd_test_constraints(ppd, resoption, reschoice, + num_newopts, newopts, + _PPD_ALL_CONSTRAINTS)) == NULL) + { + /* + * That worked... + */ + + changed = 1; + } + else + cupsArrayDelete(test); + + /* + * Add the option/choice from the resolver regardless of whether it + * worked; this makes sure that we can cascade several changes to + * make things resolve... + */ + + num_newopts = cupsAddOption(resoption, reschoice, num_newopts, + &newopts); + } + } + else + { + /* + * Try resolving by choosing the default values for non-installable + * options, then by iterating through the possible choices... + */ + + int j; /* Looping var */ + ppd_choice_t *cptr; /* Current choice */ + ppd_size_t *size; /* Current page size */ + + + for (i = consts->num_constraints, constptr = consts->constraints; + i > 0 && !changed; + i --, constptr ++) + { + /* + * Can't resolve by changing an installable option... + */ + + if (constptr->installable) + continue; + + /* + * Is this the option we are changing? + */ + + if (option && + (!_cups_strcasecmp(constptr->option->keyword, option) || + (!_cups_strcasecmp(option, "PageSize") && + !_cups_strcasecmp(constptr->option->keyword, "PageRegion")) || + (!_cups_strcasecmp(option, "PageRegion") && + !_cups_strcasecmp(constptr->option->keyword, "PageSize")))) + continue; + + /* + * Get the current option choice... + */ + + if ((value = cupsGetOption(constptr->option->keyword, num_newopts, + newopts)) == NULL) + { + if (!_cups_strcasecmp(constptr->option->keyword, "PageSize") || + !_cups_strcasecmp(constptr->option->keyword, "PageRegion")) + { + if ((value = cupsGetOption("PageSize", num_newopts, + newopts)) == NULL) + value = cupsGetOption("PageRegion", num_newopts, newopts); + + if (!value) + { + if ((size = ppdPageSize(ppd, NULL)) != NULL) + value = size->name; + else + value = ""; + } + } + else + { + marked = ppdFindMarkedChoice(ppd, constptr->option->keyword); + value = marked ? marked->choice : ""; + } + } + + if (!_cups_strncasecmp(value, "Custom.", 7)) + value = "Custom"; + + /* + * Try the default choice... + */ + + test = NULL; + + if (_cups_strcasecmp(value, constptr->option->defchoice) && + (test = ppd_test_constraints(ppd, constptr->option->keyword, + constptr->option->defchoice, + num_newopts, newopts, + _PPD_OPTION_CONSTRAINTS)) == NULL) + { + /* + * That worked... + */ + + num_newopts = cupsAddOption(constptr->option->keyword, + constptr->option->defchoice, + num_newopts, &newopts); + changed = 1; + } + else + { + /* + * Try each choice instead... + */ + + for (j = constptr->option->num_choices, + cptr = constptr->option->choices; + j > 0; + j --, cptr ++) + { + cupsArrayDelete(test); + test = NULL; + + if (_cups_strcasecmp(value, cptr->choice) && + _cups_strcasecmp(constptr->option->defchoice, cptr->choice) && + _cups_strcasecmp("Custom", cptr->choice) && + (test = ppd_test_constraints(ppd, constptr->option->keyword, + cptr->choice, num_newopts, + newopts, + _PPD_OPTION_CONSTRAINTS)) == NULL) + { + /* + * This choice works... + */ + + num_newopts = cupsAddOption(constptr->option->keyword, + cptr->choice, num_newopts, + &newopts); + changed = 1; + break; + } + } + + cupsArrayDelete(test); + } + } + } + } + + if (!changed) + { + DEBUG_puts("1cupsResolveConflicts: Unable to automatically resolve " + "constraint!"); + goto error; + } + + cupsArrayClear(pass); + cupsArrayDelete(active); + active = NULL; + } + + if (tries >= 100) + goto error; + + /* + * Free the caller's option array... + */ + + cupsFreeOptions(*num_options, *options); + + /* + * If Collate is the option we are testing, add it here. Otherwise, remove + * any Collate option from the resolve list since the filters automatically + * handle manual collation... + */ + + if (option && !_cups_strcasecmp(option, "Collate")) + num_newopts = cupsAddOption(option, choice, num_newopts, &newopts); + else + num_newopts = cupsRemoveOption("Collate", num_newopts, &newopts); + + /* + * Return the new list of options to the caller... + */ + + *num_options = num_newopts; + *options = newopts; + + cupsArrayDelete(pass); + cupsArrayDelete(resolvers); + + cupsArrayRestore(ppd->sorted_attrs); + + DEBUG_printf(("1cupsResolveConflicts: Returning %d options:", num_newopts)); +#ifdef DEBUG + for (i = 0; i < num_newopts; i ++) + DEBUG_printf(("1cupsResolveConflicts: options[%d]: %s=%s", i, + newopts[i].name, newopts[i].value)); +#endif /* DEBUG */ + + return (1); + + /* + * If we get here, we failed to resolve... + */ + + error: + + cupsFreeOptions(num_newopts, newopts); + + cupsArrayDelete(active); + cupsArrayDelete(pass); + cupsArrayDelete(resolvers); + + cupsArrayRestore(ppd->sorted_attrs); + + DEBUG_puts("1cupsResolveConflicts: Unable to resolve conflicts!"); + + return (0); +} + + +/* + * 'ppdConflicts()' - Check to see if there are any conflicts among the + * marked option choices. + * + * The returned value is the same as returned by @link ppdMarkOption@. + */ + +int /* O - Number of conflicts found */ +ppdConflicts(ppd_file_t *ppd) /* I - PPD to check */ +{ + int i, /* Looping variable */ + conflicts; /* Number of conflicts */ + cups_array_t *active; /* Active conflicts */ + _ppd_cups_uiconsts_t *c; /* Current constraints */ + _ppd_cups_uiconst_t *cptr; /* Current constraint */ + ppd_option_t *o; /* Current option */ + + + if (!ppd) + return (0); + + /* + * Clear all conflicts... + */ + + cupsArraySave(ppd->options); + + for (o = ppdFirstOption(ppd); o; o = ppdNextOption(ppd)) + o->conflicted = 0; + + cupsArrayRestore(ppd->options); + + /* + * Test for conflicts... + */ + + active = ppd_test_constraints(ppd, NULL, NULL, 0, NULL, + _PPD_ALL_CONSTRAINTS); + conflicts = cupsArrayCount(active); + + /* + * Loop through all of the UI constraints and flag any options + * that conflict... + */ + + for (c = (_ppd_cups_uiconsts_t *)cupsArrayFirst(active); + c; + c = (_ppd_cups_uiconsts_t *)cupsArrayNext(active)) + { + for (i = c->num_constraints, cptr = c->constraints; + i > 0; + i --, cptr ++) + cptr->option->conflicted = 1; + } + + cupsArrayDelete(active); + + /* + * Return the number of conflicts found... + */ + + return (conflicts); +} + + +/* + * 'ppdInstallableConflict()' - Test whether an option choice conflicts with + * an installable option. + * + * This function tests whether a particular option choice is available based + * on constraints against options in the "InstallableOptions" group. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +int /* O - 1 if conflicting, 0 if not conflicting */ +ppdInstallableConflict( + ppd_file_t *ppd, /* I - PPD file */ + const char *option, /* I - Option */ + const char *choice) /* I - Choice */ +{ + cups_array_t *active; /* Active conflicts */ + + + DEBUG_printf(("2ppdInstallableConflict(ppd=%p, option=\"%s\", choice=\"%s\")", + ppd, option, choice)); + + /* + * Range check input... + */ + + if (!ppd || !option || !choice) + return (0); + + /* + * Test constraints using the new option... + */ + + active = ppd_test_constraints(ppd, option, choice, 0, NULL, + _PPD_INSTALLABLE_CONSTRAINTS); + + cupsArrayDelete(active); + + return (active != NULL); +} + + +/* + * 'ppd_is_installable()' - Determine whether an option is in the + * InstallableOptions group. + */ + +static int /* O - 1 if installable, 0 if normal */ +ppd_is_installable( + ppd_group_t *installable, /* I - InstallableOptions group */ + const char *name) /* I - Option name */ +{ + if (installable) + { + int i; /* Looping var */ + ppd_option_t *option; /* Current option */ + + + for (i = installable->num_options, option = installable->options; + i > 0; + i --, option ++) + if (!_cups_strcasecmp(option->keyword, name)) + return (1); + } + + return (0); +} + + +/* + * 'ppd_load_constraints()' - Load constraints from a PPD file. + */ + +static void +ppd_load_constraints(ppd_file_t *ppd) /* I - PPD file */ +{ + int i; /* Looping var */ + ppd_const_t *oldconst; /* Current UIConstraints data */ + ppd_attr_t *constattr; /* Current cupsUIConstraints attribute */ + _ppd_cups_uiconsts_t *consts; /* Current cupsUIConstraints data */ + _ppd_cups_uiconst_t *constptr; /* Current constraint */ + ppd_group_t *installable; /* Installable options group */ + const char *vptr; /* Pointer into constraint value */ + char option[PPD_MAX_NAME], /* Option name/MainKeyword */ + choice[PPD_MAX_NAME], /* Choice/OptionKeyword */ + *ptr; /* Pointer into option or choice */ + + + DEBUG_printf(("7ppd_load_constraints(ppd=%p)", ppd)); + + /* + * Create an array to hold the constraint data... + */ + + ppd->cups_uiconstraints = cupsArrayNew(NULL, NULL); + + /* + * Find the installable options group if it exists... + */ + + for (i = ppd->num_groups, installable = ppd->groups; + i > 0; + i --, installable ++) + if (!_cups_strcasecmp(installable->name, "InstallableOptions")) + break; + + if (i <= 0) + installable = NULL; + + /* + * Load old-style [Non]UIConstraints data... + */ + + for (i = ppd->num_consts, oldconst = ppd->consts; i > 0; i --, oldconst ++) + { + /* + * Weed out nearby duplicates, since the PPD spec requires that you + * define both "*Foo foo *Bar bar" and "*Bar bar *Foo foo"... + */ + + if (i > 1 && + !_cups_strcasecmp(oldconst[0].option1, oldconst[1].option2) && + !_cups_strcasecmp(oldconst[0].choice1, oldconst[1].choice2) && + !_cups_strcasecmp(oldconst[0].option2, oldconst[1].option1) && + !_cups_strcasecmp(oldconst[0].choice2, oldconst[1].choice1)) + continue; + + /* + * Allocate memory... + */ + + if ((consts = calloc(1, sizeof(_ppd_cups_uiconsts_t))) == NULL) + { + DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for " + "UIConstraints!"); + return; + } + + if ((constptr = calloc(2, sizeof(_ppd_cups_uiconst_t))) == NULL) + { + free(consts); + DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for " + "UIConstraints!"); + return; + } + + /* + * Fill in the information... + */ + + consts->num_constraints = 2; + consts->constraints = constptr; + + if (!_cups_strncasecmp(oldconst->option1, "Custom", 6) && + !_cups_strcasecmp(oldconst->choice1, "True")) + { + constptr[0].option = ppdFindOption(ppd, oldconst->option1 + 6); + constptr[0].choice = ppdFindChoice(constptr[0].option, "Custom"); + constptr[0].installable = 0; + } + else + { + constptr[0].option = ppdFindOption(ppd, oldconst->option1); + constptr[0].choice = ppdFindChoice(constptr[0].option, + oldconst->choice1); + constptr[0].installable = ppd_is_installable(installable, + oldconst->option1); + } + + if (!constptr[0].option || (!constptr[0].choice && oldconst->choice1[0])) + { + DEBUG_printf(("8ppd_load_constraints: Unknown option *%s %s!", + oldconst->option1, oldconst->choice1)); + free(consts->constraints); + free(consts); + continue; + } + + if (!_cups_strncasecmp(oldconst->option2, "Custom", 6) && + !_cups_strcasecmp(oldconst->choice2, "True")) + { + constptr[1].option = ppdFindOption(ppd, oldconst->option2 + 6); + constptr[1].choice = ppdFindChoice(constptr[1].option, "Custom"); + constptr[1].installable = 0; + } + else + { + constptr[1].option = ppdFindOption(ppd, oldconst->option2); + constptr[1].choice = ppdFindChoice(constptr[1].option, + oldconst->choice2); + constptr[1].installable = ppd_is_installable(installable, + oldconst->option2); + } + + if (!constptr[1].option || (!constptr[1].choice && oldconst->choice2[0])) + { + DEBUG_printf(("8ppd_load_constraints: Unknown option *%s %s!", + oldconst->option2, oldconst->choice2)); + free(consts->constraints); + free(consts); + continue; + } + + consts->installable = constptr[0].installable || constptr[1].installable; + + /* + * Add it to the constraints array... + */ + + cupsArrayAdd(ppd->cups_uiconstraints, consts); + } + + /* + * Then load new-style constraints... + */ + + for (constattr = ppdFindAttr(ppd, "cupsUIConstraints", NULL); + constattr; + constattr = ppdFindNextAttr(ppd, "cupsUIConstraints", NULL)) + { + if (!constattr->value) + { + DEBUG_puts("8ppd_load_constraints: Bad cupsUIConstraints value!"); + continue; + } + + for (i = 0, vptr = strchr(constattr->value, '*'); + vptr; + i ++, vptr = strchr(vptr + 1, '*')); + + if (i == 0) + { + DEBUG_puts("8ppd_load_constraints: Bad cupsUIConstraints value!"); + continue; + } + + if ((consts = calloc(1, sizeof(_ppd_cups_uiconsts_t))) == NULL) + { + DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for " + "cupsUIConstraints!"); + return; + } + + if ((constptr = calloc(i, sizeof(_ppd_cups_uiconst_t))) == NULL) + { + free(consts); + DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for " + "cupsUIConstraints!"); + return; + } + + consts->num_constraints = i; + consts->constraints = constptr; + + strlcpy(consts->resolver, constattr->spec, sizeof(consts->resolver)); + + for (i = 0, vptr = strchr(constattr->value, '*'); + vptr; + i ++, vptr = strchr(vptr, '*'), constptr ++) + { + /* + * Extract "*Option Choice" or just "*Option"... + */ + + for (vptr ++, ptr = option; *vptr && !_cups_isspace(*vptr); vptr ++) + if (ptr < (option + sizeof(option) - 1)) + *ptr++ = *vptr; + + *ptr = '\0'; + + while (_cups_isspace(*vptr)) + vptr ++; + + if (*vptr == '*') + choice[0] = '\0'; + else + { + for (ptr = choice; *vptr && !_cups_isspace(*vptr); vptr ++) + if (ptr < (choice + sizeof(choice) - 1)) + *ptr++ = *vptr; + + *ptr = '\0'; + } + + if (!_cups_strncasecmp(option, "Custom", 6) && !_cups_strcasecmp(choice, "True")) + { + _cups_strcpy(option, option + 6); + strcpy(choice, "Custom"); + } + + constptr->option = ppdFindOption(ppd, option); + constptr->choice = ppdFindChoice(constptr->option, choice); + constptr->installable = ppd_is_installable(installable, option); + consts->installable |= constptr->installable; + + if (!constptr->option || (!constptr->choice && choice[0])) + { + DEBUG_printf(("8ppd_load_constraints: Unknown option *%s %s!", + option, choice)); + break; + } + } + + if (!vptr) + cupsArrayAdd(ppd->cups_uiconstraints, consts); + else + { + free(consts->constraints); + free(consts); + } + } +} + + +/* + * 'ppd_test_constraints()' - See if any constraints are active. + */ + +static cups_array_t * /* O - Array of active constraints */ +ppd_test_constraints( + ppd_file_t *ppd, /* I - PPD file */ + const char *option, /* I - Current option */ + const char *choice, /* I - Current choice */ + int num_options, /* I - Number of additional options */ + cups_option_t *options, /* I - Additional options */ + int which) /* I - Which constraints to test */ +{ + int i; /* Looping var */ + _ppd_cups_uiconsts_t *consts; /* Current constraints */ + _ppd_cups_uiconst_t *constptr; /* Current constraint */ + ppd_choice_t key, /* Search key */ + *marked; /* Marked choice */ + cups_array_t *active = NULL; /* Active constraints */ + const char *value, /* Current value */ + *firstvalue; /* AP_FIRSTPAGE_Keyword value */ + char firstpage[255]; /* AP_FIRSTPAGE_Keyword string */ + + + DEBUG_printf(("7ppd_test_constraints(ppd=%p, option=\"%s\", choice=\"%s\", " + "num_options=%d, options=%p, which=%d)", ppd, option, choice, + num_options, options, which)); + + if (!ppd->cups_uiconstraints) + ppd_load_constraints(ppd); + + DEBUG_printf(("9ppd_test_constraints: %d constraints!", + cupsArrayCount(ppd->cups_uiconstraints))); + + cupsArraySave(ppd->marked); + + for (consts = (_ppd_cups_uiconsts_t *)cupsArrayFirst(ppd->cups_uiconstraints); + consts; + consts = (_ppd_cups_uiconsts_t *)cupsArrayNext(ppd->cups_uiconstraints)) + { + DEBUG_printf(("9ppd_test_constraints: installable=%d, resolver=\"%s\", " + "num_constraints=%d option1=\"%s\", choice1=\"%s\", " + "option2=\"%s\", choice2=\"%s\", ...", + consts->installable, consts->resolver, consts->num_constraints, + consts->constraints[0].option->keyword, + consts->constraints[0].choice ? + consts->constraints[0].choice->choice : "", + consts->constraints[1].option->keyword, + consts->constraints[1].choice ? + consts->constraints[1].choice->choice : "")); + + if (consts->installable && which < _PPD_INSTALLABLE_CONSTRAINTS) + continue; /* Skip installable option constraint */ + + if (!consts->installable && which == _PPD_INSTALLABLE_CONSTRAINTS) + continue; /* Skip non-installable option constraint */ + + if (which == _PPD_OPTION_CONSTRAINTS && option) + { + /* + * Skip constraints that do not involve the current option... + */ + + for (i = consts->num_constraints, constptr = consts->constraints; + i > 0; + i --, constptr ++) + { + if (!_cups_strcasecmp(constptr->option->keyword, option)) + break; + + if (!_cups_strncasecmp(option, "AP_FIRSTPAGE_", 13) && + !_cups_strcasecmp(constptr->option->keyword, option + 13)) + break; + } + + if (!i) + continue; + } + + DEBUG_puts("9ppd_test_constraints: Testing..."); + + for (i = consts->num_constraints, constptr = consts->constraints; + i > 0; + i --, constptr ++) + { + DEBUG_printf(("9ppd_test_constraints: %s=%s?", constptr->option->keyword, + constptr->choice ? constptr->choice->choice : "")); + + if (constptr->choice && + (!_cups_strcasecmp(constptr->option->keyword, "PageSize") || + !_cups_strcasecmp(constptr->option->keyword, "PageRegion"))) + { + /* + * PageSize and PageRegion are used depending on the selected input slot + * and manual feed mode. Validate against the selected page size instead + * of an individual option... + */ + + if (option && choice && + (!_cups_strcasecmp(option, "PageSize") || + !_cups_strcasecmp(option, "PageRegion"))) + { + value = choice; + } + else if ((value = cupsGetOption("PageSize", num_options, + options)) == NULL) + if ((value = cupsGetOption("PageRegion", num_options, + options)) == NULL) + if ((value = cupsGetOption("media", num_options, options)) == NULL) + { + ppd_size_t *size = ppdPageSize(ppd, NULL); + + if (size) + value = size->name; + } + + if (value && !_cups_strncasecmp(value, "Custom.", 7)) + value = "Custom"; + + if (option && choice && + (!_cups_strcasecmp(option, "AP_FIRSTPAGE_PageSize") || + !_cups_strcasecmp(option, "AP_FIRSTPAGE_PageRegion"))) + { + firstvalue = choice; + } + else if ((firstvalue = cupsGetOption("AP_FIRSTPAGE_PageSize", + num_options, options)) == NULL) + firstvalue = cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options, + options); + + if (firstvalue && !_cups_strncasecmp(firstvalue, "Custom.", 7)) + firstvalue = "Custom"; + + if ((!value || _cups_strcasecmp(value, constptr->choice->choice)) && + (!firstvalue || _cups_strcasecmp(firstvalue, constptr->choice->choice))) + { + DEBUG_puts("9ppd_test_constraints: NO"); + break; + } + } + else if (constptr->choice) + { + /* + * Compare against the constrained choice... + */ + + if (option && choice && !_cups_strcasecmp(option, constptr->option->keyword)) + { + if (!_cups_strncasecmp(choice, "Custom.", 7)) + value = "Custom"; + else + value = choice; + } + else if ((value = cupsGetOption(constptr->option->keyword, num_options, + options)) != NULL) + { + if (!_cups_strncasecmp(value, "Custom.", 7)) + value = "Custom"; + } + else if (constptr->choice->marked) + value = constptr->choice->choice; + else + value = NULL; + + /* + * Now check AP_FIRSTPAGE_option... + */ + + snprintf(firstpage, sizeof(firstpage), "AP_FIRSTPAGE_%s", + constptr->option->keyword); + + if (option && choice && !_cups_strcasecmp(option, firstpage)) + { + if (!_cups_strncasecmp(choice, "Custom.", 7)) + firstvalue = "Custom"; + else + firstvalue = choice; + } + else if ((firstvalue = cupsGetOption(firstpage, num_options, + options)) != NULL) + { + if (!_cups_strncasecmp(firstvalue, "Custom.", 7)) + firstvalue = "Custom"; + } + else + firstvalue = NULL; + + DEBUG_printf(("9ppd_test_constraints: value=%s, firstvalue=%s", value, + firstvalue)); + + if ((!value || _cups_strcasecmp(value, constptr->choice->choice)) && + (!firstvalue || _cups_strcasecmp(firstvalue, constptr->choice->choice))) + { + DEBUG_puts("9ppd_test_constraints: NO"); + break; + } + } + else if (option && choice && + !_cups_strcasecmp(option, constptr->option->keyword)) + { + if (!_cups_strcasecmp(choice, "None") || !_cups_strcasecmp(choice, "Off") || + !_cups_strcasecmp(choice, "False")) + { + DEBUG_puts("9ppd_test_constraints: NO"); + break; + } + } + else if ((value = cupsGetOption(constptr->option->keyword, num_options, + options)) != NULL) + { + if (!_cups_strcasecmp(value, "None") || !_cups_strcasecmp(value, "Off") || + !_cups_strcasecmp(value, "False")) + { + DEBUG_puts("9ppd_test_constraints: NO"); + break; + } + } + else + { + key.option = constptr->option; + + if ((marked = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) + == NULL || + (!_cups_strcasecmp(marked->choice, "None") || + !_cups_strcasecmp(marked->choice, "Off") || + !_cups_strcasecmp(marked->choice, "False"))) + { + DEBUG_puts("9ppd_test_constraints: NO"); + break; + } + } + } + + if (i <= 0) + { + if (!active) + active = cupsArrayNew(NULL, NULL); + + cupsArrayAdd(active, consts); + DEBUG_puts("9ppd_test_constraints: Added..."); + } + } + + cupsArrayRestore(ppd->marked); + + DEBUG_printf(("8ppd_test_constraints: Found %d active constraints!", + cupsArrayCount(active))); + + return (active); +} + + +/* + * End of "$Id: conflicts.c 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/cups-private.h b/cups/cups-private.h new file mode 100644 index 0000000..027d3a1 --- /dev/null +++ b/cups/cups-private.h @@ -0,0 +1,272 @@ +/* + * "$Id: cups-private.h 9596 2011-03-11 18:26:36Z mike $" + * + * Private definitions for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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. + */ + +#ifndef _CUPS_CUPS_PRIVATE_H_ +# define _CUPS_CUPS_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include "string-private.h" +# include "debug-private.h" +# include "ipp-private.h" +# include "http-private.h" +# include "language-private.h" +# include "pwg-private.h" +# include "ppd-private.h" +# include "thread-private.h" +# include +# ifdef __APPLE__ +# include +# include +# endif /* __APPLE__ */ + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Types... + */ + +typedef struct _cups_buffer_s /**** Read/write buffer ****/ +{ + struct _cups_buffer_s *next; /* Next buffer in list */ + size_t size; /* Size of buffer */ + char used, /* Is this buffer used? */ + d[1]; /* Data buffer */ +} _cups_buffer_t; + +typedef struct _cups_globals_s /**** CUPS global state data ****/ +{ + /* Multiple places... */ + const char *cups_datadir, /* CUPS_DATADIR environment var */ + *cups_serverbin,/* CUPS_SERVERBIN environment var */ + *cups_serverroot, + /* CUPS_SERVERROOT environment var */ + *cups_statedir, /* CUPS_STATEDIR environment var */ + *localedir; /* LOCALDIR environment var */ + + /* adminutil.c */ + time_t cupsd_update; /* Last time we got or set cupsd.conf */ + char cupsd_hostname[HTTP_MAX_HOST]; + /* Hostname for connection */ + int cupsd_num_settings; + /* Number of server settings */ + cups_option_t *cupsd_settings;/* Server settings */ + + /* auth.c */ +# ifdef HAVE_GSSAPI + char gss_service_name[32]; + /* Kerberos service name */ +# endif /* HAVE_GSSAPI */ + + /* backend.c */ + char resolved_uri[1024]; + /* Buffer for cupsBackendDeviceURI */ + + /* file.c */ + cups_file_t *stdio_files[3];/* stdin, stdout, stderr */ + + /* http.c */ + char http_date[256]; /* Date+time buffer */ + + /* http-addr.c */ + unsigned ip_addr; /* Packed IPv4 address */ + char *ip_ptrs[2]; /* Pointer to packed address */ + struct hostent hostent; /* Host entry for IP address */ +# ifdef HAVE_GETADDRINFO + char hostname[1024]; /* Hostname */ +# endif /* HAVE_GETADDRINFO */ + int need_res_init; /* Need to reinitialize resolver? */ + + /* ipp.c */ + ipp_uchar_t ipp_date[11]; /* RFC-1903 date/time data */ + _cups_buffer_t *cups_buffers; /* Buffer list */ + + /* ipp-support.c */ + int ipp_port; /* IPP port number */ + char ipp_unknown[255]; + /* Unknown error statuses */ + + /* language.c */ + cups_lang_t *lang_default; /* Default language */ +# ifdef __APPLE__ + char language[32]; /* Cached language */ +# endif /* __APPLE__ */ + + /* ppd.c */ + ppd_status_t ppd_status; /* Status of last ppdOpen*() */ + int ppd_line; /* Current line number */ + ppd_conform_t ppd_conform; /* Level of conformance required */ + + /* pwg-media.c */ + cups_array_t *leg_size_lut, /* Lookup table for legacy names */ + *ppd_size_lut, /* Lookup table for PPD names */ + *pwg_size_lut; /* Lookup table for PWG names */ + _pwg_media_t pwg_media; /* PWG media data for custom size */ + char pwg_name[65]; /* PWG media name for custom size */ + + /* request.c */ + http_t *http; /* Current server connection */ + ipp_status_t last_error; /* Last IPP error */ + char *last_status_message; + /* Last IPP status-message */ + + /* snmp.c */ + char snmp_community[255]; + /* Default SNMP community name */ + int snmp_debug; /* Log SNMP IO to stderr? */ + + /* tempfile.c */ + char tempfile[1024]; /* cupsTempFd/File buffer */ + + /* usersys.c */ + http_encryption_t encryption; /* Encryption setting */ + char user[65], /* User name */ + server[256], /* Server address */ + servername[256],/* Server hostname */ + password[128]; /* Password for default callback */ + cups_password_cb2_t password_cb; /* Password callback */ + void *password_data; /* Password user data */ + http_tls_credentials_t tls_credentials; + /* Default client credentials */ + cups_client_cert_cb_t client_cert_cb; /* Client certificate callback */ + void *client_cert_data; + /* Client certificate user data */ + cups_server_cert_cb_t server_cert_cb; /* Server certificate callback */ + void *server_cert_data; + /* Server certificate user data */ + int server_version, /* Server IPP version */ + any_root, /* Allow any root */ + expired_certs, /* Allow expired certs */ + expired_root; /* Allow expired root */ + + /* util.c */ + char def_printer[256]; + /* Default printer */ + char ppd_filename[HTTP_MAX_URI]; + /* PPD filename */ +} _cups_globals_t; + +typedef struct _cups_media_db_s /* Media database */ +{ + char *color, /* Media color, if any */ + *key, /* Media key, if any */ + *info, /* Media human-readable name, if any */ + *size_name, /* Media PWG size name, if provided */ + *source, /* Media source, if any */ + *type; /* Media type, if any */ + int width, /* Width in hundredths of millimeters */ + length, /* Length in hundredths of + * millimeters */ + bottom, /* Bottom margin in hundredths of + * millimeters */ + left, /* Left margin in hundredths of + * millimeters */ + right, /* Right margin in hundredths of + * millimeters */ + top; /* Top margin in hundredths of + * millimeters */ +} _cups_media_db_t; + +typedef struct _cups_dconstres_s /* Constraint/resolver */ +{ + char *name; /* Name of resolver */ + ipp_t *collection; /* Collection containing attrs */ +} _cups_dconstres_t; + +struct _cups_dinfo_s /* Destination capability and status + * information */ +{ + const char *uri; /* Printer URI */ + char *resource; /* Resource path */ + ipp_t *attrs; /* Printer attributes */ + int num_defaults; /* Number of default options */ + cups_option_t *defaults; /* Default options */ + cups_array_t *constraints; /* Job constraints */ + cups_array_t *resolvers; /* Job resolvers */ + cups_array_t *localizations; /* Localization information */ + cups_array_t *media_db; /* Media database */ + _cups_media_db_t min_size, /* Minimum size */ + max_size; /* Maximum size */ +}; + + +/* + * Prototypes... + */ + +# ifdef __APPLE__ +extern CFStringRef _cupsAppleCopyDefaultPaperID(void); +extern CFStringRef _cupsAppleCopyDefaultPrinter(void); +extern int _cupsAppleGetUseLastPrinter(void); +extern void _cupsAppleSetDefaultPaperID(CFStringRef name); +extern void _cupsAppleSetDefaultPrinter(CFStringRef name); +extern void _cupsAppleSetUseLastPrinter(int uselast); +# endif /* __APPLE__ */ + +extern char *_cupsBufferGet(size_t size); +extern void _cupsBufferRelease(char *b); + +extern http_t *_cupsConnect(void); +extern int _cupsGet1284Values(const char *device_id, + cups_option_t **values); +extern const char *_cupsGetDestResource(cups_dest_t *dest, char *resource, + size_t resourcesize); +extern int _cupsGetDests(http_t *http, ipp_op_t op, + const char *name, cups_dest_t **dests, + cups_ptype_t type, cups_ptype_t mask); +extern const char *_cupsGetPassword(const char *prompt); +extern void _cupsGlobalLock(void); +extern _cups_globals_t *_cupsGlobals(void); +extern void _cupsGlobalUnlock(void); +# ifdef HAVE_GSSAPI +extern const char *_cupsGSSServiceName(void); +# endif /* HAVE_GSSAPI */ +extern int _cupsNextDelay(int current, int *previous); +extern void _cupsSetDefaults(void); +extern void _cupsSetError(ipp_status_t status, const char *message, + int localize); +extern void _cupsSetHTTPError(http_status_t status); +# ifdef HAVE_GSSAPI +extern int _cupsSetNegotiateAuthString(http_t *http, + const char *method, + const char *resource); +# endif /* HAVE_GSSAPI */ +extern char *_cupsUserDefault(char *name, size_t namesize); + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_CUPS_PRIVATE_H_ */ + +/* + * End of "$Id: cups-private.h 9596 2011-03-11 18:26:36Z mike $". + */ diff --git a/cups/cups.h b/cups/cups.h new file mode 100644 index 0000000..4edf3d0 --- /dev/null +++ b/cups/cups.h @@ -0,0 +1,601 @@ +/* + * "$Id: cups.h 8781 2009-08-28 17:34:54Z mike $" + * + * API definitions for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 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. + */ + +#ifndef _CUPS_CUPS_H_ +# define _CUPS_CUPS_H_ + +/* + * Include necessary headers... + */ + +# include +# if defined(WIN32) && !defined(__CUPS_SSIZE_T_DEFINED) +# define __CUPS_SSIZE_T_DEFINED +# include +/* Windows does not support the ssize_t type, so map it to off_t... */ +typedef off_t ssize_t; /* @private@ */ +# endif /* WIN32 && !__CUPS_SSIZE_T_DEFINED */ + +# ifdef __BLOCKS__ +# include +# endif /* __BLOCKS__ */ + +# include "file.h" +# include "ipp.h" +# include "language.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Constants... + */ + +# define CUPS_VERSION 1.0603 +# define CUPS_VERSION_MAJOR 1 +# define CUPS_VERSION_MINOR 6 +# define CUPS_VERSION_PATCH 3 + +# define CUPS_BC_FD 3 + /* Back-channel file descriptor for + * select/poll */ +# define CUPS_DATE_ANY (time_t)-1 +# define CUPS_EXCLUDE_NONE (const char *)0 +# define CUPS_FORMAT_AUTO "application/octet-stream" +# define CUPS_FORMAT_COMMAND "application/vnd.cups-command" +# define CUPS_FORMAT_JPEG "image/jpeg" +# define CUPS_FORMAT_PDF "application/pdf" +# define CUPS_FORMAT_POSTSCRIPT "application/postscript" +# define CUPS_FORMAT_RAW "application/vnd.cups-raw" +# define CUPS_FORMAT_TEXT "text/plain" +# define CUPS_HTTP_DEFAULT (http_t *)0 +# define CUPS_INCLUDE_ALL (const char *)0 +# define CUPS_JOBID_ALL -1 +# define CUPS_JOBID_CURRENT 0 +# define CUPS_LENGTH_VARIABLE (ssize_t)0 +# define CUPS_TIMEOUT_DEFAULT 0 +# define CUPS_WHICHJOBS_ALL -1 +# define CUPS_WHICHJOBS_ACTIVE 0 +# define CUPS_WHICHJOBS_COMPLETED 1 + +/* Flags for cupsConnectDest and cupsEnumDests */ +# define CUPS_DEST_FLAGS_NONE 0x00 + /* No flags are set */ +# define CUPS_DEST_FLAGS_UNCONNECTED 0x01 + /* There is not connection */ +# define CUPS_DEST_FLAGS_MORE 0x02 + /* There are more destinations */ +# define CUPS_DEST_FLAGS_REMOVED 0x04 + /* The destination has gone away */ +# define CUPS_DEST_FLAGS_ERROR 0x08 + /* An error occurred */ +# define CUPS_DEST_FLAGS_RESOLVING 0x10 + /* The destination address is being + * resolved */ +# define CUPS_DEST_FLAGS_CONNECTING 0x20 + /* A connection is being established */ +# define CUPS_DEST_FLAGS_CANCELED 0x40 + /* Operation was canceled */ + +/* Flags for cupsGetDestMediaByName/Size */ +# define CUPS_MEDIA_FLAGS_DEFAULT 0x00 + /* Find the closest size supported by + * the printer */ +# define CUPS_MEDIA_FLAGS_BORDERLESS 0x01 + /* Find a borderless size */ +# define CUPS_MEDIA_FLAGS_DUPLEX 0x02 + /* Find a size compatible with 2-sided + * printing */ +# define CUPS_MEDIA_FLAGS_EXACT 0x04 + /* Find an exact match for the size */ +# define CUPS_MEDIA_FLAGS_READY 0x08 + /* If the printer supports media + * sensing, find the size amongst the + * "ready" media. */ + +/* Options and values */ +# define CUPS_COPIES "copies" +# define CUPS_COPIES_SUPPORTED "copies-supported" + +# define CUPS_FINISHINGS "finishings" +# define CUPS_FINISHINGS_SUPPORTED "finishings-supported" + +# define CUPS_FINISHINGS_BIND "7" +# define CUPS_FINISHINGS_COVER "6" +# define CUPS_FINISHINGS_FOLD "10" +# define CUPS_FINISHINGS_NONE "3" +# define CUPS_FINISHINGS_PUNCH "5" +# define CUPS_FINISHINGS_STAPLE "4" +# define CUPS_FINISHINGS_TRIM "11" + +# define CUPS_MEDIA "media" +# define CUPS_MEDIA_READY "media-ready" +# define CUPS_MEDIA_SUPPORTED "media-supported" + +# define CUPS_MEDIA_3X5 "na_index-3x5_3x5in" +# define CUPS_MEDIA_4X6 "na_index-4x6_4x6in" +# define CUPS_MEDIA_5X7 "na_5x7_5x7in" +# define CUPS_MEDIA_8X10 "na_govt-letter_8x10in" +# define CUPS_MEDIA_A3 "iso_a3_297x420mm" +# define CUPS_MEDIA_A4 "iso_a4_210x297mm" +# define CUPS_MEDIA_A5 "iso_a5_148x210mm" +# define CUPS_MEDIA_A6 "iso_a6_105x148mm" +# define CUPS_MEDIA_ENV10 "na_number-10_4.125x9.5in" +# define CUPS_MEDIA_ENVDL "iso_dl_110x220mm" +# define CUPS_MEDIA_LEGAL "na_legal_8.5x14in" +# define CUPS_MEDIA_LETTER "na_letter_8.5x11in" +# define CUPS_MEDIA_PHOTO_L "oe_photo-l_3.5x5in" +# define CUPS_MEDIA_SUPERBA3 "na_super-b_13x19in" +# define CUPS_MEDIA_TABLOID "na_ledger_11x17in" + +# define CUPS_MEDIA_SOURCE "media-source" +# define CUPS_MEDIA_SOURCE_SUPPORTED "media-source-supported" + +# define CUPS_MEDIA_SOURCE_AUTO "auto" +# define CUPS_MEDIA_SOURCE_MANUAL "manual" + +# define CUPS_MEDIA_TYPE "media-type" +# define CUPS_MEDIA_TYPE_SUPPORTED "media-type-supported" + +# define CUPS_MEDIA_TYPE_AUTO "auto" +# define CUPS_MEDIA_TYPE_ENVELOPE "envelope" +# define CUPS_MEDIA_TYPE_LABELS "labels" +# define CUPS_MEDIA_TYPE_LETTERHEAD "stationery-letterhead" +# define CUPS_MEDIA_TYPE_PHOTO "photographic" +# define CUPS_MEDIA_TYPE_PHOTO_GLOSSY "photographic-glossy" +# define CUPS_MEDIA_TYPE_PHOTO_MATTE "photographic-matte" +# define CUPS_MEDIA_TYPE_PLAIN "stationery" +# define CUPS_MEDIA_TYPE_TRANSPARENCY "transparency" + +# define CUPS_NUMBER_UP "number-up" +# define CUPS_NUMBER_UP_SUPPORTED "number-up-supported" + +# define CUPS_ORIENTATION "orientation-requested" +# define CUPS_ORIENTATION_SUPPORTED "orientation-requested-supported" + +# define CUPS_ORIENTATION_PORTRAIT "3" +# define CUPS_ORIENTATION_LANDSCAPE "4" + +# define CUPS_PRINT_COLOR_MODE "print-color-mode" +# define CUPS_PRINT_COLOR_MODE_SUPPORTED "print-color-mode-supported" + +# define CUPS_PRINT_COLOR_MODE_AUTO "auto" +# define CUPS_PRINT_COLOR_MODE_MONOCHROME "monochrome" +# define CUPS_PRINT_COLOR_MODE_COLOR "color" + +# define CUPS_PRINT_QUALITY "print-quality" +# define CUPS_PRINT_QUALITY_SUPPORTED "print-quality-supported" + +# define CUPS_PRINT_QUALITY_DRAFT "3" +# define CUPS_PRINT_QUALITY_NORMAL "4" +# define CUPS_PRINT_QUALITY_HIGH "5" + +# define CUPS_SIDES "sides" +# define CUPS_SIDES_SUPPORTED "sides-supported" + +# define CUPS_SIDES_ONE_SIDED "one-sided" +# define CUPS_SIDES_TWO_SIDED_PORTRAIT "two-sided-long-edge" +# define CUPS_SIDES_TWO_SIDED_LANDSCAPE "two-sided-short-edge" + + +/* + * Types and structures... + */ + +typedef unsigned cups_ptype_t; /* Printer type/capability bits */ +enum cups_ptype_e /* Printer type/capability bit + * constants */ +{ /* Not a typedef'd enum so we can OR */ + CUPS_PRINTER_LOCAL = 0x0000, /* Local printer or class */ + CUPS_PRINTER_CLASS = 0x0001, /* Printer class */ + CUPS_PRINTER_REMOTE = 0x0002, /* Remote printer or class */ + CUPS_PRINTER_BW = 0x0004, /* Can do B&W printing */ + CUPS_PRINTER_COLOR = 0x0008, /* Can do color printing */ + CUPS_PRINTER_DUPLEX = 0x0010, /* Can do duplexing */ + CUPS_PRINTER_STAPLE = 0x0020, /* Can staple output */ + CUPS_PRINTER_COPIES = 0x0040, /* Can do copies */ + CUPS_PRINTER_COLLATE = 0x0080, /* Can collage copies */ + CUPS_PRINTER_PUNCH = 0x0100, /* Can punch output */ + CUPS_PRINTER_COVER = 0x0200, /* Can cover output */ + CUPS_PRINTER_BIND = 0x0400, /* Can bind output */ + CUPS_PRINTER_SORT = 0x0800, /* Can sort output */ + CUPS_PRINTER_SMALL = 0x1000, /* Can do Letter/Legal/A4 */ + CUPS_PRINTER_MEDIUM = 0x2000, /* Can do Tabloid/B/C/A3/A2 */ + CUPS_PRINTER_LARGE = 0x4000, /* Can do D/E/A1/A0 */ + CUPS_PRINTER_VARIABLE = 0x8000, /* Can do variable sizes */ + CUPS_PRINTER_IMPLICIT = 0x10000, /* Implicit class @private@ + * @since Deprecated@ */ + CUPS_PRINTER_DEFAULT = 0x20000, /* Default printer on network */ + CUPS_PRINTER_FAX = 0x40000, /* Fax queue */ + CUPS_PRINTER_REJECTING = 0x80000, /* Printer is rejecting jobs */ + CUPS_PRINTER_DELETE = 0x100000, /* Delete printer + * @since CUPS 1.2/OS X 10.5@ */ + CUPS_PRINTER_NOT_SHARED = 0x200000, /* Printer is not shared + * @since CUPS 1.2/OS X 10.5@ */ + CUPS_PRINTER_AUTHENTICATED = 0x400000,/* Printer requires authentication + * @since CUPS 1.2/OS X 10.5@ */ + CUPS_PRINTER_COMMANDS = 0x800000, /* Printer supports maintenance commands + * @since CUPS 1.2/OS X 10.5@ */ + CUPS_PRINTER_DISCOVERED = 0x1000000, /* Printer was automatically discovered + * and added @private@ + * @since Deprecated@ */ + CUPS_PRINTER_SCANNER = 0x2000000, /* Scanner-only device + * @since CUPS 1.4/OS X 10.6@ */ + CUPS_PRINTER_MFP = 0x4000000, /* Printer with scanning capabilities + * @since CUPS 1.4/OS X 10.6@ */ + CUPS_PRINTER_OPTIONS = 0x6fffc /* ~(CLASS | REMOTE | IMPLICIT | + * DEFAULT | FAX | REJECTING | DELETE | + * NOT_SHARED | AUTHENTICATED | + * COMMANDS | DISCOVERED) @private@ */ +}; + +typedef struct cups_option_s /**** Printer Options ****/ +{ + char *name; /* Name of option */ + char *value; /* Value of option */ +} cups_option_t; + +typedef struct cups_dest_s /**** Destination ****/ +{ + char *name, /* Printer or class name */ + *instance; /* Local instance name or NULL */ + int is_default; /* Is this printer the default? */ + int num_options; /* Number of options */ + cups_option_t *options; /* Options */ +} cups_dest_t; + +typedef struct _cups_dinfo_s cups_dinfo_t; + /* Destination capability and status + * information @since CUPS 1.6/OS X 10.8@ */ + +typedef struct cups_job_s /**** Job ****/ +{ + int id; /* The job ID */ + char *dest; /* Printer or class name */ + char *title; /* Title/job name */ + char *user; /* User the submitted the job */ + char *format; /* Document format */ + ipp_jstate_t state; /* Job state */ + int size; /* Size in kilobytes */ + int priority; /* Priority (1-100) */ + time_t completed_time; /* Time the job was completed */ + time_t creation_time; /* Time the job was created */ + time_t processing_time; /* Time the job was processed */ +} cups_job_t; + +typedef struct cups_size_s /**** Media Size @since CUPS 1.6/OS X 10.8@ ****/ +{ + char media[128]; /* Media name to use */ + int width, /* Width in hundredths of millimeters */ + length, /* Length in hundredths of + * millimeters */ + bottom, /* Bottom margin in hundredths of + * millimeters */ + left, /* Left margin in hundredths of + * millimeters */ + right, /* Right margin in hundredths of + * millimeters */ + top; /* Top margin in hundredths of + * millimeters */ +} cups_size_t; + +typedef int (*cups_client_cert_cb_t)(http_t *http, void *tls, + cups_array_t *distinguished_names, + void *user_data); + /* Client credentials callback + * @since CUPS 1.5/OS X 10.7@ */ + +typedef int (*cups_dest_cb_t)(void *user_data, unsigned flags, + cups_dest_t *dest); + /* Destination enumeration callback + * @since CUPS 1.6/OS X 10.8@ */ + +# ifdef __BLOCKS__ +typedef int (^cups_dest_block_t)(unsigned flags, cups_dest_t *dest); + /* Destination enumeration block + * @since CUPS 1.6/OS X 10.8@ */ +# endif /* __BLOCKS__ */ + +typedef void (*cups_device_cb_t)(const char *device_class, + const char *device_id, const char *device_info, + const char *device_make_and_model, + const char *device_uri, + const char *device_location, void *user_data); + /* Device callback + * @since CUPS 1.4/OS X 10.6@ */ + +typedef const char *(*cups_password_cb_t)(const char *prompt); + /* Password callback */ + +typedef const char *(*cups_password_cb2_t)(const char *prompt, http_t *http, + const char *method, + const char *resource, + void *user_data); + /* New password callback + * @since CUPS 1.4/OS X 10.6@ */ + +typedef int (*cups_server_cert_cb_t)(http_t *http, void *tls, + cups_array_t *certs, void *user_data); + /* Server credentials callback + * @since CUPS 1.5/OS X 10.7@ */ + + +/* + * Functions... + */ + +extern int cupsCancelJob(const char *name, int job_id); +extern ipp_t *cupsDoFileRequest(http_t *http, ipp_t *request, + const char *resource, + const char *filename); +extern ipp_t *cupsDoRequest(http_t *http, ipp_t *request, + const char *resource); +extern http_encryption_t cupsEncryption(void); +extern void cupsFreeJobs(int num_jobs, cups_job_t *jobs); +extern int cupsGetClasses(char ***classes) _CUPS_DEPRECATED; +extern const char *cupsGetDefault(void); +extern int cupsGetJobs(cups_job_t **jobs, const char *name, + int myjobs, int whichjobs); +extern const char *cupsGetPPD(const char *name); +extern int cupsGetPrinters(char ***printers) _CUPS_DEPRECATED; +extern ipp_status_t cupsLastError(void); +extern int cupsPrintFile(const char *name, const char *filename, + const char *title, int num_options, + cups_option_t *options); +extern int cupsPrintFiles(const char *name, int num_files, + const char **files, const char *title, + int num_options, cups_option_t *options); +extern char *cupsTempFile(char *filename, int len) _CUPS_DEPRECATED; +extern int cupsTempFd(char *filename, int len); + +extern int cupsAddDest(const char *name, const char *instance, + int num_dests, cups_dest_t **dests); +extern void cupsFreeDests(int num_dests, cups_dest_t *dests); +extern cups_dest_t *cupsGetDest(const char *name, const char *instance, + int num_dests, cups_dest_t *dests); +extern int cupsGetDests(cups_dest_t **dests); +extern void cupsSetDests(int num_dests, cups_dest_t *dests); + +extern int cupsAddOption(const char *name, const char *value, + int num_options, cups_option_t **options); +extern void cupsEncodeOptions(ipp_t *ipp, int num_options, + cups_option_t *options); +extern void cupsFreeOptions(int num_options, + cups_option_t *options); +extern const char *cupsGetOption(const char *name, int num_options, + cups_option_t *options); +extern int cupsParseOptions(const char *arg, int num_options, + cups_option_t **options); + +extern const char *cupsGetPassword(const char *prompt); +extern const char *cupsServer(void); +extern void cupsSetEncryption(http_encryption_t e); +extern void cupsSetPasswordCB(cups_password_cb_t cb); +extern void cupsSetServer(const char *server); +extern void cupsSetUser(const char *user); +extern const char *cupsUser(void); + +/**** New in CUPS 1.1.20 ****/ +extern int cupsDoAuthentication(http_t *http, const char *method, + const char *resource) + _CUPS_API_1_1_20; +extern http_status_t cupsGetFile(http_t *http, const char *resource, + const char *filename) _CUPS_API_1_1_20; +extern http_status_t cupsGetFd(http_t *http, const char *resource, int fd); +extern http_status_t cupsPutFile(http_t *http, const char *resource, + const char *filename) _CUPS_API_1_1_20; +extern http_status_t cupsPutFd(http_t *http, const char *resource, int fd) + _CUPS_API_1_1_20; + +/**** New in CUPS 1.1.21 ****/ +extern const char *cupsGetDefault2(http_t *http) _CUPS_API_1_1_21; +extern int cupsGetDests2(http_t *http, cups_dest_t **dests) + _CUPS_API_1_1_21; +extern int cupsGetJobs2(http_t *http, cups_job_t **jobs, + const char *name, int myjobs, + int whichjobs) _CUPS_API_1_1_21; +extern const char *cupsGetPPD2(http_t *http, const char *name) + _CUPS_API_1_1_21; +extern int cupsPrintFile2(http_t *http, const char *name, + const char *filename, + const char *title, int num_options, + cups_option_t *options) _CUPS_API_1_1_21; +extern int cupsPrintFiles2(http_t *http, const char *name, + int num_files, const char **files, + const char *title, int num_options, + cups_option_t *options) + _CUPS_API_1_1_21; +extern int cupsSetDests2(http_t *http, int num_dests, + cups_dest_t *dests) _CUPS_API_1_1_21; + +/**** New in CUPS 1.2/OS X 10.5 ****/ +extern ssize_t cupsBackChannelRead(char *buffer, size_t bytes, + double timeout) _CUPS_API_1_2; +extern ssize_t cupsBackChannelWrite(const char *buffer, size_t bytes, + double timeout) _CUPS_API_1_2; +extern void cupsEncodeOptions2(ipp_t *ipp, int num_options, + cups_option_t *options, + ipp_tag_t group_tag) _CUPS_API_1_2; +extern const char *cupsLastErrorString(void) _CUPS_API_1_2; +extern char *cupsNotifySubject(cups_lang_t *lang, ipp_t *event) + _CUPS_API_1_2; +extern char *cupsNotifyText(cups_lang_t *lang, ipp_t *event) + _CUPS_API_1_2; +extern int cupsRemoveOption(const char *name, int num_options, + cups_option_t **options) _CUPS_API_1_2; +extern cups_file_t *cupsTempFile2(char *filename, int len) _CUPS_API_1_2; + +/**** New in CUPS 1.3/OS X 10.5 ****/ +extern ipp_t *cupsDoIORequest(http_t *http, ipp_t *request, + const char *resource, int infile, + int outfile) _CUPS_API_1_3; +extern char *cupsGetServerPPD(http_t *http, const char *name) + _CUPS_API_1_3; +extern int cupsRemoveDest(const char *name, + const char *instance, + int num_dests, cups_dest_t **dests) + _CUPS_API_1_3; +extern void cupsSetDefaultDest(const char *name, + const char *instance, + int num_dests, + cups_dest_t *dests) _CUPS_API_1_3; + +/**** New in CUPS 1.4/OS X 10.6 ****/ +extern ipp_status_t cupsCancelJob2(http_t *http, const char *name, + int job_id, int purge) _CUPS_API_1_4; +extern int cupsCreateJob(http_t *http, const char *name, + const char *title, int num_options, + cups_option_t *options) _CUPS_API_1_4; +extern ipp_status_t cupsFinishDocument(http_t *http, + const char *name) _CUPS_API_1_4; +extern ipp_status_t cupsGetDevices(http_t *http, int timeout, + const char *include_schemes, + const char *exclude_schemes, + cups_device_cb_t callback, + void *user_data) _CUPS_API_1_4; +extern cups_dest_t *cupsGetNamedDest(http_t *http, const char *name, + const char *instance) _CUPS_API_1_4; +extern const char *cupsGetPassword2(const char *prompt, http_t *http, + const char *method, + const char *resource) _CUPS_API_1_4; +extern http_status_t cupsGetPPD3(http_t *http, const char *name, + time_t *modtime, char *buffer, + size_t bufsize) _CUPS_API_1_4; +extern ipp_t *cupsGetResponse(http_t *http, + const char *resource) _CUPS_API_1_4; +extern ssize_t cupsReadResponseData(http_t *http, char *buffer, + size_t length) _CUPS_API_1_4; +extern http_status_t cupsSendRequest(http_t *http, ipp_t *request, + const char *resource, + size_t length) _CUPS_API_1_4; +extern void cupsSetPasswordCB2(cups_password_cb2_t cb, + void *user_data) _CUPS_API_1_4; +extern http_status_t cupsStartDocument(http_t *http, const char *name, + int job_id, const char *docname, + const char *format, + int last_document) _CUPS_API_1_4; +extern http_status_t cupsWriteRequestData(http_t *http, const char *buffer, + size_t length) _CUPS_API_1_4; + +/**** New in CUPS 1.5/OS X 10.7 ****/ +extern void cupsSetClientCertCB(cups_client_cert_cb_t cb, + void *user_data) _CUPS_API_1_5; +extern int cupsSetCredentials(cups_array_t *certs) _CUPS_API_1_5; +extern void cupsSetServerCertCB(cups_server_cert_cb_t cb, + void *user_data) _CUPS_API_1_5; + +/**** New in CUPS 1.6/OS X 10.8 ****/ +extern ipp_status_t cupsCancelDestJob(http_t *http, cups_dest_t *dest, + int job_id) _CUPS_API_1_6; +extern int cupsCheckDestSupported(http_t *http, cups_dest_t *dest, + cups_dinfo_t *info, + const char *option, + const char *value) _CUPS_API_1_6; +extern ipp_status_t cupsCloseDestJob(http_t *http, cups_dest_t *dest, + cups_dinfo_t *info, int job_id) + _CUPS_API_1_6; +extern http_t *cupsConnectDest(cups_dest_t *dest, unsigned flags, + int msec, int *cancel, + char *resource, size_t resourcesize, + cups_dest_cb_t cb, void *user_data) + _CUPS_API_1_6; +# ifdef __BLOCKS__ +extern http_t *cupsConnectDestBlock(cups_dest_t *dest, + unsigned flags, int msec, + int *cancel, char *resource, + size_t resourcesize, + cups_dest_block_t block) + _CUPS_API_1_6; +# endif /* __BLOCKS__ */ +extern int cupsCopyDest(cups_dest_t *dest, int num_dests, + cups_dest_t **dests) _CUPS_API_1_6; +extern cups_dinfo_t *cupsCopyDestInfo(http_t *http, cups_dest_t *dest) + _CUPS_API_1_6; +extern int cupsCopyDestConflicts(http_t *http, cups_dest_t *dest, + cups_dinfo_t *info, + int num_options, + cups_option_t *options, + const char *new_option, + const char *new_value, + int *num_conflicts, + cups_option_t **conflicts, + int *num_resolved, + cups_option_t **resolved) + _CUPS_API_1_6; +extern ipp_status_t cupsCreateDestJob(http_t *http, cups_dest_t *dest, + cups_dinfo_t *info, int *job_id, + const char *title, int num_options, + cups_option_t *options) _CUPS_API_1_6; +extern int cupsEnumDests(unsigned flags, int msec, int *cancel, + cups_ptype_t type, cups_ptype_t mask, + cups_dest_cb_t cb, void *user_data) + _CUPS_API_1_6; +# ifdef __BLOCKS__ +extern int cupsEnumDestsBlock(unsigned flags, int msec, + int *cancel, cups_ptype_t type, + cups_ptype_t mask, + cups_dest_block_t block) + _CUPS_API_1_6; +# endif /* __BLOCKS__ */ +extern ipp_status_t cupsFinishDestDocument(http_t *http, + cups_dest_t *dest, + cups_dinfo_t *info) + _CUPS_API_1_6; +extern void cupsFreeDestInfo(cups_dinfo_t *dinfo) _CUPS_API_1_6; +extern int cupsGetDestMediaByName(http_t *http, cups_dest_t *dest, + cups_dinfo_t *dinfo, + const char *media, + unsigned flags, + cups_size_t *size) _CUPS_API_1_6; +extern int cupsGetDestMediaBySize(http_t *http, cups_dest_t *dest, + cups_dinfo_t *dinfo, + int width, int length, + unsigned flags, + cups_size_t *size) _CUPS_API_1_6; +extern const char *cupsLocalizeDestOption(http_t *http, cups_dest_t *dest, + cups_dinfo_t *info, + const char *option) + _CUPS_API_1_6; +extern const char *cupsLocalizeDestValue(http_t *http, cups_dest_t *dest, + cups_dinfo_t *info, + const char *option, + const char *value) + _CUPS_API_1_6; +extern http_status_t cupsStartDestDocument(http_t *http, cups_dest_t *dest, + cups_dinfo_t *info, int job_id, + const char *docname, + const char *format, + int num_options, + cups_option_t *options, + int last_document) _CUPS_API_1_6; + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_CUPS_H_ */ + +/* + * End of "$Id: cups.h 8781 2009-08-28 17:34:54Z mike $". + */ diff --git a/cups/custom.c b/cups/custom.c new file mode 100644 index 0000000..4c4cc8a --- /dev/null +++ b/cups/custom.c @@ -0,0 +1,122 @@ +/* + * "$Id: custom.c 6649 2007-07-11 21:46:42Z mike $" + * + * PPD custom option routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This code and any derivative of it may be used and distributed + * freely under the terms of the GNU General Public License when + * used with GNU Ghostscript or its derivatives. Use of the code + * (or any derivative of it) with software other than GNU + * GhostScript (or its derivatives) is governed by the CUPS license + * agreement. + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * ppdFindCustomOption() - Find a custom option. + * ppdFindCustomParam() - Find a parameter for a custom option. + * ppdFirstCustomParam() - Return the first parameter for a custom option. + * ppdNextCustomParam() - Return the next parameter for a custom option. + */ + +/* + * Include necessary headers. + */ + +#include "cups-private.h" + + +/* + * 'ppdFindCustomOption()' - Find a custom option. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_coption_t * /* O - Custom option or NULL */ +ppdFindCustomOption(ppd_file_t *ppd, /* I - PPD file */ + const char *keyword)/* I - Custom option name */ +{ + ppd_coption_t key; /* Custom option search key */ + + + if (!ppd) + return (NULL); + + strlcpy(key.keyword, keyword, sizeof(key.keyword)); + return ((ppd_coption_t *)cupsArrayFind(ppd->coptions, &key)); +} + + +/* + * 'ppdFindCustomParam()' - Find a parameter for a custom option. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_cparam_t * /* O - Custom parameter or NULL */ +ppdFindCustomParam(ppd_coption_t *opt, /* I - Custom option */ + const char *name) /* I - Parameter name */ +{ + ppd_cparam_t *param; /* Current custom parameter */ + + + if (!opt) + return (NULL); + + for (param = (ppd_cparam_t *)cupsArrayFirst(opt->params); + param; + param = (ppd_cparam_t *)cupsArrayNext(opt->params)) + if (!_cups_strcasecmp(param->name, name)) + break; + + return (param); +} + + +/* + * 'ppdFirstCustomParam()' - Return the first parameter for a custom option. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_cparam_t * /* O - Custom parameter or NULL */ +ppdFirstCustomParam(ppd_coption_t *opt) /* I - Custom option */ +{ + if (!opt) + return (NULL); + + return ((ppd_cparam_t *)cupsArrayFirst(opt->params)); +} + + +/* + * 'ppdNextCustomParam()' - Return the next parameter for a custom option. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_cparam_t * /* O - Custom parameter or NULL */ +ppdNextCustomParam(ppd_coption_t *opt) /* I - Custom option */ +{ + if (!opt) + return (NULL); + + return ((ppd_cparam_t *)cupsArrayNext(opt->params)); +} + + +/* + * End of "$Id: custom.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/cups/debug-private.h b/cups/debug-private.h new file mode 100644 index 0000000..26c75a3 --- /dev/null +++ b/cups/debug-private.h @@ -0,0 +1,117 @@ +/* + * "$Id$" + * + * Private debugging macros for CUPS. + * + * Copyright 2007-2012 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. + */ + +#ifndef _CUPS_DEBUG_PRIVATE_H_ +# define _CUPS_DEBUG_PRIVATE_H_ + + +/* + * Include necessary headers... + */ + +# include + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * The debug macros are used if you compile with DEBUG defined. + * + * Usage: + * + * DEBUG_puts("string") + * DEBUG_printf(("format string", arg, arg, ...)); + * + * Note the extra parenthesis around the DEBUG_printf macro... + * + * Newlines are not required on the end of messages, as both add one when + * writing the output. + * + * If the first character is a digit, then it represents the "log level" of the + * message from 0 to 9. The default level is 1. The following defines the + * current levels we use: + * + * 0 = public APIs, other than value accessor functions + * 1 = return values for public APIs + * 2 = public value accessor APIs, progress for public APIs + * 3 = return values for value accessor APIs + * 4 = private APIs, progress for value accessor APIs + * 5 = return values for private APIs + * 6 = progress for private APIs + * 7 = static functions + * 8 = return values for static functions + * 9 = progress for static functions + * + * The DEBUG_set macro allows an application to programmatically enable (or + * disable) debug logging. The arguments correspond to the CUPS_DEBUG_LOG, + * CUPS_DEBUG_LEVEL, and CUPS_DEBUG_FILTER environment variables. + */ + +# ifdef DEBUG +# ifdef WIN32 +# ifdef LIBCUPS2_EXPORTS +# define DLLExport __declspec(dllexport) +# else +# define DLLExport +# endif /* LIBCUPS2_EXPORTS */ +# else +# define DLLExport +# endif /* WIN32 */ +# define DEBUG_puts(x) _cups_debug_puts(x) +# define DEBUG_printf(x) _cups_debug_printf x +# define DEBUG_set(logfile,level,filter) _cups_debug_set(logfile,level,filter,1) +# else +# define DLLExport +# define DEBUG_puts(x) +# define DEBUG_printf(x) +# define DEBUG_set(logfile,level,filter) +# endif /* DEBUG */ + + +/* + * Prototypes... + */ + +extern int _cups_debug_fd; +extern int _cups_debug_level; +extern void DLLExport _cups_debug_printf(const char *format, ...) + __attribute__ ((__format__ (__printf__, 1, 2))); +extern void DLLExport _cups_debug_puts(const char *s); +extern void DLLExport _cups_debug_set(const char *logfile, + const char *level, const char *filter, + int force); +# ifdef WIN32 +extern int _cups_gettimeofday(struct timeval *tv, void *tz); +# define gettimeofday(a,b) _cups_gettimeofday(a, b) +# endif /* WIN32 */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_DEBUG_PRIVATE_H_ */ + +/* + * End of "$Id$". + */ diff --git a/cups/debug.c b/cups/debug.c new file mode 100644 index 0000000..dce9427 --- /dev/null +++ b/cups/debug.c @@ -0,0 +1,658 @@ +/* + * "$Id: debug.c 3643 2012-02-13 16:35:48Z msweet $" + * + * Debugging functions for CUPS. + * + * Copyright 2008-2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * debug_vsnprintf() - Format a string into a fixed size buffer. + * _cups_debug_printf() - Write a formatted line to the log. + * _cups_debug_puts() - Write a single line to the log. + * _cups_debug_set() - Enable or disable debug logging. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include "thread-private.h" +#ifdef WIN32 +# include +# include +# include +# define getpid (int)GetCurrentProcessId +int /* O - 0 on success, -1 on failure */ +_cups_gettimeofday(struct timeval *tv, /* I - Timeval struct */ + void *tz) /* I - Timezone */ +{ + struct _timeb timebuffer; /* Time buffer struct */ + _ftime(&timebuffer); + tv->tv_sec = (long)timebuffer.time; + tv->tv_usec = timebuffer.millitm * 1000; + return 0; +} +#else +# include +# include +#endif /* WIN32 */ +#include +#include + + +/* + * Globals... + */ + +int _cups_debug_fd = -1; + /* Debug log file descriptor */ +int _cups_debug_level = 1; + /* Log level (0 to 9) */ + + +#ifdef DEBUG +/* + * Local globals... + */ + +static regex_t *debug_filter = NULL; + /* Filter expression for messages */ +static int debug_init = 0; /* Did we initialize debugging? */ +static _cups_mutex_t debug_mutex = _CUPS_MUTEX_INITIALIZER; + /* Mutex to control initialization */ + + +/* + * 'debug_vsnprintf()' - Format a string into a fixed size buffer. + */ + +static int /* O - Number of bytes formatted */ +debug_vsnprintf(char *buffer, /* O - Output buffer */ + size_t bufsize, /* O - Size of output buffer */ + const char *format, /* I - printf-style format string */ + va_list ap) /* I - Pointer to additional arguments */ +{ + char *bufptr, /* Pointer to position in buffer */ + *bufend, /* Pointer to end of buffer */ + size, /* Size character (h, l, L) */ + type; /* Format type character */ + int width, /* Width of field */ + prec; /* Number of characters of precision */ + char tformat[100], /* Temporary format string for sprintf() */ + *tptr, /* Pointer into temporary format */ + temp[1024]; /* Buffer for formatted numbers */ + char *s; /* Pointer to string */ + int bytes; /* Total number of bytes needed */ + + + if (!buffer || bufsize < 2 || !format) + return (-1); + + /* + * Loop through the format string, formatting as needed... + */ + + bufptr = buffer; + bufend = buffer + bufsize - 1; + bytes = 0; + + while (*format) + { + if (*format == '%') + { + tptr = tformat; + *tptr++ = *format++; + + if (*format == '%') + { + if (bufptr < bufend) + *bufptr++ = *format; + bytes ++; + format ++; + continue; + } + else if (strchr(" -+#\'", *format)) + *tptr++ = *format++; + + if (*format == '*') + { + /* + * Get width from argument... + */ + + format ++; + width = va_arg(ap, int); + + snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", width); + tptr += strlen(tptr); + } + else + { + width = 0; + + while (isdigit(*format & 255)) + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + width = width * 10 + *format++ - '0'; + } + } + + if (*format == '.') + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + format ++; + + if (*format == '*') + { + /* + * Get precision from argument... + */ + + format ++; + prec = va_arg(ap, int); + + snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", prec); + tptr += strlen(tptr); + } + else + { + prec = 0; + + while (isdigit(*format & 255)) + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + prec = prec * 10 + *format++ - '0'; + } + } + } + + if (*format == 'l' && format[1] == 'l') + { + size = 'L'; + + if (tptr < (tformat + sizeof(tformat) - 2)) + { + *tptr++ = 'l'; + *tptr++ = 'l'; + } + + format += 2; + } + else if (*format == 'h' || *format == 'l' || *format == 'L') + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + size = *format++; + } + else + size = 0; + + if (!*format) + break; + + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + type = *format++; + *tptr = '\0'; + + switch (type) + { + case 'E' : /* Floating point formats */ + case 'G' : + case 'e' : + case 'f' : + case 'g' : + if ((width + 2) > sizeof(temp)) + break; + + sprintf(temp, tformat, va_arg(ap, double)); + + bytes += (int)strlen(temp); + + if (bufptr) + { + if ((bufptr + strlen(temp)) > bufend) + { + strncpy(bufptr, temp, (size_t)(bufend - bufptr)); + bufptr = bufend; + } + else + { + strcpy(bufptr, temp); + bufptr += strlen(temp); + } + } + break; + + case 'B' : /* Integer formats */ + case 'X' : + case 'b' : + case 'd' : + case 'i' : + case 'o' : + case 'u' : + case 'x' : + if ((width + 2) > sizeof(temp)) + break; + +# ifdef HAVE_LONG_LONG + if (size == 'L') + sprintf(temp, tformat, va_arg(ap, long long)); + else +# endif /* HAVE_LONG_LONG */ + if (size == 'l') + sprintf(temp, tformat, va_arg(ap, long)); + else + sprintf(temp, tformat, va_arg(ap, int)); + + bytes += (int)strlen(temp); + + if (bufptr) + { + if ((bufptr + strlen(temp)) > bufend) + { + strncpy(bufptr, temp, (size_t)(bufend - bufptr)); + bufptr = bufend; + } + else + { + strcpy(bufptr, temp); + bufptr += strlen(temp); + } + } + break; + + case 'p' : /* Pointer value */ + if ((width + 2) > sizeof(temp)) + break; + + sprintf(temp, tformat, va_arg(ap, void *)); + + bytes += (int)strlen(temp); + + if (bufptr) + { + if ((bufptr + strlen(temp)) > bufend) + { + strncpy(bufptr, temp, (size_t)(bufend - bufptr)); + bufptr = bufend; + } + else + { + strcpy(bufptr, temp); + bufptr += strlen(temp); + } + } + break; + + case 'c' : /* Character or character array */ + bytes += width; + + if (bufptr) + { + if (width <= 1) + *bufptr++ = va_arg(ap, int); + else + { + if ((bufptr + width) > bufend) + width = (int)(bufend - bufptr); + + memcpy(bufptr, va_arg(ap, char *), (size_t)width); + bufptr += width; + } + } + break; + + case 's' : /* String */ + if ((s = va_arg(ap, char *)) == NULL) + s = "(null)"; + + /* + * Copy the C string, replacing control chars and \ with + * C character escapes... + */ + + for (bufend --; *s && bufptr < bufend; s ++) + { + if (*s == '\n') + { + *bufptr++ = '\\'; + *bufptr++ = 'n'; + bytes += 2; + } + else if (*s == '\r') + { + *bufptr++ = '\\'; + *bufptr++ = 'r'; + bytes += 2; + } + else if (*s == '\t') + { + *bufptr++ = '\\'; + *bufptr++ = 't'; + bytes += 2; + } + else if (*s == '\\') + { + *bufptr++ = '\\'; + *bufptr++ = '\\'; + bytes += 2; + } + else if (*s == '\'') + { + *bufptr++ = '\\'; + *bufptr++ = '\''; + bytes += 2; + } + else if (*s == '\"') + { + *bufptr++ = '\\'; + *bufptr++ = '\"'; + bytes += 2; + } + else if ((*s & 255) < ' ') + { + if ((bufptr + 2) >= bufend) + break; + + *bufptr++ = '\\'; + *bufptr++ = '0'; + *bufptr++ = '0' + *s / 8; + *bufptr++ = '0' + (*s & 7); + bytes += 4; + } + else + { + *bufptr++ = *s; + bytes ++; + } + } + + bufend ++; + break; + + case 'n' : /* Output number of chars so far */ + *(va_arg(ap, int *)) = bytes; + break; + } + } + else + { + bytes ++; + + if (bufptr < bufend) + *bufptr++ = *format; + + format ++; + } + } + + /* + * Nul-terminate the string and return the number of characters needed. + */ + + *bufptr = '\0'; + + return (bytes); +} + + +/* + * '_cups_debug_printf()' - Write a formatted line to the log. + */ + +void DLLExport +_cups_debug_printf(const char *format, /* I - Printf-style format string */ + ...) /* I - Additional arguments as needed */ +{ + va_list ap; /* Pointer to arguments */ + struct timeval curtime; /* Current time */ + char buffer[2048]; /* Output buffer */ + size_t bytes; /* Number of bytes in buffer */ + int level; /* Log level in message */ + + + /* + * See if we need to do any logging... + */ + + if (!debug_init) + _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"), + getenv("CUPS_DEBUG_FILTER"), 0); + + if (_cups_debug_fd < 0) + return; + + /* + * Filter as needed... + */ + + if (isdigit(format[0])) + level = *format++ - '0'; + else + level = 0; + + if (level > _cups_debug_level) + return; + + if (debug_filter) + { + int result; /* Filter result */ + + _cupsMutexLock(&debug_mutex); + result = regexec(debug_filter, format, 0, NULL, 0); + _cupsMutexUnlock(&debug_mutex); + + if (result) + return; + } + + /* + * Format the message... + */ + + gettimeofday(&curtime, NULL); + snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d.%03d ", + (int)((curtime.tv_sec / 3600) % 24), + (int)((curtime.tv_sec / 60) % 60), + (int)(curtime.tv_sec % 60), (int)(curtime.tv_usec / 1000)); + + va_start(ap, format); + bytes = debug_vsnprintf(buffer + 13, sizeof(buffer) - 14, format, ap) + 13; + va_end(ap); + + if (bytes >= (sizeof(buffer) - 1)) + { + buffer[sizeof(buffer) - 2] = '\n'; + bytes = sizeof(buffer) - 1; + } + else if (buffer[bytes - 1] != '\n') + { + buffer[bytes++] = '\n'; + buffer[bytes] = '\0'; + } + + /* + * Write it out... + */ + + write(_cups_debug_fd, buffer, bytes); +} + + +/* + * '_cups_debug_puts()' - Write a single line to the log. + */ + +void DLLExport +_cups_debug_puts(const char *s) /* I - String to output */ +{ + struct timeval curtime; /* Current time */ + char buffer[2048]; /* Output buffer */ + size_t bytes; /* Number of bytes in buffer */ + int level; /* Log level in message */ + + + /* + * See if we need to do any logging... + */ + + if (!debug_init) + _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"), + getenv("CUPS_DEBUG_FILTER"), 0); + + if (_cups_debug_fd < 0) + return; + + /* + * Filter as needed... + */ + + if (isdigit(s[0])) + level = *s++ - '0'; + else + level = 0; + + if (level > _cups_debug_level) + return; + + if (debug_filter) + { + int result; /* Filter result */ + + _cupsMutexLock(&debug_mutex); + result = regexec(debug_filter, s, 0, NULL, 0); + _cupsMutexUnlock(&debug_mutex); + + if (result) + return; + } + + /* + * Format the message... + */ + + gettimeofday(&curtime, NULL); + bytes = snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d.%03d %s", + (int)((curtime.tv_sec / 3600) % 24), + (int)((curtime.tv_sec / 60) % 60), + (int)(curtime.tv_sec % 60), (int)(curtime.tv_usec / 1000), + s); + + if (bytes >= (sizeof(buffer) - 1)) + { + buffer[sizeof(buffer) - 2] = '\n'; + bytes = sizeof(buffer) - 1; + } + else if (buffer[bytes - 1] != '\n') + { + buffer[bytes++] = '\n'; + buffer[bytes] = '\0'; + } + + /* + * Write it out... + */ + + write(_cups_debug_fd, buffer, bytes); +} + + +/* + * '_cups_debug_set()' - Enable or disable debug logging. + */ + +void DLLExport +_cups_debug_set(const char *logfile, /* I - Log file or NULL */ + const char *level, /* I - Log level or NULL */ + const char *filter, /* I - Filter string or NULL */ + int force) /* I - Force initialization */ +{ + _cupsMutexLock(&debug_mutex); + + if (!debug_init || force) + { + /* + * Restore debug settings to defaults... + */ + + if (_cups_debug_fd != -1) + { + close(_cups_debug_fd); + _cups_debug_fd = -1; + } + + if (debug_filter) + { + regfree((regex_t *)debug_filter); + debug_filter = NULL; + } + + _cups_debug_level = 1; + + /* + * Open logs, set log levels, etc. + */ + + if (!logfile) + _cups_debug_fd = -1; + else if (!strcmp(logfile, "-")) + _cups_debug_fd = 2; + else + { + char buffer[1024]; /* Filename buffer */ + + snprintf(buffer, sizeof(buffer), logfile, getpid()); + + if (buffer[0] == '+') + _cups_debug_fd = open(buffer + 1, O_WRONLY | O_APPEND | O_CREAT, 0644); + else + _cups_debug_fd = open(buffer, O_WRONLY | O_TRUNC | O_CREAT, 0644); + } + + if (level) + _cups_debug_level = atoi(level); + + if (filter) + { + if ((debug_filter = (regex_t *)calloc(1, sizeof(regex_t))) == NULL) + fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not " + "filtered!\n", stderr); + else if (regcomp(debug_filter, filter, REG_EXTENDED)) + { + fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not " + "filtered!\n", stderr); + free(debug_filter); + debug_filter = NULL; + } + } + + debug_init = 1; + } + + _cupsMutexUnlock(&debug_mutex); +} +#endif /* DEBUG */ + + +/* + * End of "$Id: debug.c 3643 2012-02-13 16:35:48Z msweet $". + */ diff --git a/cups/dest-job.c b/cups/dest-job.c new file mode 100644 index 0000000..dc47f04 --- /dev/null +++ b/cups/dest-job.c @@ -0,0 +1,358 @@ +/* + * "$Id: dest-job.c 3833 2012-05-23 22:51:18Z msweet $" + * + * Destination job support for CUPS. + * + * Copyright 2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * cupsCancelDestJob() - Cancel a job on a destination. + * cupsCloseDestJob() - Close a job and start printing. + * cupsCreateDestJob() - Create a job on a destination. + * cupsFinishDestDocument() - Finish the current document. + * cupsStartDestDocument() - Start a new document. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * 'cupsCancelDestJob()' - Cancel a job on a destination. + * + * The "job_id" is the number returned by cupsCreateDestJob. + * + * Returns IPP_OK on success and IPP_NOT_AUTHORIZED or IPP_FORBIDDEN on + * failure. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_status_t +cupsCancelDestJob(http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + int job_id) /* I - Job ID */ +{ + return (IPP_NOT_FOUND); +} + + +/* + * 'cupsCloseDestJob()' - Close a job and start printing. + * + * Use when the last call to cupsStartDocument passed 0 for "last_document". + * "job_id" is the job ID returned by cupsCreateDestJob. Returns @code IPP_OK@ + * on success. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_status_t /* O - IPP status code */ +cupsCloseDestJob( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *info, /* I - Destination information */ + int job_id) /* I - Job ID */ +{ + int i; /* Looping var */ + ipp_t *request = NULL;/* Close-Job/Send-Document request */ + ipp_attribute_t *attr; /* operations-supported attribute */ + + + DEBUG_printf(("cupsCloseDestJob(http=%p, dest=%p(%s/%s), info=%p, job_id=%d)", + http, dest, dest ? dest->name : NULL, + dest ? dest->instance : NULL, info, job_id)); + + /* + * Range check input... + */ + + if (!http || !dest || !info || job_id <= 0) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + DEBUG_puts("1cupsCloseDestJob: Bad arguments."); + return (IPP_INTERNAL_ERROR); + } + + /* + * Build a Close-Job or empty Send-Document request... + */ + + if ((attr = ippFindAttribute(info->attrs, "operations-supported", + IPP_TAG_ENUM)) != NULL) + { + for (i = 0; i < attr->num_values; i ++) + if (attr->values[i].integer == IPP_CLOSE_JOB) + { + request = ippNewRequest(IPP_CLOSE_JOB); + break; + } + } + + if (!request) + request = ippNewRequest(IPP_SEND_DOCUMENT); + + if (!request) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); + DEBUG_puts("1cupsCloseDestJob: Unable to create Close-Job/Send-Document " + "request."); + return (IPP_INTERNAL_ERROR); + } + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", + NULL, info->uri); + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", + job_id); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", + NULL, cupsUser()); + if (ippGetOperation(request) == IPP_SEND_DOCUMENT) + ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1); + + /* + * Send the request and return the status... + */ + + ippDelete(cupsDoRequest(http, request, info->resource)); + + DEBUG_printf(("1cupsCloseDestJob: %s (%s)", ippErrorString(cupsLastError()), + cupsLastErrorString())); + + return (cupsLastError()); +} + + +/* + * 'cupsCreateDestJob()' - Create a job on a destination. + * + * Returns @code IPP_OK@ or @code IPP_OK_SUBST@ on success, saving the job ID + * in the variable pointed to by "job_id". + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_status_t /* O - IPP status code */ +cupsCreateDestJob( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *info, /* I - Destination information */ + int *job_id, /* O - Job ID or 0 on error */ + const char *title, /* I - Job name */ + int num_options, /* I - Number of job options */ + cups_option_t *options) /* I - Job options */ +{ + ipp_t *request, /* Create-Job request */ + *response; /* Create-Job response */ + ipp_attribute_t *attr; /* job-id attribute */ + + + DEBUG_printf(("cupsCreateDestJob(http=%p, dest=%p(%s/%s), info=%p, " + "job_id=%p, title=\"%s\", num_options=%d, options=%p)", + http, dest, dest ? dest->name : NULL, + dest ? dest->instance : NULL, info, job_id, title, num_options, + options)); + + /* + * Range check input... + */ + + if (job_id) + *job_id = 0; + + if (!http || !dest || !info || !job_id) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + DEBUG_puts("1cupsCreateDestJob: Bad arguments."); + return (IPP_INTERNAL_ERROR); + } + + /* + * Build a Create-Job request... + */ + + if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); + DEBUG_puts("1cupsCreateDestJob: Unable to create Create-Job request."); + return (IPP_INTERNAL_ERROR); + } + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", + NULL, info->uri); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", + NULL, cupsUser()); + if (title) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, + title); + + cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB); + cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION); + + /* + * Send the request and get the job-id... + */ + + response = cupsDoRequest(http, request, info->resource); + + if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL) + { + *job_id = attr->values[0].integer; + DEBUG_printf(("1cupsCreateDestJob: job-id=%d", *job_id)); + } + + ippDelete(response); + + /* + * Return the status code from the Create-Job request... + */ + + DEBUG_printf(("1cupsCreateDestJob: %s (%s)", ippErrorString(cupsLastError()), + cupsLastErrorString())); + + return (cupsLastError()); +} + + +/* + * 'cupsFinishDestDocument()' - Finish the current document. + * + * Returns @code IPP_OK@ or @code IPP_OK_SUBST@ on success. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_status_t /* O - Status of document submission */ +cupsFinishDestDocument( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *info) /* I - Destination information */ +{ + DEBUG_printf(("cupsFinishDestDocument(http=%p, dest=%p(%s/%s), info=%p)", + http, dest, dest ? dest->name : NULL, + dest ? dest->instance : NULL, info)); + + /* + * Range check input... + */ + + if (!http || !dest || !info) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + DEBUG_puts("1cupsFinishDestDocument: Bad arguments."); + return (IPP_INTERNAL_ERROR); + } + + /* + * Get the response at the end of the document and return it... + */ + + ippDelete(cupsGetResponse(http, info->resource)); + + DEBUG_printf(("1cupsFinishDestDocument: %s (%s)", + ippErrorString(cupsLastError()), cupsLastErrorString())); + + return (cupsLastError()); +} + + +/* + * 'cupsStartDestDocument()' - Start a new document. + * + * "job_id" is the job ID returned by cupsCreateDestJob. "docname" is the name + * of the document/file being printed, "format" is the MIME media type for the + * document (see CUPS_FORMAT_xxx constants), and "num_options" and "options" + * are the options do be applied to the document. "last_document" should be 1 + * if this is the last document to be submitted in the job. Returns + * @code HTTP_CONTINUE@ on success. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +http_status_t /* O - Status of document creation */ +cupsStartDestDocument( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *info, /* I - Destination information */ + int job_id, /* I - Job ID */ + const char *docname, /* I - Document name */ + const char *format, /* I - Document format */ + int num_options, /* I - Number of document options */ + cups_option_t *options, /* I - Document options */ + int last_document) /* I - 1 if this is the last document */ +{ + ipp_t *request; /* Send-Document request */ + http_status_t status; /* HTTP status */ + + + DEBUG_printf(("cupsStartDestDocument(http=%p, dest=%p(%s/%s), info=%p, " + "job_id=%d, docname=\"%s\", format=\"%s\", num_options=%d, " + "options=%p, last_document=%d)", + http, dest, dest ? dest->name : NULL, + dest ? dest->instance : NULL, info, job_id, docname, format, + num_options, options, last_document)); + + /* + * Range check input... + */ + + if (!http || !dest || !info || job_id <= 0) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + DEBUG_puts("1cupsStartDestDocument: Bad arguments."); + return (HTTP_ERROR); + } + + /* + * Create a Send-Document request... + */ + + if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); + DEBUG_puts("1cupsStartDestDocument: Unable to create Send-Document " + "request."); + return (HTTP_ERROR); + } + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", + NULL, info->uri); + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", + NULL, cupsUser()); + if (docname) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name", + NULL, docname); + if (format) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, + "document-format", NULL, format); + ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document); + + cupsEncodeOptions2(request, num_options, options, IPP_TAG_DOCUMENT); + + /* + * Send and delete the request, then return the status... + */ + + status = cupsSendRequest(http, request, info->resource, CUPS_LENGTH_VARIABLE); + + ippDelete(request); + + return (status); +} + + +/* + * End of "$Id: dest-job.c 3833 2012-05-23 22:51:18Z msweet $". + */ diff --git a/cups/dest-localization.c b/cups/dest-localization.c new file mode 100644 index 0000000..1398056 --- /dev/null +++ b/cups/dest-localization.c @@ -0,0 +1,386 @@ +/* + * "$Id: dest-localization.c 3833 2012-05-23 22:51:18Z msweet $" + * + * Destination localization support for CUPS. + * + * Copyright 2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * cupsLocalizeDestOption() - Get the localized string for a destination + * option. + * cupsLocalizeDestValue() - Get the localized string for a destination + * option+value pair. + * cups_create_localizations() - Create the localizations array for a + * destination. + * cups_read_strings() - Read a pair of strings from a .strings file. + * cups_scan_strings() - Scan a quoted string. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * Local functions... + */ + +static void cups_create_localizations(http_t *http, cups_dinfo_t *dinfo); +static int cups_read_strings(cups_file_t *fp, char *buffer, size_t bufsize, + char **id, char **str); +static char *cups_scan_strings(char *buffer); + + +/* + * 'cupsLocalizeDestOption()' - Get the localized string for a destination + * option. + * + * The returned string is stored in the destination information and will become + * invalid if the destination information is deleted. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +const char * /* O - Localized string */ +cupsLocalizeDestOption( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *dinfo, /* I - Destination information */ + const char *option) /* I - Option to localize */ +{ + _cups_message_t key, /* Search key */ + *match; /* Matching entry */ + + + if (!http || !dest || !dinfo) + return (option); + + if (!dinfo->localizations) + cups_create_localizations(http, dinfo); + + if (cupsArrayCount(dinfo->localizations) == 0) + return (option); + + key.id = (char *)option; + if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, + &key)) != NULL) + return (match->str); + else + return (option); +} + + +/* + * 'cupsLocalizeDestValue()' - Get the localized string for a destination + * option+value pair. + * + * The returned string is stored in the destination information and will become + * invalid if the destination information is deleted. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +const char * /* O - Localized string */ +cupsLocalizeDestValue( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *dinfo, /* I - Destination information */ + const char *option, /* I - Option to localize */ + const char *value) /* I - Value to localize */ +{ + _cups_message_t key, /* Search key */ + *match; /* Matching entry */ + char pair[256]; /* option.value pair */ + + + if (!http || !dest || !dinfo) + return (value); + + if (!dinfo->localizations) + cups_create_localizations(http, dinfo); + + if (cupsArrayCount(dinfo->localizations) == 0) + return (value); + + snprintf(pair, sizeof(pair), "%s.%s", option, value); + key.id = pair; + if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, + &key)) != NULL) + return (match->str); + else + return (value); +} + + +/* + * 'cups_create_localizations()' - Create the localizations array for a + * destination. + */ + +static void +cups_create_localizations( + http_t *http, /* I - Connection to destination */ + cups_dinfo_t *dinfo) /* I - Destination informations */ +{ + http_t *http2; /* Connection for strings file */ + http_status_t status; /* Request status */ + ipp_attribute_t *attr; /* "printer-strings-uri" attribute */ + char scheme[32], /* URI scheme */ + userpass[256], /* Username/password info */ + hostname[256], /* Hostname */ + resource[1024], /* Resource */ + http_hostname[256], + /* Hostname of connection */ + tempfile[1024]; /* Temporary filename */ + int port; /* Port number */ + http_encryption_t encryption; /* Encryption to use */ + cups_file_t *temp; /* Temporary file */ + + + /* + * Create an empty message catalog... + */ + + dinfo->localizations = _cupsMessageNew(NULL); + + /* + * See if there are any localizations... + */ + + if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri", + IPP_TAG_URI)) == NULL) + { + /* + * Nope... + */ + + DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) " + "value."); + return; /* Nope */ + } + + /* + * Pull apart the URI and determine whether we need to try a different + * server... + */ + + if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, + scheme, sizeof(scheme), userpass, sizeof(userpass), + hostname, sizeof(hostname), &port, resource, + sizeof(resource)) < HTTP_URI_OK) + { + DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value " + "\"%s\".", attr->values[0].string.text)); + return; + } + + httpGetHostname(http, http_hostname, sizeof(http_hostname)); + + if (!_cups_strcasecmp(http_hostname, hostname) && + port == _httpAddrPort(http->hostaddr)) + { + /* + * Use the same connection... + */ + + http2 = http; + } + else + { + /* + * Connect to the alternate host... + */ + + if (!strcmp(scheme, "https")) + encryption = HTTP_ENCRYPT_ALWAYS; + else + encryption = HTTP_ENCRYPT_IF_REQUESTED; + + if ((http2 = httpConnectEncrypt(hostname, port, encryption)) == NULL) + { + DEBUG_printf(("4cups_create_localizations: Unable to connect to " + "%s:%d: %s", hostname, port, cupsLastErrorString())); + return; + } + } + + /* + * Get a temporary file... + */ + + if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL) + { + DEBUG_printf(("4cups_create_localizations: Unable to create temporary " + "file: %s", cupsLastErrorString())); + if (http2 != http) + httpClose(http2); + return; + } + + status = cupsGetFd(http2, resource, cupsFileNumber(temp)); + + DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource, + httpStatus(status))); + + if (status == HTTP_OK) + { + /* + * Got the file, read it... + */ + + char buffer[8192], /* Message buffer */ + *id, /* ID string */ + *str; /* Translated message */ + _cups_message_t *m; /* Current message */ + + lseek(cupsFileNumber(temp), 0, SEEK_SET); + + while (cups_read_strings(temp, buffer, sizeof(buffer), &id, &str)) + { + if ((m = malloc(sizeof(_cups_message_t))) == NULL) + break; + + m->id = strdup(id); + m->str = strdup(str); + + if (m->id && m->str) + cupsArrayAdd(dinfo->localizations, m); + else + { + if (m->id) + free(m->id); + + if (m->str) + free(m->str); + + free(m); + break; + } + } + } + + DEBUG_printf(("4cups_create_localizations: %d messages loaded.", + cupsArrayCount(dinfo->localizations))); + + /* + * Cleanup... + */ + + unlink(tempfile); + cupsFileClose(temp); + + if (http2 != http) + httpClose(http2); +} + + +/* + * 'cups_read_strings()' - Read a pair of strings from a .strings file. + */ + +static int /* O - 1 on success, 0 on failure */ +cups_read_strings(cups_file_t *strings, /* I - .strings file */ + char *buffer, /* I - Line buffer */ + size_t bufsize, /* I - Size of line buffer */ + char **id, /* O - Pointer to ID string */ + char **str) /* O - Pointer to translation string */ +{ + char *bufptr; /* Pointer into buffer */ + + + while (cupsFileGets(strings, buffer, bufsize)) + { + if (buffer[0] != '\"') + continue; + + *id = buffer + 1; + bufptr = cups_scan_strings(buffer); + + if (*bufptr != '\"') + continue; + + *bufptr++ = '\0'; + + while (*bufptr && *bufptr != '\"') + bufptr ++; + + if (!*bufptr) + continue; + + *str = bufptr + 1; + bufptr = cups_scan_strings(bufptr); + + if (*bufptr != '\"') + continue; + + *bufptr = '\0'; + + return (1); + } + + return (0); +} + + +/* + * 'cups_scan_strings()' - Scan a quoted string. + */ + +static char * /* O - End of string */ +cups_scan_strings(char *buffer) /* I - Start of string */ +{ + char *bufptr; /* Pointer into string */ + + + for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++) + { + if (*bufptr == '\\') + { + if (bufptr[1] >= '0' && bufptr[1] <= '3' && + bufptr[2] >= '0' && bufptr[2] <= '7' && + bufptr[3] >= '0' && bufptr[3] <= '7') + { + /* + * Decode \nnn octal escape... + */ + + *bufptr = ((((bufptr[1] - '0') << 3) | (bufptr[2] - '0')) << 3) | + (bufptr[3] - '0'); + _cups_strcpy(bufptr + 1, bufptr + 4); + } + else + { + /* + * Decode \C escape... + */ + + _cups_strcpy(bufptr, bufptr + 1); + if (*bufptr == 'n') + *bufptr = '\n'; + else if (*bufptr == 'r') + *bufptr = '\r'; + else if (*bufptr == 't') + *bufptr = '\t'; + } + } + } + + return (bufptr); +} + + + +/* + * End of "$Id: dest-localization.c 3833 2012-05-23 22:51:18Z msweet $". + */ diff --git a/cups/dest-options.c b/cups/dest-options.c new file mode 100644 index 0000000..234dd13 --- /dev/null +++ b/cups/dest-options.c @@ -0,0 +1,1763 @@ +/* + * "$Id: dest-options.c 4185 2013-02-20 02:19:13Z msweet $" + * + * Destination option/media support for CUPS. + * + * Copyright 2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * cupsCheckDestSupported() - Check that the option and value are supported + * by the destination. + * cupsCopyDestConflicts() - Get conflicts and resolutions for a new + * option/value pair. + * cupsCopyDestInfo() - Get the supported values/capabilities for the + * destination. + * cupsFreeDestInfo() - Free destination information obtained using + * @link cupsCopyDestInfo@. + * cupsGetDestMediaByName() - Get media names, dimensions, and margins. + * cupsGetDestMediaBySize() - Get media names, dimensions, and margins. + * cups_add_dconstres() - Add a constraint or resolver to an array. + * cups_compare_dconstres() - Compare to resolver entries. + * cups_compare_media_db() - Compare two media entries. + * cups_copy_media_db() - Copy a media entry. + * cups_create_constraints() - Create the constraints and resolvers arrays. + * cups_create_defaults() - Create the -default option array. + * cups_create_media_db() - Create the media database. + * cups_free_media_cb() - Free a media entry. + * cups_get_media_db() - Lookup the media entry for a given size. + * cups_is_close_media_db() - Compare two media entries to see if they are + * close to the same size. + * cups_test_constraints() - Test constraints. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * Local functions... + */ + +static void cups_add_dconstres(cups_array_t *a, ipp_t *collection); +static int cups_compare_dconstres(_cups_dconstres_t *a, + _cups_dconstres_t *b); +static int cups_compare_media_db(_cups_media_db_t *a, + _cups_media_db_t *b); +static _cups_media_db_t *cups_copy_media_db(_cups_media_db_t *mdb); +static void cups_create_constraints(cups_dinfo_t *dinfo); +static void cups_create_defaults(cups_dinfo_t *dinfo); +static void cups_create_media_db(cups_dinfo_t *dinfo); +static void cups_free_media_db(_cups_media_db_t *mdb); +static int cups_get_media_db(cups_dinfo_t *dinfo, + _pwg_media_t *pwg, unsigned flags, + cups_size_t *size); +static int cups_is_close_media_db(_cups_media_db_t *a, + _cups_media_db_t *b); +static cups_array_t *cups_test_constraints(cups_dinfo_t *dinfo, + const char *new_option, + const char *new_value, + int num_options, + cups_option_t *options, + int *num_conflicts, + cups_option_t **conflicts); + + +/* + * 'cupsCheckDestSupported()' - Check that the option and value are supported + * by the destination. + * + * Returns 1 if supported, 0 otherwise. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 if supported, 0 otherwise */ +cupsCheckDestSupported( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *dinfo, /* I - Destination information */ + const char *option, /* I - Option */ + const char *value) /* I - Value */ +{ + int i; /* Looping var */ + char temp[1024]; /* Temporary string */ + int int_value; /* Integer value */ + int xres_value, /* Horizontal resolution */ + yres_value; /* Vertical resolution */ + ipp_res_t units_value; /* Resolution units */ + ipp_attribute_t *attr; /* Attribute */ + _ipp_value_t *attrval; /* Current attribute value */ + + + /* + * Range check input... + */ + + if (!http || !dest || !dinfo || !option || !value) + return (0); + + /* + * Lookup the attribute... + */ + + if (strstr(option, "-supported")) + attr = ippFindAttribute(dinfo->attrs, option, IPP_TAG_ZERO); + else + { + snprintf(temp, sizeof(temp), "%s-supported", option); + attr = ippFindAttribute(dinfo->attrs, temp, IPP_TAG_ZERO); + } + + if (!attr) + return (0); + + /* + * Compare values... + */ + + if (!strcmp(option, "media") && !strncmp(value, "custom_", 7)) + { + /* + * Check range of custom media sizes... + */ + + _pwg_media_t *pwg; /* Current PWG media size info */ + int min_width, /* Minimum width */ + min_length, /* Minimum length */ + max_width, /* Maximum width */ + max_length; /* Maximum length */ + + /* + * Get the minimum and maximum size... + */ + + min_width = min_length = INT_MAX; + max_width = max_length = 0; + + for (i = attr->num_values, attrval = attr->values; + i > 0; + i --, attrval ++) + { + if (!strncmp(attrval->string.text, "custom_min_", 11) && + (pwg = _pwgMediaForPWG(attrval->string.text)) != NULL) + { + min_width = pwg->width; + min_length = pwg->length; + } + else if (!strncmp(attrval->string.text, "custom_max_", 11) && + (pwg = _pwgMediaForPWG(attrval->string.text)) != NULL) + { + max_width = pwg->width; + max_length = pwg->length; + } + } + + /* + * Check the range... + */ + + if (min_width < INT_MAX && max_width > 0 && + (pwg = _pwgMediaForPWG(value)) != NULL && + pwg->width >= min_width && pwg->width <= max_width && + pwg->length >= min_length && pwg->length <= max_length) + return (1); + } + else + { + /* + * Check literal values... + */ + + switch (attr->value_tag) + { + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + int_value = atoi(value); + + for (i = 0; i < attr->num_values; i ++) + if (attr->values[i].integer == int_value) + return (1); + break; + + case IPP_TAG_BOOLEAN : + return (attr->values[0].boolean); + + case IPP_TAG_RANGE : + int_value = atoi(value); + + for (i = 0; i < attr->num_values; i ++) + if (int_value >= attr->values[i].range.lower && + int_value <= attr->values[i].range.upper) + return (1); + break; + + case IPP_TAG_RESOLUTION : + if (sscanf(value, "%dx%d%15s", &xres_value, &yres_value, temp) != 3) + { + if (sscanf(value, "%d%15s", &xres_value, temp) != 2) + return (0); + + yres_value = xres_value; + } + + if (!strcmp(temp, "dpi")) + units_value = IPP_RES_PER_INCH; + else if (!strcmp(temp, "dpc") || !strcmp(temp, "dpcm")) + units_value = IPP_RES_PER_CM; + else + return (0); + + for (i = attr->num_values, attrval = attr->values; + i > 0; + i --, attrval ++) + { + if (attrval->resolution.xres == xres_value && + attrval->resolution.yres == yres_value && + attrval->resolution.units == units_value) + return (1); + } + break; + + case IPP_TAG_TEXT : + case IPP_TAG_NAME : + case IPP_TAG_KEYWORD : + case IPP_TAG_CHARSET : + case IPP_TAG_URI : + case IPP_TAG_URISCHEME : + case IPP_TAG_MIMETYPE : + case IPP_TAG_LANGUAGE : + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + for (i = 0; i < attr->num_values; i ++) + if (!strcmp(attr->values[i].string.text, value)) + return (1); + break; + + default : + break; + } + } + + /* + * If we get there the option+value is not supported... + */ + + return (0); +} + + +/* + * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new + * option/value pair. + * + * "num_options" and "options" represent the currently selected options by the + * user. "new_option" and "new_value" are the setting the user has just + * changed. + * + * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if + * there was an unrecoverable error such as a resolver loop. + * + * If "num_conflicts" and "conflicts" are not @code NULL@, they are set to + * contain the list of conflicting option/value pairs. Similarly, if + * "num_resolved" and "resolved" are not @code NULL@ they will be set to the + * list of changes needed to resolve the conflict. + * + * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set + * to 0 and @code NULL@, respectively, then the conflict cannot be resolved. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 if there is a conflict, 0 if none, -1 on error */ +cupsCopyDestConflicts( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *dinfo, /* I - Destination information */ + int num_options, /* I - Number of current options */ + cups_option_t *options, /* I - Current options */ + const char *new_option, /* I - New option */ + const char *new_value, /* I - New value */ + int *num_conflicts, /* O - Number of conflicting options */ + cups_option_t **conflicts, /* O - Conflicting options */ + int *num_resolved, /* O - Number of options to resolve */ + cups_option_t **resolved) /* O - Resolved options */ +{ + int i, /* Looping var */ + have_conflicts = 0, /* Do we have conflicts? */ + changed, /* Did we change something? */ + tries, /* Number of tries for resolution */ + num_myconf = 0, /* My number of conflicting options */ + num_myres = 0; /* My number of resolved options */ + cups_option_t *myconf = NULL, /* My conflicting options */ + *myres = NULL, /* My resolved options */ + *myoption, /* My current option */ + *option; /* Current option */ + cups_array_t *active, /* Active conflicts */ + *pass = NULL, /* Resolvers for this pass */ + *resolvers = NULL, /* Resolvers we have used */ + *test; /* Test array for conflicts */ + _cups_dconstres_t *c, /* Current constraint */ + *r; /* Current resolver */ + ipp_attribute_t *attr; /* Current attribute */ + char value[2048]; /* Current attribute value as string */ + const char *myvalue; /* Current value of an option */ + + + /* + * Clear returned values... + */ + + if (num_conflicts) + *num_conflicts = 0; + + if (conflicts) + *conflicts = NULL; + + if (num_resolved) + *num_resolved = 0; + + if (resolved) + *resolved = NULL; + + /* + * Range check input... + */ + + if (!http || !dest || !dinfo || + (num_conflicts != NULL) != (conflicts != NULL) || + (num_resolved != NULL) != (resolved != NULL)) + return (0); + + /* + * Load constraints as needed... + */ + + if (!dinfo->constraints) + cups_create_constraints(dinfo); + + if (cupsArrayCount(dinfo->constraints) == 0) + return (0); + + if (!dinfo->num_defaults) + cups_create_defaults(dinfo); + + /* + * If we are resolving, create a shadow array... + */ + + if (num_resolved) + { + for (i = num_options, option = options; i > 0; i --, option ++) + num_myres = cupsAddOption(option->name, option->value, num_myres, &myres); + + if (new_option && new_value) + num_myres = cupsAddOption(new_option, new_value, num_myres, &myres); + } + else + { + num_myres = num_options; + myres = options; + } + + /* + * Check for any conflicts... + */ + + if (num_resolved) + pass = cupsArrayNew((cups_array_func_t)cups_compare_dconstres, NULL); + + for (tries = 0; tries < 100; tries ++) + { + /* + * Check for any conflicts... + */ + + if (num_conflicts || num_resolved) + { + cupsFreeOptions(num_myconf, myconf); + + num_myconf = 0; + myconf = NULL; + active = cups_test_constraints(dinfo, new_option, new_value, + num_myres, myres, &num_myconf, + &myconf); + } + else + active = cups_test_constraints(dinfo, new_option, new_value, num_myres, + myres, NULL, NULL); + + have_conflicts = (active != NULL); + + if (!active || !num_resolved) + break; /* All done */ + + /* + * Scan the constraints that were triggered to apply resolvers... + */ + + if (!resolvers) + resolvers = cupsArrayNew((cups_array_func_t)cups_compare_dconstres, NULL); + + for (c = (_cups_dconstres_t *)cupsArrayFirst(active), changed = 0; + c; + c = (_cups_dconstres_t *)cupsArrayNext(active)) + { + if (cupsArrayFind(pass, c)) + continue; /* Already applied this resolver... */ + + if (cupsArrayFind(resolvers, c)) + { + DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.", + c->name)); + have_conflicts = -1; + goto cleanup; + } + + if ((r = cupsArrayFind(dinfo->resolvers, c)) == NULL) + { + DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.", + c->name)); + have_conflicts = -1; + goto cleanup; + } + + /* + * Add the options from the resolver... + */ + + cupsArrayAdd(pass, r); + cupsArrayAdd(resolvers, r); + + for (attr = ippFirstAttribute(r->collection); + attr; + attr = ippNextAttribute(r->collection)) + { + if (new_option && !strcmp(attr->name, new_option)) + continue; /* Ignore this if we just changed it */ + + if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value)) + continue; /* Ignore if the value is too long */ + + if ((test = cups_test_constraints(dinfo, attr->name, value, num_myres, + myres, NULL, NULL)) == NULL) + { + /* + * That worked, flag it... + */ + + changed = 1; + } + else + cupsArrayDelete(test); + + /* + * Add the option/value from the resolver regardless of whether it + * worked; this makes sure that we can cascade several changes to + * make things resolve... + */ + + num_myres = cupsAddOption(attr->name, value, num_myres, &myres); + } + } + + if (!changed) + { + DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints."); + have_conflicts = -1; + goto cleanup; + } + + cupsArrayClear(pass); + + cupsArrayDelete(active); + active = NULL; + } + + if (tries >= 100) + { + DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries."); + have_conflicts = -1; + goto cleanup; + } + + /* + * Copy resolved options as needed... + */ + + if (num_resolved) + { + for (i = num_myres, myoption = myres; i > 0; i --, myoption ++) + { + if ((myvalue = cupsGetOption(myoption->name, num_options, + options)) == NULL || + strcmp(myvalue, myoption->value)) + { + if (new_option && !strcmp(new_option, myoption->name) && + new_value && !strcmp(new_value, myoption->value)) + continue; + + *num_resolved = cupsAddOption(myoption->name, myoption->value, + *num_resolved, resolved); + } + } + } + + /* + * Clean up... + */ + + cleanup: + + cupsArrayDelete(active); + cupsArrayDelete(pass); + cupsArrayDelete(resolvers); + + if (num_resolved) + { + /* + * Free shadow copy of options... + */ + + cupsFreeOptions(num_myres, myres); + } + + if (num_conflicts) + { + /* + * Return conflicting options to caller... + */ + + *num_conflicts = num_myconf; + *conflicts = myconf; + } + else + { + /* + * Free conflicting options... + */ + + cupsFreeOptions(num_myconf, myconf); + } + + return (have_conflicts); +} + + +/* + * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the + * destination. + * + * The caller is responsible for calling @link cupsFreeDestInfo@ on the return + * value. @code NULL@ is returned on error. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +cups_dinfo_t * /* O - Destination information */ +cupsCopyDestInfo( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest) /* I - Destination */ +{ + cups_dinfo_t *dinfo; /* Destination information */ + ipp_t *request, /* Get-Printer-Attributes request */ + *response; /* Supported attributes */ + int tries, /* Number of tries so far */ + delay, /* Current retry delay */ + prev_delay; /* Next retry delay */ + const char *uri; /* Printer URI */ + char resource[1024]; /* Resource path */ + int version; /* IPP version */ + ipp_status_t status; /* Status of request */ + static const char * const requested_attrs[] = + { /* Requested attributes */ + "job-template", + "media-col-database", + "printer-description" + }; + + + DEBUG_printf(("cupsCopyDestSupported(http=%p, dest=%p(%s))", http, dest, + dest ? dest->name : "")); + + /* + * Range check input... + */ + + if (!http || !dest) + return (NULL); + + /* + * Get the printer URI and resource path... + */ + + if ((uri = _cupsGetDestResource(dest, resource, sizeof(resource))) == NULL) + return (NULL); + + /* + * Get the supported attributes... + */ + + delay = 1; + prev_delay = 1; + tries = 0; + version = 20; + + do + { + /* + * Send a Get-Printer-Attributes request... + */ + + request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, + uri); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", + NULL, cupsUser()); + ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "requested-attributes", + (int)(sizeof(requested_attrs) / sizeof(requested_attrs[0])), + NULL, requested_attrs); + response = cupsDoRequest(http, request, resource); + status = cupsLastError(); + + if (status > IPP_OK_SUBST) + { + DEBUG_printf(("cupsCopyDestSupported: Get-Printer-Attributes for '%s' " + "returned %s (%s)", dest->name, ippErrorString(status), + cupsLastErrorString())); + + ippDelete(response); + response = NULL; + + if (status == IPP_VERSION_NOT_SUPPORTED && version > 11) + version = 11; + else if (status == IPP_PRINTER_BUSY) + { + sleep(delay); + + delay = _cupsNextDelay(delay, &prev_delay); + } + else + return (NULL); + } + + tries ++; + } + while (!response && tries < 10); + + if (!response) + return (NULL); + + /* + * Allocate a cups_dinfo_t structure and return it... + */ + + if ((dinfo = calloc(1, sizeof(cups_dinfo_t))) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + ippDelete(response); + return (NULL); + } + + dinfo->uri = uri; + dinfo->resource = _cupsStrAlloc(resource); + dinfo->attrs = response; + + return (dinfo); +} + + +/* + * 'cupsFreeDestInfo()' - Free destination information obtained using + * @link cupsCopyDestInfo@. + */ + +void +cupsFreeDestInfo(cups_dinfo_t *dinfo) /* I - Destination information */ +{ + /* + * Range check input... + */ + + if (!dinfo) + return; + + /* + * Free memory and return... + */ + + _cupsStrFree(dinfo->resource); + + cupsArrayDelete(dinfo->constraints); + cupsArrayDelete(dinfo->resolvers); + + cupsArrayDelete(dinfo->localizations); + + cupsArrayDelete(dinfo->media_db); + + ippDelete(dinfo->attrs); + + free(dinfo); +} + + +/* + * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins. + * + * The "media" string is a PWG media name. "Flags" provides some matching + * guidance (multiple flags can be combined): + * + * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer, + * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size, + * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing, + * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and + * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the + * size amongst the "ready" media. + * + * The matching result (if any) is returned in the "cups_size_t" structure. + * + * Returns 1 when there is a match and 0 if there is not a match. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on match, 0 on failure */ +cupsGetDestMediaByName( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *dinfo, /* I - Destination information */ + const char *media, /* I - Media name */ + unsigned flags, /* I - Media matching flags */ + cups_size_t *size) /* O - Media size information */ +{ + _pwg_media_t *pwg; /* PWG media info */ + + + /* + * Range check input... + */ + + if (size) + memset(size, 0, sizeof(cups_size_t)); + + if (!http || !dest || !dinfo || !media || !size) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (0); + } + + /* + * Lookup the media size name... + */ + + if ((pwg = _pwgMediaForPWG(media)) == NULL) + if ((pwg = _pwgMediaForLegacy(media)) == NULL) + { + DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Unknown media size name."), 1); + return (0); + } + + /* + * Lookup the size... + */ + + return (cups_get_media_db(dinfo, pwg, flags, size)); +} + + +/* + * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins. + * + * "Width" and "length" are the dimensions in hundredths of millimeters. + * "Flags" provides some matching guidance (multiple flags can be combined): + * + * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer, + * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size, + * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing, + * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and + * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the + * size amongst the "ready" media. + * + * The matching result (if any) is returned in the "cups_size_t" structure. + * + * Returns 1 when there is a match and 0 if there is not a match. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on match, 0 on failure */ +cupsGetDestMediaBySize( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *dinfo, /* I - Destination information */ + int width, /* I - Media width in hundredths of + * of millimeters */ + int length, /* I - Media length in hundredths of + * of millimeters */ + unsigned flags, /* I - Media matching flags */ + cups_size_t *size) /* O - Media size information */ +{ + _pwg_media_t *pwg; /* PWG media info */ + + + /* + * Range check input... + */ + + if (size) + memset(size, 0, sizeof(cups_size_t)); + + if (!http || !dest || !dinfo || width <= 0 || length <= 0 || !size) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (0); + } + + /* + * Lookup the media size name... + */ + + if ((pwg = _pwgMediaForSize(width, length)) == NULL) + { + DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width, + length)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Invalid media size."), 1); + return (0); + } + + /* + * Lookup the size... + */ + + return (cups_get_media_db(dinfo, pwg, flags, size)); +} + + +/* + * 'cups_add_dconstres()' - Add a constraint or resolver to an array. + */ + +static void +cups_add_dconstres( + cups_array_t *a, /* I - Array */ + ipp_t *collection) /* I - Collection value */ +{ + ipp_attribute_t *attr; /* Attribute */ + _cups_dconstres_t *temp; /* Current constraint/resolver */ + + + if ((attr = ippFindAttribute(collection, "resolver-name", + IPP_TAG_NAME)) == NULL) + return; + + if ((temp = calloc(1, sizeof(_cups_dconstres_t))) == NULL) + return; + + temp->name = attr->values[0].string.text; + temp->collection = collection; + + cupsArrayAdd(a, temp); +} + + +/* + * 'cups_compare_dconstres()' - Compare to resolver entries. + */ + +static int /* O - Result of comparison */ +cups_compare_dconstres( + _cups_dconstres_t *a, /* I - First resolver */ + _cups_dconstres_t *b) /* I - Second resolver */ +{ + return (strcmp(a->name, b->name)); +} + + +/* + * 'cups_compare_media_db()' - Compare two media entries. + */ + +static int /* O - Result of comparison */ +cups_compare_media_db( + _cups_media_db_t *a, /* I - First media entries */ + _cups_media_db_t *b) /* I - Second media entries */ +{ + int result; /* Result of comparison */ + + + if ((result = a->width - b->width) == 0) + result = a->length - b->length; + + return (result); +} + + +/* + * 'cups_copy_media_db()' - Copy a media entry. + */ + +static _cups_media_db_t * /* O - New media entry */ +cups_copy_media_db( + _cups_media_db_t *mdb) /* I - Media entry to copy */ +{ + _cups_media_db_t *temp; /* New media entry */ + + + if ((temp = calloc(1, sizeof(_cups_media_db_t))) == NULL) + return (NULL); + + if (mdb->color) + temp->color = _cupsStrAlloc(mdb->color); + if (mdb->key) + temp->key = _cupsStrAlloc(mdb->key); + if (mdb->info) + temp->info = _cupsStrAlloc(mdb->info); + if (mdb->size_name) + temp->size_name = _cupsStrAlloc(mdb->size_name); + if (mdb->source) + temp->source = _cupsStrAlloc(mdb->source); + if (mdb->type) + temp->type = _cupsStrAlloc(mdb->type); + + temp->width = mdb->width; + temp->length = mdb->length; + temp->bottom = mdb->bottom; + temp->left = mdb->left; + temp->right = mdb->right; + temp->top = mdb->top; + + return (temp); +} + + +/* + * 'cups_create_constraints()' - Create the constraints and resolvers arrays. + */ + +static void +cups_create_constraints( + cups_dinfo_t *dinfo) /* I - Destination information */ +{ + int i; /* Looping var */ + ipp_attribute_t *attr; /* Attribute */ + _ipp_value_t *val; /* Current value */ + + + dinfo->constraints = cupsArrayNew3(NULL, NULL, NULL, 0, NULL, + (cups_afree_func_t)free); + dinfo->resolvers = cupsArrayNew3((cups_array_func_t)cups_compare_dconstres, + NULL, NULL, 0, NULL, + (cups_afree_func_t)free); + + if ((attr = ippFindAttribute(dinfo->attrs, "job-constraints-supported", + IPP_TAG_BEGIN_COLLECTION)) != NULL) + { + for (i = attr->num_values, val = attr->values; i > 0; i --, val ++) + cups_add_dconstres(dinfo->constraints, val->collection); + } + + if ((attr = ippFindAttribute(dinfo->attrs, "job-resolvers-supported", + IPP_TAG_BEGIN_COLLECTION)) != NULL) + { + for (i = attr->num_values, val = attr->values; i > 0; i --, val ++) + cups_add_dconstres(dinfo->resolvers, val->collection); + } +} + + +/* + * 'cups_create_defaults()' - Create the -default option array. + * + * TODO: Need to support collection defaults... + */ + +static void +cups_create_defaults( + cups_dinfo_t *dinfo) /* I - Destination information */ +{ + ipp_attribute_t *attr; /* Current attribute */ + char name[IPP_MAX_NAME + 1], + /* Current name */ + *nameptr, /* Pointer into current name */ + value[2048]; /* Current value */ + + + /* + * Iterate through the printer attributes looking for xxx-default and adding + * xxx=value to the defaults option array. + */ + + for (attr = ippFirstAttribute(dinfo->attrs); + attr; + attr = ippNextAttribute(dinfo->attrs)) + { + if (!attr->name || attr->group_tag != IPP_TAG_PRINTER) + continue; + + if (attr->value_tag == IPP_TAG_BEGIN_COLLECTION) + continue; /* TODO: STR #4096 */ + + if ((nameptr = attr->name + strlen(attr->name) - 8) <= attr->name || + strcmp(nameptr, "-default")) + continue; + + strlcpy(name, attr->name, sizeof(name)); + if ((nameptr = name + strlen(name) - 8) <= name || + strcmp(nameptr, "-default")) + continue; + + *nameptr = '\0'; + + if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value)) + continue; + + dinfo->num_defaults = cupsAddOption(name, value, dinfo->num_defaults, + &dinfo->defaults); + } +} + + +/* + * 'cups_create_media_db()' - Create the media database. + */ + +static void +cups_create_media_db( + cups_dinfo_t *dinfo) /* I - Destination information */ +{ + int i; /* Looping var */ + _ipp_value_t *val; /* Current value */ + ipp_attribute_t *media_col_db, /* media-col-database */ + *media_attr, /* media-xxx */ + *x_dimension, /* x-dimension */ + *y_dimension; /* y-dimension */ + _pwg_media_t *pwg; /* PWG media info */ + _cups_media_db_t mdb; /* Media entry */ + + + dinfo->media_db = cupsArrayNew3((cups_array_func_t)cups_compare_media_db, + NULL, NULL, 0, + (cups_acopy_func_t)cups_copy_media_db, + (cups_afree_func_t)cups_free_media_db); + dinfo->min_size.width = INT_MAX; + dinfo->min_size.length = INT_MAX; + dinfo->max_size.width = 0; + dinfo->max_size.length = 0; + + if ((media_col_db = ippFindAttribute(dinfo->attrs, "media-col-database", + IPP_TAG_BEGIN_COLLECTION)) != NULL) + { + _ipp_value_t *custom = NULL; /* Custom size range value */ + + for (i = media_col_db->num_values, val = media_col_db->values; + i > 0; + i --, val ++) + { + memset(&mdb, 0, sizeof(mdb)); + + if ((media_attr = ippFindAttribute(val->collection, "media-size", + IPP_TAG_BEGIN_COLLECTION)) != NULL) + { + ipp_t *media_size = media_attr->values[0].collection; + /* media-size collection value */ + + if ((x_dimension = ippFindAttribute(media_size, "x-dimension", + IPP_TAG_INTEGER)) != NULL && + (y_dimension = ippFindAttribute(media_size, "y-dimension", + IPP_TAG_INTEGER)) != NULL) + { + mdb.width = x_dimension->values[0].integer; + mdb.length = y_dimension->values[0].integer; + } + else if ((x_dimension = ippFindAttribute(media_size, "x-dimension", + IPP_TAG_RANGE)) != NULL && + (y_dimension = ippFindAttribute(media_size, "y-dimension", + IPP_TAG_RANGE)) != NULL) + { + /* + * Custom size range; save this as the custom size value with default + * margins, then continue; we'll capture the real margins below... + */ + + custom = val; + + dinfo->min_size.width = x_dimension->values[0].range.lower; + dinfo->min_size.length = y_dimension->values[0].range.lower; + dinfo->min_size.left = + dinfo->min_size.right = 635; /* Default 1/4" side margins */ + dinfo->min_size.top = + dinfo->min_size.bottom = 1270; /* Default 1/2" top/bottom margins */ + + dinfo->max_size.width = x_dimension->values[0].range.upper; + dinfo->max_size.length = y_dimension->values[0].range.upper; + dinfo->max_size.left = + dinfo->max_size.right = 635; /* Default 1/4" side margins */ + dinfo->max_size.top = + dinfo->max_size.bottom = 1270; /* Default 1/2" top/bottom margins */ + + continue; + } + } + + if ((media_attr = ippFindAttribute(val->collection, "media-color", + IPP_TAG_ZERO)) != NULL && + (media_attr->value_tag == IPP_TAG_NAME || + media_attr->value_tag == IPP_TAG_NAMELANG || + media_attr->value_tag == IPP_TAG_KEYWORD)) + mdb.color = media_attr->values[0].string.text; + + if ((media_attr = ippFindAttribute(val->collection, "media-info", + IPP_TAG_TEXT)) != NULL) + mdb.info = media_attr->values[0].string.text; + + if ((media_attr = ippFindAttribute(val->collection, "media-key", + IPP_TAG_ZERO)) != NULL && + (media_attr->value_tag == IPP_TAG_NAME || + media_attr->value_tag == IPP_TAG_NAMELANG || + media_attr->value_tag == IPP_TAG_KEYWORD)) + mdb.key = media_attr->values[0].string.text; + + if ((media_attr = ippFindAttribute(val->collection, "media-size-name", + IPP_TAG_ZERO)) != NULL && + (media_attr->value_tag == IPP_TAG_NAME || + media_attr->value_tag == IPP_TAG_NAMELANG || + media_attr->value_tag == IPP_TAG_KEYWORD)) + mdb.size_name = media_attr->values[0].string.text; + + if ((media_attr = ippFindAttribute(val->collection, "media-source", + IPP_TAG_ZERO)) != NULL && + (media_attr->value_tag == IPP_TAG_NAME || + media_attr->value_tag == IPP_TAG_NAMELANG || + media_attr->value_tag == IPP_TAG_KEYWORD)) + mdb.source = media_attr->values[0].string.text; + + if ((media_attr = ippFindAttribute(val->collection, "media-type", + IPP_TAG_ZERO)) != NULL && + (media_attr->value_tag == IPP_TAG_NAME || + media_attr->value_tag == IPP_TAG_NAMELANG || + media_attr->value_tag == IPP_TAG_KEYWORD)) + mdb.type = media_attr->values[0].string.text; + + if ((media_attr = ippFindAttribute(val->collection, "media-bottom-margin", + IPP_TAG_INTEGER)) != NULL) + mdb.bottom = media_attr->values[0].integer; + + if ((media_attr = ippFindAttribute(val->collection, "media-left-margin", + IPP_TAG_INTEGER)) != NULL) + mdb.left = media_attr->values[0].integer; + + if ((media_attr = ippFindAttribute(val->collection, "media-right-margin", + IPP_TAG_INTEGER)) != NULL) + mdb.right = media_attr->values[0].integer; + + if ((media_attr = ippFindAttribute(val->collection, "media-top-margin", + IPP_TAG_INTEGER)) != NULL) + mdb.top = media_attr->values[0].integer; + + cupsArrayAdd(dinfo->media_db, &mdb); + } + + if (custom) + { + if ((media_attr = ippFindAttribute(custom->collection, + "media-bottom-margin", + IPP_TAG_INTEGER)) != NULL) + { + dinfo->min_size.top = + dinfo->max_size.top = media_attr->values[0].integer; + } + + if ((media_attr = ippFindAttribute(custom->collection, + "media-left-margin", + IPP_TAG_INTEGER)) != NULL) + { + dinfo->min_size.left = + dinfo->max_size.left = media_attr->values[0].integer; + } + + if ((media_attr = ippFindAttribute(custom->collection, + "media-right-margin", + IPP_TAG_INTEGER)) != NULL) + { + dinfo->min_size.right = + dinfo->max_size.right = media_attr->values[0].integer; + } + + if ((media_attr = ippFindAttribute(custom->collection, + "media-top-margin", + IPP_TAG_INTEGER)) != NULL) + { + dinfo->min_size.top = + dinfo->max_size.top = media_attr->values[0].integer; + } + } + } + else if ((media_attr = ippFindAttribute(dinfo->attrs, "media-supported", + IPP_TAG_ZERO)) != NULL && + (media_attr->value_tag == IPP_TAG_NAME || + media_attr->value_tag == IPP_TAG_NAMELANG || + media_attr->value_tag == IPP_TAG_KEYWORD)) + { + memset(&mdb, 0, sizeof(mdb)); + + mdb.left = + mdb.right = 635; /* Default 1/4" side margins */ + mdb.top = + mdb.bottom = 1270; /* Default 1/2" top/bottom margins */ + + for (i = media_attr->num_values, val = media_attr->values; + i > 0; + i --, val ++) + { + if ((pwg = _pwgMediaForPWG(val->string.text)) == NULL) + if ((pwg = _pwgMediaForLegacy(val->string.text)) == NULL) + { + DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.", + val->string.text)); + continue; + } + + mdb.width = pwg->width; + mdb.length = pwg->length; + + if (!strncmp(val->string.text, "custom_min_", 11)) + { + mdb.size_name = NULL; + dinfo->min_size = mdb; + } + else if (!strncmp(val->string.text, "custom_max_", 11)) + { + mdb.size_name = NULL; + dinfo->max_size = mdb; + } + else + { + mdb.size_name = val->string.text; + + cupsArrayAdd(dinfo->media_db, &mdb); + } + } + } +} + + +/* + * 'cups_free_media_cb()' - Free a media entry. + */ + +static void +cups_free_media_db( + _cups_media_db_t *mdb) /* I - Media entry to free */ +{ + if (mdb->color) + _cupsStrFree(mdb->color); + if (mdb->key) + _cupsStrFree(mdb->key); + if (mdb->info) + _cupsStrFree(mdb->info); + if (mdb->size_name) + _cupsStrFree(mdb->size_name); + if (mdb->source) + _cupsStrFree(mdb->source); + if (mdb->type) + _cupsStrFree(mdb->type); + + free(mdb); +} + + +/* + * 'cups_get_media_db()' - Lookup the media entry for a given size. + */ + +static int /* O - 1 on match, 0 on failure */ +cups_get_media_db(cups_dinfo_t *dinfo, /* I - Destination information */ + _pwg_media_t *pwg, /* I - PWG media info */ + unsigned flags, /* I - Media matching flags */ + cups_size_t *size) /* O - Media size/margin/name info */ +{ + _cups_media_db_t *mdb, /* Current media database entry */ + *best = NULL, /* Best matching entry */ + key; /* Search key */ + + + /* + * Create the media database as needed... + */ + + if (!dinfo->media_db) + cups_create_media_db(dinfo); + + /* + * Find a match... + */ + + memset(&key, 0, sizeof(key)); + key.width = pwg->width; + key.length = pwg->length; + + if ((mdb = cupsArrayFind(dinfo->media_db, &key)) != NULL) + { + /* + * Found an exact match, let's figure out the best margins for the flags + * supplied... + */ + + best = mdb; + + if (flags & CUPS_MEDIA_FLAGS_BORDERLESS) + { + /* + * Look for the smallest margins... + */ + + if (best->left != 0 || best->right != 0 || best->top != 0 || + best->bottom != 0) + { + for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db); + mdb && !cups_compare_media_db(mdb, &key); + mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db)) + { + if (mdb->left <= best->left && mdb->right <= best->right && + mdb->top <= best->top && mdb->bottom <= best->bottom) + { + best = mdb; + if (mdb->left == 0 && mdb->right == 0 && mdb->bottom == 0 && + mdb->top == 0) + break; + } + } + } + + /* + * If we need an exact match, return no-match if the size is not + * borderless. + */ + + if ((flags & CUPS_MEDIA_FLAGS_EXACT) && + (best->left || best->right || best->top || best->bottom)) + return (0); + } + else if (flags & CUPS_MEDIA_FLAGS_DUPLEX) + { + /* + * Look for the largest margins... + */ + + for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db); + mdb && !cups_compare_media_db(mdb, &key); + mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db)) + { + if (mdb->left >= best->left && mdb->right >= best->right && + mdb->top >= best->top && mdb->bottom >= best->bottom) + best = mdb; + } + } + else + { + /* + * Look for the smallest non-zero margins... + */ + + for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db); + mdb && !cups_compare_media_db(mdb, &key); + mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db)) + { + if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) && + ((mdb->right > 0 && mdb->right <= best->right) || + best->right == 0) && + ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) && + ((mdb->bottom > 0 && mdb->bottom <= best->bottom) || + best->bottom == 0)) + best = mdb; + } + } + } + else if (flags & CUPS_MEDIA_FLAGS_EXACT) + { + /* + * See if we can do this as a custom size... + */ + + if (pwg->width < dinfo->min_size.width || + pwg->width > dinfo->max_size.width || + pwg->length < dinfo->min_size.length || + pwg->length > dinfo->max_size.length) + return (0); /* Out of range */ + + if ((flags & CUPS_MEDIA_FLAGS_BORDERLESS) && + (dinfo->min_size.left > 0 || dinfo->min_size.right > 0 || + dinfo->min_size.top > 0 || dinfo->min_size.bottom > 0)) + return (0); /* Not borderless */ + + key.size_name = (char *)pwg->pwg; + key.bottom = dinfo->min_size.bottom; + key.left = dinfo->min_size.left; + key.right = dinfo->min_size.right; + key.top = dinfo->min_size.top; + + best = &key; + } + else if (pwg->width >= dinfo->min_size.width && + pwg->width <= dinfo->max_size.width && + pwg->length >= dinfo->min_size.length && + pwg->length <= dinfo->max_size.length) + { + /* + * Map to custom size... + */ + + key.size_name = (char *)pwg->pwg; + key.bottom = dinfo->min_size.bottom; + key.left = dinfo->min_size.left; + key.right = dinfo->min_size.right; + key.top = dinfo->min_size.top; + + best = &key; + } + else + { + /* + * Find a close size... + */ + + for (mdb = (_cups_media_db_t *)cupsArrayFirst(dinfo->media_db); + mdb; + mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db)) + if (cups_is_close_media_db(mdb, &key)) + break; + + if (!mdb) + return (0); + + best = mdb; + + if (flags & CUPS_MEDIA_FLAGS_BORDERLESS) + { + /* + * Look for the smallest margins... + */ + + if (best->left != 0 || best->right != 0 || best->top != 0 || + best->bottom != 0) + { + for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db); + mdb && cups_is_close_media_db(mdb, &key); + mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db)) + { + if (mdb->left <= best->left && mdb->right <= best->right && + mdb->top <= best->top && mdb->bottom <= best->bottom) + { + best = mdb; + if (mdb->left == 0 && mdb->right == 0 && mdb->bottom == 0 && + mdb->top == 0) + break; + } + } + } + } + else if (flags & CUPS_MEDIA_FLAGS_DUPLEX) + { + /* + * Look for the largest margins... + */ + + for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db); + mdb && cups_is_close_media_db(mdb, &key); + mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db)) + { + if (mdb->left >= best->left && mdb->right >= best->right && + mdb->top >= best->top && mdb->bottom >= best->bottom) + best = mdb; + } + } + else + { + /* + * Look for the smallest non-zero margins... + */ + + for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db); + mdb && cups_is_close_media_db(mdb, &key); + mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db)) + { + if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) && + ((mdb->right > 0 && mdb->right <= best->right) || + best->right == 0) && + ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) && + ((mdb->bottom > 0 && mdb->bottom <= best->bottom) || + best->bottom == 0)) + best = mdb; + } + } + } + + if (best) + { + /* + * Return the matching size... + */ + + if (best->size_name) + strlcpy(size->media, best->size_name, sizeof(size->media)); + else if (best->key) + strlcpy(size->media, best->key, sizeof(size->media)); + else + strlcpy(size->media, pwg->pwg, sizeof(size->media)); + + size->width = best->width; + size->length = best->length; + size->bottom = best->bottom; + size->left = best->left; + size->right = best->right; + size->top = best->top; + + return (1); + } + + return (0); +} + + +/* + * 'cups_is_close_media_db()' - Compare two media entries to see if they are + * close to the same size. + * + * Currently we use 5 points (from PostScript) as the matching range... + */ + +static int /* O - 1 if the sizes are close */ +cups_is_close_media_db( + _cups_media_db_t *a, /* I - First media entries */ + _cups_media_db_t *b) /* I - Second media entries */ +{ + int dwidth, /* Difference in width */ + dlength; /* Difference in length */ + + + dwidth = a->width - b->width; + dlength = a->length - b->length; + + return (dwidth >= -176 && dwidth <= 176 && + dlength >= -176 && dlength <= 176); +} + + +/* + * 'cups_test_constraints()' - Test constraints. + * + * TODO: STR #4096 - Need to properly support media-col contraints... + */ + +static cups_array_t * /* O - Active constraints */ +cups_test_constraints( + cups_dinfo_t *dinfo, /* I - Destination information */ + const char *new_option, /* I - Newly selected option */ + const char *new_value, /* I - Newly selected value */ + int num_options, /* I - Number of options */ + cups_option_t *options, /* I - Options */ + int *num_conflicts, /* O - Number of conflicting options */ + cups_option_t **conflicts) /* O - Conflicting options */ +{ + int i, /* Looping var */ + match; /* Value matches? */ + int num_matching; /* Number of matching options */ + cups_option_t *matching; /* Matching options */ + _cups_dconstres_t *c; /* Current constraint */ + cups_array_t *active = NULL; /* Active constraints */ + ipp_attribute_t *attr; /* Current attribute */ + _ipp_value_t *attrval; /* Current attribute value */ + const char *value; /* Current value */ + char temp[1024]; /* Temporary string */ + int int_value; /* Integer value */ + int xres_value, /* Horizontal resolution */ + yres_value; /* Vertical resolution */ + ipp_res_t units_value; /* Resolution units */ + + + for (c = (_cups_dconstres_t *)cupsArrayFirst(dinfo->constraints); + c; + c = (_cups_dconstres_t *)cupsArrayNext(dinfo->constraints)) + { + num_matching = 0; + matching = NULL; + + for (attr = ippFirstAttribute(c->collection); + attr; + attr = ippNextAttribute(c->collection)) + { + if (attr->value_tag == IPP_TAG_BEGIN_COLLECTION) + break; /* TODO: STR #4096 */ + + /* + * Get the value for the current attribute in the constraint... + */ + + if (new_option && new_value && !strcmp(attr->name, new_option)) + value = new_value; + else if ((value = cupsGetOption(attr->name, num_options, + options)) == NULL) + value = cupsGetOption(attr->name, dinfo->num_defaults, dinfo->defaults); + + if (!value) + { + /* + * Not set so this constraint does not apply... + */ + + break; + } + + match = 0; + + switch (attr->value_tag) + { + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + int_value = atoi(value); + + for (i = attr->num_values, attrval = attr->values; + i > 0; + i --, attrval ++) + { + if (attrval->integer == int_value) + { + match = 1; + break; + } + } + break; + + case IPP_TAG_BOOLEAN : + int_value = !strcmp(value, "true"); + + for (i = attr->num_values, attrval = attr->values; + i > 0; + i --, attrval ++) + { + if (attrval->boolean == int_value) + { + match = 1; + break; + } + } + break; + + case IPP_TAG_RANGE : + int_value = atoi(value); + + for (i = attr->num_values, attrval = attr->values; + i > 0; + i --, attrval ++) + { + if (int_value >= attrval->range.lower && + int_value <= attrval->range.upper) + { + match = 1; + break; + } + } + break; + + case IPP_TAG_RESOLUTION : + if (sscanf(value, "%dx%d%15s", &xres_value, &yres_value, temp) != 3) + { + if (sscanf(value, "%d%15s", &xres_value, temp) != 2) + break; + + yres_value = xres_value; + } + + if (!strcmp(temp, "dpi")) + units_value = IPP_RES_PER_INCH; + else if (!strcmp(temp, "dpc") || !strcmp(temp, "dpcm")) + units_value = IPP_RES_PER_CM; + else + break; + + for (i = attr->num_values, attrval = attr->values; + i > 0; + i --, attrval ++) + { + if (attrval->resolution.xres == xres_value && + attrval->resolution.yres == yres_value && + attrval->resolution.units == units_value) + { + match = 1; + break; + } + } + break; + + case IPP_TAG_TEXT : + case IPP_TAG_NAME : + case IPP_TAG_KEYWORD : + case IPP_TAG_CHARSET : + case IPP_TAG_URI : + case IPP_TAG_URISCHEME : + case IPP_TAG_MIMETYPE : + case IPP_TAG_LANGUAGE : + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + for (i = attr->num_values, attrval = attr->values; + i > 0; + i --, attrval ++) + { + if (!strcmp(attrval->string.text, value)) + { + match = 1; + break; + } + } + break; + + default : + break; + } + + if (!match) + break; + + num_matching = cupsAddOption(attr->name, value, num_matching, &matching); + } + + if (!attr) + { + if (!active) + active = cupsArrayNew(NULL, NULL); + + cupsArrayAdd(active, c); + + if (num_conflicts && conflicts) + { + cups_option_t *moption; /* Matching option */ + + for (i = num_matching, moption = matching; i > 0; i --, moption ++) + *num_conflicts = cupsAddOption(moption->name, moption->value, + *num_conflicts, conflicts); + } + } + + cupsFreeOptions(num_matching, matching); + } + + return (active); +} + + +/* + * End of "$Id: dest-options.c 4185 2013-02-20 02:19:13Z msweet $". + */ diff --git a/cups/dest.c b/cups/dest.c new file mode 100644 index 0000000..acd64e7 --- /dev/null +++ b/cups/dest.c @@ -0,0 +1,3891 @@ +/* + * "$Id: dest.c 9568 2011-02-25 06:13:56Z mike $" + * + * User-defined destination (and option) support for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * cupsAddDest() - Add a destination to the list of + * destinations. + * _cupsAppleCopyDefaultPaperID() - Get the default paper ID. + * _cupsAppleCopyDefaultPrinter() - Get the default printer at this location. + * _cupsAppleGetUseLastPrinter() - Get whether to use the last used printer. + * _cupsAppleSetDefaultPaperID() - Set the default paper id. + * _cupsAppleSetDefaultPrinter() - Set the default printer for this + * location. + * _cupsAppleSetUseLastPrinter() - Set whether to use the last used printer. + * cupsConnectDest() - Connect to the server for a destination. + * cupsConnectDestBlock() - Connect to the server for a destination. + * cupsCopyDest() - Copy a destination. + * cupsEnumDests() - Enumerate available destinations with a + * callback function. + * cupsEnumDestsBlock() - Enumerate available destinations with a + * block. + * cupsFreeDests() - Free the memory used by the list of + * destinations. + * cupsGetDest() - Get the named destination from the list. + * _cupsGetDestResource() - Get the resource path and URI for a + * destination. + * _cupsGetDests() - Get destinations from a server. + * cupsGetDests() - Get the list of destinations from the + * default server. + * cupsGetDests2() - Get the list of destinations from the + * specified server. + * cupsGetNamedDest() - Get options for the named destination. + * cupsRemoveDest() - Remove a destination from the destination + * list. + * cupsSetDefaultDest() - Set the default destination. + * cupsSetDests() - Save the list of destinations for the + * default server. + * cupsSetDests2() - Save the list of destinations for the + * specified server. + * _cupsUserDefault() - Get the user default printer from + * environment variables and location + * information. + * appleCopyLocations() - Copy the location history array. + * appleCopyNetwork() - Get the network ID for the current + * location. + * appleGetPaperSize() - Get the default paper size. + * appleGetPrinter() - Get a printer from the history array. + * cups_add_dest() - Add a destination to the array. + * cups_block_cb() - Enumeration callback for block API. + * cups_compare_dests() - Compare two destinations. + * cups_dnssd_browse_cb() - Browse for printers. + * cups_dnssd_browse_cb() - Browse for printers. + * cups_dnssd_client_cb() - Avahi client callback function. + * cups_dnssd_compare_device() - Compare two devices. + * cups_dnssd_free_device() - Free the memory used by a device. + * cups_dnssd_get_device() - Lookup a device and create it as needed. + * cups_dnssd_local_cb() - Browse for local printers. + * cups_dnssd_poll_cb() - Wait for input on the specified file + * descriptors. + * cups_dnssd_query_cb() - Process query data. + * cups_dnssd_resolve() - Resolve a Bonjour printer URI. + * cups_dnssd_resolve_cb() - See if we should continue resolving. + * cups_dnssd_unquote() - Unquote a name string. + * cups_find_dest() - Find a destination using a binary search. + * cups_get_default() - Get the default destination from an + * lpoptions file. + * cups_get_dests() - Get destinations from a file. + * cups_make_string() - Make a comma-separated string of values + * from an IPP attribute. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include + +#ifdef HAVE_NOTIFY_H +# include +#endif /* HAVE_NOTIFY_H */ + +#ifdef HAVE_POLL +# include +#endif /* HAVE_POLL */ + +#ifdef HAVE_DNSSD +# include +#endif /* HAVE_DNSSD */ + +#ifdef HAVE_AVAHI +# include +# include +# include +# include +# include +# include +#define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX +#endif /* HAVE_AVAHI */ + + +/* + * Constants... + */ + +#ifdef __APPLE__ +# include +# define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs") +# define kDefaultPaperIDKey CFSTR("DefaultPaperID") +# define kLastUsedPrintersKey CFSTR("LastUsedPrinters") +# define kLocationNetworkKey CFSTR("Network") +# define kLocationPrinterIDKey CFSTR("PrinterID") +# define kUseLastPrinter CFSTR("UseLastPrinter") +#endif /* __APPLE__ */ + + +/* + * Types... + */ + +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) +typedef enum _cups_dnssd_state_e /* Enumerated device state */ +{ + _CUPS_DNSSD_NEW, + _CUPS_DNSSD_QUERY, + _CUPS_DNSSD_PENDING, + _CUPS_DNSSD_ACTIVE, + _CUPS_DNSSD_LOCAL, + _CUPS_DNSSD_INCOMPATIBLE, + _CUPS_DNSSD_ERROR +} _cups_dnssd_state_t; + +typedef struct _cups_dnssd_data_s /* Enumeration data */ +{ +# ifdef HAVE_DNSSD + DNSServiceRef main_ref; /* Main service reference */ +# else /* HAVE_AVAHI */ + AvahiSimplePoll *simple_poll; /* Polling interface */ + AvahiClient *client; /* Client information */ + int got_data; /* Did we get data? */ +# endif /* HAVE_DNSSD */ + cups_dest_cb_t cb; /* Callback */ + void *user_data; /* User data pointer */ + cups_ptype_t type, /* Printer type filter */ + mask; /* Printer type mask */ + cups_array_t *devices; /* Devices found so far */ +} _cups_dnssd_data_t; + +typedef struct _cups_dnssd_device_s /* Enumerated device */ +{ + _cups_dnssd_state_t state; /* State of device listing */ +# ifdef HAVE_DNSSD + DNSServiceRef ref; /* Service reference for query */ +# else /* HAVE_AVAHI */ + AvahiRecordBrowser *ref; /* Browser for query */ +# endif /* HAVE_DNSSD */ + char *domain, /* Domain name */ + *fullName, /* Full name */ + *regtype; /* Registration type */ + cups_ptype_t type; /* Device registration type */ + cups_dest_t dest; /* Destination record */ +} _cups_dnssd_device_t; + +typedef struct _cups_dnssd_resolve_s /* Data for resolving URI */ +{ + int *cancel; /* Pointer to "cancel" variable */ + struct timeval end_time; /* Ending time */ +} _cups_dnssd_resolve_t; +#endif /* HAVE_DNSSD */ + + +/* + * Local functions... + */ + +#ifdef __APPLE__ +static CFArrayRef appleCopyLocations(void); +static CFStringRef appleCopyNetwork(void); +static char *appleGetPaperSize(char *name, int namesize); +static CFStringRef appleGetPrinter(CFArrayRef locations, + CFStringRef network, CFIndex *locindex); +#endif /* __APPLE__ */ +static cups_dest_t *cups_add_dest(const char *name, const char *instance, + int *num_dests, cups_dest_t **dests); +#ifdef __BLOCKS__ +static int cups_block_cb(cups_dest_block_t block, unsigned flags, + cups_dest_t *dest); +#endif /* __BLOCKS__ */ +static int cups_compare_dests(cups_dest_t *a, cups_dest_t *b); +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) +# ifdef HAVE_DNSSD +static void cups_dnssd_browse_cb(DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *serviceName, + const char *regtype, + const char *replyDomain, + void *context); +# else /* HAVE_AVAHI */ +static void cups_dnssd_browse_cb(AvahiServiceBrowser *browser, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *serviceName, + const char *regtype, + const char *replyDomain, + AvahiLookupResultFlags flags, + void *context); +static void cups_dnssd_client_cb(AvahiClient *client, + AvahiClientState state, + void *context); +# endif /* HAVE_DNSSD */ +static int cups_dnssd_compare_devices(_cups_dnssd_device_t *a, + _cups_dnssd_device_t *b); +static void cups_dnssd_free_device(_cups_dnssd_device_t *device, + _cups_dnssd_data_t *data); +static _cups_dnssd_device_t * + cups_dnssd_get_device(_cups_dnssd_data_t *data, + const char *serviceName, + const char *regtype, + const char *replyDomain); +# ifdef HAVE_DNSSD +static void cups_dnssd_local_cb(DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *serviceName, + const char *regtype, + const char *replyDomain, + void *context); +static void cups_dnssd_query_cb(DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *fullName, + uint16_t rrtype, uint16_t rrclass, + uint16_t rdlen, const void *rdata, + uint32_t ttl, void *context); +# else /* HAVE_AVAHI */ +static int cups_dnssd_poll_cb(struct pollfd *pollfds, + unsigned int num_pollfds, + int timeout, void *context); +static void cups_dnssd_query_cb(AvahiRecordBrowser *browser, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *name, uint16_t rrclass, + uint16_t rrtype, const void *rdata, + size_t rdlen, + AvahiLookupResultFlags flags, + void *context); +# endif /* HAVE_DNSSD */ +static const char *cups_dnssd_resolve(cups_dest_t *dest, const char *uri, + int msec, int *cancel, + cups_dest_cb_t cb, void *user_data); +static int cups_dnssd_resolve_cb(void *context); +static void cups_dnssd_unquote(char *dst, const char *src, + size_t dstsize); +#endif /* HAVE_DNSSD || HAVE_AVAHI */ +static int cups_find_dest(const char *name, const char *instance, + int num_dests, cups_dest_t *dests, int prev, + int *rdiff); +static char *cups_get_default(const char *filename, char *namebuf, + size_t namesize, const char **instance); +static int cups_get_dests(const char *filename, const char *match_name, + const char *match_inst, int user_default_set, + int num_dests, cups_dest_t **dests); +static char *cups_make_string(ipp_attribute_t *attr, char *buffer, + size_t bufsize); + + +/* + * 'cupsAddDest()' - Add a destination to the list of destinations. + * + * This function cannot be used to add a new class or printer queue, + * it only adds a new container of saved options for the named + * destination or instance. + * + * If the named destination already exists, the destination list is + * returned unchanged. Adding a new instance of a destination creates + * a copy of that destination's options. + * + * Use the @link cupsSaveDests@ function to save the updated list of + * destinations to the user's lpoptions file. + */ + +int /* O - New number of destinations */ +cupsAddDest(const char *name, /* I - Destination name */ + const char *instance, /* I - Instance name or @code NULL@ for none/primary */ + int num_dests, /* I - Number of destinations */ + cups_dest_t **dests) /* IO - Destinations */ +{ + int i; /* Looping var */ + cups_dest_t *dest; /* Destination pointer */ + cups_dest_t *parent = NULL; /* Parent destination */ + cups_option_t *doption, /* Current destination option */ + *poption; /* Current parent option */ + + + if (!name || !dests) + return (0); + + if (!cupsGetDest(name, instance, num_dests, *dests)) + { + if (instance && !cupsGetDest(name, NULL, num_dests, *dests)) + return (num_dests); + + dest = cups_add_dest(name, instance, &num_dests, dests); + + /* + * Find the base dest again now the array has been realloc'd. + */ + + parent = cupsGetDest(name, NULL, num_dests, *dests); + + if (instance && parent && parent->num_options > 0) + { + /* + * Copy options from parent... + */ + + dest->options = calloc(sizeof(cups_option_t), parent->num_options); + + if (dest->options) + { + dest->num_options = parent->num_options; + + for (i = dest->num_options, doption = dest->options, + poption = parent->options; + i > 0; + i --, doption ++, poption ++) + { + doption->name = _cupsStrRetain(poption->name); + doption->value = _cupsStrRetain(poption->value); + } + } + } + } + + return (num_dests); +} + + +#ifdef __APPLE__ +/* + * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID. + */ + +CFStringRef /* O - Default paper ID */ +_cupsAppleCopyDefaultPaperID(void) +{ + return (CFPreferencesCopyAppValue(kDefaultPaperIDKey, + kCUPSPrintingPrefs)); +} + + +/* + * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location. + */ + +CFStringRef /* O - Default printer name */ +_cupsAppleCopyDefaultPrinter(void) +{ + CFStringRef network; /* Network location */ + CFArrayRef locations; /* Location array */ + CFStringRef locprinter; /* Current printer */ + + + /* + * Use location-based defaults only if "use last printer" is selected in the + * system preferences... + */ + + if (!_cupsAppleGetUseLastPrinter()) + { + DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as " + "default."); + return (NULL); + } + + /* + * Get the current location... + */ + + if ((network = appleCopyNetwork()) == NULL) + { + DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current " + "network."); + return (NULL); + } + + /* + * Lookup the network in the preferences... + */ + + if ((locations = appleCopyLocations()) == NULL) + { + /* + * Missing or bad location array, so no location-based default... + */ + + DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used " + "printer array."); + + CFRelease(network); + + return (NULL); + } + + DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.", + (int)CFArrayGetCount(locations))); + + if ((locprinter = appleGetPrinter(locations, network, NULL)) != NULL) + CFRetain(locprinter); + + CFRelease(network); + CFRelease(locations); + + return (locprinter); +} + + +/* + * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer. + */ + +int /* O - 1 to use last printer, 0 otherwise */ +_cupsAppleGetUseLastPrinter(void) +{ + Boolean uselast, /* Use last printer preference value */ + uselast_set; /* Valid is set? */ + + + if (getenv("CUPS_DISABLE_APPLE_DEFAULT")) + return (0); + + uselast = CFPreferencesGetAppBooleanValue(kUseLastPrinter, + kCUPSPrintingPrefs, + &uselast_set); + if (!uselast_set) + return (1); + else + return (uselast); +} + + +/* + * '_cupsAppleSetDefaultPaperID()' - Set the default paper id. + */ + +void +_cupsAppleSetDefaultPaperID( + CFStringRef name) /* I - New paper ID */ +{ + CFPreferencesSetAppValue(kDefaultPaperIDKey, name, kCUPSPrintingPrefs); + CFPreferencesAppSynchronize(kCUPSPrintingPrefs); + notify_post("com.apple.printerPrefsChange"); +} + + +/* + * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location. + */ + +void +_cupsAppleSetDefaultPrinter( + CFStringRef name) /* I - Default printer/class name */ +{ + CFStringRef network; /* Current network */ + CFArrayRef locations; /* Old locations array */ + CFIndex locindex; /* Index in locations array */ + CFStringRef locprinter; /* Current printer */ + CFMutableArrayRef newlocations; /* New locations array */ + CFMutableDictionaryRef newlocation; /* New location */ + + + /* + * Get the current location... + */ + + if ((network = appleCopyNetwork()) == NULL) + { + DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network..."); + return; + } + + /* + * Lookup the network in the preferences... + */ + + if ((locations = appleCopyLocations()) != NULL) + locprinter = appleGetPrinter(locations, network, &locindex); + else + { + locprinter = NULL; + locindex = -1; + } + + if (!locprinter || CFStringCompare(locprinter, name, 0) != kCFCompareEqualTo) + { + /* + * Need to change the locations array... + */ + + if (locations) + { + newlocations = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, + locations); + + if (locprinter) + CFArrayRemoveValueAtIndex(newlocations, locindex); + } + else + newlocations = CFArrayCreateMutable(kCFAllocatorDefault, 0, + &kCFTypeArrayCallBacks); + + newlocation = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); + + if (newlocation && newlocations) + { + /* + * Put the new location at the front of the array... + */ + + CFDictionaryAddValue(newlocation, kLocationNetworkKey, network); + CFDictionaryAddValue(newlocation, kLocationPrinterIDKey, name); + CFArrayInsertValueAtIndex(newlocations, 0, newlocation); + + /* + * Limit the number of locations to 10... + */ + + while (CFArrayGetCount(newlocations) > 10) + CFArrayRemoveValueAtIndex(newlocations, 10); + + /* + * Push the changes out... + */ + + CFPreferencesSetAppValue(kLastUsedPrintersKey, newlocations, + kCUPSPrintingPrefs); + CFPreferencesAppSynchronize(kCUPSPrintingPrefs); + notify_post("com.apple.printerPrefsChange"); + } + + if (newlocations) + CFRelease(newlocations); + + if (newlocation) + CFRelease(newlocation); + } + + if (locations) + CFRelease(locations); + + CFRelease(network); +} + + +/* + * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer. + */ + +void +_cupsAppleSetUseLastPrinter( + int uselast) /* O - 1 to use last printer, 0 otherwise */ +{ + CFPreferencesSetAppValue(kUseLastPrinter, + uselast ? kCFBooleanTrue : kCFBooleanFalse, + kCUPSPrintingPrefs); + CFPreferencesAppSynchronize(kCUPSPrintingPrefs); + notify_post("com.apple.printerPrefsChange"); +} +#endif /* __APPLE__ */ + + +/* + * 'cupsConnectDest()' - Connect to the server for a destination. + * + * Connect to the destination, returning a new http_t connection object and + * optionally the resource path to use for the destination. These calls will + * block until a connection is made, the timeout expires, the integer pointed + * to by "cancel" is non-zero, or the callback function (or block) returns 0, + * The caller is responsible for calling httpClose() on the returned object. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +http_t * /* O - Connection to server or @code NULL@ */ +cupsConnectDest( + cups_dest_t *dest, /* I - Destination */ + unsigned flags, /* I - Connection flags */ + int msec, /* I - Timeout in milliseconds */ + int *cancel, /* I - Pointer to "cancel" variable */ + char *resource, /* I - Resource buffer */ + size_t resourcesize, /* I - Size of resource buffer */ + cups_dest_cb_t cb, /* I - Callback function */ + void *user_data) /* I - User data pointer */ +{ + const char *uri; /* Printer URI */ + char scheme[32], /* URI scheme */ + userpass[256], /* Username and password (unused) */ + hostname[256], /* Hostname */ + tempresource[1024]; /* Temporary resource buffer */ + int port; /* Port number */ + char portstr[16]; /* Port number string */ + http_encryption_t encryption; /* Encryption to use */ + http_addrlist_t *addrlist; /* Address list for server */ + http_t *http; /* Connection to server */ + + + /* + * Range check input... + */ + + if (!dest) + { + if (resource) + *resource = '\0'; + + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (NULL); + } + + if (!resource || resourcesize < 1) + { + resource = tempresource; + resourcesize = sizeof(tempresource); + } + + /* + * Grab the printer URI... + */ + + if ((uri = cupsGetOption("printer-uri-supported", dest->num_options, + dest->options)) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOENT), 0); + + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, + dest); + + return (NULL); + } + +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) + if (strstr(uri, "._tcp")) + { + if ((uri = cups_dnssd_resolve(dest, uri, msec, cancel, cb, + user_data)) == NULL) + return (NULL); + } +#endif /* HAVE_DNSSD || HAVE_AVAHI */ + + if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), + userpass, sizeof(userpass), hostname, sizeof(hostname), + &port, resource, resourcesize) < HTTP_URI_OK) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad printer URI."), 1); + + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, + dest); + + return (NULL); + } + + /* + * Lookup the address for the server... + */ + + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, + dest); + + snprintf(portstr, sizeof(portstr), "%d", port); + + if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portstr)) == NULL) + { + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, + dest); + + return (NULL); + } + + if (cancel && *cancel) + { + httpAddrFreeList(addrlist); + + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CANCELED, + dest); + + return (NULL); + } + + /* + * Create the HTTP object pointing to the server referenced by the URI... + */ + + if (!strcmp(scheme, "ipps") || port == 443) + encryption = HTTP_ENCRYPT_ALWAYS; + else + encryption = HTTP_ENCRYPT_IF_REQUESTED; + + http = _httpCreate(hostname, port, addrlist, encryption, AF_UNSPEC); + + /* + * Connect if requested... + */ + + if (flags & CUPS_DEST_FLAGS_UNCONNECTED) + { + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED, dest); + } + else + { + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CONNECTING, + dest); + + if (!httpReconnect2(http, msec, cancel) && cb) + { + if (cancel && *cancel) + (*cb)(user_data, + CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CONNECTING, dest); + else + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, + dest); + } + else if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_NONE, dest); + } + + return (http); +} + + +#ifdef __BLOCKS__ +/* + * 'cupsConnectDestBlock()' - Connect to the server for a destination. + * + * Connect to the destination, returning a new http_t connection object and + * optionally the resource path to use for the destination. These calls will + * block until a connection is made, the timeout expires, the integer pointed + * to by "cancel" is non-zero, or the callback function (or block) returns 0, + * The caller is responsible for calling httpClose() on the returned object. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +http_t * /* O - Connection to server or @code NULL@ */ +cupsConnectDestBlock( + cups_dest_t *dest, /* I - Destination */ + unsigned flags, /* I - Connection flags */ + int msec, /* I - Timeout in milliseconds */ + int *cancel, /* I - Pointer to "cancel" variable */ + char *resource, /* I - Resource buffer */ + size_t resourcesize, /* I - Size of resource buffer */ + cups_dest_block_t block) /* I - Callback block */ +{ + return (cupsConnectDest(dest, flags, msec, cancel, resource, resourcesize, + (cups_dest_cb_t)cups_block_cb, (void *)block)); +} +#endif /* __BLOCKS__ */ + + +/* + * 'cupsCopyDest()' - Copy a destination. + * + * Make a copy of the destination to an array of destinations (or just a single + * copy) - for use with the cupsEnumDests* functions. The caller is responsible + * for calling cupsFreeDests() on the returned object(s). + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int +cupsCopyDest(cups_dest_t *dest, + int num_dests, + cups_dest_t **dests) +{ + int i; /* Looping var */ + cups_dest_t *new_dest; /* New destination pointer */ + cups_option_t *new_option, /* Current destination option */ + *option; /* Current parent option */ + + + /* + * Range check input... + */ + + if (!dest || num_dests < 0 || !dests) + return (num_dests); + + /* + * See if the destination already exists... + */ + + if ((new_dest = cupsGetDest(dest->name, dest->instance, num_dests, + *dests)) != NULL) + { + /* + * Protect against copying destination to itself... + */ + + if (new_dest == dest) + return (num_dests); + + /* + * Otherwise, free the options... + */ + + cupsFreeOptions(new_dest->num_options, new_dest->options); + + new_dest->num_options = 0; + new_dest->options = NULL; + } + else + new_dest = cups_add_dest(dest->name, dest->instance, &num_dests, dests); + + if (new_dest) + { + if ((new_dest->options = calloc(sizeof(cups_option_t), + dest->num_options)) == NULL) + return (cupsRemoveDest(dest->name, dest->instance, num_dests, dests)); + + new_dest->num_options = dest->num_options; + + for (i = dest->num_options, option = dest->options, + new_option = new_dest->options; + i > 0; + i --, option ++, new_option ++) + { + new_option->name = _cupsStrRetain(option->name); + new_option->value = _cupsStrRetain(option->value); + } + } + + return (num_dests); +} + + +/* + * 'cupsEnumDests()' - Enumerate available destinations with a callback function. + * + * Destinations are enumerated from one or more sources. The callback function + * receives the @code user_data@ pointer, destination name, instance, number of + * options, and options which can be used as input to the @link cupsAddDest@ + * function. The function must return 1 to continue enumeration or 0 to stop. + * + * Enumeration happens on the current thread and does not return until all + * destinations have been enumerated or the callback function returns 0. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsEnumDests( + unsigned flags, /* I - Enumeration flags */ + int msec, /* I - Timeout in milliseconds, + * -1 for indefinite */ + int *cancel, /* I - Pointer to "cancel" variable */ + cups_ptype_t type, /* I - Printer type bits */ + cups_ptype_t mask, /* I - Mask for printer type bits */ + cups_dest_cb_t cb, /* I - Callback function */ + void *user_data) /* I - User data */ +{ + int i, /* Looping var */ + num_dests; /* Number of destinations */ + cups_dest_t *dests = NULL, /* Destinations */ + *dest; /* Current destination */ +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) + int count, /* Number of queries started */ + remaining; /* Remainder of timeout */ + _cups_dnssd_data_t data; /* Data for callback */ + _cups_dnssd_device_t *device; /* Current device */ +# ifdef HAVE_DNSSD + int nfds, /* Number of files responded */ + main_fd; /* File descriptor for lookups */ + DNSServiceRef ipp_ref, /* IPP browser */ + local_ipp_ref; /* Local IPP browser */ +# ifdef HAVE_SSL + DNSServiceRef ipps_ref, /* IPPS browser */ + local_ipps_ref; /* Local IPPS browser */ +# endif /* HAVE_SSL */ +# ifdef HAVE_POLL + struct pollfd pfd; /* Polling data */ +# else + fd_set input; /* Input set for select() */ + struct timeval timeout; /* Timeout for select() */ +# endif /* HAVE_POLL */ +# else /* HAVE_AVAHI */ + int error; /* Error value */ + AvahiServiceBrowser *ipp_ref; /* IPP browser */ +# ifdef HAVE_SSL + AvahiServiceBrowser *ipps_ref; /* IPPS browser */ +# endif /* HAVE_SSL */ +# endif /* HAVE_DNSSD */ +#endif /* HAVE_DNSSD || HAVE_AVAHI */ + + /* + * Range check input... + */ + + (void)flags; + + if (!cb) + return (0); + + /* + * Get the list of local printers and pass them to the callback function... + */ + + num_dests = _cupsGetDests(CUPS_HTTP_DEFAULT, CUPS_GET_PRINTERS, NULL, &dests, + type, mask); + + for (i = num_dests, dest = dests; + i > 0 && (!cancel || !*cancel); + i --, dest ++) + if (!(*cb)(user_data, i > 1 ? CUPS_DEST_FLAGS_MORE : CUPS_DEST_FLAGS_NONE, + dest)) + break; + + cupsFreeDests(num_dests, dests); + + if (i > 0 || msec == 0) + return (1); + +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) + /* + * Get Bonjour-shared printers... + */ + + data.type = type; + data.mask = mask; + data.devices = cupsArrayNew3((cups_array_func_t)cups_dnssd_compare_devices, + NULL, NULL, 0, NULL, + (cups_afree_func_t)cups_dnssd_free_device); + +# ifdef HAVE_DNSSD + if (DNSServiceCreateConnection(&data.main_ref) != kDNSServiceErr_NoError) + return (0); + + main_fd = DNSServiceRefSockFD(data.main_ref); + + ipp_ref = data.main_ref; + DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0, + "_ipp._tcp", NULL, + (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data); + + local_ipp_ref = data.main_ref; + DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection, + kDNSServiceInterfaceIndexLocalOnly, + "_ipp._tcp", NULL, + (DNSServiceBrowseReply)cups_dnssd_local_cb, &data); + +# ifdef HAVE_SSL + ipps_ref = data.main_ref; + DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0, + "_ipps._tcp", NULL, + (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data); + + local_ipps_ref = data.main_ref; + DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection, + kDNSServiceInterfaceIndexLocalOnly, + "_ipps._tcp", NULL, + (DNSServiceBrowseReply)cups_dnssd_local_cb, &data); +# endif /* HAVE_SSL */ + +# else /* HAVE_AVAHI */ + if ((data.simple_poll = avahi_simple_poll_new()) == NULL) + { + DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object."); + return (1); + } + + avahi_simple_poll_set_func(data.simple_poll, cups_dnssd_poll_cb, &data); + + data.client = avahi_client_new(avahi_simple_poll_get(data.simple_poll), + 0, cups_dnssd_client_cb, &data, + &error); + if (!data.client) + { + DEBUG_puts("cupsEnumDests: Unable to create Avahi client."); + avahi_simple_poll_free(data.simple_poll); + return (1); + } + + ipp_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL, + 0, cups_dnssd_browse_cb, &data); +# ifdef HAVE_SSL + ipps_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, "_ipps._tcp", NULL, + 0, cups_dnssd_browse_cb, &data); +# endif /* HAVE_SSL */ +# endif /* HAVE_DNSSD */ + + if (msec < 0) + remaining = INT_MAX; + else + remaining = msec; + + while (remaining > 0 && (!cancel || !*cancel)) + { + /* + * Check for input... + */ + +# ifdef HAVE_DNSSD +# ifdef HAVE_POLL + pfd.fd = main_fd; + pfd.events = POLLIN; + + nfds = poll(&pfd, 1, remaining > 250 ? 250 : remaining); + +# else + FD_ZERO(&input); + FD_SET(main_fd, &input); + + timeout.tv_sec = 0; + timeout.tv_usec = remaining > 250 ? 250000 : remaining * 1000; + + nfds = select(main_fd + 1, &input, NULL, NULL, &timeout); +# endif /* HAVE_POLL */ + + if (nfds > 0) + DNSServiceProcessResult(data.main_ref); + else if (nfds == 0) + remaining -= 250; + +# else /* HAVE_AVAHI */ + data.got_data = 0; + + if ((error = avahi_simple_poll_iterate(data.simple_poll, 250)) > 0) + { + /* + * We've been told to exit the loop. Perhaps the connection to + * Avahi failed. + */ + + break; + } + + if (!data.got_data) + remaining -= 250; +# endif /* HAVE_DNSSD */ + + for (device = (_cups_dnssd_device_t *)cupsArrayFirst(data.devices), + count = 0; + device; + device = (_cups_dnssd_device_t *)cupsArrayNext(data.devices)) + { + if (device->ref) + count ++; + + if (!device->ref && device->state == _CUPS_DNSSD_NEW) + { + DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device->fullName)); + +# ifdef HAVE_DNSSD + device->ref = data.main_ref; + + if (DNSServiceQueryRecord(&(device->ref), + kDNSServiceFlagsShareConnection, + 0, device->fullName, + kDNSServiceType_TXT, + kDNSServiceClass_IN, + (DNSServiceQueryRecordReply)cups_dnssd_query_cb, + &data) == kDNSServiceErr_NoError) + { + count ++; + } + else + { + device->ref = 0; + device->state = _CUPS_DNSSD_ERROR; + + DEBUG_puts("1cupsEnumDests: Query failed."); + } + +# else /* HAVE_AVAHI */ + if ((device->ref = avahi_record_browser_new(data.client, + AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, + device->fullName, + AVAHI_DNS_CLASS_IN, + AVAHI_DNS_TYPE_TXT, + 0, + cups_dnssd_query_cb, + &data)) != NULL) + { + count ++; + } + else + { + device->state = _CUPS_DNSSD_ERROR; + + DEBUG_printf(("1cupsEnumDests: Query failed: %s", + avahi_strerror(avahi_client_errno(data.client)))); + } +# endif /* HAVE_DNSSD */ + } + else if (device->ref && device->state == _CUPS_DNSSD_PENDING) + { + if ((device->type & mask) == type) + { + if (!(*cb)(user_data, CUPS_DEST_FLAGS_NONE, &device->dest)) + { + remaining = -1; + break; + } + } + + device->state = _CUPS_DNSSD_ACTIVE; + } + } + } + + cupsArrayDelete(data.devices); + +# ifdef HAVE_DNSSD + DNSServiceRefDeallocate(ipp_ref); + DNSServiceRefDeallocate(local_ipp_ref); + +# ifdef HAVE_SSL + DNSServiceRefDeallocate(ipp_ref); + DNSServiceRefDeallocate(local_ipp_ref); +# endif /* HAVE_SSL */ + + DNSServiceRefDeallocate(data.main_ref); + +# else /* HAVE_AVAHI */ + avahi_service_browser_free(ipp_ref); +# ifdef HAVE_SSL + avahi_service_browser_free(ipps_ref); +# endif /* HAVE_SSL */ + + avahi_client_free(data.client); + avahi_simple_poll_free(data.simple_poll); +# endif /* HAVE_DNSSD */ +#endif /* HAVE_DNSSD || HAVE_DNSSD */ + + return (1); +} + + +# ifdef __BLOCKS__ +/* + * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block. + * + * Destinations are enumerated from one or more sources. The block receives the + * destination name, instance, number of options, and options which can be used + * as input to the @link cupsAddDest@ function. The block must return 1 to + * continue enumeration or 0 to stop. + * + * Enumeration happens on the current thread and does not return until all + * destinations have been enumerated or the block returns 0. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +cupsEnumDestsBlock( + unsigned flags, /* I - Enumeration flags */ + int timeout, /* I - Timeout in milliseconds, 0 for indefinite */ + int *cancel, /* I - Pointer to "cancel" variable */ + cups_ptype_t type, /* I - Printer type bits */ + cups_ptype_t mask, /* I - Mask for printer type bits */ + cups_dest_block_t block) /* I - Block */ +{ + return (cupsEnumDests(flags, timeout, cancel, type, mask, + (cups_dest_cb_t)cups_block_cb, (void *)block)); +} +# endif /* __BLOCKS__ */ + + +/* + * 'cupsFreeDests()' - Free the memory used by the list of destinations. + */ + +void +cupsFreeDests(int num_dests, /* I - Number of destinations */ + cups_dest_t *dests) /* I - Destinations */ +{ + int i; /* Looping var */ + cups_dest_t *dest; /* Current destination */ + + + if (num_dests == 0 || dests == NULL) + return; + + for (i = num_dests, dest = dests; i > 0; i --, dest ++) + { + _cupsStrFree(dest->name); + _cupsStrFree(dest->instance); + + cupsFreeOptions(dest->num_options, dest->options); + } + + free(dests); +} + + +/* + * 'cupsGetDest()' - Get the named destination from the list. + * + * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a + * list of supported destinations for the current user. + */ + +cups_dest_t * /* O - Destination pointer or @code NULL@ */ +cupsGetDest(const char *name, /* I - Destination name or @code NULL@ for the default destination */ + const char *instance, /* I - Instance name or @code NULL@ */ + int num_dests, /* I - Number of destinations */ + cups_dest_t *dests) /* I - Destinations */ +{ + int diff, /* Result of comparison */ + match; /* Matching index */ + + + if (num_dests <= 0 || !dests) + return (NULL); + + if (!name) + { + /* + * NULL name for default printer. + */ + + while (num_dests > 0) + { + if (dests->is_default) + return (dests); + + num_dests --; + dests ++; + } + } + else + { + /* + * Lookup name and optionally the instance... + */ + + match = cups_find_dest(name, instance, num_dests, dests, -1, &diff); + + if (!diff) + return (dests + match); + } + + return (NULL); +} + + +/* + * '_cupsGetDestResource()' - Get the resource path and URI for a destination. + */ + +const char * /* O - Printer URI */ +_cupsGetDestResource( + cups_dest_t *dest, /* I - Destination */ + char *resource, /* I - Resource buffer */ + size_t resourcesize) /* I - Size of resource buffer */ +{ + const char *uri; /* Printer URI */ + char scheme[32], /* URI scheme */ + userpass[256], /* Username and password (unused) */ + hostname[256]; /* Hostname */ + int port; /* Port number */ + + + /* + * Range check input... + */ + + if (!dest || !resource || resourcesize < 1) + { + if (resource) + *resource = '\0'; + + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (NULL); + } + + /* + * Grab the printer URI... + */ + + if ((uri = cupsGetOption("printer-uri-supported", dest->num_options, + dest->options)) == NULL) + { + if (resource) + *resource = '\0'; + + _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOENT), 0); + + return (NULL); + } + +#ifdef HAVE_DNSSD + if (strstr(uri, "._tcp")) + { + if ((uri = cups_dnssd_resolve(dest, uri, 5000, NULL, NULL, NULL)) == NULL) + return (NULL); + } +#endif /* HAVE_DNSSD */ + + if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), + userpass, sizeof(userpass), hostname, sizeof(hostname), + &port, resource, resourcesize) < HTTP_URI_OK) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad printer URI."), 1); + + return (NULL); + } + + return (uri); +} + + +/* + * '_cupsGetDests()' - Get destinations from a server. + * + * "op" is CUPS_GET_PRINTERS to get a full list, CUPS_GET_DEFAULT to get the + * system-wide default printer, or IPP_GET_PRINTER_ATTRIBUTES for a known + * printer. + * + * "name" is the name of an existing printer and is only used when "op" is + * IPP_GET_PRINTER_ATTRIBUTES. + * + * "dest" is initialized to point to the array of destinations. + * + * 0 is returned if there are no printers, no default printer, or the named + * printer does not exist, respectively. + * + * Free the memory used by the destination array using the @link cupsFreeDests@ + * function. + * + * Note: On OS X this function also gets the default paper from the system + * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the + * options array for each destination that supports it. + */ + +int /* O - Number of destinations */ +_cupsGetDests(http_t *http, /* I - Connection to server or + * @code CUPS_HTTP_DEFAULT@ */ + ipp_op_t op, /* I - IPP operation */ + const char *name, /* I - Name of destination */ + cups_dest_t **dests, /* IO - Destinations */ + cups_ptype_t type, /* I - Printer type bits */ + cups_ptype_t mask) /* I - Printer type mask */ +{ + int num_dests = 0; /* Number of destinations */ + cups_dest_t *dest; /* Current destination */ + ipp_t *request, /* IPP Request */ + *response; /* IPP Response */ + ipp_attribute_t *attr; /* Current attribute */ + const char *printer_name; /* printer-name attribute */ + char uri[1024]; /* printer-uri value */ + int num_options; /* Number of options */ + cups_option_t *options; /* Options */ +#ifdef __APPLE__ + char media_default[41]; /* Default paper size */ +#endif /* __APPLE__ */ + char optname[1024], /* Option name */ + value[2048], /* Option value */ + *ptr; /* Pointer into name/value */ + static const char * const pattrs[] = /* Attributes we're interested in */ + { + "auth-info-required", + "device-uri", + "job-sheets-default", + "marker-change-time", + "marker-colors", + "marker-high-levels", + "marker-levels", + "marker-low-levels", + "marker-message", + "marker-names", + "marker-types", +#ifdef __APPLE__ + "media-supported", +#endif /* __APPLE__ */ + "printer-commands", + "printer-defaults", + "printer-info", + "printer-is-accepting-jobs", + "printer-is-shared", + "printer-location", + "printer-make-and-model", + "printer-name", + "printer-state", + "printer-state-change-time", + "printer-state-reasons", + "printer-type", + "printer-uri-supported" + }; + + +#ifdef __APPLE__ + /* + * Get the default paper size... + */ + + appleGetPaperSize(media_default, sizeof(media_default)); +#endif /* __APPLE__ */ + + /* + * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which + * require the following attributes: + * + * attributes-charset + * attributes-natural-language + * requesting-user-name + * printer-uri [for IPP_GET_PRINTER_ATTRIBUTES] + */ + + request = ippNewRequest(op); + + ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), + NULL, pattrs); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, + "requesting-user-name", NULL, cupsUser()); + + if (name && op != CUPS_GET_DEFAULT) + { + httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, + "localhost", ippPort(), "/printers/%s", name); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, + uri); + } + else if (mask) + { + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type", + type); + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask", + mask); + } + + /* + * Do the request and get back a response... + */ + + if ((response = cupsDoRequest(http, request, "/")) != NULL) + { + for (attr = response->attrs; attr != NULL; attr = attr->next) + { + /* + * Skip leading attributes until we hit a printer... + */ + + while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) + attr = attr->next; + + if (attr == NULL) + break; + + /* + * Pull the needed attributes from this printer... + */ + + printer_name = NULL; + num_options = 0; + options = NULL; + + for (; attr && attr->group_tag == IPP_TAG_PRINTER; attr = attr->next) + { + if (attr->value_tag != IPP_TAG_INTEGER && + attr->value_tag != IPP_TAG_ENUM && + attr->value_tag != IPP_TAG_BOOLEAN && + attr->value_tag != IPP_TAG_TEXT && + attr->value_tag != IPP_TAG_TEXTLANG && + attr->value_tag != IPP_TAG_NAME && + attr->value_tag != IPP_TAG_NAMELANG && + attr->value_tag != IPP_TAG_KEYWORD && + attr->value_tag != IPP_TAG_RANGE && + attr->value_tag != IPP_TAG_URI) + continue; + + if (!strcmp(attr->name, "auth-info-required") || + !strcmp(attr->name, "device-uri") || + !strcmp(attr->name, "marker-change-time") || + !strcmp(attr->name, "marker-colors") || + !strcmp(attr->name, "marker-high-levels") || + !strcmp(attr->name, "marker-levels") || + !strcmp(attr->name, "marker-low-levels") || + !strcmp(attr->name, "marker-message") || + !strcmp(attr->name, "marker-names") || + !strcmp(attr->name, "marker-types") || + !strcmp(attr->name, "printer-commands") || + !strcmp(attr->name, "printer-info") || + !strcmp(attr->name, "printer-is-shared") || + !strcmp(attr->name, "printer-make-and-model") || + !strcmp(attr->name, "printer-state") || + !strcmp(attr->name, "printer-state-change-time") || + !strcmp(attr->name, "printer-type") || + !strcmp(attr->name, "printer-is-accepting-jobs") || + !strcmp(attr->name, "printer-location") || + !strcmp(attr->name, "printer-state-reasons") || + !strcmp(attr->name, "printer-uri-supported")) + { + /* + * Add a printer description attribute... + */ + + num_options = cupsAddOption(attr->name, + cups_make_string(attr, value, + sizeof(value)), + num_options, &options); + } +#ifdef __APPLE__ + else if (!strcmp(attr->name, "media-supported")) + { + /* + * See if we can set a default media size... + */ + + int i; /* Looping var */ + + for (i = 0; i < attr->num_values; i ++) + if (!_cups_strcasecmp(media_default, attr->values[i].string.text)) + { + num_options = cupsAddOption("media", media_default, num_options, + &options); + break; + } + } +#endif /* __APPLE__ */ + else if (!strcmp(attr->name, "printer-name") && + attr->value_tag == IPP_TAG_NAME) + printer_name = attr->values[0].string.text; + else if (strncmp(attr->name, "notify-", 7) && + (attr->value_tag == IPP_TAG_BOOLEAN || + attr->value_tag == IPP_TAG_ENUM || + attr->value_tag == IPP_TAG_INTEGER || + attr->value_tag == IPP_TAG_KEYWORD || + attr->value_tag == IPP_TAG_NAME || + attr->value_tag == IPP_TAG_RANGE) && + (ptr = strstr(attr->name, "-default")) != NULL) + { + /* + * Add a default option... + */ + + strlcpy(optname, attr->name, sizeof(optname)); + optname[ptr - attr->name] = '\0'; + + if (_cups_strcasecmp(optname, "media") || + !cupsGetOption("media", num_options, options)) + num_options = cupsAddOption(optname, + cups_make_string(attr, value, + sizeof(value)), + num_options, &options); + } + } + + /* + * See if we have everything needed... + */ + + if (!printer_name) + { + cupsFreeOptions(num_options, options); + + if (attr == NULL) + break; + else + continue; + } + + if ((dest = cups_add_dest(printer_name, NULL, &num_dests, dests)) != NULL) + { + dest->num_options = num_options; + dest->options = options; + } + else + cupsFreeOptions(num_options, options); + + if (attr == NULL) + break; + } + + ippDelete(response); + } + + /* + * Return the count... + */ + + return (num_dests); +} + + +/* + * 'cupsGetDests()' - Get the list of destinations from the default server. + * + * Starting with CUPS 1.2, the returned list of destinations include the + * printer-info, printer-is-accepting-jobs, printer-is-shared, + * printer-make-and-model, printer-state, printer-state-change-time, + * printer-state-reasons, and printer-type attributes as options. CUPS 1.4 + * adds the marker-change-time, marker-colors, marker-high-levels, + * marker-levels, marker-low-levels, marker-message, marker-names, + * marker-types, and printer-commands attributes as well. + * + * Use the @link cupsFreeDests@ function to free the destination list and + * the @link cupsGetDest@ function to find a particular destination. + */ + +int /* O - Number of destinations */ +cupsGetDests(cups_dest_t **dests) /* O - Destinations */ +{ + return (cupsGetDests2(CUPS_HTTP_DEFAULT, dests)); +} + + +/* + * 'cupsGetDests2()' - Get the list of destinations from the specified server. + * + * Starting with CUPS 1.2, the returned list of destinations include the + * printer-info, printer-is-accepting-jobs, printer-is-shared, + * printer-make-and-model, printer-state, printer-state-change-time, + * printer-state-reasons, and printer-type attributes as options. CUPS 1.4 + * adds the marker-change-time, marker-colors, marker-high-levels, + * marker-levels, marker-low-levels, marker-message, marker-names, + * marker-types, and printer-commands attributes as well. + * + * Use the @link cupsFreeDests@ function to free the destination list and + * the @link cupsGetDest@ function to find a particular destination. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +int /* O - Number of destinations */ +cupsGetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + cups_dest_t **dests) /* O - Destinations */ +{ + int num_dests; /* Number of destinations */ + cups_dest_t *dest; /* Destination pointer */ + const char *home; /* HOME environment variable */ + char filename[1024]; /* Local ~/.cups/lpoptions file */ + const char *defprinter; /* Default printer */ + char name[1024], /* Copy of printer name */ + *instance, /* Pointer to instance name */ + *user_default; /* User default printer */ + int num_reals; /* Number of real queues */ + cups_dest_t *reals; /* Real queues */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * Range check the input... + */ + + if (!dests) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad NULL dests pointer"), 1); + return (0); + } + + /* + * Grab the printers and classes... + */ + + *dests = (cups_dest_t *)0; + num_dests = _cupsGetDests(http, CUPS_GET_PRINTERS, NULL, dests, 0, 0); + + if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE) + { + cupsFreeDests(num_dests, *dests); + *dests = (cups_dest_t *)0; + return (0); + } + + /* + * Make a copy of the "real" queues for a later sanity check... + */ + + if (num_dests > 0) + { + num_reals = num_dests; + reals = calloc(num_reals, sizeof(cups_dest_t)); + + if (reals) + memcpy(reals, *dests, num_reals * sizeof(cups_dest_t)); + else + num_reals = 0; + } + else + { + num_reals = 0; + reals = NULL; + } + + /* + * Grab the default destination... + */ + + if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL) + defprinter = name; + else if ((defprinter = cupsGetDefault2(http)) != NULL) + { + strlcpy(name, defprinter, sizeof(name)); + defprinter = name; + } + + if (defprinter) + { + /* + * Separate printer and instance name... + */ + + if ((instance = strchr(name, '/')) != NULL) + *instance++ = '\0'; + + /* + * Lookup the printer and instance and make it the default... + */ + + if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL) + dest->is_default = 1; + } + else + instance = NULL; + + /* + * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files... + */ + + snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); + num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL, + num_dests, dests); + + if ((home = getenv("HOME")) != NULL) + { + snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); + + num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL, + num_dests, dests); + } + + /* + * Validate the current default destination - this prevents old + * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from + * pointing to a non-existent printer or class... + */ + + if (num_reals) + { + /* + * See if we have a default printer... + */ + + if ((dest = cupsGetDest(NULL, NULL, num_dests, *dests)) != NULL) + { + /* + * Have a default; see if it is real... + */ + + if (!cupsGetDest(dest->name, NULL, num_reals, reals)) + { + /* + * Remove the non-real printer from the list, since we don't want jobs + * going to an unexpected printer... () + */ + + num_dests = cupsRemoveDest(dest->name, dest->instance, num_dests, + dests); + } + } + + /* + * Free memory... + */ + + free(reals); + } + + /* + * Return the number of destinations... + */ + + if (num_dests > 0) + _cupsSetError(IPP_OK, NULL, 0); + + return (num_dests); +} + + +/* + * 'cupsGetNamedDest()' - Get options for the named destination. + * + * This function is optimized for retrieving a single destination and should + * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either + * know the name of the destination or want to print to the default destination. + * If @code NULL@ is returned, the destination does not exist or there is no + * default destination. + * + * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print + * server will be used. + * + * If "name" is @code NULL@, the default printer for the current user will be + * returned. + * + * The returned destination must be freed using @link cupsFreeDests@ with a + * "num_dests" value of 1. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +cups_dest_t * /* O - Destination or @code NULL@ */ +cupsGetNamedDest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name, /* I - Destination name or @code NULL@ for the default destination */ + const char *instance) /* I - Instance name or @code NULL@ */ +{ + cups_dest_t *dest; /* Destination */ + char filename[1024], /* Path to lpoptions */ + defname[256]; /* Default printer name */ + const char *home = getenv("HOME"); /* Home directory */ + int set_as_default = 0; /* Set returned destination as default */ + ipp_op_t op = IPP_GET_PRINTER_ATTRIBUTES; + /* IPP operation to get server ops */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * If "name" is NULL, find the default destination... + */ + + if (!name) + { + set_as_default = 1; + name = _cupsUserDefault(defname, sizeof(defname)); + + if (name) + { + char *ptr; /* Temporary pointer... */ + + if ((ptr = strchr(defname, '/')) != NULL) + { + *ptr++ = '\0'; + instance = ptr; + } + else + instance = NULL; + } + else if (home) + { + /* + * No default in the environment, try the user's lpoptions files... + */ + + snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); + + name = cups_get_default(filename, defname, sizeof(defname), &instance); + } + + if (!name) + { + /* + * Still not there? Try the system lpoptions file... + */ + + snprintf(filename, sizeof(filename), "%s/lpoptions", + cg->cups_serverroot); + name = cups_get_default(filename, defname, sizeof(defname), &instance); + } + + if (!name) + { + /* + * No locally-set default destination, ask the server... + */ + + op = CUPS_GET_DEFAULT; + } + } + + /* + * Get the printer's attributes... + */ + + if (!_cupsGetDests(http, op, name, &dest, 0, 0)) + return (NULL); + + if (instance) + dest->instance = _cupsStrAlloc(instance); + + if (set_as_default) + dest->is_default = 1; + + /* + * Then add local options... + */ + + snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); + cups_get_dests(filename, name, instance, 1, 1, &dest); + + if (home) + { + snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); + + cups_get_dests(filename, name, instance, 1, 1, &dest); + } + + /* + * Return the result... + */ + + return (dest); +} + + +/* + * 'cupsRemoveDest()' - Remove a destination from the destination list. + * + * Removing a destination/instance does not delete the class or printer + * queue, merely the lpoptions for that destination/instance. Use the + * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new + * options for the user. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +int /* O - New number of destinations */ +cupsRemoveDest(const char *name, /* I - Destination name */ + const char *instance, /* I - Instance name or @code NULL@ */ + int num_dests, /* I - Number of destinations */ + cups_dest_t **dests) /* IO - Destinations */ +{ + int i; /* Index into destinations */ + cups_dest_t *dest; /* Pointer to destination */ + + + /* + * Find the destination... + */ + + if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL) + return (num_dests); + + /* + * Free memory... + */ + + _cupsStrFree(dest->name); + _cupsStrFree(dest->instance); + cupsFreeOptions(dest->num_options, dest->options); + + /* + * Remove the destination from the array... + */ + + num_dests --; + + i = dest - *dests; + + if (i < num_dests) + memmove(dest, dest + 1, (num_dests - i) * sizeof(cups_dest_t)); + + return (num_dests); +} + + +/* + * 'cupsSetDefaultDest()' - Set the default destination. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +void +cupsSetDefaultDest( + const char *name, /* I - Destination name */ + const char *instance, /* I - Instance name or @code NULL@ */ + int num_dests, /* I - Number of destinations */ + cups_dest_t *dests) /* I - Destinations */ +{ + int i; /* Looping var */ + cups_dest_t *dest; /* Current destination */ + + + /* + * Range check input... + */ + + if (!name || num_dests <= 0 || !dests) + return; + + /* + * Loop through the array and set the "is_default" flag for the matching + * destination... + */ + + for (i = num_dests, dest = dests; i > 0; i --, dest ++) + dest->is_default = !_cups_strcasecmp(name, dest->name) && + ((!instance && !dest->instance) || + (instance && dest->instance && + !_cups_strcasecmp(instance, dest->instance))); +} + + +/* + * 'cupsSetDests()' - Save the list of destinations for the default server. + * + * This function saves the destinations to /etc/cups/lpoptions when run + * as root and ~/.cups/lpoptions when run as a normal user. + */ + +void +cupsSetDests(int num_dests, /* I - Number of destinations */ + cups_dest_t *dests) /* I - Destinations */ +{ + cupsSetDests2(CUPS_HTTP_DEFAULT, num_dests, dests); +} + + +/* + * 'cupsSetDests2()' - Save the list of destinations for the specified server. + * + * This function saves the destinations to /etc/cups/lpoptions when run + * as root and ~/.cups/lpoptions when run as a normal user. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +int /* O - 0 on success, -1 on error */ +cupsSetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + int num_dests, /* I - Number of destinations */ + cups_dest_t *dests) /* I - Destinations */ +{ + int i, j; /* Looping vars */ + int wrote; /* Wrote definition? */ + cups_dest_t *dest; /* Current destination */ + cups_option_t *option; /* Current option */ + _ipp_option_t *match; /* Matching attribute for option */ + FILE *fp; /* File pointer */ +#ifndef WIN32 + const char *home; /* HOME environment variable */ +#endif /* WIN32 */ + char filename[1024]; /* lpoptions file */ + int num_temps; /* Number of temporary destinations */ + cups_dest_t *temps = NULL, /* Temporary destinations */ + *temp; /* Current temporary dest */ + const char *val; /* Value of temporary option */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * Range check the input... + */ + + if (!num_dests || !dests) + return (-1); + + /* + * Get the server destinations... + */ + + num_temps = _cupsGetDests(http, CUPS_GET_PRINTERS, NULL, &temps, 0, 0); + + if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE) + { + cupsFreeDests(num_temps, temps); + return (-1); + } + + /* + * Figure out which file to write to... + */ + + snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); + +#ifndef WIN32 + if (getuid()) + { + /* + * Merge in server defaults... + */ + + num_temps = cups_get_dests(filename, NULL, NULL, 0, num_temps, &temps); + + /* + * Point to user defaults... + */ + + if ((home = getenv("HOME")) != NULL) + { + /* + * Create ~/.cups subdirectory... + */ + + snprintf(filename, sizeof(filename), "%s/.cups", home); + if (access(filename, 0)) + mkdir(filename, 0700); + + snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); + } + } +#endif /* !WIN32 */ + + /* + * Try to open the file... + */ + + if ((fp = fopen(filename, "w")) == NULL) + { + cupsFreeDests(num_temps, temps); + return (-1); + } + +#ifndef WIN32 + /* + * Set the permissions to 0644 when saving to the /etc/cups/lpoptions + * file... + */ + + if (!getuid()) + fchmod(fileno(fp), 0644); +#endif /* !WIN32 */ + + /* + * Write each printer; each line looks like: + * + * Dest name[/instance] options + * Default name[/instance] options + */ + + for (i = num_dests, dest = dests; i > 0; i --, dest ++) + if (dest->instance != NULL || dest->num_options != 0 || dest->is_default) + { + if (dest->is_default) + { + fprintf(fp, "Default %s", dest->name); + if (dest->instance) + fprintf(fp, "/%s", dest->instance); + + wrote = 1; + } + else + wrote = 0; + + if ((temp = cupsGetDest(dest->name, dest->instance, num_temps, temps)) == NULL) + temp = cupsGetDest(dest->name, NULL, num_temps, temps); + + for (j = dest->num_options, option = dest->options; j > 0; j --, option ++) + { + /* + * See if this option is a printer attribute; if so, skip it... + */ + + if ((match = _ippFindOption(option->name)) != NULL && + match->group_tag == IPP_TAG_PRINTER) + continue; + + /* + * See if the server/global options match these; if so, don't + * write 'em. + */ + + if (temp && + (val = cupsGetOption(option->name, temp->num_options, + temp->options)) != NULL && + !_cups_strcasecmp(val, option->value)) + continue; + + /* + * Options don't match, write to the file... + */ + + if (!wrote) + { + fprintf(fp, "Dest %s", dest->name); + if (dest->instance) + fprintf(fp, "/%s", dest->instance); + wrote = 1; + } + + if (option->value[0]) + { + if (strchr(option->value, ' ') || + strchr(option->value, '\\') || + strchr(option->value, '\"') || + strchr(option->value, '\'')) + { + /* + * Quote the value... + */ + + fprintf(fp, " %s=\"", option->name); + + for (val = option->value; *val; val ++) + { + if (strchr("\"\'\\", *val)) + putc('\\', fp); + + putc(*val, fp); + } + + putc('\"', fp); + } + else + { + /* + * Store the literal value... + */ + + fprintf(fp, " %s=%s", option->name, option->value); + } + } + else + fprintf(fp, " %s", option->name); + } + + if (wrote) + fputs("\n", fp); + } + + /* + * Free the temporary destinations and close the file... + */ + + cupsFreeDests(num_temps, temps); + + fclose(fp); + +#ifdef __APPLE__ + /* + * Set the default printer for this location - this allows command-line + * and GUI applications to share the same default destination... + */ + + if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL) + { + CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, + dest->name, + kCFStringEncodingUTF8); + /* Default printer name */ + + if (name) + { + _cupsAppleSetDefaultPrinter(name); + CFRelease(name); + } + } +#endif /* __APPLE__ */ + +#ifdef HAVE_NOTIFY_POST + /* + * Send a notification so that MacOS X applications can know about the + * change, too. + */ + + notify_post("com.apple.printerListChange"); +#endif /* HAVE_NOTIFY_POST */ + + return (0); +} + + +/* + * '_cupsUserDefault()' - Get the user default printer from environment + * variables and location information. + */ + +char * /* O - Default printer or NULL */ +_cupsUserDefault(char *name, /* I - Name buffer */ + size_t namesize) /* I - Size of name buffer */ +{ + const char *env; /* LPDEST or PRINTER env variable */ +#ifdef __APPLE__ + CFStringRef locprinter; /* Last printer as this location */ +#endif /* __APPLE__ */ + + + if ((env = getenv("LPDEST")) == NULL) + if ((env = getenv("PRINTER")) != NULL && !strcmp(env, "lp")) + env = NULL; + + if (env) + { + strlcpy(name, env, namesize); + return (name); + } + +#ifdef __APPLE__ + /* + * Use location-based defaults if "use last printer" is selected in the + * system preferences... + */ + + if ((locprinter = _cupsAppleCopyDefaultPrinter()) != NULL) + { + CFStringGetCString(locprinter, name, namesize, kCFStringEncodingUTF8); + CFRelease(locprinter); + } + else + name[0] = '\0'; + + DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name)); + + return (*name ? name : NULL); + +#else + /* + * No location-based defaults on this platform... + */ + + name[0] = '\0'; + return (NULL); +#endif /* __APPLE__ */ +} + + +#ifdef __APPLE__ +/* + * 'appleCopyLocations()' - Copy the location history array. + */ + +static CFArrayRef /* O - Location array or NULL */ +appleCopyLocations(void) +{ + CFArrayRef locations; /* Location array */ + + + /* + * Look up the location array in the preferences... + */ + + if ((locations = CFPreferencesCopyAppValue(kLastUsedPrintersKey, + kCUPSPrintingPrefs)) == NULL) + return (NULL); + + if (CFGetTypeID(locations) != CFArrayGetTypeID()) + { + CFRelease(locations); + return (NULL); + } + + return (locations); +} + + +/* + * 'appleCopyNetwork()' - Get the network ID for the current location. + */ + +static CFStringRef /* O - Network ID */ +appleCopyNetwork(void) +{ + SCDynamicStoreRef dynamicStore; /* System configuration data */ + CFStringRef key; /* Current network configuration key */ + CFDictionaryRef ip_dict; /* Network configuration data */ + CFStringRef network = NULL; /* Current network ID */ + + + if ((dynamicStore = SCDynamicStoreCreate(NULL, CFSTR("libcups"), NULL, + NULL)) != NULL) + { + /* + * First use the IPv6 router address, if available, since that will generally + * be a globally-unique link-local address. + */ + + if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity( + NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)) != NULL) + { + if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL) + { + if ((network = CFDictionaryGetValue(ip_dict, + kSCPropNetIPv6Router)) != NULL) + CFRetain(network); + + CFRelease(ip_dict); + } + + CFRelease(key); + } + + /* + * If that doesn't work, try the IPv4 router address. This isn't as unique + * and will likely be a 10.x.y.z or 192.168.y.z address... + */ + + if (!network) + { + if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity( + NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)) != NULL) + { + if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL) + { + if ((network = CFDictionaryGetValue(ip_dict, + kSCPropNetIPv4Router)) != NULL) + CFRetain(network); + + CFRelease(ip_dict); + } + + CFRelease(key); + } + } + + CFRelease(dynamicStore); + } + + return (network); +} + + +/* + * 'appleGetPaperSize()' - Get the default paper size. + */ + +static char * /* O - Default paper size */ +appleGetPaperSize(char *name, /* I - Paper size name buffer */ + int namesize) /* I - Size of buffer */ +{ + CFStringRef defaultPaperID; /* Default paper ID */ + _pwg_media_t *pwgmedia; /* PWG media size */ + + + defaultPaperID = _cupsAppleCopyDefaultPaperID(); + if (!defaultPaperID || + CFGetTypeID(defaultPaperID) != CFStringGetTypeID() || + !CFStringGetCString(defaultPaperID, name, namesize, + kCFStringEncodingUTF8)) + name[0] = '\0'; + else if ((pwgmedia = _pwgMediaForLegacy(name)) != NULL) + strlcpy(name, pwgmedia->pwg, namesize); + + if (defaultPaperID) + CFRelease(defaultPaperID); + + return (name); +} + + +/* + * 'appleGetPrinter()' - Get a printer from the history array. + */ + +static CFStringRef /* O - Printer name or NULL */ +appleGetPrinter(CFArrayRef locations, /* I - Location array */ + CFStringRef network, /* I - Network name */ + CFIndex *locindex) /* O - Index in array */ +{ + CFIndex i, /* Looping var */ + count; /* Number of locations */ + CFDictionaryRef location; /* Current location */ + CFStringRef locnetwork, /* Current network */ + locprinter; /* Current printer */ + + + for (i = 0, count = CFArrayGetCount(locations); i < count; i ++) + if ((location = CFArrayGetValueAtIndex(locations, i)) != NULL && + CFGetTypeID(location) == CFDictionaryGetTypeID()) + { + if ((locnetwork = CFDictionaryGetValue(location, + kLocationNetworkKey)) != NULL && + CFGetTypeID(locnetwork) == CFStringGetTypeID() && + CFStringCompare(network, locnetwork, 0) == kCFCompareEqualTo && + (locprinter = CFDictionaryGetValue(location, + kLocationPrinterIDKey)) != NULL && + CFGetTypeID(locprinter) == CFStringGetTypeID()) + { + if (locindex) + *locindex = i; + + return (locprinter); + } + } + + return (NULL); +} +#endif /* __APPLE__ */ + + +/* + * 'cups_add_dest()' - Add a destination to the array. + * + * Unlike cupsAddDest(), this function does not check for duplicates. + */ + +static cups_dest_t * /* O - New destination */ +cups_add_dest(const char *name, /* I - Name of destination */ + const char *instance, /* I - Instance or NULL */ + int *num_dests, /* IO - Number of destinations */ + cups_dest_t **dests) /* IO - Destinations */ +{ + int insert, /* Insertion point */ + diff; /* Result of comparison */ + cups_dest_t *dest; /* Destination pointer */ + + + /* + * Add new destination... + */ + + if (*num_dests == 0) + dest = malloc(sizeof(cups_dest_t)); + else + dest = realloc(*dests, sizeof(cups_dest_t) * (*num_dests + 1)); + + if (!dest) + return (NULL); + + *dests = dest; + + /* + * Find where to insert the destination... + */ + + if (*num_dests == 0) + insert = 0; + else + { + insert = cups_find_dest(name, instance, *num_dests, *dests, *num_dests - 1, + &diff); + + if (diff > 0) + insert ++; + } + + /* + * Move the array elements as needed... + */ + + if (insert < *num_dests) + memmove(*dests + insert + 1, *dests + insert, + (*num_dests - insert) * sizeof(cups_dest_t)); + + (*num_dests) ++; + + /* + * Initialize the destination... + */ + + dest = *dests + insert; + dest->name = _cupsStrAlloc(name); + dest->instance = _cupsStrAlloc(instance); + dest->is_default = 0; + dest->num_options = 0; + dest->options = (cups_option_t *)0; + + return (dest); +} + + +# ifdef __BLOCKS__ +/* + * 'cups_block_cb()' - Enumeration callback for block API. + */ + +static int /* O - 1 to continue, 0 to stop */ +cups_block_cb( + cups_dest_block_t block, /* I - Block */ + unsigned flags, /* I - Destination flags */ + cups_dest_t *dest) /* I - Destination */ +{ + return ((block)(flags, dest)); +} +# endif /* __BLOCKS__ */ + + +/* + * 'cups_compare_dests()' - Compare two destinations. + */ + +static int /* O - Result of comparison */ +cups_compare_dests(cups_dest_t *a, /* I - First destination */ + cups_dest_t *b) /* I - Second destination */ +{ + int diff; /* Difference */ + + + if ((diff = _cups_strcasecmp(a->name, b->name)) != 0) + return (diff); + else if (a->instance && b->instance) + return (_cups_strcasecmp(a->instance, b->instance)); + else + return ((a->instance && !b->instance) - (!a->instance && b->instance)); +} + + +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) +# ifdef HAVE_DNSSD +/* + * 'cups_dnssd_browse_cb()' - Browse for printers. + */ + +static void +cups_dnssd_browse_cb( + DNSServiceRef sdRef, /* I - Service reference */ + DNSServiceFlags flags, /* I - Option flags */ + uint32_t interfaceIndex, /* I - Interface number */ + DNSServiceErrorType errorCode, /* I - Error, if any */ + const char *serviceName, /* I - Name of service/device */ + const char *regtype, /* I - Type of service */ + const char *replyDomain, /* I - Service domain */ + void *context) /* I - Enumeration data */ +{ + _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; + /* Enumeration data */ + + + DEBUG_printf(("5cups_dnssd_browse_cb(sdRef=%p, flags=%x, " + "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", " + "regtype=\"%s\", replyDomain=\"%s\", context=%p)", + sdRef, flags, interfaceIndex, errorCode, serviceName, regtype, + replyDomain, context)); + + /* + * Don't do anything on error... + */ + + if (errorCode != kDNSServiceErr_NoError) + return; + + /* + * Get the device... + */ + + cups_dnssd_get_device(data, serviceName, regtype, replyDomain); +} + + +# else /* HAVE_AVAHI */ +/* + * 'cups_dnssd_browse_cb()' - Browse for printers. + */ + +static void +cups_dnssd_browse_cb( + AvahiServiceBrowser *browser, /* I - Browser */ + AvahiIfIndex interface, /* I - Interface index (unused) */ + AvahiProtocol protocol, /* I - Network protocol (unused) */ + AvahiBrowserEvent event, /* I - What happened */ + const char *name, /* I - Service name */ + const char *type, /* I - Registration type */ + const char *domain, /* I - Domain */ + AvahiLookupResultFlags flags, /* I - Flags */ + void *context) /* I - Devices array */ +{ +#ifdef DEBUG + AvahiClient *client = avahi_service_browser_get_client(browser); + /* Client information */ +#endif /* DEBUG */ + _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; + /* Enumeration data */ + + + (void)interface; + (void)protocol; + (void)context; + + switch (event) + { + case AVAHI_BROWSER_FAILURE: + DEBUG_printf(("cups_dnssd_browse_cb: %s", + avahi_strerror(avahi_client_errno(client)))); + avahi_simple_poll_quit(data->simple_poll); + break; + + case AVAHI_BROWSER_NEW: + /* + * This object is new on the network. + */ + + if (flags & AVAHI_LOOKUP_RESULT_LOCAL) + { + /* + * This comes from the local machine so ignore it. + */ + + DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", + name)); + } + else + { + /* + * Create a device entry for it if it doesn't yet exist. + */ + + cups_dnssd_get_device(data, name, type, domain); + } + break; + + case AVAHI_BROWSER_REMOVE: + case AVAHI_BROWSER_ALL_FOR_NOW: + case AVAHI_BROWSER_CACHE_EXHAUSTED: + break; + } +} + + +/* + * 'cups_dnssd_client_cb()' - Avahi client callback function. + */ + +static void +cups_dnssd_client_cb( + AvahiClient *client, /* I - Client information (unused) */ + AvahiClientState state, /* I - Current state */ + void *context) /* I - User data (unused) */ +{ + _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; + /* Enumeration data */ + + + (void)client; + + /* + * If the connection drops, quit. + */ + + if (state == AVAHI_CLIENT_FAILURE) + { + DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed."); + avahi_simple_poll_quit(data->simple_poll); + } +} +# endif /* HAVE_DNSSD */ + + +/* + * 'cups_dnssd_compare_device()' - Compare two devices. + */ + +static int /* O - Result of comparison */ +cups_dnssd_compare_devices( + _cups_dnssd_device_t *a, /* I - First device */ + _cups_dnssd_device_t *b) /* I - Second device */ +{ + return (strcmp(a->dest.name, b->dest.name)); +} + + +/* + * 'cups_dnssd_free_device()' - Free the memory used by a device. + */ + +static void +cups_dnssd_free_device( + _cups_dnssd_device_t *device, /* I - Device */ + _cups_dnssd_data_t *data) /* I - Enumeration data */ +{ + DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", device, + device->dest.name, data)); + +# ifdef HAVE_DNSSD + if (device->ref) + DNSServiceRefDeallocate(device->ref); +# else /* HAVE_AVAHI */ + if (device->ref) + avahi_record_browser_free(device->ref); +# endif /* HAVE_DNSSD */ + + _cupsStrFree(device->domain); + _cupsStrFree(device->fullName); + _cupsStrFree(device->regtype); + _cupsStrFree(device->dest.name); + + cupsFreeOptions(device->dest.num_options, device->dest.options); + + free(device); +} + + +/* + * 'cups_dnssd_get_device()' - Lookup a device and create it as needed. + */ + +static _cups_dnssd_device_t * /* O - Device */ +cups_dnssd_get_device( + _cups_dnssd_data_t *data, /* I - Enumeration data */ + const char *serviceName, /* I - Service name */ + const char *regtype, /* I - Registration type */ + const char *replyDomain) /* I - Domain name */ +{ + _cups_dnssd_device_t key, /* Search key */ + *device; /* Device */ + char fullName[kDNSServiceMaxDomainName]; + /* Full name for query */ + + + DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", " + "regtype=\"%s\", replyDomain=\"%s\")", data, serviceName, + regtype, replyDomain)); + + /* + * See if this is an existing device... + */ + + key.dest.name = (char *)serviceName; + + if ((device = cupsArrayFind(data->devices, &key)) != NULL) + { + /* + * Yes, see if we need to do anything with this... + */ + + int update = 0; /* Non-zero if we need to update */ + + if (!_cups_strcasecmp(replyDomain, "local.") && + _cups_strcasecmp(device->domain, replyDomain)) + { + /* + * Update the "global" listing to use the .local domain name instead. + */ + + _cupsStrFree(device->domain); + device->domain = _cupsStrAlloc(replyDomain); + + DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local " + "domain.", device->dest.name)); + + update = 1; + } + + if (!_cups_strcasecmp(regtype, "_ipps._tcp") && + _cups_strcasecmp(device->regtype, regtype)) + { + /* + * Prefer IPPS over IPP. + */ + + _cupsStrFree(device->regtype); + device->regtype = _cupsStrAlloc(regtype); + + DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.", + device->dest.name)); + + update = 1; + } + + if (!update) + { + DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.", + device->dest.name)); + return (device); + } + } + else + { + /* + * No, add the device... + */ + + DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain " + "'%s'.", serviceName, + !strcmp(regtype, "_ipps._tcp") ? "IPPS" : "IPP", + replyDomain)); + + device = calloc(sizeof(_cups_dnssd_device_t), 1); + device->dest.name = _cupsStrAlloc(serviceName); + device->domain = _cupsStrAlloc(replyDomain); + device->regtype = _cupsStrAlloc(regtype); + + cupsArrayAdd(data->devices, device); + } + + /* + * Set the "full name" of this service, which is used for queries... + */ + +# ifdef HAVE_DNSSD + DNSServiceConstructFullName(fullName, device->dest.name, device->regtype, + device->domain); +# else /* HAVE_AVAHI */ + avahi_service_name_join(fullName, kDNSServiceMaxDomainName, serviceName, + regtype, replyDomain); +# endif /* HAVE_DNSSD */ + + _cupsStrFree(device->fullName); + device->fullName = _cupsStrAlloc(fullName); + + if (device->ref) + { +# ifdef HAVE_DNSSD + DNSServiceRefDeallocate(device->ref); +# else /* HAVE_AVAHI */ + avahi_record_browser_free(device->ref); +# endif /* HAVE_DNSSD */ + + device->ref = 0; + } + + if (device->state == _CUPS_DNSSD_ACTIVE) + { + (*data->cb)(data->user_data, CUPS_DEST_FLAGS_REMOVED, &device->dest); + device->state = _CUPS_DNSSD_NEW; + } + + return (device); +} + + +# ifdef HAVE_DNSSD +/* + * 'cups_dnssd_local_cb()' - Browse for local printers. + */ + +static void +cups_dnssd_local_cb( + DNSServiceRef sdRef, /* I - Service reference */ + DNSServiceFlags flags, /* I - Option flags */ + uint32_t interfaceIndex, /* I - Interface number */ + DNSServiceErrorType errorCode, /* I - Error, if any */ + const char *serviceName, /* I - Name of service/device */ + const char *regtype, /* I - Type of service */ + const char *replyDomain, /* I - Service domain */ + void *context) /* I - Devices array */ +{ + _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; + /* Enumeration data */ + _cups_dnssd_device_t *device; /* Device */ + + + DEBUG_printf(("5cups_dnssd_local_cb(sdRef=%p, flags=%x, " + "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", " + "regtype=\"%s\", replyDomain=\"%s\", context=%p)", + sdRef, flags, interfaceIndex, errorCode, serviceName, + regtype, replyDomain, context)); + + /* + * Only process "add" data... + */ + + if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd)) + return; + + /* + * Get the device... + */ + + device = cups_dnssd_get_device(data, serviceName, regtype, replyDomain); + + /* + * Hide locally-registered devices... + */ + + DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.", + serviceName)); + + if (device->ref) + { + DNSServiceRefDeallocate(device->ref); + device->ref = 0; + } + + if (device->state == _CUPS_DNSSD_ACTIVE) + (*data->cb)(data->user_data, CUPS_DEST_FLAGS_REMOVED, &device->dest); + + device->state = _CUPS_DNSSD_LOCAL; +} +# endif /* HAVE_DNSSD */ + + +# ifdef HAVE_AVAHI +/* + * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors. + * + * Note: This function is needed because avahi_simple_poll_iterate is broken + * and always uses a timeout of 0 (!) milliseconds. + * (Avahi Ticket #364) + */ + +static int /* O - Number of file descriptors matching */ +cups_dnssd_poll_cb( + struct pollfd *pollfds, /* I - File descriptors */ + unsigned int num_pollfds, /* I - Number of file descriptors */ + int timeout, /* I - Timeout in milliseconds (unused) */ + void *context) /* I - User data (unused) */ +{ + _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; + /* Enumeration data */ + int val; /* Return value */ + + + (void)timeout; + + val = poll(pollfds, num_pollfds, 250); + + if (val < 0) + { + DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno))); + } + else if (val > 0) + data->got_data = 1; + + return (val); +} +# endif /* HAVE_AVAHI */ + + +/* + * 'cups_dnssd_query_cb()' - Process query data. + */ + +# ifdef HAVE_DNSSD +static void +cups_dnssd_query_cb( + DNSServiceRef sdRef, /* I - Service reference */ + DNSServiceFlags flags, /* I - Data flags */ + uint32_t interfaceIndex, /* I - Interface */ + DNSServiceErrorType errorCode, /* I - Error, if any */ + const char *fullName, /* I - Full service name */ + uint16_t rrtype, /* I - Record type */ + uint16_t rrclass, /* I - Record class */ + uint16_t rdlen, /* I - Length of record data */ + const void *rdata, /* I - Record data */ + uint32_t ttl, /* I - Time-to-live */ + void *context) /* I - Enumeration data */ +{ +# else /* HAVE_AVAHI */ +static void +cups_dnssd_query_cb( + AvahiRecordBrowser *browser, /* I - Record browser */ + AvahiIfIndex interfaceIndex, + /* I - Interface index (unused) */ + AvahiProtocol protocol, /* I - Network protocol (unused) */ + AvahiBrowserEvent event, /* I - What happened? */ + const char *fullName, /* I - Service name */ + uint16_t rrclass, /* I - Record class */ + uint16_t rrtype, /* I - Record type */ + const void *rdata, /* I - TXT record */ + size_t rdlen, /* I - Length of TXT record */ + AvahiLookupResultFlags flags, /* I - Flags */ + void *context) /* I - Enumeration data */ +{ +# ifdef DEBUG + AvahiClient *client = avahi_record_browser_get_client(browser); + /* Client information */ +# endif /* DEBUG */ +# endif /* HAVE_DNSSD */ + _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; + /* Enumeration data */ + char name[1024], /* Service name */ + *ptr; /* Pointer into string */ + _cups_dnssd_device_t dkey, /* Search key */ + *device; /* Device */ + + +# ifdef HAVE_DNSSD + DEBUG_printf(("5cups_dnssd_query_cb(sdRef=%p, flags=%x, " + "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", " + "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, " + "context=%p)", sdRef, flags, interfaceIndex, errorCode, + fullName, rrtype, rrclass, rdlen, rdata, ttl, context)); + + /* + * Only process "add" data... + */ + + if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd)) + return; + +# else /* HAVE_AVAHI */ + DEBUG_printf(("5cups_dnssd_query_cb(browser=%p, interfaceIndex=%d, " + "protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, " + "rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)", + browser, interfaceIndex, protocol, event, fullName, rrclass, + rrtype, rdata, (unsigned)rdlen, flags, context)); + + /* + * Only process "add" data... + */ + + if (event != AVAHI_BROWSER_NEW) + { + if (event == AVAHI_BROWSER_FAILURE) + DEBUG_printf(("cups_dnssd_query_cb: %s", + avahi_strerror(avahi_client_errno(client)))); + + return; + } +# endif /* HAVE_DNSSD */ + + /* + * Lookup the service in the devices array. + */ + + dkey.dest.name = name; + + cups_dnssd_unquote(name, fullName, sizeof(name)); + + if ((ptr = strstr(name, "._")) != NULL) + *ptr = '\0'; + + if ((device = cupsArrayFind(data->devices, &dkey)) != NULL) + { + /* + * Found it, pull out the make and model from the TXT record and save it... + */ + + const uint8_t *txt, /* Pointer into data */ + *txtnext, /* Next key/value pair */ + *txtend; /* End of entire TXT record */ + uint8_t txtlen; /* Length of current key/value pair */ + char key[256], /* Key string */ + value[256], /* Value string */ + make_and_model[512], + /* Manufacturer and model */ + model[256], /* Model */ + uriname[1024], /* Name for URI */ + uri[1024]; /* Printer URI */ + cups_ptype_t type = CUPS_PRINTER_REMOTE | CUPS_PRINTER_BW; + /* Printer type */ + int saw_printer_type = 0; + /* Did we see a printer-type key? */ + + device->state = _CUPS_DNSSD_PENDING; + make_and_model[0] = '\0'; + + strcpy(model, "Unknown"); + + for (txt = rdata, txtend = txt + rdlen; + txt < txtend; + txt = txtnext) + { + /* + * Read a key/value pair starting with an 8-bit length. Since the + * length is 8 bits and the size of the key/value buffers is 256, we + * don't need to check for overflow... + */ + + txtlen = *txt++; + + if (!txtlen || (txt + txtlen) > txtend) + break; + + txtnext = txt + txtlen; + + for (ptr = key; txt < txtnext && *txt != '='; txt ++) + *ptr++ = *txt; + *ptr = '\0'; + + if (txt < txtnext && *txt == '=') + { + txt ++; + + if (txt < txtnext) + memcpy(value, txt, txtnext - txt); + value[txtnext - txt] = '\0'; + + DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key, value)); + } + else + { + DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key)); + continue; + } + + if (!_cups_strcasecmp(key, "usb_MFG") || + !_cups_strcasecmp(key, "usb_MANU") || + !_cups_strcasecmp(key, "usb_MANUFACTURER")) + strcpy(make_and_model, value); + else if (!_cups_strcasecmp(key, "usb_MDL") || + !_cups_strcasecmp(key, "usb_MODEL")) + strcpy(model, value); + else if (!_cups_strcasecmp(key, "product") && !strstr(value, "Ghostscript")) + { + if (value[0] == '(') + { + /* + * Strip parenthesis... + */ + + if ((ptr = value + strlen(value) - 1) > value && *ptr == ')') + *ptr = '\0'; + + strcpy(model, value + 1); + } + else + strcpy(model, value); + } + else if (!_cups_strcasecmp(key, "ty")) + { + strcpy(model, value); + + if ((ptr = strchr(model, ',')) != NULL) + *ptr = '\0'; + } + else if (!_cups_strcasecmp(key, "note")) + device->dest.num_options = cupsAddOption("printer-location", value, + device->dest.num_options, + &device->dest.options); + else if (!_cups_strcasecmp(key, "pdl")) + { + /* + * Look for PDF-capable printers; only PDF-capable printers are shown. + */ + + const char *start, *next; /* Pointer into value */ + int have_pdf = 0; /* Have PDF? */ + + for (start = value; start && *start; start = next) + { + if (!_cups_strncasecmp(start, "application/pdf", 15) && + (!start[15] || start[15] == ',')) + { + have_pdf = 1; + break; + } + + if ((next = strchr(start, ',')) != NULL) + next ++; + } + + if (!have_pdf) + device->state = _CUPS_DNSSD_INCOMPATIBLE; + } + else if (!_cups_strcasecmp(key, "printer-type")) + { + /* + * Value is either NNNN or 0xXXXX + */ + + saw_printer_type = 1; + type = strtol(value, NULL, 0); + } + else if (!saw_printer_type) + { + if (!_cups_strcasecmp(key, "air") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_AUTHENTICATED; + else if (!_cups_strcasecmp(key, "bind") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_BIND; + else if (!_cups_strcasecmp(key, "collate") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_COLLATE; + else if (!_cups_strcasecmp(key, "color") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_COLOR; + else if (!_cups_strcasecmp(key, "copies") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_COPIES; + else if (!_cups_strcasecmp(key, "duplex") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_DUPLEX; + else if (!_cups_strcasecmp(key, "fax") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_MFP; + else if (!_cups_strcasecmp(key, "papercustom") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_VARIABLE; + else if (!_cups_strcasecmp(key, "papermax")) + { + if (!_cups_strcasecmp(value, "legal-a4")) + type |= CUPS_PRINTER_SMALL; + else if (!_cups_strcasecmp(value, "isoc-a2")) + type |= CUPS_PRINTER_MEDIUM; + else if (!_cups_strcasecmp(value, ">isoc-a2")) + type |= CUPS_PRINTER_LARGE; + } + else if (!_cups_strcasecmp(key, "punch") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_PUNCH; + else if (!_cups_strcasecmp(key, "scan") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_MFP; + else if (!_cups_strcasecmp(key, "sort") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_SORT; + else if (!_cups_strcasecmp(key, "staple") && + !_cups_strcasecmp(value, "t")) + type |= CUPS_PRINTER_STAPLE; + } + } + + /* + * Save the printer-xxx values... + */ + + device->dest.num_options = cupsAddOption("printer-info", name, + device->dest.num_options, + &device->dest.options); + + if (make_and_model[0]) + { + strlcat(make_and_model, " ", sizeof(make_and_model)); + strlcat(make_and_model, model, sizeof(make_and_model)); + + device->dest.num_options = cupsAddOption("printer-make-and-model", + make_and_model, + device->dest.num_options, + &device->dest.options); + } + else + device->dest.num_options = cupsAddOption("printer-make-and-model", + model, + device->dest.num_options, + &device->dest.options); + + device->type = type; + snprintf(value, sizeof(value), "%u", type); + device->dest.num_options = cupsAddOption("printer-type", value, + device->dest.num_options, + &device->dest.options); + + /* + * Save the URI... + */ + + cups_dnssd_unquote(uriname, device->fullName, sizeof(uriname)); + httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), + !strcmp(device->regtype, "_ipps._tcp") ? "ipps" : "ipp", + NULL, uriname, 0, saw_printer_type ? "/cups" : "/"); + + DEBUG_printf(("6cups_dnssd_query: printer-uri-supported=\"%s\"", uri)); + + device->dest.num_options = cupsAddOption("printer-uri-supported", uri, + device->dest.num_options, + &device->dest.options); + } + else + DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.", + fullName)); +} + + +/* + * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI. + */ + +static const char * /* O - Resolved URI or NULL */ +cups_dnssd_resolve( + cups_dest_t *dest, /* I - Destination */ + const char *uri, /* I - Current printer URI */ + int msec, /* I - Time in milliseconds */ + int *cancel, /* I - Pointer to "cancel" variable */ + cups_dest_cb_t cb, /* I - Callback */ + void *user_data) /* I - User data for callback */ +{ + char tempuri[1024]; /* Temporary URI buffer */ + _cups_dnssd_resolve_t resolve; /* Resolve data */ + + + /* + * Resolve the URI... + */ + + resolve.cancel = cancel; + gettimeofday(&resolve.end_time, NULL); + if (msec > 0) + { + resolve.end_time.tv_sec += msec / 1000; + resolve.end_time.tv_usec += (msec % 1000) * 1000; + + while (resolve.end_time.tv_usec >= 1000000) + { + resolve.end_time.tv_sec ++; + resolve.end_time.tv_usec -= 1000000; + } + } + else + resolve.end_time.tv_sec += 75; + + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, + dest); + + if ((uri = _httpResolveURI(uri, tempuri, sizeof(tempuri), + _HTTP_RESOLVE_FQDN, cups_dnssd_resolve_cb, + &resolve)) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to resolve printer URI."), 1); + + if (cb) + (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, + dest); + + return (NULL); + } + + /* + * Save the resolved URI... + */ + + dest->num_options = cupsAddOption("printer-uri-supported", uri, + dest->num_options, &dest->options); + + return (cupsGetOption("printer-uri-supported", dest->num_options, + dest->options)); +} + + +/* + * 'cups_dnssd_resolve_cb()' - See if we should continue resolving. + */ + +static int /* O - 1 to continue, 0 to stop */ +cups_dnssd_resolve_cb(void *context) /* I - Resolve data */ +{ + _cups_dnssd_resolve_t *resolve = (_cups_dnssd_resolve_t *)context; + /* Resolve data */ + struct timeval curtime; /* Current time */ + + + /* + * If the cancel variable is set, return immediately. + */ + + if (*resolve->cancel) + return (0); + + /* + * Otherwise check the end time... + */ + + gettimeofday(&curtime, NULL); + + return (curtime.tv_sec > resolve->end_time.tv_sec || + (curtime.tv_sec == resolve->end_time.tv_sec && + curtime.tv_usec > resolve->end_time.tv_usec)); +} + + +/* + * 'cups_dnssd_unquote()' - Unquote a name string. + */ + +static void +cups_dnssd_unquote(char *dst, /* I - Destination buffer */ + const char *src, /* I - Source string */ + size_t dstsize) /* I - Size of destination buffer */ +{ + char *dstend = dst + dstsize - 1; /* End of destination buffer */ + + + while (*src && dst < dstend) + { + if (*src == '\\') + { + src ++; + if (isdigit(src[0] & 255) && isdigit(src[1] & 255) && + isdigit(src[2] & 255)) + { + *dst++ = ((((src[0] - '0') * 10) + src[1] - '0') * 10) + src[2] - '0'; + src += 3; + } + else + *dst++ = *src++; + } + else + *dst++ = *src ++; + } + + *dst = '\0'; +} +#endif /* HAVE_DNSSD */ + + +/* + * 'cups_find_dest()' - Find a destination using a binary search. + */ + +static int /* O - Index of match */ +cups_find_dest(const char *name, /* I - Destination name */ + const char *instance, /* I - Instance or NULL */ + int num_dests, /* I - Number of destinations */ + cups_dest_t *dests, /* I - Destinations */ + int prev, /* I - Previous index */ + int *rdiff) /* O - Difference of match */ +{ + int left, /* Low mark for binary search */ + right, /* High mark for binary search */ + current, /* Current index */ + diff; /* Result of comparison */ + cups_dest_t key; /* Search key */ + + + key.name = (char *)name; + key.instance = (char *)instance; + + if (prev >= 0) + { + /* + * Start search on either side of previous... + */ + + if ((diff = cups_compare_dests(&key, dests + prev)) == 0 || + (diff < 0 && prev == 0) || + (diff > 0 && prev == (num_dests - 1))) + { + *rdiff = diff; + return (prev); + } + else if (diff < 0) + { + /* + * Start with previous on right side... + */ + + left = 0; + right = prev; + } + else + { + /* + * Start wih previous on left side... + */ + + left = prev; + right = num_dests - 1; + } + } + else + { + /* + * Start search in the middle... + */ + + left = 0; + right = num_dests - 1; + } + + do + { + current = (left + right) / 2; + diff = cups_compare_dests(&key, dests + current); + + if (diff == 0) + break; + else if (diff < 0) + right = current; + else + left = current; + } + while ((right - left) > 1); + + if (diff != 0) + { + /* + * Check the last 1 or 2 elements... + */ + + if ((diff = cups_compare_dests(&key, dests + left)) <= 0) + current = left; + else + { + diff = cups_compare_dests(&key, dests + right); + current = right; + } + } + + /* + * Return the closest destination and the difference... + */ + + *rdiff = diff; + + return (current); +} + + +/* + * 'cups_get_default()' - Get the default destination from an lpoptions file. + */ + +static char * /* O - Default destination or NULL */ +cups_get_default(const char *filename, /* I - File to read */ + char *namebuf, /* I - Name buffer */ + size_t namesize, /* I - Size of name buffer */ + const char **instance) /* I - Instance */ +{ + cups_file_t *fp; /* lpoptions file */ + char line[8192], /* Line from file */ + *value, /* Value for line */ + *nameptr; /* Pointer into name */ + int linenum; /* Current line */ + + + *namebuf = '\0'; + + if ((fp = cupsFileOpen(filename, "r")) != NULL) + { + linenum = 0; + + while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) + { + if (!_cups_strcasecmp(line, "default") && value) + { + strlcpy(namebuf, value, namesize); + + if ((nameptr = strchr(namebuf, ' ')) != NULL) + *nameptr = '\0'; + if ((nameptr = strchr(namebuf, '\t')) != NULL) + *nameptr = '\0'; + + if ((nameptr = strchr(namebuf, '/')) != NULL) + *nameptr++ = '\0'; + + *instance = nameptr; + break; + } + } + + cupsFileClose(fp); + } + + return (*namebuf ? namebuf : NULL); +} + + +/* + * 'cups_get_dests()' - Get destinations from a file. + */ + +static int /* O - Number of destinations */ +cups_get_dests( + const char *filename, /* I - File to read from */ + const char *match_name, /* I - Destination name we want */ + const char *match_inst, /* I - Instance name we want */ + int user_default_set, /* I - User default printer set? */ + int num_dests, /* I - Number of destinations */ + cups_dest_t **dests) /* IO - Destinations */ +{ + int i; /* Looping var */ + cups_dest_t *dest; /* Current destination */ + cups_file_t *fp; /* File pointer */ + char line[8192], /* Line from file */ + *lineptr, /* Pointer into line */ + *name, /* Name of destination/option */ + *instance; /* Instance of destination */ + int linenum; /* Current line number */ + + + DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", " + "match_inst=\"%s\", user_default_set=%d, num_dests=%d, " + "dests=%p)", filename, match_name, match_inst, + user_default_set, num_dests, dests)); + + /* + * Try to open the file... + */ + + if ((fp = cupsFileOpen(filename, "r")) == NULL) + return (num_dests); + + /* + * Read each printer; each line looks like: + * + * Dest name[/instance] options + * Default name[/instance] options + */ + + linenum = 0; + + while (cupsFileGetConf(fp, line, sizeof(line), &lineptr, &linenum)) + { + /* + * See what type of line it is... + */ + + DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"", + linenum, line, lineptr)); + + if ((_cups_strcasecmp(line, "dest") && _cups_strcasecmp(line, "default")) || !lineptr) + { + DEBUG_puts("9cups_get_dests: Not a dest or default line..."); + continue; + } + + name = lineptr; + + /* + * Search for an instance... + */ + + while (!isspace(*lineptr & 255) && *lineptr && *lineptr != '/') + lineptr ++; + + if (*lineptr == '/') + { + /* + * Found an instance... + */ + + *lineptr++ = '\0'; + instance = lineptr; + + /* + * Search for an instance... + */ + + while (!isspace(*lineptr & 255) && *lineptr) + lineptr ++; + } + else + instance = NULL; + + if (*lineptr) + *lineptr++ = '\0'; + + DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name, + instance)); + + /* + * See if the primary instance of the destination exists; if not, + * ignore this entry and move on... + */ + + if (match_name) + { + if (_cups_strcasecmp(name, match_name) || + (!instance && match_inst) || + (instance && !match_inst) || + (instance && _cups_strcasecmp(instance, match_inst))) + continue; + + dest = *dests; + } + else if (cupsGetDest(name, NULL, num_dests, *dests) == NULL) + { + DEBUG_puts("9cups_get_dests: Not found!"); + continue; + } + else + { + /* + * Add the destination... + */ + + num_dests = cupsAddDest(name, instance, num_dests, dests); + + if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL) + { + /* + * Out of memory! + */ + + DEBUG_puts("9cups_get_dests: Out of memory!"); + break; + } + } + + /* + * Add options until we hit the end of the line... + */ + + dest->num_options = cupsParseOptions(lineptr, dest->num_options, + &(dest->options)); + + /* + * If we found what we were looking for, stop now... + */ + + if (match_name) + break; + + /* + * Set this as default if needed... + */ + + if (!user_default_set && !_cups_strcasecmp(line, "default")) + { + DEBUG_puts("9cups_get_dests: Setting as default..."); + + for (i = 0; i < num_dests; i ++) + (*dests)[i].is_default = 0; + + dest->is_default = 1; + } + } + + /* + * Close the file and return... + */ + + cupsFileClose(fp); + + return (num_dests); +} + + +/* + * 'cups_make_string()' - Make a comma-separated string of values from an IPP + * attribute. + */ + +static char * /* O - New string */ +cups_make_string( + ipp_attribute_t *attr, /* I - Attribute to convert */ + char *buffer, /* I - Buffer */ + size_t bufsize) /* I - Size of buffer */ +{ + int i; /* Looping var */ + char *ptr, /* Pointer into buffer */ + *end, /* Pointer to end of buffer */ + *valptr; /* Pointer into string attribute */ + + + /* + * Return quickly if we have a single string value... + */ + + if (attr->num_values == 1 && + attr->value_tag != IPP_TAG_INTEGER && + attr->value_tag != IPP_TAG_ENUM && + attr->value_tag != IPP_TAG_BOOLEAN && + attr->value_tag != IPP_TAG_RANGE) + return (attr->values[0].string.text); + + /* + * Copy the values to the string, separating with commas and escaping strings + * as needed... + */ + + end = buffer + bufsize - 1; + + for (i = 0, ptr = buffer; i < attr->num_values && ptr < end; i ++) + { + if (i) + *ptr++ = ','; + + switch (attr->value_tag) + { + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + snprintf(ptr, end - ptr + 1, "%d", attr->values[i].integer); + break; + + case IPP_TAG_BOOLEAN : + if (attr->values[i].boolean) + strlcpy(ptr, "true", end - ptr + 1); + else + strlcpy(ptr, "false", end - ptr + 1); + break; + + case IPP_TAG_RANGE : + if (attr->values[i].range.lower == attr->values[i].range.upper) + snprintf(ptr, end - ptr + 1, "%d", attr->values[i].range.lower); + else + snprintf(ptr, end - ptr + 1, "%d-%d", attr->values[i].range.lower, + attr->values[i].range.upper); + break; + + default : + for (valptr = attr->values[i].string.text; + *valptr && ptr < end;) + { + if (strchr(" \t\n\\\'\"", *valptr)) + { + if (ptr >= (end - 1)) + break; + + *ptr++ = '\\'; + } + + *ptr++ = *valptr++; + } + + *ptr = '\0'; + break; + } + + ptr += strlen(ptr); + } + + *ptr = '\0'; + + return (buffer); +} + + +/* + * End of "$Id: dest.c 9568 2011-02-25 06:13:56Z mike $". + */ diff --git a/cups/dir.c b/cups/dir.c new file mode 100644 index 0000000..b98f07c --- /dev/null +++ b/cups/dir.c @@ -0,0 +1,472 @@ +/* + * "$Id: dir.c 7279 2008-01-31 01:50:44Z mike $" + * + * Directory routines for CUPS. + * + * This set of APIs abstracts enumeration of directory entries. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2005 by Easy Software Products, all rights reserved. + * + * 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/". + * + * Contents: + * + * _cups_dir_time() - Convert a FILETIME value to a UNIX time value. + * cupsDirClose() - Close a directory. + * cupsDirOpen() - Open a directory. + * cupsDirRead() - Read the next directory entry. + * cupsDirRewind() - Rewind to the start of the directory. + * cupsDirClose() - Close a directory. + * cupsDirOpen() - Open a directory. + * cupsDirRead() - Read the next directory entry. + * cupsDirRewind() - Rewind to the start of the directory. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "debug-private.h" +#include "dir.h" + + +/* + * Windows implementation... + */ + +#ifdef WIN32 +# include + +/* + * Types and structures... + */ + +struct _cups_dir_s /**** Directory data structure ****/ +{ + char directory[1024]; /* Directory filename */ + HANDLE dir; /* Directory handle */ + cups_dentry_t entry; /* Directory entry */ +}; + + +/* + * '_cups_dir_time()' - Convert a FILETIME value to a UNIX time value. + */ + +time_t /* O - UNIX time */ +_cups_dir_time(FILETIME ft) /* I - File time */ +{ + ULONGLONG val; /* File time in 0.1 usecs */ + + + /* + * Convert file time (1/10 microseconds since Jan 1, 1601) to UNIX + * time (seconds since Jan 1, 1970). There are 11,644,732,800 seconds + * between them... + */ + + val = ft.dwLowDateTime + ((ULONGLONG)ft.dwHighDateTime << 32); + return ((time_t)(val / 10000000 - 11644732800)); +} + + +/* + * 'cupsDirClose()' - Close a directory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +cupsDirClose(cups_dir_t *dp) /* I - Directory pointer */ +{ + /* + * Range check input... + */ + + if (!dp) + return; + + /* + * Close an open directory handle... + */ + + if (dp->dir != INVALID_HANDLE_VALUE) + FindClose(dp->dir); + + /* + * Free memory used... + */ + + free(dp); +} + + +/* + * 'cupsDirOpen()' - Open a directory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_dir_t * /* O - Directory pointer or @code NULL@ if the directory could not be opened. */ +cupsDirOpen(const char *directory) /* I - Directory name */ +{ + cups_dir_t *dp; /* Directory */ + + + /* + * Range check input... + */ + + if (!directory) + return (NULL); + + /* + * Allocate memory for the directory structure... + */ + + dp = (cups_dir_t *)calloc(1, sizeof(cups_dir_t)); + if (!dp) + return (NULL); + + /* + * Copy the directory name for later use... + */ + + dp->dir = INVALID_HANDLE_VALUE; + + strlcpy(dp->directory, directory, sizeof(dp->directory)); + + /* + * Return the new directory structure... + */ + + return (dp); +} + + +/* + * 'cupsDirRead()' - Read the next directory entry. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_dentry_t * /* O - Directory entry or @code NULL@ if there are no more */ +cupsDirRead(cups_dir_t *dp) /* I - Directory pointer */ +{ + WIN32_FIND_DATA entry; /* Directory entry data */ + + + /* + * Range check input... + */ + + if (!dp) + return (NULL); + + /* + * See if we have already started finding files... + */ + + if (dp->dir == INVALID_HANDLE_VALUE) + { + /* + * No, find the first file... + */ + + dp->dir = FindFirstFile(dp->directory, &entry); + if (dp->dir == INVALID_HANDLE_VALUE) + return (NULL); + } + else if (!FindNextFile(dp->dir, &entry)) + return (NULL); + + /* + * Copy the name over and convert the file information... + */ + + strlcpy(dp->entry.filename, entry.cFileName, sizeof(dp->entry.filename)); + + if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + dp->entry.fileinfo.st_mode = 0755 | S_IFDIR; + else + dp->entry.fileinfo.st_mode = 0644; + + dp->entry.fileinfo.st_atime = _cups_dir_time(entry.ftLastAccessTime); + dp->entry.fileinfo.st_ctime = _cups_dir_time(entry.ftCreationTime); + dp->entry.fileinfo.st_mtime = _cups_dir_time(entry.ftLastWriteTime); + dp->entry.fileinfo.st_size = entry.nFileSizeLow + ((unsigned long long)entry.nFileSizeHigh << 32); + + /* + * Return the entry... + */ + + return (&(dp->entry)); +} + + +/* + * 'cupsDirRewind()' - Rewind to the start of the directory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +cupsDirRewind(cups_dir_t *dp) /* I - Directory pointer */ +{ + /* + * Range check input... + */ + + if (!dp) + return; + + /* + * Close an open directory handle... + */ + + if (dp->dir != INVALID_HANDLE_VALUE) + { + FindClose(dp->dir); + dp->dir = INVALID_HANDLE_VALUE; + } +} + + +#else + +/* + * POSIX implementation... + */ + +# include +# include + + +/* + * Types and structures... + */ + +struct _cups_dir_s /**** Directory data structure ****/ +{ + char directory[1024]; /* Directory filename */ + DIR *dir; /* Directory file */ + cups_dentry_t entry; /* Directory entry */ +}; + + +/* + * 'cupsDirClose()' - Close a directory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +cupsDirClose(cups_dir_t *dp) /* I - Directory pointer */ +{ + DEBUG_printf(("cupsDirClose(dp=%p)", dp)); + + /* + * Range check input... + */ + + if (!dp) + return; + + /* + * Close the directory and free memory... + */ + + closedir(dp->dir); + free(dp); +} + + +/* + * 'cupsDirOpen()' - Open a directory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_dir_t * /* O - Directory pointer or @code NULL@ if the directory could not be opened. */ +cupsDirOpen(const char *directory) /* I - Directory name */ +{ + cups_dir_t *dp; /* Directory */ + + + DEBUG_printf(("cupsDirOpen(directory=\"%s\")", directory)); + + /* + * Range check input... + */ + + if (!directory) + return (NULL); + + /* + * Allocate memory for the directory structure... + */ + + dp = (cups_dir_t *)calloc(1, sizeof(cups_dir_t)); + if (!dp) + return (NULL); + + /* + * Open the directory... + */ + + dp->dir = opendir(directory); + if (!dp->dir) + { + free(dp); + return (NULL); + } + + /* + * Copy the directory name for later use... + */ + + strlcpy(dp->directory, directory, sizeof(dp->directory)); + + /* + * Return the new directory structure... + */ + + return (dp); +} + + +/* + * 'cupsDirRead()' - Read the next directory entry. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_dentry_t * /* O - Directory entry or @code NULL@ when there are no more */ +cupsDirRead(cups_dir_t *dp) /* I - Directory pointer */ +{ + struct dirent *entry; /* Pointer to entry */ + char filename[1024]; /* Full filename */ +# ifdef HAVE_PTHREAD_H + char buffer[sizeof(struct dirent) + 1024]; + /* Directory entry buffer */ +# endif /* HAVE_PTHREAD_H */ + + + DEBUG_printf(("2cupsDirRead(dp=%p)", dp)); + + /* + * Range check input... + */ + + if (!dp) + return (NULL); + + /* + * Try reading an entry that is not "." or ".."... + */ + + for (;;) + { +# ifdef HAVE_PTHREAD_H + /* + * Read the next entry using the reentrant version of readdir... + */ + + if (readdir_r(dp->dir, (struct dirent *)buffer, &entry)) + { + DEBUG_printf(("3cupsDirRead: readdir_r() failed - %s\n", strerror(errno))); + return (NULL); + } + + if (!entry) + { + DEBUG_puts("3cupsDirRead: readdir_r() returned a NULL pointer!"); + return (NULL); + } + + DEBUG_printf(("4cupsDirRead: readdir_r() returned \"%s\"...", + entry->d_name)); + +# else + /* + * Read the next entry using the original version of readdir... + */ + + if ((entry = readdir(dp->dir)) == NULL) + { + DEBUG_puts("3cupsDirRead: readdir() returned a NULL pointer!"); + return (NULL); + } + + DEBUG_printf(("4cupsDirRead: readdir() returned \"%s\"...", entry->d_name)); + +# endif /* HAVE_PTHREAD_H */ + + /* + * Skip "." and ".."... + */ + + if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) + continue; + + /* + * Copy the name over and get the file information... + */ + + strlcpy(dp->entry.filename, entry->d_name, sizeof(dp->entry.filename)); + + snprintf(filename, sizeof(filename), "%s/%s", dp->directory, entry->d_name); + + if (stat(filename, &(dp->entry.fileinfo))) + { + DEBUG_printf(("3cupsDirRead: stat() failed for \"%s\" - %s...", filename, + strerror(errno))); + continue; + } + + /* + * Return the entry... + */ + + return (&(dp->entry)); + } +} + + +/* + * 'cupsDirRewind()' - Rewind to the start of the directory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +cupsDirRewind(cups_dir_t *dp) /* I - Directory pointer */ +{ + DEBUG_printf(("cupsDirRewind(dp=%p)", dp)); + + /* + * Range check input... + */ + + if (!dp) + return; + + /* + * Rewind the directory... + */ + + rewinddir(dp->dir); +} + + +#endif /* WIN32 */ + +/* + * End of "$Id: dir.c 7279 2008-01-31 01:50:44Z mike $". + */ diff --git a/cups/dir.h b/cups/dir.h new file mode 100644 index 0000000..362ea20 --- /dev/null +++ b/cups/dir.h @@ -0,0 +1,69 @@ +/* + * "$Id: dir.h 7026 2007-10-19 00:57:45Z mike $" + * + * Public directory definitions for CUPS. + * + * This set of APIs abstracts enumeration of directory entries. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products, all rights reserved. + * + * 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/". + */ + +#ifndef _CUPS_DIR_H_ +# define _CUPS_DIR_H_ + + +/* + * Include necessary headers... + */ + +# include "versioning.h" +# include + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Data types... + */ + +typedef struct _cups_dir_s cups_dir_t; /**** Directory type ****/ + +typedef struct cups_dentry_s /**** Directory entry type ****/ +{ + char filename[260]; /* File name */ + struct stat fileinfo; /* File information */ +} cups_dentry_t; + + +/* + * Prototypes... + */ + +extern void cupsDirClose(cups_dir_t *dp) _CUPS_API_1_2; +extern cups_dir_t *cupsDirOpen(const char *directory) _CUPS_API_1_2; +extern cups_dentry_t *cupsDirRead(cups_dir_t *dp) _CUPS_API_1_2; +extern void cupsDirRewind(cups_dir_t *dp) _CUPS_API_1_2; + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_DIR_H_ */ + +/* + * End of "$Id: dir.h 7026 2007-10-19 00:57:45Z mike $". + */ diff --git a/cups/emit.c b/cups/emit.c new file mode 100644 index 0000000..650e847 --- /dev/null +++ b/cups/emit.c @@ -0,0 +1,1229 @@ +/* + * "$Id: emit.c 7863 2008-08-26 03:39:59Z mike $" + * + * PPD code emission routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * ppdCollect() - Collect all marked options that reside in the + * specified section. + * ppdCollect2() - Collect all marked options that reside in the + * specified section and minimum order. + * ppdEmit() - Emit code for marked options to a file. + * ppdEmitAfterOrder() - Emit a subset of the code for marked options to a + * file. + * ppdEmitFd() - Emit code for marked options to a file. + * ppdEmitJCL() - Emit code for JCL options to a file. + * ppdEmitJCLEnd() - Emit JCLEnd code to a file. + * ppdEmitString() - Get a string containing the code for marked + * options. + * ppd_compare_cparams() - Compare the order of two custom parameters. + * ppd_handle_media() - Handle media selection... + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#if defined(WIN32) || defined(__EMX__) +# include +#else +# include +#endif /* WIN32 || __EMX__ */ + + +/* + * Local functions... + */ + +static int ppd_compare_cparams(ppd_cparam_t *a, ppd_cparam_t *b); +static void ppd_handle_media(ppd_file_t *ppd); + + +/* + * Local globals... + */ + +static const char ppd_custom_code[] = + "pop pop pop\n" + "<>setpagedevice\n"; + + +/* + * 'ppdCollect()' - Collect all marked options that reside in the specified + * section. + * + * The choices array should be freed using @code free@ when you are + * finished with it. + */ + +int /* O - Number of options marked */ +ppdCollect(ppd_file_t *ppd, /* I - PPD file data */ + ppd_section_t section, /* I - Section to collect */ + ppd_choice_t ***choices) /* O - Pointers to choices */ +{ + return (ppdCollect2(ppd, section, 0.0, choices)); +} + + +/* + * 'ppdCollect2()' - Collect all marked options that reside in the + * specified section and minimum order. + * + * The choices array should be freed using @code free@ when you are + * finished with it. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Number of options marked */ +ppdCollect2(ppd_file_t *ppd, /* I - PPD file data */ + ppd_section_t section, /* I - Section to collect */ + float min_order, /* I - Minimum OrderDependency value */ + ppd_choice_t ***choices) /* O - Pointers to choices */ +{ + ppd_choice_t *c; /* Current choice */ + ppd_section_t csection; /* Current section */ + float corder; /* Current OrderDependency value */ + int count; /* Number of choices collected */ + ppd_choice_t **collect; /* Collected choices */ + float *orders; /* Collected order values */ + + + DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)", + ppd, section, min_order, choices)); + + if (!ppd || !choices) + { + if (choices) + *choices = NULL; + + return (0); + } + + /* + * Allocate memory for up to N selected choices... + */ + + count = 0; + if ((collect = calloc(sizeof(ppd_choice_t *), + cupsArrayCount(ppd->marked))) == NULL) + { + *choices = NULL; + return (0); + } + + if ((orders = calloc(sizeof(float), cupsArrayCount(ppd->marked))) == NULL) + { + *choices = NULL; + free(collect); + return (0); + } + + /* + * Loop through all options and add choices as needed... + */ + + for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked); + c; + c = (ppd_choice_t *)cupsArrayNext(ppd->marked)) + { + csection = c->option->section; + corder = c->option->order; + + if (!strcmp(c->choice, "Custom")) + { + ppd_attr_t *attr; /* NonUIOrderDependency value */ + float aorder; /* Order value */ + char asection[17], /* Section name */ + amain[PPD_MAX_NAME + 1], + aoption[PPD_MAX_NAME]; + /* *CustomFoo and True */ + + + for (attr = ppdFindAttr(ppd, "NonUIOrderDependency", NULL); + attr; + attr = ppdFindNextAttr(ppd, "NonUIOrderDependency", NULL)) + if (attr->value && + sscanf(attr->value, "%f%16s%41s%40s", &aorder, asection, amain, + aoption) == 4 && + !strncmp(amain, "*Custom", 7) && + !strcmp(amain + 7, c->option->keyword) && !strcmp(aoption, "True")) + { + /* + * Use this NonUIOrderDependency... + */ + + corder = aorder; + + if (!strcmp(asection, "DocumentSetup")) + csection = PPD_ORDER_DOCUMENT; + else if (!strcmp(asection, "ExitServer")) + csection = PPD_ORDER_EXIT; + else if (!strcmp(asection, "JCLSetup")) + csection = PPD_ORDER_JCL; + else if (!strcmp(asection, "PageSetup")) + csection = PPD_ORDER_PAGE; + else if (!strcmp(asection, "Prolog")) + csection = PPD_ORDER_PROLOG; + else + csection = PPD_ORDER_ANY; + + break; + } + } + + if (csection == section && corder >= min_order) + { + collect[count] = c; + orders[count] = corder; + count ++; + } + } + + /* + * If we have more than 1 marked choice, sort them... + */ + + if (count > 1) + { + int i, j; /* Looping vars */ + + for (i = 0; i < (count - 1); i ++) + for (j = i + 1; j < count; j ++) + if (orders[i] > orders[j]) + { + c = collect[i]; + corder = orders[i]; + collect[i] = collect[j]; + orders[i] = orders[j]; + collect[j] = c; + orders[j] = corder; + } + } + + free(orders); + + DEBUG_printf(("2ppdCollect2: %d marked choices...", count)); + + /* + * Return the array and number of choices; if 0, free the array since + * it isn't needed. + */ + + if (count > 0) + { + *choices = collect; + return (count); + } + else + { + *choices = NULL; + free(collect); + return (0); + } +} + + +/* + * 'ppdEmit()' - Emit code for marked options to a file. + */ + +int /* O - 0 on success, -1 on failure */ +ppdEmit(ppd_file_t *ppd, /* I - PPD file record */ + FILE *fp, /* I - File to write to */ + ppd_section_t section) /* I - Section to write */ +{ + return (ppdEmitAfterOrder(ppd, fp, section, 0, 0.0)); +} + + +/* + * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file. + * + * When "limit" is non-zero, this function only emits options whose + * OrderDependency value is greater than or equal to "min_order". + * + * When "limit" is zero, this function is identical to ppdEmit(). + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on failure */ +ppdEmitAfterOrder( + ppd_file_t *ppd, /* I - PPD file record */ + FILE *fp, /* I - File to write to */ + ppd_section_t section, /* I - Section to write */ + int limit, /* I - Non-zero to use min_order */ + float min_order) /* I - Lowest OrderDependency */ +{ + char *buffer; /* Option code */ + int status; /* Return status */ + + + /* + * Range check input... + */ + + if (!ppd || !fp) + return (-1); + + /* + * Get the string... + */ + + buffer = ppdEmitString(ppd, section, limit ? min_order : 0.0f); + + /* + * Write it as needed and return... + */ + + if (buffer) + { + status = fputs(buffer, fp) < 0 ? -1 : 0; + + free(buffer); + } + else + status = 0; + + return (status); +} + + +/* + * 'ppdEmitFd()' - Emit code for marked options to a file. + */ + +int /* O - 0 on success, -1 on failure */ +ppdEmitFd(ppd_file_t *ppd, /* I - PPD file record */ + int fd, /* I - File to write to */ + ppd_section_t section) /* I - Section to write */ +{ + char *buffer, /* Option code */ + *bufptr; /* Pointer into code */ + size_t buflength; /* Length of option code */ + ssize_t bytes; /* Bytes written */ + int status; /* Return status */ + + + /* + * Range check input... + */ + + if (!ppd || fd < 0) + return (-1); + + /* + * Get the string... + */ + + buffer = ppdEmitString(ppd, section, 0.0); + + /* + * Write it as needed and return... + */ + + if (buffer) + { + buflength = strlen(buffer); + bufptr = buffer; + bytes = 0; + + while (buflength > 0) + { +#ifdef WIN32 + if ((bytes = (ssize_t)write(fd, bufptr, (unsigned)buflength)) < 0) +#else + if ((bytes = write(fd, bufptr, buflength)) < 0) +#endif /* WIN32 */ + { + if (errno == EAGAIN || errno == EINTR) + continue; + + break; + } + + buflength -= bytes; + bufptr += bytes; + } + + status = bytes < 0 ? -1 : 0; + + free(buffer); + } + else + status = 0; + + return (status); +} + + +/* + * 'ppdEmitJCL()' - Emit code for JCL options to a file. + */ + +int /* O - 0 on success, -1 on failure */ +ppdEmitJCL(ppd_file_t *ppd, /* I - PPD file record */ + FILE *fp, /* I - File to write to */ + int job_id, /* I - Job ID */ + const char *user, /* I - Username */ + const char *title) /* I - Title */ +{ + char *ptr; /* Pointer into JCL string */ + char temp[65], /* Local title string */ + displaymsg[33]; /* Local display string */ + + + /* + * Range check the input... + */ + + if (!ppd || !ppd->jcl_begin || !ppd->jcl_ps) + return (0); + + /* + * See if the printer supports HP PJL... + */ + + if (!strncmp(ppd->jcl_begin, "\033%-12345X@", 10)) + { + /* + * This printer uses HP PJL commands for output; filter the output + * so that we only have a single "@PJL JOB" command in the header... + * + * To avoid bugs in the PJL implementation of certain vendors' products + * (Xerox in particular), we add a dummy "@PJL" command at the beginning + * of the PJL commands to initialize PJL processing. + */ + + ppd_attr_t *charset; /* PJL charset */ + ppd_attr_t *display; /* PJL display command */ + + + if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL) + { + if (!charset->value || _cups_strcasecmp(charset->value, "UTF-8")) + charset = NULL; + } + + if ((display = ppdFindAttr(ppd, "cupsPJLDisplay", NULL)) != NULL) + { + if (!display->value) + display = NULL; + } + + fputs("\033%-12345X@PJL\n", fp); + for (ptr = ppd->jcl_begin + 9; *ptr;) + if (!strncmp(ptr, "@PJL JOB", 8)) + { + /* + * Skip job command... + */ + + for (;*ptr; ptr ++) + if (*ptr == '\n') + break; + + if (*ptr) + ptr ++; + } + else + { + /* + * Copy line... + */ + + for (;*ptr; ptr ++) + { + putc(*ptr, fp); + if (*ptr == '\n') + break; + } + + if (*ptr) + ptr ++; + } + + /* + * Clean up the job title... + */ + + if ((ptr = strrchr(title, '/')) != NULL) + { + /* + * Only show basename of file path... + */ + + title = ptr + 1; + } + + if (!strncmp(title, "smbprn.", 7)) + { + /* + * Skip leading smbprn.######## from Samba jobs... + */ + + for (title += 7; *title && isdigit(*title & 255); title ++); + while (_cups_isspace(*title)) + title ++; + + if ((ptr = strstr(title, " - ")) != NULL) + { + /* + * Skip application name in "Some Application - Title of job"... + */ + + title = ptr + 3; + } + } + + /* + * Replace double quotes with single quotes and UTF-8 characters with + * question marks so that the title does not cause a PJL syntax error. + */ + + strlcpy(temp, title, sizeof(temp)); + + for (ptr = temp; *ptr; ptr ++) + if (*ptr == '\"') + *ptr = '\''; + else if (!charset && (*ptr & 128)) + *ptr = '?'; + + /* + * CUPS STR #3125: Long PJL JOB NAME causes problems with some printers + * + * Generate the display message, truncating at 32 characters + nul to avoid + * issues with some printer's PJL implementations... + */ + + snprintf(displaymsg, sizeof(displaymsg), "%d %s %s", job_id, user, temp); + + /* + * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode... + */ + + if (display && strcmp(display->value, "job")) + fprintf(fp, "@PJL JOB NAME = \"%s\"\n", temp); + else if (display && !strcmp(display->value, "rdymsg")) + fprintf(fp, "@PJL RDYMSG DISPLAY = \"%s\"\n", displaymsg); + else + fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%s\"\n", temp, + displaymsg); + + /* + * Replace double quotes with single quotes and UTF-8 characters with + * question marks so that the user does not cause a PJL syntax error. + */ + + strlcpy(temp, user, sizeof(temp)); + + for (ptr = temp; *ptr; ptr ++) + if (*ptr == '\"') + *ptr = '\''; + else if (!charset && (*ptr & 128)) + *ptr = '?'; + + fprintf(fp, "@PJL SET USERNAME = \"%s\"\n", temp); + } + else + fputs(ppd->jcl_begin, fp); + + ppdEmit(ppd, fp, PPD_ORDER_JCL); + fputs(ppd->jcl_ps, fp); + + return (0); +} + + +/* + * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on failure */ +ppdEmitJCLEnd(ppd_file_t *ppd, /* I - PPD file record */ + FILE *fp) /* I - File to write to */ +{ + /* + * Range check the input... + */ + + if (!ppd) + return (0); + + if (!ppd->jcl_end) + { + if (ppd->num_filters == 0) + putc(0x04, fp); + + return (0); + } + + /* + * See if the printer supports HP PJL... + */ + + if (!strncmp(ppd->jcl_end, "\033%-12345X@", 10)) + { + /* + * This printer uses HP PJL commands for output; filter the output + * so that we only have a single "@PJL JOB" command in the header... + * + * To avoid bugs in the PJL implementation of certain vendors' products + * (Xerox in particular), we add a dummy "@PJL" command at the beginning + * of the PJL commands to initialize PJL processing. + */ + + fputs("\033%-12345X@PJL\n", fp); + fputs("@PJL RDYMSG DISPLAY = \"\"\n", fp); + fputs(ppd->jcl_end + 9, fp); + } + else + fputs(ppd->jcl_end, fp); + + return (0); +} + + +/* + * 'ppdEmitString()' - Get a string containing the code for marked options. + * + * When "min_order" is greater than zero, this function only includes options + * whose OrderDependency value is greater than or equal to "min_order". + * Otherwise, all options in the specified section are included in the + * returned string. + * + * The return string is allocated on the heap and should be freed using + * @code free@ when you are done with it. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - String containing option code or @code NULL@ if there is no option code */ +ppdEmitString(ppd_file_t *ppd, /* I - PPD file record */ + ppd_section_t section, /* I - Section to write */ + float min_order) /* I - Lowest OrderDependency */ +{ + int i, j, /* Looping vars */ + count; /* Number of choices */ + ppd_choice_t **choices; /* Choices */ + ppd_size_t *size; /* Custom page size */ + ppd_coption_t *coption; /* Custom option */ + ppd_cparam_t *cparam; /* Custom parameter */ + size_t bufsize; /* Size of string buffer needed */ + char *buffer, /* String buffer */ + *bufptr, /* Pointer into buffer */ + *bufend; /* End of buffer */ + struct lconv *loc; /* Locale data */ + + + DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)", + ppd, section, min_order)); + + /* + * Range check input... + */ + + if (!ppd) + return (NULL); + + /* + * Use PageSize or PageRegion as required... + */ + + ppd_handle_media(ppd); + + /* + * Collect the options we need to emit... + */ + + if ((count = ppdCollect2(ppd, section, min_order, &choices)) == 0) + return (NULL); + + /* + * Count the number of bytes that are required to hold all of the + * option code... + */ + + for (i = 0, bufsize = 1; i < count; i ++) + { + if (section == PPD_ORDER_JCL) + { + if (!_cups_strcasecmp(choices[i]->choice, "Custom") && + (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword)) + != NULL) + { + /* + * Add space to account for custom parameter substitution... + */ + + for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) + { + switch (cparam->type) + { + case PPD_CUSTOM_CURVE : + case PPD_CUSTOM_INVCURVE : + case PPD_CUSTOM_POINTS : + case PPD_CUSTOM_REAL : + case PPD_CUSTOM_INT : + bufsize += 10; + break; + + case PPD_CUSTOM_PASSCODE : + case PPD_CUSTOM_PASSWORD : + case PPD_CUSTOM_STRING : + if (cparam->current.custom_string) + bufsize += strlen(cparam->current.custom_string); + break; + } + } + } + } + else if (section != PPD_ORDER_EXIT) + { + bufsize += 3; /* [{\n */ + + if ((!_cups_strcasecmp(choices[i]->option->keyword, "PageSize") || + !_cups_strcasecmp(choices[i]->option->keyword, "PageRegion")) && + !_cups_strcasecmp(choices[i]->choice, "Custom")) + { + DEBUG_puts("2ppdEmitString: Custom size set!"); + + bufsize += 37; /* %%BeginFeature: *CustomPageSize True\n */ + bufsize += 50; /* Five 9-digit numbers + newline */ + } + else if (!_cups_strcasecmp(choices[i]->choice, "Custom") && + (coption = ppdFindCustomOption(ppd, + choices[i]->option->keyword)) + != NULL) + { + bufsize += 23 + strlen(choices[i]->option->keyword) + 6; + /* %%BeginFeature: *Customkeyword True\n */ + + + for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) + { + switch (cparam->type) + { + case PPD_CUSTOM_CURVE : + case PPD_CUSTOM_INVCURVE : + case PPD_CUSTOM_POINTS : + case PPD_CUSTOM_REAL : + case PPD_CUSTOM_INT : + bufsize += 10; + break; + + case PPD_CUSTOM_PASSCODE : + case PPD_CUSTOM_PASSWORD : + case PPD_CUSTOM_STRING : + bufsize += 3; + if (cparam->current.custom_string) + bufsize += 4 * strlen(cparam->current.custom_string); + break; + } + } + } + else + bufsize += 17 + strlen(choices[i]->option->keyword) + 1 + + strlen(choices[i]->choice) + 1; + /* %%BeginFeature: *keyword choice\n */ + + bufsize += 13; /* %%EndFeature\n */ + bufsize += 22; /* } stopped cleartomark\n */ + } + + if (choices[i]->code) + bufsize += strlen(choices[i]->code) + 1; + else + bufsize += strlen(ppd_custom_code); + } + + /* + * Allocate memory... + */ + + DEBUG_printf(("2ppdEmitString: Allocating %d bytes for string...", + (int)bufsize)); + + if ((buffer = calloc(1, bufsize)) == NULL) + { + free(choices); + return (NULL); + } + + bufend = buffer + bufsize - 1; + loc = localeconv(); + + /* + * Copy the option code to the buffer... + */ + + for (i = 0, bufptr = buffer; i < count; i ++, bufptr += strlen(bufptr)) + if (section == PPD_ORDER_JCL) + { + if (!_cups_strcasecmp(choices[i]->choice, "Custom") && + choices[i]->code && + (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword)) + != NULL) + { + /* + * Handle substitutions in custom JCL options... + */ + + char *cptr; /* Pointer into code */ + int pnum; /* Parameter number */ + + + for (cptr = choices[i]->code; *cptr && bufptr < bufend;) + { + if (*cptr == '\\') + { + cptr ++; + + if (isdigit(*cptr & 255)) + { + /* + * Substitute parameter... + */ + + pnum = *cptr++ - '0'; + while (isdigit(*cptr & 255)) + pnum = pnum * 10 + *cptr++ - '0'; + + for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) + if (cparam->order == pnum) + break; + + if (cparam) + { + switch (cparam->type) + { + case PPD_CUSTOM_CURVE : + case PPD_CUSTOM_INVCURVE : + case PPD_CUSTOM_POINTS : + case PPD_CUSTOM_REAL : + bufptr = _cupsStrFormatd(bufptr, bufend, + cparam->current.custom_real, + loc); + break; + + case PPD_CUSTOM_INT : + snprintf(bufptr, bufend - bufptr, "%d", + cparam->current.custom_int); + bufptr += strlen(bufptr); + break; + + case PPD_CUSTOM_PASSCODE : + case PPD_CUSTOM_PASSWORD : + case PPD_CUSTOM_STRING : + if (cparam->current.custom_string) + { + strlcpy(bufptr, cparam->current.custom_string, + bufend - bufptr); + bufptr += strlen(bufptr); + } + break; + } + } + } + else if (*cptr) + *bufptr++ = *cptr++; + } + else + *bufptr++ = *cptr++; + } + } + else + { + /* + * Otherwise just copy the option code directly... + */ + + strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1); + bufptr += strlen(bufptr); + } + } + else if (section != PPD_ORDER_EXIT) + { + /* + * Add wrapper commands to prevent printer errors for unsupported + * options... + */ + + strlcpy(bufptr, "[{\n", bufend - bufptr + 1); + bufptr += 3; + + /* + * Send DSC comments with option... + */ + + DEBUG_printf(("2ppdEmitString: Adding code for %s=%s...", + choices[i]->option->keyword, choices[i]->choice)); + + if ((!_cups_strcasecmp(choices[i]->option->keyword, "PageSize") || + !_cups_strcasecmp(choices[i]->option->keyword, "PageRegion")) && + !_cups_strcasecmp(choices[i]->choice, "Custom")) + { + /* + * Variable size; write out standard size options, using the + * parameter positions defined in the PPD file... + */ + + ppd_attr_t *attr; /* PPD attribute */ + int pos, /* Position of custom value */ + orientation; /* Orientation to use */ + float values[5]; /* Values for custom command */ + + + strlcpy(bufptr, "%%BeginFeature: *CustomPageSize True\n", + bufend - bufptr + 1); + bufptr += 37; + + size = ppdPageSize(ppd, "Custom"); + + memset(values, 0, sizeof(values)); + + if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Width")) != NULL) + { + pos = atoi(attr->value) - 1; + + if (pos < 0 || pos > 4) + pos = 0; + } + else + pos = 0; + + values[pos] = size->width; + + if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Height")) != NULL) + { + pos = atoi(attr->value) - 1; + + if (pos < 0 || pos > 4) + pos = 1; + } + else + pos = 1; + + values[pos] = size->length; + + /* + * According to the Adobe PPD specification, an orientation of 1 + * will produce a print that comes out upside-down with the X + * axis perpendicular to the direction of feed, which is exactly + * what we want to be consistent with non-PS printers. + * + * We could also use an orientation of 3 to produce output that + * comes out rightside-up (this is the default for many large format + * printer PPDs), however for consistency we will stick with the + * value 1. + * + * If we wanted to get fancy, we could use orientations of 0 or + * 2 and swap the width and length, however we don't want to get + * fancy, we just want it to work consistently. + * + * The orientation value is range limited by the Orientation + * parameter definition, so certain non-PS printer drivers that + * only support an Orientation of 0 will get the value 0 as + * expected. + */ + + orientation = 1; + + if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", + "Orientation")) != NULL) + { + int min_orient, max_orient; /* Minimum and maximum orientations */ + + + if (sscanf(attr->value, "%d%*s%d%d", &pos, &min_orient, + &max_orient) != 3) + pos = 4; + else + { + pos --; + + if (pos < 0 || pos > 4) + pos = 4; + + if (orientation > max_orient) + orientation = max_orient; + else if (orientation < min_orient) + orientation = min_orient; + } + } + else + pos = 4; + + values[pos] = (float)orientation; + + for (pos = 0; pos < 5; pos ++) + { + bufptr = _cupsStrFormatd(bufptr, bufend, values[pos], loc); + *bufptr++ = '\n'; + } + + if (!choices[i]->code) + { + /* + * This can happen with certain buggy PPD files that don't include + * a CustomPageSize command sequence... We just use a generic + * Level 2 command sequence... + */ + + strlcpy(bufptr, ppd_custom_code, bufend - bufptr + 1); + bufptr += strlen(bufptr); + } + } + else if (!_cups_strcasecmp(choices[i]->choice, "Custom") && + (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword)) + != NULL) + { + /* + * Custom option... + */ + + const char *s; /* Pointer into string value */ + cups_array_t *params; /* Parameters in the correct output order */ + + + params = cupsArrayNew((cups_array_func_t)ppd_compare_cparams, NULL); + + for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) + cupsArrayAdd(params, cparam); + + snprintf(bufptr, bufend - bufptr + 1, + "%%%%BeginFeature: *Custom%s True\n", coption->keyword); + bufptr += strlen(bufptr); + + for (cparam = (ppd_cparam_t *)cupsArrayFirst(params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(params)) + { + switch (cparam->type) + { + case PPD_CUSTOM_CURVE : + case PPD_CUSTOM_INVCURVE : + case PPD_CUSTOM_POINTS : + case PPD_CUSTOM_REAL : + bufptr = _cupsStrFormatd(bufptr, bufend, + cparam->current.custom_real, loc); + *bufptr++ = '\n'; + break; + + case PPD_CUSTOM_INT : + snprintf(bufptr, bufend - bufptr + 1, "%d\n", + cparam->current.custom_int); + bufptr += strlen(bufptr); + break; + + case PPD_CUSTOM_PASSCODE : + case PPD_CUSTOM_PASSWORD : + case PPD_CUSTOM_STRING : + *bufptr++ = '('; + + if (cparam->current.custom_string) + { + for (s = cparam->current.custom_string; *s; s ++) + { + if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127) + { + snprintf(bufptr, bufend - bufptr + 1, "\\%03o", *s & 255); + bufptr += strlen(bufptr); + } + else + *bufptr++ = *s; + } + } + + *bufptr++ = ')'; + *bufptr++ = '\n'; + break; + } + } + + cupsArrayDelete(params); + } + else + { + snprintf(bufptr, bufend - bufptr + 1, "%%%%BeginFeature: *%s %s\n", + choices[i]->option->keyword, choices[i]->choice); + bufptr += strlen(bufptr); + } + + if (choices[i]->code && choices[i]->code[0]) + { + j = (int)strlen(choices[i]->code); + memcpy(bufptr, choices[i]->code, j); + bufptr += j; + + if (choices[i]->code[j - 1] != '\n') + *bufptr++ = '\n'; + } + + strlcpy(bufptr, "%%EndFeature\n" + "} stopped cleartomark\n", bufend - bufptr + 1); + bufptr += strlen(bufptr); + + DEBUG_printf(("2ppdEmitString: Offset in string is %d...", + (int)(bufptr - buffer))); + } + else + { + strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1); + bufptr += strlen(bufptr); + } + + /* + * Nul-terminate, free, and return... + */ + + *bufptr = '\0'; + + free(choices); + + return (buffer); +} + + +/* + * 'ppd_compare_cparams()' - Compare the order of two custom parameters. + */ + +static int /* O - Result of comparison */ +ppd_compare_cparams(ppd_cparam_t *a, /* I - First parameter */ + ppd_cparam_t *b) /* I - Second parameter */ +{ + return (a->order - b->order); +} + + +/* + * 'ppd_handle_media()' - Handle media selection... + */ + +static void +ppd_handle_media(ppd_file_t *ppd) /* I - PPD file */ +{ + ppd_choice_t *manual_feed, /* ManualFeed choice, if any */ + *input_slot; /* InputSlot choice, if any */ + ppd_size_t *size; /* Current media size */ + ppd_attr_t *rpr; /* RequiresPageRegion value */ + + + /* + * This function determines what page size code to use, if any, for the + * current media size, InputSlot, and ManualFeed selections. + * + * We use the PageSize code if: + * + * 1. A custom media size is selected. + * 2. ManualFeed and InputSlot are not selected (or do not exist). + * 3. ManualFeed is selected but is False and InputSlot is not selected or + * the selection has no code - the latter check done to support "auto" or + * "printer default" InputSlot options. + * + * We use the PageRegion code if: + * + * 4. RequiresPageRegion does not exist and the PPD contains cupsFilter + * keywords, indicating this is a CUPS-based driver. + * 5. RequiresPageRegion exists for the selected InputSlot (or "All" for any + * InputSlot or ManualFeed selection) and is True. + * + * If none of the 5 conditions are true, no page size code is used and we + * unmark any existing PageSize or PageRegion choices. + */ + + if ((size = ppdPageSize(ppd, NULL)) == NULL) + return; + + manual_feed = ppdFindMarkedChoice(ppd, "ManualFeed"); + input_slot = ppdFindMarkedChoice(ppd, "InputSlot"); + + if (input_slot != NULL) + rpr = ppdFindAttr(ppd, "RequiresPageRegion", input_slot->choice); + else + rpr = NULL; + + if (!rpr) + rpr = ppdFindAttr(ppd, "RequiresPageRegion", "All"); + + if (!_cups_strcasecmp(size->name, "Custom") || + (!manual_feed && !input_slot) || + (manual_feed && !_cups_strcasecmp(manual_feed->choice, "False") && + (!input_slot || (input_slot->code && !input_slot->code[0]))) || + (!rpr && ppd->num_filters > 0)) + { + /* + * Use PageSize code... + */ + + ppdMarkOption(ppd, "PageSize", size->name); + } + else if (rpr && rpr->value && !_cups_strcasecmp(rpr->value, "True")) + { + /* + * Use PageRegion code... + */ + + ppdMarkOption(ppd, "PageRegion", size->name); + } + else + { + /* + * Do not use PageSize or PageRegion code... + */ + + ppd_choice_t *page; /* PageSize/Region choice, if any */ + + if ((page = ppdFindMarkedChoice(ppd, "PageSize")) != NULL) + { + /* + * Unmark PageSize... + */ + + page->marked = 0; + cupsArrayRemove(ppd->marked, page); + } + + if ((page = ppdFindMarkedChoice(ppd, "PageRegion")) != NULL) + { + /* + * Unmark PageRegion... + */ + + page->marked = 0; + cupsArrayRemove(ppd->marked, page); + } + } +} + + +/* + * End of "$Id: emit.c 7863 2008-08-26 03:39:59Z mike $". + */ diff --git a/cups/encode.c b/cups/encode.c new file mode 100644 index 0000000..17c0bae --- /dev/null +++ b/cups/encode.c @@ -0,0 +1,664 @@ +/* + * "$Id: encode.c 7696 2008-06-26 00:54:42Z mike $" + * + * Option encoding routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * cupsEncodeOptions() - Encode printer options into IPP attributes. + * cupsEncodeOptions2() - Encode printer options into IPP attributes for + * a group. + * _ippFindOption() - Find the attribute information for an option. + * compare_ipp_options() - Compare two IPP options. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * Local list of option names and the value tags they should use... + * + * **** THIS LIST MUST BE SORTED **** + */ + +static const _ipp_option_t ipp_options[] = +{ + { 1, "auth-info", IPP_TAG_TEXT, IPP_TAG_JOB }, + { 1, "auth-info-required", IPP_TAG_KEYWORD, IPP_TAG_PRINTER }, + { 0, "blackplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB }, + { 0, "blackplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "brightness", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "brightness-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "columns", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "columns-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "compression", IPP_TAG_KEYWORD, IPP_TAG_OPERATION }, + { 0, "copies", IPP_TAG_INTEGER, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "copies-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "device-uri", IPP_TAG_URI, IPP_TAG_PRINTER }, + { 1, "document-copies", IPP_TAG_RANGE, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "document-format", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION }, + { 0, "document-format-default", IPP_TAG_MIMETYPE, IPP_TAG_PRINTER }, + { 1, "document-numbers", IPP_TAG_RANGE, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 1, "exclude-schemes", IPP_TAG_NAME, IPP_TAG_OPERATION }, + { 1, "finishings", IPP_TAG_ENUM, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 1, "finishings-default", IPP_TAG_ENUM, IPP_TAG_PRINTER }, + { 0, "fit-to-page", IPP_TAG_BOOLEAN, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "fit-to-page-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB }, + { 0, "fitplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "gamma-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "hue", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "hue-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "include-schemes", IPP_TAG_NAME, IPP_TAG_OPERATION }, + { 0, "job-id", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-impressions", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-impressions-completed", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-k-limit", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "job-k-octets", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-k-octets-completed",IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-media-sheets", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-media-sheets-completed", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-page-limit", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "job-priority", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "job-quota-period", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "job-sheets", IPP_TAG_NAME, IPP_TAG_JOB }, + { 1, "job-sheets-default", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 0, "job-state", IPP_TAG_ENUM, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-state-message", IPP_TAG_TEXT, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_ZERO }, /* never send as option */ + { 0, "job-uuid", IPP_TAG_URI, IPP_TAG_JOB }, + { 0, "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB }, + { 1, "marker-change-time", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "marker-colors", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 1, "marker-high-levels", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "marker-levels", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "marker-low-levels", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "marker-message", IPP_TAG_TEXT, IPP_TAG_PRINTER }, + { 1, "marker-names", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 1, "marker-types", IPP_TAG_KEYWORD, IPP_TAG_PRINTER }, + { 1, "media", IPP_TAG_KEYWORD, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "media-col", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "media-col-default", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_PRINTER }, + { 0, "media-color", IPP_TAG_KEYWORD, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 1, "media-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER }, + { 0, "media-key", IPP_TAG_KEYWORD, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "media-size", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "media-type", IPP_TAG_KEYWORD, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB }, + { 0, "mirror-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "natural-scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION }, + { 1, "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION }, + { 1, "notify-events-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER }, + { 0, "notify-lease-duration", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION }, + { 0, "notify-lease-duration-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "notify-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_SUBSCRIPTION }, + { 0, "notify-pull-method", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION }, + { 0, "notify-recipient-uri", IPP_TAG_URI, IPP_TAG_SUBSCRIPTION }, + { 0, "notify-time-interval", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION }, + { 0, "notify-user-data", IPP_TAG_STRING, IPP_TAG_SUBSCRIPTION }, + { 0, "number-up", IPP_TAG_INTEGER, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "number-up-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "orientation-requested-default", IPP_TAG_ENUM, IPP_TAG_PRINTER }, + { 1, "overrides", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "page-bottom-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "page-left-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 1, "page-ranges-default", IPP_TAG_RANGE, IPP_TAG_PRINTER }, + { 0, "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "page-right-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "page-top-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "pages", IPP_TAG_RANGE, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "penwidth-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "port-monitor", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 0, "ppd-name", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 0, "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB }, + { 0, "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER }, + { 1, "printer-commands", IPP_TAG_KEYWORD, IPP_TAG_PRINTER }, + { 0, "printer-error-policy", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 0, "printer-info", IPP_TAG_TEXT, IPP_TAG_PRINTER }, + { 0, "printer-is-accepting-jobs", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "printer-is-shared", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "printer-location", IPP_TAG_TEXT, IPP_TAG_PRINTER }, + { 0, "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER }, + { 0, "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER }, + { 0, "printer-op-policy", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 0, "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "printer-state", IPP_TAG_ENUM, IPP_TAG_PRINTER }, + { 0, "printer-state-change-time", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 1, "printer-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_PRINTER }, + { 0, "printer-type", IPP_TAG_ENUM, IPP_TAG_PRINTER }, + { 0, "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION }, + { 1, "printer-uri-supported", IPP_TAG_URI, IPP_TAG_PRINTER }, + { 0, "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION }, + { 1, "requested-attributes", IPP_TAG_NAME, IPP_TAG_OPERATION }, + { 1, "requesting-user-name-allowed", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 1, "requesting-user-name-denied", IPP_TAG_NAME, IPP_TAG_PRINTER }, + { 0, "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB }, + { 0, "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER }, + { 0, "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "saturation-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB }, + { 0, "scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER }, + { 0, "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "sides-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER }, + { 0, "time-at-completed", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "time-at-creation", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "time-at-processing", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */ + { 0, "wrap", IPP_TAG_BOOLEAN, IPP_TAG_JOB }, + { 0, "wrap-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }, + { 0, "x-dimension", IPP_TAG_INTEGER, IPP_TAG_JOB, + IPP_TAG_DOCUMENT }, + { 0, "y-dimension", IPP_TAG_INTEGER, IPP_TAG_JOB, + IPP_TAG_DOCUMENT } +}; + + +/* + * Local functions... + */ + +static int compare_ipp_options(_ipp_option_t *a, _ipp_option_t *b); + + +/* + * 'cupsEncodeOptions()' - Encode printer options into IPP attributes. + * + * This function adds operation, job, and then subscription attributes, + * in that order. Use the cupsEncodeOptions2() function to add attributes + * for a single group. + */ + +void +cupsEncodeOptions(ipp_t *ipp, /* I - Request to add to */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)", ipp, num_options, options)); + + /* + * Add the options in the proper groups & order... + */ + + cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_OPERATION); + cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_JOB); + cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_SUBSCRIPTION); +} + + +/* + * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group. + * + * This function only adds attributes for a single group. Call this + * function multiple times for each group, or use cupsEncodeOptions() + * to add the standard groups. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +cupsEncodeOptions2( + ipp_t *ipp, /* I - Request to add to */ + int num_options, /* I - Number of options */ + cups_option_t *options, /* I - Options */ + ipp_tag_t group_tag) /* I - Group to encode */ +{ + int i, j; /* Looping vars */ + int count; /* Number of values */ + char *s, /* Pointer into option value */ + *val, /* Pointer to option value */ + *copy, /* Copy of option value */ + *sep, /* Option separator */ + quote; /* Quote character */ + ipp_attribute_t *attr; /* IPP attribute */ + ipp_tag_t value_tag; /* IPP value tag */ + cups_option_t *option; /* Current option */ + ipp_t *collection; /* Collection value */ + int num_cols; /* Number of collection values */ + cups_option_t *cols; /* Collection values */ + + + DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, " + "group_tag=%x)", ipp, num_options, options, group_tag)); + + /* + * Range check input... + */ + + if (!ipp || num_options < 1 || !options) + return; + + /* + * Do special handling for the document-format/raw options... + */ + + if (group_tag == IPP_TAG_OPERATION) + { + /* + * Handle the document format stuff first... + */ + + if ((val = (char *)cupsGetOption("document-format", num_options, options)) != NULL) + ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", + NULL, val); + else if (cupsGetOption("raw", num_options, options)) + ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", + NULL, "application/vnd.cups-raw"); + else + ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", + NULL, "application/octet-stream"); + } + + /* + * Then loop through the options... + */ + + for (i = num_options, option = options; i > 0; i --, option ++) + { + _ipp_option_t *match; /* Matching attribute */ + + + /* + * Skip document format options that are handled above... + */ + + if (!_cups_strcasecmp(option->name, "raw") || + !_cups_strcasecmp(option->name, "document-format") || + !option->name[0]) + continue; + + /* + * Figure out the proper value and group tags for this option... + */ + + if ((match = _ippFindOption(option->name)) != NULL) + { + if (match->group_tag != group_tag && match->alt_group_tag != group_tag) + continue; + + value_tag = match->value_tag; + } + else + { + int namelen; /* Length of name */ + + + namelen = (int)strlen(option->name); + + if (namelen < 10 || + (strcmp(option->name + namelen - 8, "-default") && + strcmp(option->name + namelen - 10, "-supported"))) + { + if (group_tag != IPP_TAG_JOB && group_tag != IPP_TAG_DOCUMENT) + continue; + } + else if (group_tag != IPP_TAG_PRINTER) + continue; + + if (!_cups_strcasecmp(option->value, "true") || + !_cups_strcasecmp(option->value, "false")) + value_tag = IPP_TAG_BOOLEAN; + else + value_tag = IPP_TAG_NAME; + } + + /* + * Count the number of values... + */ + + if (match && match->multivalue) + { + for (count = 1, sep = option->value, quote = 0; *sep; sep ++) + { + if (*sep == quote) + quote = 0; + else if (!quote && (*sep == '\'' || *sep == '\"')) + { + /* + * Skip quoted option value... + */ + + quote = *sep++; + } + else if (*sep == ',' && !quote) + count ++; + else if (*sep == '\\' && sep[1]) + sep ++; + } + } + else + count = 1; + + DEBUG_printf(("2cupsEncodeOptions2: option=\"%s\", count=%d", + option->name, count)); + + /* + * Allocate memory for the attribute values... + */ + + if ((attr = ippAddStrings(ipp, group_tag, value_tag, option->name, count, + NULL, NULL)) == NULL) + { + /* + * Ran out of memory! + */ + + DEBUG_puts("1cupsEncodeOptions2: Ran out of memory for attributes!"); + return; + } + + if (count > 1) + { + /* + * Make a copy of the value we can fiddle with... + */ + + if ((copy = strdup(option->value)) == NULL) + { + /* + * Ran out of memory! + */ + + DEBUG_puts("1cupsEncodeOptions2: Ran out of memory for value copy!"); + ippDeleteAttribute(ipp, attr); + return; + } + + val = copy; + } + else + { + /* + * Since we have a single value, use the value directly... + */ + + val = option->value; + copy = NULL; + } + + /* + * Scan the value string for values... + */ + + for (j = 0, sep = val; j < count; val = sep, j ++) + { + /* + * Find the end of this value and mark it if needed... + */ + + if (count > 1) + { + for (quote = 0; *sep; sep ++) + { + if (*sep == quote) + { + /* + * Finish quoted value... + */ + + quote = 0; + } + else if (!quote && (*sep == '\'' || *sep == '\"')) + { + /* + * Handle quoted option value... + */ + + quote = *sep; + } + else if (*sep == ',' && count > 1) + break; + else if (*sep == '\\' && sep[1]) + { + /* + * Skip quoted character... + */ + + sep ++; + } + } + + if (*sep == ',') + *sep++ = '\0'; + } + + /* + * Copy the option value(s) over as needed by the type... + */ + + switch (attr->value_tag) + { + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + /* + * Integer/enumeration value... + */ + + attr->values[j].integer = strtol(val, &s, 10); + + DEBUG_printf(("2cupsEncodeOptions2: Added integer option value " + "%d...", attr->values[j].integer)); + break; + + case IPP_TAG_BOOLEAN : + if (!_cups_strcasecmp(val, "true") || + !_cups_strcasecmp(val, "on") || + !_cups_strcasecmp(val, "yes")) + { + /* + * Boolean value - true... + */ + + attr->values[j].boolean = 1; + + DEBUG_puts("2cupsEncodeOptions2: Added boolean true value..."); + } + else + { + /* + * Boolean value - false... + */ + + attr->values[j].boolean = 0; + + DEBUG_puts("2cupsEncodeOptions2: Added boolean false value..."); + } + break; + + case IPP_TAG_RANGE : + /* + * Range... + */ + + if (*val == '-') + { + attr->values[j].range.lower = 1; + s = val; + } + else + attr->values[j].range.lower = strtol(val, &s, 10); + + if (*s == '-') + { + if (s[1]) + attr->values[j].range.upper = strtol(s + 1, NULL, 10); + else + attr->values[j].range.upper = 2147483647; + } + else + attr->values[j].range.upper = attr->values[j].range.lower; + + DEBUG_printf(("2cupsEncodeOptions2: Added range option value " + "%d-%d...", attr->values[j].range.lower, + attr->values[j].range.upper)); + break; + + case IPP_TAG_RESOLUTION : + /* + * Resolution... + */ + + attr->values[j].resolution.xres = strtol(val, &s, 10); + + if (*s == 'x') + attr->values[j].resolution.yres = strtol(s + 1, &s, 10); + else + attr->values[j].resolution.yres = attr->values[j].resolution.xres; + + if (!_cups_strcasecmp(s, "dpc") || + !_cups_strcasecmp(s, "dpcm")) + attr->values[j].resolution.units = IPP_RES_PER_CM; + else + attr->values[j].resolution.units = IPP_RES_PER_INCH; + + DEBUG_printf(("2cupsEncodeOptions2: Added resolution option value " + "%s...", val)); + break; + + case IPP_TAG_STRING : + /* + * octet-string + */ + + attr->values[j].unknown.length = (int)strlen(val); + attr->values[j].unknown.data = strdup(val); + + DEBUG_printf(("2cupsEncodeOptions2: Added octet-string value " + "\"%s\"...", (char *)attr->values[j].unknown.data)); + break; + + case IPP_TAG_BEGIN_COLLECTION : + /* + * Collection value + */ + + num_cols = cupsParseOptions(val, 0, &cols); + if ((collection = ippNew()) == NULL) + { + cupsFreeOptions(num_cols, cols); + + if (copy) + free(copy); + + ippDeleteAttribute(ipp, attr); + return; + } + + attr->values[j].collection = collection; + cupsEncodeOptions2(collection, num_cols, cols, IPP_TAG_JOB); + cupsFreeOptions(num_cols, cols); + break; + + default : + if ((attr->values[j].string.text = _cupsStrAlloc(val)) == NULL) + { + /* + * Ran out of memory! + */ + + DEBUG_puts("1cupsEncodeOptions2: Ran out of memory for string!"); + + if (copy) + free(copy); + + ippDeleteAttribute(ipp, attr); + return; + } + + DEBUG_printf(("2cupsEncodeOptions2: Added string value \"%s\"...", + val)); + break; + } + } + + if (copy) + free(copy); + } +} + + +/* + * '_ippFindOption()' - Find the attribute information for an option. + */ + +_ipp_option_t * /* O - Attribute information */ +_ippFindOption(const char *name) /* I - Option/attribute name */ +{ + _ipp_option_t key; /* Search key */ + + + /* + * Lookup the proper value and group tags for this option... + */ + + key.name = name; + + return ((_ipp_option_t *)bsearch(&key, ipp_options, + sizeof(ipp_options) / sizeof(ipp_options[0]), + sizeof(ipp_options[0]), + (int (*)(const void *, const void *)) + compare_ipp_options)); +} + + +/* + * 'compare_ipp_options()' - Compare two IPP options. + */ + +static int /* O - Result of comparison */ +compare_ipp_options(_ipp_option_t *a, /* I - First option */ + _ipp_option_t *b) /* I - Second option */ +{ + return (strcmp(a->name, b->name)); +} + + +/* + * End of "$Id: encode.c 7696 2008-06-26 00:54:42Z mike $". + */ diff --git a/cups/file-private.h b/cups/file-private.h new file mode 100644 index 0000000..bb83c40 --- /dev/null +++ b/cups/file-private.h @@ -0,0 +1,137 @@ +/* + * "$Id: file-private.h 3275 2011-05-20 07:26:13Z msweet $" + * + * Private file definitions for CUPS. + * + * Since stdio files max out at 256 files on many systems, we have to + * write similar functions without this limit. At the same time, using + * our own file functions allows us to provide transparent support of + * gzip'd print files, PPD files, etc. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + */ + +#ifndef _CUPS_FILE_PRIVATE_H_ +# define _CUPS_FILE_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include "cups-private.h" +# include +# include +# include +# include + +# ifdef HAVE_LIBZ +# include +# endif /* HAVE_LIBZ */ +# ifdef WIN32 +# include +# include +# endif /* WIN32 */ + + +/* + * Some operating systems support large files via open flag O_LARGEFILE... + */ + +# ifndef O_LARGEFILE +# define O_LARGEFILE 0 +# endif /* !O_LARGEFILE */ + + +/* + * Some operating systems don't define O_BINARY, which is used by Microsoft + * and IBM to flag binary files... + */ + +# ifndef O_BINARY +# define O_BINARY 0 +# endif /* !O_BINARY */ + + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Types and structures... + */ + +typedef enum /**** _cupsFileCheck return values ****/ +{ + _CUPS_FILE_CHECK_OK = 0, /* Everything OK */ + _CUPS_FILE_CHECK_MISSING = 1, /* File is missing */ + _CUPS_FILE_CHECK_PERMISSIONS = 2, /* File (or parent dir) has bad perms */ + _CUPS_FILE_CHECK_WRONG_TYPE = 3, /* File has wrong type */ + _CUPS_FILE_CHECK_RELATIVE_PATH = 4 /* File contains a relative path */ +} _cups_fc_result_t; + +typedef enum /**** _cupsFileCheck file type values ****/ +{ + _CUPS_FILE_CHECK_FILE = 0, /* Check the file and parent directory */ + _CUPS_FILE_CHECK_PROGRAM = 1, /* Check the program and parent directory */ + _CUPS_FILE_CHECK_FILE_ONLY = 2, /* Check the file only */ + _CUPS_FILE_CHECK_DIRECTORY = 3 /* Check the directory */ +} _cups_fc_filetype_t; + +typedef void (*_cups_fc_func_t)(void *context, _cups_fc_result_t result, + const char *message); + +struct _cups_file_s /**** CUPS file structure... ****/ + +{ + int fd; /* File descriptor */ + char mode, /* Mode ('r' or 'w') */ + compressed, /* Compression used? */ + is_stdio, /* stdin/out/err? */ + eof, /* End of file? */ + buf[4096], /* Buffer */ + *ptr, /* Pointer into buffer */ + *end; /* End of buffer data */ + off_t pos, /* Position in file */ + bufpos; /* File position for start of buffer */ + +#ifdef HAVE_LIBZ + z_stream stream; /* (De)compression stream */ + Bytef cbuf[4096]; /* (De)compression buffer */ + uLong crc; /* (De)compression CRC */ +#endif /* HAVE_LIBZ */ + + char *printf_buffer; /* cupsFilePrintf buffer */ + size_t printf_size; /* Size of cupsFilePrintf buffer */ +}; + + +/* + * Prototypes... + */ + +extern _cups_fc_result_t _cupsFileCheck(const char *filename, + _cups_fc_filetype_t filetype, + int dorootchecks, + _cups_fc_func_t cb, + void *context); +extern void _cupsFileCheckFilter(void *context, + _cups_fc_result_t result, + const char *message); + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_FILE_PRIVATE_H_ */ + +/* + * End of "$Id: file-private.h 3275 2011-05-20 07:26:13Z msweet $". + */ diff --git a/cups/file.c b/cups/file.c new file mode 100644 index 0000000..06aafa9 --- /dev/null +++ b/cups/file.c @@ -0,0 +1,2718 @@ +/* + * "$Id: file.c 7672 2008-06-18 22:03:02Z mike $" + * + * File functions for CUPS. + * + * Since stdio files max out at 256 files on many systems, we have to + * write similar functions without this limit. At the same time, using + * our own file functions allows us to provide transparent support of + * gzip'd print files, PPD files, etc. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * Contents: + * + * _cupsFileCheck() - Check the permissions of the given filename. + * _cupsFileCheckFilter() - Report file check results as CUPS filter messages. + * cupsFileClose() - Close a CUPS file. + * cupsFileCompression() - Return whether a file is compressed. + * cupsFileEOF() - Return the end-of-file status. + * cupsFileFind() - Find a file using the specified path. + * cupsFileFlush() - Flush pending output. + * cupsFileGetChar() - Get a single character from a file. + * cupsFileGetConf() - Get a line from a configuration file. + * cupsFileGetLine() - Get a CR and/or LF-terminated line that may + * contain binary data. + * cupsFileGets() - Get a CR and/or LF-terminated line. + * cupsFileLock() - Temporarily lock access to a file. + * cupsFileNumber() - Return the file descriptor associated with a CUPS + * file. + * cupsFileOpen() - Open a CUPS file. + * cupsFileOpenFd() - Open a CUPS file using a file descriptor. + * cupsFilePeekChar() - Peek at the next character from a file. + * cupsFilePrintf() - Write a formatted string. + * cupsFilePutChar() - Write a character. + * cupsFilePutConf() - Write a configuration line. + * cupsFilePuts() - Write a string. + * cupsFileRead() - Read from a file. + * cupsFileRewind() - Set the current file position to the beginning of + * the file. + * cupsFileSeek() - Seek in a file. + * cupsFileStderr() - Return a CUPS file associated with stderr. + * cupsFileStdin() - Return a CUPS file associated with stdin. + * cupsFileStdout() - Return a CUPS file associated with stdout. + * cupsFileTell() - Return the current file position. + * cupsFileUnlock() - Unlock access to a file. + * cupsFileWrite() - Write to a file. + * cups_compress() - Compress a buffer of data. + * cups_fill() - Fill the input buffer. + * cups_open() - Safely open a file for writing. + * cups_read() - Read from a file descriptor. + * cups_write() - Write to a file descriptor. + */ + +/* + * Include necessary headers... + */ + +#include "file-private.h" +#include +#include + + +/* + * Local functions... + */ + +#ifdef HAVE_LIBZ +static ssize_t cups_compress(cups_file_t *fp, const char *buf, size_t bytes); +#endif /* HAVE_LIBZ */ +static ssize_t cups_fill(cups_file_t *fp); +static int cups_open(const char *filename, int mode); +static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes); +static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes); + + +#ifndef WIN32 +/* + * '_cupsFileCheck()' - Check the permissions of the given filename. + */ + +_cups_fc_result_t /* O - Check result */ +_cupsFileCheck( + const char *filename, /* I - Filename to check */ + _cups_fc_filetype_t filetype, /* I - Type of file checks? */ + int dorootchecks, /* I - Check for root permissions? */ + _cups_fc_func_t cb, /* I - Callback function */ + void *context) /* I - Context pointer for callback */ + +{ + struct stat fileinfo; /* File information */ + char message[1024], /* Message string */ + temp[1024], /* Parent directory filename */ + *ptr; /* Pointer into parent directory */ + _cups_fc_result_t result; /* Check result */ + + + /* + * Does the filename contain a relative path ("../")? + */ + + if (strstr(filename, "../")) + { + /* + * Yes, fail it! + */ + + result = _CUPS_FILE_CHECK_RELATIVE_PATH; + goto finishup; + } + + /* + * Does the program even exist and is it accessible? + */ + + if (stat(filename, &fileinfo)) + { + /* + * Nope... + */ + + result = _CUPS_FILE_CHECK_MISSING; + goto finishup; + } + + /* + * Check the execute bit... + */ + + result = _CUPS_FILE_CHECK_OK; + + switch (filetype) + { + case _CUPS_FILE_CHECK_DIRECTORY : + if (!S_ISDIR(fileinfo.st_mode)) + result = _CUPS_FILE_CHECK_WRONG_TYPE; + break; + + default : + if (!S_ISREG(fileinfo.st_mode)) + result = _CUPS_FILE_CHECK_WRONG_TYPE; + break; + } + + if (result) + goto finishup; + + /* + * Are we doing root checks? + */ + + if (!dorootchecks) + { + /* + * Nope, so anything (else) goes... + */ + + goto finishup; + } + + /* + * Verify permission of the file itself: + * + * 1. Must be owned by root + * 2. Must not be writable by group + * 3. Must not be setuid + * 4. Must not be writable by others + */ + + if (fileinfo.st_uid || /* 1. Must be owned by root */ + (fileinfo.st_mode & S_IWGRP) || /* 2. Must not be writable by group */ + (fileinfo.st_mode & S_ISUID) || /* 3. Must not be setuid */ + (fileinfo.st_mode & S_IWOTH)) /* 4. Must not be writable by others */ + { + result = _CUPS_FILE_CHECK_PERMISSIONS; + goto finishup; + } + + if (filetype == _CUPS_FILE_CHECK_DIRECTORY || + filetype == _CUPS_FILE_CHECK_FILE_ONLY) + goto finishup; + + /* + * Now check the containing directory... + */ + + strlcpy(temp, filename, sizeof(temp)); + if ((ptr = strrchr(temp, '/')) != NULL) + { + if (ptr == temp) + ptr[1] = '\0'; + else + *ptr = '\0'; + } + + if (stat(temp, &fileinfo)) + { + /* + * Doesn't exist?!? + */ + + result = _CUPS_FILE_CHECK_MISSING; + filetype = _CUPS_FILE_CHECK_DIRECTORY; + filename = temp; + + goto finishup; + } + + if (fileinfo.st_uid || /* 1. Must be owned by root */ + (fileinfo.st_mode & S_IWGRP) || /* 2. Must not be writable by group */ + (fileinfo.st_mode & S_ISUID) || /* 3. Must not be setuid */ + (fileinfo.st_mode & S_IWOTH)) /* 4. Must not be writable by others */ + { + result = _CUPS_FILE_CHECK_PERMISSIONS; + filetype = _CUPS_FILE_CHECK_DIRECTORY; + filename = temp; + } + + /* + * Common return point... + */ + + finishup: + + if (cb) + { + cups_lang_t *lang = cupsLangDefault(); + /* Localization information */ + + switch (result) + { + case _CUPS_FILE_CHECK_OK : + if (filetype == _CUPS_FILE_CHECK_DIRECTORY) + snprintf(message, sizeof(message), + _cupsLangString(lang, _("Directory \"%s\" permissions OK " + "(0%o/uid=%d/gid=%d).")), + filename, fileinfo.st_mode, (int)fileinfo.st_uid, + (int)fileinfo.st_gid); + else + snprintf(message, sizeof(message), + _cupsLangString(lang, _("File \"%s\" permissions OK " + "(0%o/uid=%d/gid=%d).")), + filename, fileinfo.st_mode, (int)fileinfo.st_uid, + (int)fileinfo.st_gid); + break; + + case _CUPS_FILE_CHECK_MISSING : + if (filetype == _CUPS_FILE_CHECK_DIRECTORY) + snprintf(message, sizeof(message), + _cupsLangString(lang, _("Directory \"%s\" not available: " + "%s")), + filename, strerror(errno)); + else + snprintf(message, sizeof(message), + _cupsLangString(lang, _("File \"%s\" not available: %s")), + filename, strerror(errno)); + break; + + case _CUPS_FILE_CHECK_PERMISSIONS : + if (filetype == _CUPS_FILE_CHECK_DIRECTORY) + snprintf(message, sizeof(message), + _cupsLangString(lang, _("Directory \"%s\" has insecure " + "permissions " + "(0%o/uid=%d/gid=%d).")), + filename, fileinfo.st_mode, (int)fileinfo.st_uid, + (int)fileinfo.st_gid); + else + snprintf(message, sizeof(message), + _cupsLangString(lang, _("File \"%s\" has insecure " + "permissions " + "(0%o/uid=%d/gid=%d).")), + filename, fileinfo.st_mode, (int)fileinfo.st_uid, + (int)fileinfo.st_gid); + break; + + case _CUPS_FILE_CHECK_WRONG_TYPE : + if (filetype == _CUPS_FILE_CHECK_DIRECTORY) + snprintf(message, sizeof(message), + _cupsLangString(lang, _("Directory \"%s\" is a file.")), + filename); + else + snprintf(message, sizeof(message), + _cupsLangString(lang, _("File \"%s\" is a directory.")), + filename); + break; + + case _CUPS_FILE_CHECK_RELATIVE_PATH : + if (filetype == _CUPS_FILE_CHECK_DIRECTORY) + snprintf(message, sizeof(message), + _cupsLangString(lang, _("Directory \"%s\" contains a " + "relative path.")), filename); + else + snprintf(message, sizeof(message), + _cupsLangString(lang, _("File \"%s\" contains a relative " + "path.")), filename); + break; + } + + (*cb)(context, result, message); + } + + return (result); +} + + +/* + * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages. + */ + +void +_cupsFileCheckFilter( + void *context, /* I - Context pointer (unused) */ + _cups_fc_result_t result, /* I - Result code */ + const char *message) /* I - Message text */ +{ + const char *prefix; /* Messaging prefix */ + + + (void)context; + + switch (result) + { + default : + case _CUPS_FILE_CHECK_OK : + prefix = "DEBUG2"; + break; + + case _CUPS_FILE_CHECK_MISSING : + case _CUPS_FILE_CHECK_WRONG_TYPE : + prefix = "ERROR"; + fputs("STATE: +cups-missing-filter-warning\n", stderr); + break; + + case _CUPS_FILE_CHECK_PERMISSIONS : + case _CUPS_FILE_CHECK_RELATIVE_PATH : + prefix = "ERROR"; + fputs("STATE: +cups-insecure-filter-warning\n", stderr); + break; + } + + fprintf(stderr, "%s: %s\n", prefix, message); +} +#endif /* !WIN32 */ + + +/* + * 'cupsFileClose()' - Close a CUPS file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +cupsFileClose(cups_file_t *fp) /* I - CUPS file */ +{ + int fd; /* File descriptor */ + char mode; /* Open mode */ + int status; /* Return status */ + int is_stdio; /* Is a stdio file? */ + + + DEBUG_printf(("cupsFileClose(fp=%p)", fp)); + + /* + * Range check... + */ + + if (!fp) + return (-1); + + /* + * Flush pending write data... + */ + + if (fp->mode == 'w') + status = cupsFileFlush(fp); + else + status = 0; + +#ifdef HAVE_LIBZ + if (fp->compressed && status >= 0) + { + if (fp->mode == 'r') + { + /* + * Free decompression data... + */ + + inflateEnd(&fp->stream); + } + else + { + /* + * Flush any remaining compressed data... + */ + + unsigned char trailer[8]; /* Trailer CRC and length */ + int done; /* Done writing... */ + + + fp->stream.avail_in = 0; + + for (done = 0;;) + { + if (fp->stream.next_out > fp->cbuf) + { + if (cups_write(fp, (char *)fp->cbuf, + fp->stream.next_out - fp->cbuf) < 0) + status = -1; + + fp->stream.next_out = fp->cbuf; + fp->stream.avail_out = sizeof(fp->cbuf); + } + + if (done || status < 0) + break; + + done = deflate(&fp->stream, Z_FINISH) == Z_STREAM_END && + fp->stream.next_out == fp->cbuf; + } + + /* + * Write the CRC and length... + */ + + trailer[0] = fp->crc; + trailer[1] = fp->crc >> 8; + trailer[2] = fp->crc >> 16; + trailer[3] = fp->crc >> 24; + trailer[4] = fp->pos; + trailer[5] = fp->pos >> 8; + trailer[6] = fp->pos >> 16; + trailer[7] = fp->pos >> 24; + + if (cups_write(fp, (char *)trailer, 8) < 0) + status = -1; + + /* + * Free all memory used by the compression stream... + */ + + deflateEnd(&(fp->stream)); + } + } +#endif /* HAVE_LIBZ */ + + /* + * Save the file descriptor we used and free memory... + */ + + fd = fp->fd; + mode = fp->mode; + is_stdio = fp->is_stdio; + + if (fp->printf_buffer) + free(fp->printf_buffer); + + free(fp); + + /* + * Close the file, returning the close status... + */ + + if (mode == 's') + { + if (closesocket(fd) < 0) + status = -1; + } + else if (!is_stdio) + { + if (close(fd) < 0) + status = -1; + } + + return (status); +} + + +/* + * 'cupsFileCompression()' - Return whether a file is compressed. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */ +cupsFileCompression(cups_file_t *fp) /* I - CUPS file */ +{ + return (fp ? fp->compressed : CUPS_FILE_NONE); +} + + +/* + * 'cupsFileEOF()' - Return the end-of-file status. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 on end of file, 0 otherwise */ +cupsFileEOF(cups_file_t *fp) /* I - CUPS file */ +{ + return (fp ? fp->eof : 1); +} + + +/* + * 'cupsFileFind()' - Find a file using the specified path. + * + * This function allows the paths in the path string to be separated by + * colons (UNIX standard) or semicolons (Windows standard) and stores the + * result in the buffer supplied. If the file cannot be found in any of + * the supplied paths, @code NULL@ is returned. A @code NULL@ path only + * matches the current directory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +const char * /* O - Full path to file or @code NULL@ if not found */ +cupsFileFind(const char *filename, /* I - File to find */ + const char *path, /* I - Colon/semicolon-separated path */ + int executable, /* I - 1 = executable files, 0 = any file/dir */ + char *buffer, /* I - Filename buffer */ + int bufsize) /* I - Size of filename buffer */ +{ + char *bufptr, /* Current position in buffer */ + *bufend; /* End of buffer */ + + + /* + * Range check input... + */ + + DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, " + "buffer=%p, bufsize=%d)", filename, path, executable, buffer, + bufsize)); + + if (!filename || !buffer || bufsize < 2) + return (NULL); + + if (!path) + { + /* + * No path, so check current directory... + */ + + if (!access(filename, 0)) + { + strlcpy(buffer, filename, bufsize); + return (buffer); + } + else + return (NULL); + } + + /* + * Now check each path and return the first match... + */ + + bufend = buffer + bufsize - 1; + bufptr = buffer; + + while (*path) + { +#ifdef WIN32 + if (*path == ';' || (*path == ':' && ((bufptr - buffer) > 1 || !isalpha(buffer[0] & 255)))) +#else + if (*path == ';' || *path == ':') +#endif /* WIN32 */ + { + if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend) + *bufptr++ = '/'; + + strlcpy(bufptr, filename, bufend - bufptr); + +#ifdef WIN32 + if (!access(buffer, 0)) +#else + if (!access(buffer, executable ? X_OK : 0)) +#endif /* WIN32 */ + { + DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer)); + return (buffer); + } + + bufptr = buffer; + } + else if (bufptr < bufend) + *bufptr++ = *path; + + path ++; + } + + /* + * Check the last path... + */ + + if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend) + *bufptr++ = '/'; + + strlcpy(bufptr, filename, bufend - bufptr); + + if (!access(buffer, 0)) + { + DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer)); + return (buffer); + } + else + { + DEBUG_puts("1cupsFileFind: Returning NULL"); + return (NULL); + } +} + + +/* + * 'cupsFileFlush()' - Flush pending output. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +cupsFileFlush(cups_file_t *fp) /* I - CUPS file */ +{ + ssize_t bytes; /* Bytes to write */ + + + DEBUG_printf(("cupsFileFlush(fp=%p)", fp)); + + /* + * Range check input... + */ + + if (!fp || fp->mode != 'w') + { + DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file..."); + return (-1); + } + + bytes = (ssize_t)(fp->ptr - fp->buf); + + DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT " bytes...", + CUPS_LLCAST bytes)); + + if (bytes > 0) + { +#ifdef HAVE_LIBZ + if (fp->compressed) + bytes = cups_compress(fp, fp->buf, bytes); + else +#endif /* HAVE_LIBZ */ + bytes = cups_write(fp, fp->buf, bytes); + + if (bytes < 0) + return (-1); + + fp->ptr = fp->buf; + } + + return (0); +} + + +/* + * 'cupsFileGetChar()' - Get a single character from a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Character or -1 on end of file */ +cupsFileGetChar(cups_file_t *fp) /* I - CUPS file */ +{ + /* + * Range check input... + */ + + if (!fp || (fp->mode != 'r' && fp->mode != 's')) + { + DEBUG_puts("5cupsFileGetChar: Bad arguments!"); + return (-1); + } + + /* + * If the input buffer is empty, try to read more data... + */ + + if (fp->ptr >= fp->end) + if (cups_fill(fp) < 0) + { + DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!"); + return (-1); + } + + /* + * Return the next character in the buffer... + */ + + DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp->ptr) & 255)); + + fp->pos ++; + + DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (*(fp->ptr)++ & 255); +} + + +/* + * 'cupsFileGetConf()' - Get a line from a configuration file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - Line read or @code NULL@ on end of file or error */ +cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ + char *buf, /* O - String buffer */ + size_t buflen, /* I - Size of string buffer */ + char **value, /* O - Pointer to value */ + int *linenum) /* IO - Current line number */ +{ + char *ptr; /* Pointer into line */ + + + /* + * Range check input... + */ + + DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT + ", value=%p, linenum=%p)", fp, buf, CUPS_LLCAST buflen, + value, linenum)); + + if (!fp || (fp->mode != 'r' && fp->mode != 's') || + !buf || buflen < 2 || !value) + { + if (value) + *value = NULL; + + return (NULL); + } + + /* + * Read the next non-comment line... + */ + + *value = NULL; + + while (cupsFileGets(fp, buf, buflen)) + { + (*linenum) ++; + + /* + * Strip any comments... + */ + + if ((ptr = strchr(buf, '#')) != NULL) + { + if (ptr > buf && ptr[-1] == '\\') + { + // Unquote the #... + _cups_strcpy(ptr - 1, ptr); + } + else + { + // Strip the comment and any trailing whitespace... + while (ptr > buf) + { + if (!_cups_isspace(ptr[-1])) + break; + + ptr --; + } + + *ptr = '\0'; + } + } + + /* + * Strip leading whitespace... + */ + + for (ptr = buf; _cups_isspace(*ptr); ptr ++); + + if (ptr > buf) + _cups_strcpy(buf, ptr); + + /* + * See if there is anything left... + */ + + if (buf[0]) + { + /* + * Yes, grab any value and return... + */ + + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; + + if (*ptr) + { + /* + * Have a value, skip any other spaces... + */ + + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; + + if (*ptr) + *value = ptr; + + /* + * Strip trailing whitespace and > for lines that begin with <... + */ + + ptr += strlen(ptr) - 1; + + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ + + *value = NULL; + return (buf); + } + + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } + + /* + * Return the line... + */ + + return (buf); + } + } + + return (NULL); +} + + +/* + * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may + * contain binary data. + * + * This function differs from @link cupsFileGets@ in that the trailing CR + * and LF are preserved, as is any binary data on the line. The buffer is + * nul-terminated, however you should use the returned length to determine + * the number of bytes on the line. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +size_t /* O - Number of bytes on line or 0 on end of file */ +cupsFileGetLine(cups_file_t *fp, /* I - File to read from */ + char *buf, /* I - Buffer */ + size_t buflen) /* I - Size of buffer */ +{ + int ch; /* Character from file */ + char *ptr, /* Current position in line buffer */ + *end; /* End of line buffer */ + + + /* + * Range check input... + */ + + DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")", + fp, buf, CUPS_LLCAST buflen)); + + if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 3) + return (0); + + /* + * Now loop until we have a valid line... + */ + + for (ptr = buf, end = buf + buflen - 2; ptr < end ;) + { + if (fp->ptr >= fp->end) + if (cups_fill(fp) <= 0) + break; + + *ptr++ = ch = *(fp->ptr)++; + fp->pos ++; + + if (ch == '\r') + { + /* + * Check for CR LF... + */ + + if (fp->ptr >= fp->end) + if (cups_fill(fp) <= 0) + break; + + if (*(fp->ptr) == '\n') + { + *ptr++ = *(fp->ptr)++; + fp->pos ++; + } + + break; + } + else if (ch == '\n') + { + /* + * Line feed ends a line... + */ + + break; + } + } + + *ptr = '\0'; + + DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (ptr - buf); +} + + +/* + * 'cupsFileGets()' - Get a CR and/or LF-terminated line. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - Line read or @code NULL@ on end of file or error */ +cupsFileGets(cups_file_t *fp, /* I - CUPS file */ + char *buf, /* O - String buffer */ + size_t buflen) /* I - Size of string buffer */ +{ + int ch; /* Character from file */ + char *ptr, /* Current position in line buffer */ + *end; /* End of line buffer */ + + + /* + * Range check input... + */ + + DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")", fp, buf, + CUPS_LLCAST buflen)); + + if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 2) + return (NULL); + + /* + * Now loop until we have a valid line... + */ + + for (ptr = buf, end = buf + buflen - 1; ptr < end ;) + { + if (fp->ptr >= fp->end) + if (cups_fill(fp) <= 0) + { + if (ptr == buf) + return (NULL); + else + break; + } + + ch = *(fp->ptr)++; + fp->pos ++; + + if (ch == '\r') + { + /* + * Check for CR LF... + */ + + if (fp->ptr >= fp->end) + if (cups_fill(fp) <= 0) + break; + + if (*(fp->ptr) == '\n') + { + fp->ptr ++; + fp->pos ++; + } + + break; + } + else if (ch == '\n') + { + /* + * Line feed ends a line... + */ + + break; + } + else + *ptr++ = ch; + } + + *ptr = '\0'; + + DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (buf); +} + + +/* + * 'cupsFileLock()' - Temporarily lock access to a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +cupsFileLock(cups_file_t *fp, /* I - CUPS file */ + int block) /* I - 1 to wait for the lock, 0 to fail right away */ +{ + /* + * Range check... + */ + + if (!fp || fp->mode == 's') + return (-1); + + /* + * Try the lock... + */ + +#ifdef WIN32 + return (_locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0)); +#else + // 07/22/2016 Mopria-notice: lockf is not defined in the Android, commenting out + //return (lockf(fp->fd, block ? F_LOCK : F_TLOCK, 0)); + return 0; +#endif /* WIN32 */ +} + + +/* + * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - File descriptor */ +cupsFileNumber(cups_file_t *fp) /* I - CUPS file */ +{ + if (fp) + return (fp->fd); + else + return (-1); +} + + +/* + * 'cupsFileOpen()' - Open a CUPS file. + * + * The "mode" parameter can be "r" to read, "w" to write, overwriting any + * existing file, "a" to append to an existing file or create a new file, + * or "s" to open a socket connection. + * + * When opening for writing ("w"), an optional number from 1 to 9 can be + * supplied which enables Flate compression of the file. Compression is + * not supported for the "a" (append) mode. + * + * When opening a socket connection, the filename is a string of the form + * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6 + * connection as needed, generally preferring IPv6 connections when there is + * a choice. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_file_t * /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */ +cupsFileOpen(const char *filename, /* I - Name of file */ + const char *mode) /* I - Open mode */ +{ + cups_file_t *fp; /* New CUPS file */ + int fd; /* File descriptor */ + char hostname[1024], /* Hostname */ + *portname; /* Port "name" (number or service) */ + http_addrlist_t *addrlist; /* Host address list */ + + + DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename, + mode)); + + /* + * Range check input... + */ + + if (!filename || !mode || + (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') || + (*mode == 'a' && isdigit(mode[1] & 255))) + return (NULL); + + /* + * Open the file... + */ + + switch (*mode) + { + case 'a' : /* Append file */ + fd = cups_open(filename, + O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY); + break; + + case 'r' : /* Read file */ + fd = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0); + break; + + case 'w' : /* Write file */ + fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY); + if (fd < 0 && errno == ENOENT) + { + fd = cups_open(filename, + O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE | O_BINARY); + if (fd < 0 && errno == EEXIST) + fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY); + } + + if (fd >= 0) +#ifdef WIN32 + _chsize(fd, 0); +#else + ftruncate(fd, 0); +#endif /* WIN32 */ + break; + + case 's' : /* Read/write socket */ + strlcpy(hostname, filename, sizeof(hostname)); + if ((portname = strrchr(hostname, ':')) != NULL) + *portname++ = '\0'; + else + return (NULL); + + /* + * Lookup the hostname and service... + */ + + if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) + return (NULL); + + /* + * Connect to the server... + */ + + if (!httpAddrConnect(addrlist, &fd)) + { + httpAddrFreeList(addrlist); + return (NULL); + } + + httpAddrFreeList(addrlist); + break; + + default : /* Remove bogus compiler warning... */ + return (NULL); + } + + if (fd < 0) + return (NULL); + + /* + * Create the CUPS file structure... + */ + + if ((fp = cupsFileOpenFd(fd, mode)) == NULL) + { + if (*mode == 's') + closesocket(fd); + else + close(fd); + } + + /* + * Return it... + */ + + return (fp); +} + +/* + * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor. + * + * The "mode" parameter can be "r" to read, "w" to write, "a" to append, + * or "s" to treat the file descriptor as a bidirectional socket connection. + * + * When opening for writing ("w"), an optional number from 1 to 9 can be + * supplied which enables Flate compression of the file. Compression is + * not supported for the "a" (append) mode. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_file_t * /* O - CUPS file or @code NULL@ if the file could not be opened */ +cupsFileOpenFd(int fd, /* I - File descriptor */ + const char *mode) /* I - Open mode */ +{ + cups_file_t *fp; /* New CUPS file */ + + + DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd, mode)); + + /* + * Range check input... + */ + + if (fd < 0 || !mode || + (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') || + (*mode == 'a' && isdigit(mode[1] & 255))) + return (NULL); + + /* + * Allocate memory... + */ + + if ((fp = calloc(1, sizeof(cups_file_t))) == NULL) + return (NULL); + + /* + * Open the file... + */ + + fp->fd = fd; + + switch (*mode) + { + case 'a' : + fp->pos = lseek(fd, 0, SEEK_END); + + case 'w' : + fp->mode = 'w'; + fp->ptr = fp->buf; + fp->end = fp->buf + sizeof(fp->buf); + +#ifdef HAVE_LIBZ + if (mode[1] >= '1' && mode[1] <= '9') + { + /* + * Open a compressed stream, so write the standard gzip file + * header... + */ + + unsigned char header[10]; /* gzip file header */ + time_t curtime; /* Current time */ + + + curtime = time(NULL); + header[0] = 0x1f; + header[1] = 0x8b; + header[2] = Z_DEFLATED; + header[3] = 0; + header[4] = curtime; + header[5] = curtime >> 8; + header[6] = curtime >> 16; + header[7] = curtime >> 24; + header[8] = 0; + header[9] = 0x03; + + cups_write(fp, (char *)header, 10); + + /* + * Initialize the compressor... + */ + + deflateInit2(&(fp->stream), mode[1] - '0', Z_DEFLATED, -15, 8, + Z_DEFAULT_STRATEGY); + + fp->stream.next_out = fp->cbuf; + fp->stream.avail_out = sizeof(fp->cbuf); + fp->compressed = 1; + fp->crc = crc32(0L, Z_NULL, 0); + } +#endif /* HAVE_LIBZ */ + break; + + case 'r' : + fp->mode = 'r'; + break; + + case 's' : + fp->mode = 's'; + break; + + default : /* Remove bogus compiler warning... */ + return (NULL); + } + + /* + * Don't pass this file to child processes... + */ + +#ifndef WIN32 + fcntl(fp->fd, F_SETFD, fcntl(fp->fd, F_GETFD) | FD_CLOEXEC); +#endif /* !WIN32 */ + + return (fp); +} + + +/* + * 'cupsFilePeekChar()' - Peek at the next character from a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Character or -1 on end of file */ +cupsFilePeekChar(cups_file_t *fp) /* I - CUPS file */ +{ + /* + * Range check input... + */ + + if (!fp || (fp->mode != 'r' && fp->mode != 's')) + return (-1); + + /* + * If the input buffer is empty, try to read more data... + */ + + if (fp->ptr >= fp->end) + if (cups_fill(fp) < 0) + return (-1); + + /* + * Return the next character in the buffer... + */ + + return (*(fp->ptr) & 255); +} + + +/* + * 'cupsFilePrintf()' - Write a formatted string. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Number of bytes written or -1 on error */ +cupsFilePrintf(cups_file_t *fp, /* I - CUPS file */ + const char *format, /* I - Printf-style format string */ + ...) /* I - Additional args as necessary */ +{ + va_list ap; /* Argument list */ + ssize_t bytes; /* Formatted size */ + + + DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp, format)); + + if (!fp || !format || (fp->mode != 'w' && fp->mode != 's')) + return (-1); + + if (!fp->printf_buffer) + { + /* + * Start with an 1k printf buffer... + */ + + if ((fp->printf_buffer = malloc(1024)) == NULL) + return (-1); + + fp->printf_size = 1024; + } + + va_start(ap, format); + bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap); + va_end(ap); + + if (bytes >= (ssize_t)fp->printf_size) + { + /* + * Expand the printf buffer... + */ + + char *temp; /* Temporary buffer pointer */ + + + if (bytes > 65535) + return (-1); + + if ((temp = realloc(fp->printf_buffer, bytes + 1)) == NULL) + return (-1); + + fp->printf_buffer = temp; + fp->printf_size = bytes + 1; + + va_start(ap, format); + bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap); + va_end(ap); + } + + if (fp->mode == 's') + { + if (cups_write(fp, fp->printf_buffer, bytes) < 0) + return (-1); + + fp->pos += bytes; + + DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (bytes); + } + + if ((fp->ptr + bytes) > fp->end) + if (cupsFileFlush(fp)) + return (-1); + + fp->pos += bytes; + + DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + if (bytes > sizeof(fp->buf)) + { +#ifdef HAVE_LIBZ + if (fp->compressed) + return (cups_compress(fp, fp->printf_buffer, bytes)); + else +#endif /* HAVE_LIBZ */ + return (cups_write(fp, fp->printf_buffer, bytes)); + } + else + { + memcpy(fp->ptr, fp->printf_buffer, bytes); + fp->ptr += bytes; + return (bytes); + } +} + + +/* + * 'cupsFilePutChar()' - Write a character. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +cupsFilePutChar(cups_file_t *fp, /* I - CUPS file */ + int c) /* I - Character to write */ +{ + /* + * Range check input... + */ + + if (!fp || (fp->mode != 'w' && fp->mode != 's')) + return (-1); + + if (fp->mode == 's') + { + /* + * Send character immediately over socket... + */ + + char ch; /* Output character */ + + + ch = c; + + if (send(fp->fd, &ch, 1, 0) < 1) + return (-1); + } + else + { + /* + * Buffer it up... + */ + + if (fp->ptr >= fp->end) + if (cupsFileFlush(fp)) + return (-1); + + *(fp->ptr) ++ = c; + } + + fp->pos ++; + + DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (0); +} + + +/* + * 'cupsFilePutConf()' - Write a configuration line. + * + * This function handles any comment escaping of the value. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +ssize_t /* O - Number of bytes written or -1 on error */ +cupsFilePutConf(cups_file_t *fp, /* I - CUPS file */ + const char *directive, /* I - Directive */ + const char *value) /* I - Value */ +{ + ssize_t bytes, /* Number of bytes written */ + temp; /* Temporary byte count */ + const char *ptr; /* Pointer into value */ + + + if (!fp || !directive || !*directive) + return (-1); + + if ((bytes = cupsFilePuts(fp, directive)) < 0) + return (-1); + + if (cupsFilePutChar(fp, ' ') < 0) + return (-1); + bytes ++; + + if (value && *value) + { + if ((ptr = strchr(value, '#')) != NULL) + { + /* + * Need to quote the first # in the info string... + */ + + if ((temp = cupsFileWrite(fp, value, ptr - value)) < 0) + return (-1); + bytes += temp; + + if (cupsFilePutChar(fp, '\\') < 0) + return (-1); + bytes ++; + + if ((temp = cupsFilePuts(fp, ptr)) < 0) + return (-1); + bytes += temp; + } + else if ((temp = cupsFilePuts(fp, value)) < 0) + return (-1); + else + bytes += temp; + } + + if (cupsFilePutChar(fp, '\n') < 0) + return (-1); + else + return (bytes + 1); +} + + +/* + * 'cupsFilePuts()' - Write a string. + * + * Like the @code fputs@ function, no newline is appended to the string. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Number of bytes written or -1 on error */ +cupsFilePuts(cups_file_t *fp, /* I - CUPS file */ + const char *s) /* I - String to write */ +{ + ssize_t bytes; /* Bytes to write */ + + + /* + * Range check input... + */ + + if (!fp || !s || (fp->mode != 'w' && fp->mode != 's')) + return (-1); + + /* + * Write the string... + */ + + bytes = (int)strlen(s); + + if (fp->mode == 's') + { + if (cups_write(fp, s, bytes) < 0) + return (-1); + + fp->pos += bytes; + + DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (bytes); + } + + if ((fp->ptr + bytes) > fp->end) + if (cupsFileFlush(fp)) + return (-1); + + fp->pos += bytes; + + DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + if (bytes > sizeof(fp->buf)) + { +#ifdef HAVE_LIBZ + if (fp->compressed) + return (cups_compress(fp, s, bytes)); + else +#endif /* HAVE_LIBZ */ + return (cups_write(fp, s, bytes)); + } + else + { + memcpy(fp->ptr, s, bytes); + fp->ptr += bytes; + return (bytes); + } +} + + +/* + * 'cupsFileRead()' - Read from a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ssize_t /* O - Number of bytes read or -1 on error */ +cupsFileRead(cups_file_t *fp, /* I - CUPS file */ + char *buf, /* O - Buffer */ + size_t bytes) /* I - Number of bytes to read */ +{ + size_t total; /* Total bytes read */ + ssize_t count; /* Bytes read */ + + + DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf, + CUPS_LLCAST bytes)); + + /* + * Range check input... + */ + + if (!fp || !buf || (fp->mode != 'r' && fp->mode != 's')) + return (-1); + + if (bytes == 0) + return (0); + + /* + * Loop until all bytes are read... + */ + + total = 0; + while (bytes > 0) + { + if (fp->ptr >= fp->end) + if (cups_fill(fp) <= 0) + { + DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total=" + CUPS_LLFMT, CUPS_LLCAST total)); + + if (total > 0) + return ((ssize_t)total); + else + return (-1); + } + + count = (ssize_t)(fp->end - fp->ptr); + if (count > (ssize_t)bytes) + count = (ssize_t)bytes; + + memcpy(buf, fp->ptr, count); + fp->ptr += count; + fp->pos += count; + + DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + /* + * Update the counts for the last read... + */ + + bytes -= count; + total += count; + buf += count; + } + + /* + * Return the total number of bytes read... + */ + + DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT, CUPS_LLCAST total)); + + return ((ssize_t)total); +} + + +/* + * 'cupsFileRewind()' - Set the current file position to the beginning of the + * file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +off_t /* O - New file position or -1 on error */ +cupsFileRewind(cups_file_t *fp) /* I - CUPS file */ +{ + /* + * Range check input... + */ + + DEBUG_printf(("cupsFileRewind(fp=%p)", fp)); + DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + if (!fp || fp->mode != 'r') + return (-1); + + /* + * Handle special cases... + */ + + if (fp->bufpos == 0) + { + /* + * No seeking necessary... + */ + + fp->pos = 0; + + if (fp->ptr) + { + fp->ptr = fp->buf; + fp->eof = 0; + } + + DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (0); + } + + /* + * Otherwise, seek in the file and cleanup any compression buffers... + */ + +#ifdef HAVE_LIBZ + if (fp->compressed) + { + inflateEnd(&fp->stream); + fp->compressed = 0; + } +#endif /* HAVE_LIBZ */ + + if (lseek(fp->fd, 0, SEEK_SET)) + { + DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno))); + return (-1); + } + + fp->bufpos = 0; + fp->pos = 0; + fp->ptr = NULL; + fp->end = NULL; + fp->eof = 0; + + DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (0); +} + + +/* + * 'cupsFileSeek()' - Seek in a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +off_t /* O - New file position or -1 on error */ +cupsFileSeek(cups_file_t *fp, /* I - CUPS file */ + off_t pos) /* I - Position in file */ +{ + ssize_t bytes; /* Number bytes in buffer */ + + + DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")", fp, + CUPS_LLCAST pos)); + DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp->ptr, fp->end)); + + /* + * Range check input... + */ + + if (!fp || pos < 0 || fp->mode != 'r') + return (-1); + + /* + * Handle special cases... + */ + + if (pos == 0) + return (cupsFileRewind(fp)); + + if (fp->ptr) + { + bytes = (ssize_t)(fp->end - fp->buf); + + DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT, CUPS_LLCAST bytes)); + + if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) + { + /* + * No seeking necessary... + */ + + fp->pos = pos; + fp->ptr = fp->buf + pos - fp->bufpos; + fp->eof = 0; + + return (pos); + } + } + +#ifdef HAVE_LIBZ + if (!fp->compressed && !fp->ptr) + { + /* + * Preload a buffer to determine whether the file is compressed... + */ + + if (cups_fill(fp) < 0) + return (-1); + } +#endif /* HAVE_LIBZ */ + + /* + * Seek forwards or backwards... + */ + + fp->eof = 0; + + if (pos < fp->bufpos) + { + /* + * Need to seek backwards... + */ + + DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS"); + +#ifdef HAVE_LIBZ + if (fp->compressed) + { + inflateEnd(&fp->stream); + + lseek(fp->fd, 0, SEEK_SET); + fp->bufpos = 0; + fp->pos = 0; + fp->ptr = NULL; + fp->end = NULL; + + while ((bytes = cups_fill(fp)) > 0) + if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) + break; + + if (bytes <= 0) + return (-1); + + fp->ptr = fp->buf + pos - fp->bufpos; + fp->pos = pos; + } + else +#endif /* HAVE_LIBZ */ + { + fp->bufpos = lseek(fp->fd, pos, SEEK_SET); + fp->pos = fp->bufpos; + fp->ptr = NULL; + fp->end = NULL; + + DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT, + CUPS_LLCAST fp->pos)); + } + } + else + { + /* + * Need to seek forwards... + */ + + DEBUG_puts("2cupsFileSeek: SEEK FORWARDS"); + +#ifdef HAVE_LIBZ + if (fp->compressed) + { + while ((bytes = cups_fill(fp)) > 0) + { + if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) + break; + } + + if (bytes <= 0) + return (-1); + + fp->ptr = fp->buf + pos - fp->bufpos; + fp->pos = pos; + } + else +#endif /* HAVE_LIBZ */ + { + fp->bufpos = lseek(fp->fd, pos, SEEK_SET); + fp->pos = fp->bufpos; + fp->ptr = NULL; + fp->end = NULL; + + DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT, + CUPS_LLCAST fp->pos)); + } + } + + DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return (fp->pos); +} + + +/* + * 'cupsFileStderr()' - Return a CUPS file associated with stderr. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_file_t * /* O - CUPS file */ +cupsFileStderr(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ + + + /* + * Open file descriptor 2 as needed... + */ + + if (!cg->stdio_files[2]) + { + /* + * Flush any pending output on the stdio file... + */ + + fflush(stderr); + + /* + * Open file descriptor 2... + */ + + if ((cg->stdio_files[2] = cupsFileOpenFd(2, "w")) != NULL) + cg->stdio_files[2]->is_stdio = 1; + } + + return (cg->stdio_files[2]); +} + + +/* + * 'cupsFileStdin()' - Return a CUPS file associated with stdin. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_file_t * /* O - CUPS file */ +cupsFileStdin(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ + + + /* + * Open file descriptor 0 as needed... + */ + + if (!cg->stdio_files[0]) + { + /* + * Open file descriptor 0... + */ + + if ((cg->stdio_files[0] = cupsFileOpenFd(0, "r")) != NULL) + cg->stdio_files[0]->is_stdio = 1; + } + + return (cg->stdio_files[0]); +} + + +/* + * 'cupsFileStdout()' - Return a CUPS file associated with stdout. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_file_t * /* O - CUPS file */ +cupsFileStdout(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ + + + /* + * Open file descriptor 1 as needed... + */ + + if (!cg->stdio_files[1]) + { + /* + * Flush any pending output on the stdio file... + */ + + fflush(stdout); + + /* + * Open file descriptor 1... + */ + + if ((cg->stdio_files[1] = cupsFileOpenFd(1, "w")) != NULL) + cg->stdio_files[1]->is_stdio = 1; + } + + return (cg->stdio_files[1]); +} + + +/* + * 'cupsFileTell()' - Return the current file position. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +off_t /* O - File position */ +cupsFileTell(cups_file_t *fp) /* I - CUPS file */ +{ + DEBUG_printf(("2cupsFileTell(fp=%p)", fp)); + DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT, + CUPS_LLCAST (fp ? fp->pos : -1))); + + return (fp ? fp->pos : 0); +} + + +/* + * 'cupsFileUnlock()' - Unlock access to a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +cupsFileUnlock(cups_file_t *fp) /* I - CUPS file */ +{ + /* + * Range check... + */ + + DEBUG_printf(("cupsFileUnlock(fp=%p)", fp)); + + if (!fp || fp->mode == 's') + return (-1); + + /* + * Unlock... + */ + +#ifdef WIN32 + return (_locking(fp->fd, _LK_UNLCK, 0)); +#else + // 07/22/2016 Mopria-notice: lockf is not defined in Android so commenting out + //return (lockf(fp->fd, F_ULOCK, 0)); + return 0; +#endif /* WIN32 */ +} + + +/* + * 'cupsFileWrite()' - Write to a file. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ssize_t /* O - Number of bytes written or -1 on error */ +cupsFileWrite(cups_file_t *fp, /* I - CUPS file */ + const char *buf, /* I - Buffer */ + size_t bytes) /* I - Number of bytes to write */ +{ + /* + * Range check input... + */ + + DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", + fp, buf, CUPS_LLCAST bytes)); + + if (!fp || !buf || (fp->mode != 'w' && fp->mode != 's')) + return (-1); + + if (bytes == 0) + return (0); + + /* + * Write the buffer... + */ + + if (fp->mode == 's') + { + if (cups_write(fp, buf, bytes) < 0) + return (-1); + + fp->pos += (off_t)bytes; + + DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + return ((ssize_t)bytes); + } + + if ((fp->ptr + bytes) > fp->end) + if (cupsFileFlush(fp)) + return (-1); + + fp->pos += (off_t)bytes; + + DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); + + if (bytes > sizeof(fp->buf)) + { +#ifdef HAVE_LIBZ + if (fp->compressed) + return (cups_compress(fp, buf, bytes)); + else +#endif /* HAVE_LIBZ */ + return (cups_write(fp, buf, bytes)); + } + else + { + memcpy(fp->ptr, buf, bytes); + fp->ptr += bytes; + return ((ssize_t)bytes); + } +} + + +#ifdef HAVE_LIBZ +/* + * 'cups_compress()' - Compress a buffer of data. + */ + +static ssize_t /* O - Number of bytes written or -1 */ +cups_compress(cups_file_t *fp, /* I - CUPS file */ + const char *buf, /* I - Buffer */ + size_t bytes) /* I - Number bytes */ +{ + DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf, + CUPS_LLCAST bytes)); + + /* + * Update the CRC... + */ + + fp->crc = crc32(fp->crc, (const Bytef *)buf, bytes); + + /* + * Deflate the bytes... + */ + + fp->stream.next_in = (Bytef *)buf; + fp->stream.avail_in = bytes; + + while (fp->stream.avail_in > 0) + { + /* + * Flush the current buffer... + */ + + DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d", + fp->stream.avail_in, fp->stream.avail_out)); + + if (fp->stream.avail_out < (int)(sizeof(fp->cbuf) / 8)) + { + if (cups_write(fp, (char *)fp->cbuf, fp->stream.next_out - fp->cbuf) < 0) + return (-1); + + fp->stream.next_out = fp->cbuf; + fp->stream.avail_out = sizeof(fp->cbuf); + } + + deflate(&(fp->stream), Z_NO_FLUSH); + } + + return (bytes); +} +#endif /* HAVE_LIBZ */ + + +/* + * 'cups_fill()' - Fill the input buffer. + */ + +static ssize_t /* O - Number of bytes or -1 */ +cups_fill(cups_file_t *fp) /* I - CUPS file */ +{ + ssize_t bytes; /* Number of bytes read */ +#ifdef HAVE_LIBZ + int status; /* Decompression status */ + const unsigned char *ptr, /* Pointer into buffer */ + *end; /* End of buffer */ +#endif /* HAVE_LIBZ */ + + + DEBUG_printf(("7cups_fill(fp=%p)", fp)); + DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, " + "fp->bufpos=" CUPS_LLFMT ", fp->eof=%d", + fp->ptr, fp->end, fp->buf, CUPS_LLCAST fp->bufpos, fp->eof)); + + if (fp->ptr && fp->end) + fp->bufpos += fp->end - fp->buf; + +#ifdef HAVE_LIBZ + DEBUG_printf(("9cups_fill: fp->compressed=%d", fp->compressed)); + + while (!fp->ptr || fp->compressed) + { + /* + * Check to see if we have read any data yet; if not, see if we have a + * compressed file... + */ + + if (!fp->ptr) + { + /* + * Reset the file position in case we are seeking... + */ + + fp->compressed = 0; + + /* + * Read the first bytes in the file to determine if we have a gzip'd + * file... + */ + + if ((bytes = cups_read(fp, (char *)fp->buf, sizeof(fp->buf))) < 0) + { + /* + * Can't read from file! + */ + + DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT, + CUPS_LLCAST bytes)); + + return (-1); + } + + if (bytes < 10 || fp->buf[0] != 0x1f || + (fp->buf[1] & 255) != 0x8b || + fp->buf[2] != 8 || (fp->buf[3] & 0xe0) != 0) + { + /* + * Not a gzip'd file! + */ + + fp->ptr = fp->buf; + fp->end = fp->buf + bytes; + + DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT, + CUPS_LLCAST bytes)); + + return (bytes); + } + + /* + * Parse header junk: extra data, original name, and comment... + */ + + ptr = (unsigned char *)fp->buf + 10; + end = (unsigned char *)fp->buf + bytes; + + if (fp->buf[3] & 0x04) + { + /* + * Skip extra data... + */ + + if ((ptr + 2) > end) + { + /* + * Can't read from file! + */ + + return (-1); + } + + bytes = ((unsigned char)ptr[1] << 8) | (unsigned char)ptr[0]; + ptr += 2 + bytes; + + if (ptr > end) + { + /* + * Can't read from file! + */ + + return (-1); + } + } + + if (fp->buf[3] & 0x08) + { + /* + * Skip original name data... + */ + + while (ptr < end && *ptr) + ptr ++; + + if (ptr < end) + ptr ++; + else + { + /* + * Can't read from file! + */ + + return (-1); + } + } + + if (fp->buf[3] & 0x10) + { + /* + * Skip comment data... + */ + + while (ptr < end && *ptr) + ptr ++; + + if (ptr < end) + ptr ++; + else + { + /* + * Can't read from file! + */ + + return (-1); + } + } + + if (fp->buf[3] & 0x02) + { + /* + * Skip header CRC data... + */ + + ptr += 2; + + if (ptr > end) + { + /* + * Can't read from file! + */ + + return (-1); + } + } + + /* + * Copy the flate-compressed data to the compression buffer... + */ + + if ((bytes = end - ptr) > 0) + memcpy(fp->cbuf, ptr, bytes); + + /* + * Setup the decompressor data... + */ + + fp->stream.zalloc = (alloc_func)0; + fp->stream.zfree = (free_func)0; + fp->stream.opaque = (voidpf)0; + fp->stream.next_in = (Bytef *)fp->cbuf; + fp->stream.next_out = NULL; + fp->stream.avail_in = bytes; + fp->stream.avail_out = 0; + fp->crc = crc32(0L, Z_NULL, 0); + + if (inflateInit2(&(fp->stream), -15) != Z_OK) + return (-1); + + fp->compressed = 1; + } + + if (fp->compressed) + { + /* + * If we have reached end-of-file, return immediately... + */ + + if (fp->eof) + return (-1); + + /* + * Fill the decompression buffer as needed... + */ + + if (fp->stream.avail_in == 0) + { + if ((bytes = cups_read(fp, (char *)fp->cbuf, sizeof(fp->cbuf))) <= 0) + return (-1); + + fp->stream.next_in = fp->cbuf; + fp->stream.avail_in = bytes; + } + + /* + * Decompress data from the buffer... + */ + + fp->stream.next_out = (Bytef *)fp->buf; + fp->stream.avail_out = sizeof(fp->buf); + + status = inflate(&(fp->stream), Z_NO_FLUSH); + + if (fp->stream.next_out > (Bytef *)fp->buf) + fp->crc = crc32(fp->crc, (Bytef *)fp->buf, + fp->stream.next_out - (Bytef *)fp->buf); + + if (status == Z_STREAM_END) + { + /* + * Read the CRC and length... + */ + + unsigned char trailer[8]; /* Trailer bytes */ + uLong tcrc; /* Trailer CRC */ + + + if (read(fp->fd, trailer, sizeof(trailer)) < sizeof(trailer)) + { + /* + * Can't get it, so mark end-of-file... + */ + + fp->eof = 1; + } + else + { + tcrc = (((((trailer[3] << 8) | trailer[2]) << 8) | trailer[1]) << 8) | + trailer[0]; + + if (tcrc != fp->crc) + { + /* + * Bad CRC, mark end-of-file... + */ + + DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x", + (unsigned int)tcrc, (unsigned int)fp->crc)); + + fp->eof = 1; + + return (-1); + } + + /* + * Otherwise, reset the compressed flag so that we re-read the + * file header... + */ + + fp->compressed = 0; + } + } + + bytes = sizeof(fp->buf) - fp->stream.avail_out; + + /* + * Return the decompressed data... + */ + + fp->ptr = fp->buf; + fp->end = fp->buf + bytes; + + if (bytes) + return (bytes); + } + } +#endif /* HAVE_LIBZ */ + + /* + * Read a buffer's full of data... + */ + + if ((bytes = cups_read(fp, fp->buf, sizeof(fp->buf))) <= 0) + { + /* + * Can't read from file! + */ + + fp->eof = 1; + fp->ptr = fp->buf; + fp->end = fp->buf; + + return (-1); + } + + /* + * Return the bytes we read... + */ + + fp->eof = 0; + fp->ptr = fp->buf; + fp->end = fp->buf + bytes; + + return (bytes); +} + + +/* + * 'cups_open()' - Safely open a file for writing. + * + * We don't allow appending to directories or files that are hard-linked or + * symlinked. + */ + +static int /* O - File descriptor or -1 otherwise */ +cups_open(const char *filename, /* I - Filename */ + int mode) /* I - Open mode */ +{ + int fd; /* File descriptor */ + struct stat fileinfo; /* File information */ +#ifndef WIN32 + struct stat linkinfo; /* Link information */ +#endif /* !WIN32 */ + + + /* + * Open the file... + */ + + if ((fd = open(filename, mode, 0666)) < 0) + return (-1); + + /* + * Then verify that the file descriptor doesn't point to a directory or hard- + * linked file. + */ + + if (fstat(fd, &fileinfo)) + { + close(fd); + return (-1); + } + + if (fileinfo.st_nlink != 1) + { + close(fd); + errno = EPERM; + return (-1); + } + +#ifdef WIN32 + if (fileinfo.st_mode & _S_IFDIR) +#else + if (S_ISDIR(fileinfo.st_mode)) +#endif /* WIN32 */ + { + close(fd); + errno = EISDIR; + return (-1); + } + +#ifndef WIN32 + /* + * Then use lstat to determine whether the filename is a symlink... + */ + + if (lstat(filename, &linkinfo)) + { + close(fd); + return (-1); + } + + if (S_ISLNK(linkinfo.st_mode) || + fileinfo.st_dev != linkinfo.st_dev || + fileinfo.st_ino != linkinfo.st_ino || +#ifdef HAVE_ST_GEN + fileinfo.st_gen != linkinfo.st_gen || +#endif /* HAVE_ST_GEN */ + fileinfo.st_nlink != linkinfo.st_nlink || + fileinfo.st_mode != linkinfo.st_mode) + { + /* + * Yes, don't allow! + */ + + close(fd); + errno = EPERM; + return (-1); + } +#endif /* !WIN32 */ + + return (fd); +} + + +/* + * 'cups_read()' - Read from a file descriptor. + */ + +static ssize_t /* O - Number of bytes read or -1 */ +cups_read(cups_file_t *fp, /* I - CUPS file */ + char *buf, /* I - Buffer */ + size_t bytes) /* I - Number bytes */ +{ + ssize_t total; /* Total bytes read */ + + + DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf, + CUPS_LLCAST bytes)); + + /* + * Loop until we read at least 0 bytes... + */ + + for (;;) + { +#ifdef WIN32 + if (fp->mode == 's') + total = (ssize_t)recv(fp->fd, buf, (unsigned)bytes, 0); + else + total = (ssize_t)read(fp->fd, buf, (unsigned)bytes); +#else + if (fp->mode == 's') + total = recv(fp->fd, buf, bytes, 0); + else + total = read(fp->fd, buf, bytes); +#endif /* WIN32 */ + + DEBUG_printf(("9cups_read: total=" CUPS_LLFMT, CUPS_LLCAST total)); + + if (total >= 0) + break; + + /* + * Reads can be interrupted by signals and unavailable resources... + */ + + if (errno == EAGAIN || errno == EINTR) + continue; + else + return (-1); + } + + /* + * Return the total number of bytes read... + */ + + return (total); +} + + +/* + * 'cups_write()' - Write to a file descriptor. + */ + +static ssize_t /* O - Number of bytes written or -1 */ +cups_write(cups_file_t *fp, /* I - CUPS file */ + const char *buf, /* I - Buffer */ + size_t bytes) /* I - Number bytes */ +{ + size_t total; /* Total bytes written */ + ssize_t count; /* Count this time */ + + + DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf, + CUPS_LLCAST bytes)); + + /* + * Loop until all bytes are written... + */ + + total = 0; + while (bytes > 0) + { +#ifdef WIN32 + if (fp->mode == 's') + count = (ssize_t)send(fp->fd, buf, (unsigned)bytes, 0); + else + count = (ssize_t)write(fp->fd, buf, (unsigned)bytes); +#else + if (fp->mode == 's') + count = send(fp->fd, buf, bytes, 0); + else + count = write(fp->fd, buf, bytes); +#endif /* WIN32 */ + + DEBUG_printf(("9cups_write: count=" CUPS_LLFMT, CUPS_LLCAST count)); + + if (count < 0) + { + /* + * Writes can be interrupted by signals and unavailable resources... + */ + + if (errno == EAGAIN || errno == EINTR) + continue; + else + return (-1); + } + + /* + * Update the counts for the last write call... + */ + + bytes -= count; + total += count; + buf += count; + } + + /* + * Return the total number of bytes written... + */ + + return ((ssize_t)total); +} + + +/* + * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $". + */ diff --git a/cups/file.h b/cups/file.h new file mode 100644 index 0000000..1428c57 --- /dev/null +++ b/cups/file.h @@ -0,0 +1,116 @@ +/* + * "$Id: file.h 7460 2008-04-16 02:19:54Z mike $" + * + * Public file definitions for CUPS. + * + * Since stdio files max out at 256 files on many systems, we have to + * write similar functions without this limit. At the same time, using + * our own file functions allows us to provide transparent support of + * gzip'd print files, PPD files, etc. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + */ + +#ifndef _CUPS_FILE_H_ +# define _CUPS_FILE_H_ + + +/* + * Include necessary headers... + */ + +# include "versioning.h" +# include +# include +# if defined(WIN32) && !defined(__CUPS_SSIZE_T_DEFINED) +# define __CUPS_SSIZE_T_DEFINED +/* Windows does not support the ssize_t type, so map it to off_t... */ +typedef off_t ssize_t; /* @private@ */ +# endif /* WIN32 && !__CUPS_SSIZE_T_DEFINED */ + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * CUPS file definitions... + */ + +# define CUPS_FILE_NONE 0 /* No compression */ +# define CUPS_FILE_GZIP 1 /* GZIP compression */ + + +/* + * Types and structures... + */ + +typedef struct _cups_file_s cups_file_t;/**** CUPS file type ****/ + + +/* + * Prototypes... + */ + +extern int cupsFileClose(cups_file_t *fp) _CUPS_API_1_2; +extern int cupsFileCompression(cups_file_t *fp) _CUPS_API_1_2; +extern int cupsFileEOF(cups_file_t *fp) _CUPS_API_1_2; +extern const char *cupsFileFind(const char *filename, const char *path, + int executable, char *buffer, + int bufsize) _CUPS_API_1_2; +extern int cupsFileFlush(cups_file_t *fp) _CUPS_API_1_2; +extern int cupsFileGetChar(cups_file_t *fp) _CUPS_API_1_2; +extern char *cupsFileGetConf(cups_file_t *fp, char *buf, + size_t buflen, char **value, + int *linenum) _CUPS_API_1_2; +extern size_t cupsFileGetLine(cups_file_t *fp, char *buf, + size_t buflen) _CUPS_API_1_2; +extern char *cupsFileGets(cups_file_t *fp, char *buf, size_t buflen) + _CUPS_API_1_2; +extern int cupsFileLock(cups_file_t *fp, int block) _CUPS_API_1_2; +extern int cupsFileNumber(cups_file_t *fp) _CUPS_API_1_2; +extern cups_file_t *cupsFileOpen(const char *filename, const char *mode) + _CUPS_API_1_2; +extern cups_file_t *cupsFileOpenFd(int fd, const char *mode) _CUPS_API_1_2; +extern int cupsFilePeekChar(cups_file_t *fp) _CUPS_API_1_2; +extern int cupsFilePrintf(cups_file_t *fp, const char *format, ...) + __attribute__((__format__ (__printf__, 2, 3))) + _CUPS_API_1_2; +extern int cupsFilePutChar(cups_file_t *fp, int c) _CUPS_API_1_2; +extern ssize_t cupsFilePutConf(cups_file_t *fp, const char *directive, + const char *value) _CUPS_API_1_4; +extern int cupsFilePuts(cups_file_t *fp, const char *s) + _CUPS_API_1_2; +extern ssize_t cupsFileRead(cups_file_t *fp, char *buf, size_t bytes) + _CUPS_API_1_2; +extern off_t cupsFileRewind(cups_file_t *fp) _CUPS_API_1_2; +extern off_t cupsFileSeek(cups_file_t *fp, off_t pos) _CUPS_API_1_2; +extern cups_file_t *cupsFileStderr(void) _CUPS_API_1_2; +extern cups_file_t *cupsFileStdin(void) _CUPS_API_1_2; +extern cups_file_t *cupsFileStdout(void) _CUPS_API_1_2; +extern off_t cupsFileTell(cups_file_t *fp) _CUPS_API_1_2; +extern int cupsFileUnlock(cups_file_t *fp) _CUPS_API_1_2; +extern ssize_t cupsFileWrite(cups_file_t *fp, const char *buf, + size_t bytes) _CUPS_API_1_2; + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_FILE_H_ */ + +/* + * End of "$Id: file.h 7460 2008-04-16 02:19:54Z mike $". + */ diff --git a/cups/getdevices.c b/cups/getdevices.c new file mode 100644 index 0000000..3416c5e --- /dev/null +++ b/cups/getdevices.c @@ -0,0 +1,283 @@ +/* + * "$Id: getdevices.c 3794 2012-04-23 22:44:16Z msweet $" + * + * cupsGetDevices implementation for CUPS. + * + * Copyright 2008-2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * cupsGetDevices() - Get available printer devices. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * 'cupsGetDevices()' - Get available printer devices. + * + * This function sends a CUPS-Get-Devices request and streams the discovered + * devices to the specified callback function. The "timeout" parameter controls + * how long the request lasts, while the "include_schemes" and "exclude_schemes" + * parameters provide comma-delimited lists of backends to include or omit from + * the request respectively. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +ipp_status_t /* O - Request status - @code IPP_OK@ on success. */ +cupsGetDevices( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + int timeout, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */ + const char *include_schemes, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */ + const char *exclude_schemes, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */ + cups_device_cb_t callback, /* I - Callback function */ + void *user_data) /* I - User data pointer */ +{ + ipp_t *request, /* CUPS-Get-Devices request */ + *response; /* CUPS-Get-Devices response */ + ipp_attribute_t *attr; /* Current attribute */ + const char *device_class, /* device-class value */ + *device_id, /* device-id value */ + *device_info, /* device-info value */ + *device_location, /* device-location value */ + *device_make_and_model, /* device-make-and-model value */ + *device_uri; /* device-uri value */ + int blocking; /* Current blocking-IO mode */ + cups_option_t option; /* in/exclude-schemes option */ + http_status_t status; /* HTTP status of request */ + ipp_state_t state; /* IPP response state */ + + + /* + * Range check input... + */ + + DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", " + "exclude_schemes=\"%s\", callback=%p, user_data=%p)", http, + timeout, include_schemes, exclude_schemes, callback, + user_data)); + + if (!callback) + return (IPP_INTERNAL_ERROR); + + if (!http) + http = _cupsConnect(); + + if (!http) + return (IPP_SERVICE_UNAVAILABLE); + + /* + * Create a CUPS-Get-Devices request... + */ + + request = ippNewRequest(CUPS_GET_DEVICES); + + if (timeout > 0) + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout", + timeout); + + if (include_schemes) + { + option.name = "include-schemes"; + option.value = (char *)include_schemes; + + cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); + } + + if (exclude_schemes) + { + option.name = "exclude-schemes"; + option.value = (char *)exclude_schemes; + + cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); + } + + /* + * Send the request and do any necessary authentication... + */ + + do + { + DEBUG_puts("2cupsGetDevices: Sending request..."); + status = cupsSendRequest(http, request, "/", ippLength(request)); + + DEBUG_puts("2cupsGetDevices: Waiting for response status..."); + while (status == HTTP_CONTINUE) + status = httpUpdate(http); + + if (status != HTTP_OK) + { + httpFlush(http); + + if (status == HTTP_UNAUTHORIZED) + { + /* + * See if we can do authentication... + */ + + DEBUG_puts("2cupsGetDevices: Need authorization..."); + + if (!cupsDoAuthentication(http, "POST", "/")) + httpReconnect(http); + else + { + status = HTTP_AUTHORIZATION_CANCELED; + break; + } + } + +#ifdef HAVE_SSL + else if (status == HTTP_UPGRADE_REQUIRED) + { + /* + * Force a reconnect with encryption... + */ + + DEBUG_puts("2cupsGetDevices: Need encryption..."); + + if (!httpReconnect(http)) + httpEncryption(http, HTTP_ENCRYPT_REQUIRED); + } +#endif /* HAVE_SSL */ + } + } + while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED); + + DEBUG_printf(("2cupsGetDevices: status=%d", status)); + + ippDelete(request); + + if (status != HTTP_OK) + { + _cupsSetHTTPError(status); + return (cupsLastError()); + } + + /* + * Read the response in non-blocking mode... + */ + + blocking = httpGetBlocking(http); + httpBlocking(http, 0); + + response = ippNew(); + device_class = NULL; + device_id = NULL; + device_info = NULL; + device_location = ""; + device_make_and_model = NULL; + device_uri = NULL; + attr = NULL; + + DEBUG_puts("2cupsGetDevices: Reading response..."); + + do + { + if ((state = ippRead(http, response)) == IPP_ERROR) + break; + + DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, + response->last)); + + if (!response->attrs) + continue; + + while (attr != response->last) + { + if (!attr) + attr = response->attrs; + else + attr = attr->next; + + DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d", + attr->name, attr->value_tag)); + + if (!attr->name) + { + if (device_class && device_id && device_info && device_make_and_model && + device_uri) + (*callback)(device_class, device_id, device_info, + device_make_and_model, device_uri, device_location, + user_data); + + device_class = NULL; + device_id = NULL; + device_info = NULL; + device_location = ""; + device_make_and_model = NULL; + device_uri = NULL; + } + else if (!strcmp(attr->name, "device-class") && + attr->value_tag == IPP_TAG_KEYWORD) + device_class = attr->values[0].string.text; + else if (!strcmp(attr->name, "device-id") && + attr->value_tag == IPP_TAG_TEXT) + device_id = attr->values[0].string.text; + else if (!strcmp(attr->name, "device-info") && + attr->value_tag == IPP_TAG_TEXT) + device_info = attr->values[0].string.text; + else if (!strcmp(attr->name, "device-location") && + attr->value_tag == IPP_TAG_TEXT) + device_location = attr->values[0].string.text; + else if (!strcmp(attr->name, "device-make-and-model") && + attr->value_tag == IPP_TAG_TEXT) + device_make_and_model = attr->values[0].string.text; + else if (!strcmp(attr->name, "device-uri") && + attr->value_tag == IPP_TAG_URI) + device_uri = attr->values[0].string.text; + } + } + while (state != IPP_DATA); + + DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, + response->last)); + + if (device_class && device_id && device_info && device_make_and_model && + device_uri) + (*callback)(device_class, device_id, device_info, + device_make_and_model, device_uri, device_location, user_data); + + /* + * Set the IPP status and return... + */ + + httpBlocking(http, blocking); + httpFlush(http); + + if (status == HTTP_ERROR) + _cupsSetError(IPP_INTERNAL_ERROR, strerror(http->error), 0); + else + { + attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); + + DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"", + ippErrorString(response->request.status.status_code), + attr ? attr->values[0].string.text : "")); + + _cupsSetError(response->request.status.status_code, + attr ? attr->values[0].string.text : + ippErrorString(response->request.status.status_code), 0); + } + + ippDelete(response); + + return (cupsLastError()); +} + + +/* + * End of "$Id: getdevices.c 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/getifaddrs.c b/cups/getifaddrs.c new file mode 100644 index 0000000..6c9cc55 --- /dev/null +++ b/cups/getifaddrs.c @@ -0,0 +1,266 @@ +/* + * "$Id: getifaddrs.c 6649 2007-07-11 21:46:42Z mike $" + * + * Network interface functions for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products, all rights reserved. + * + * 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" + * "LICENSE" which should have been included with this file. If this + * file is missing or damaged, see the license at "http://www.cups.org/". + * + * Contents: + * + * _cups_getifaddrs() - Get a list of network interfaces on the system. + * _cups_freeifaddrs() - Free an interface list... + */ + +/* + * Include necessary headers. + */ + +#include "http-private.h" + + +#ifndef HAVE_GETIFADDRS +/* + * '_cups_getifaddrs()' - Get a list of network interfaces on the system. + */ + +int /* O - 0 on success, -1 on error */ +_cups_getifaddrs(struct ifaddrs **addrs)/* O - List of interfaces */ +{ + int sock; /* Socket */ + char buffer[65536], /* Buffer for address info */ + *bufptr, /* Pointer into buffer */ + *bufend; /* End of buffer */ + struct ifconf conf; /* Interface configurations */ + struct sockaddr addr; /* Address data */ + struct ifreq *ifp; /* Interface data */ + int ifpsize; /* Size of interface data */ + struct ifaddrs *temp; /* Pointer to current interface */ + struct ifreq request; /* Interface request */ + + + /* + * Start with an empty list... + */ + + if (addrs == NULL) + return (-1); + + *addrs = NULL; + + /* + * Create a UDP socket to get the interface data... + */ + + memset (&addr, 0, sizeof(addr)); + if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) + return (-1); + + /* + * Try to get the list of interfaces... + */ + + conf.ifc_len = sizeof(buffer); + conf.ifc_buf = buffer; + + if (ioctl(sock, SIOCGIFCONF, &conf) < 0) + { + /* + * Couldn't get the list of interfaces... + */ + + close(sock); + return (-1); + } + + /* + * OK, got the list of interfaces, now lets step through the + * buffer to pull them out... + */ + +# ifdef HAVE_STRUCT_SOCKADDR_SA_LEN +# define sockaddr_len(a) ((a)->sa_len) +# else +# define sockaddr_len(a) (sizeof(struct sockaddr)) +# endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ + + for (bufptr = buffer, bufend = buffer + conf.ifc_len; + bufptr < bufend; + bufptr += ifpsize) + { + /* + * Get the current interface information... + */ + + ifp = (struct ifreq *)bufptr; + ifpsize = sizeof(ifp->ifr_name) + sockaddr_len(&(ifp->ifr_addr)); + + if (ifpsize < sizeof(struct ifreq)) + ifpsize = sizeof(struct ifreq); + + memset(&request, 0, sizeof(request)); + memcpy(request.ifr_name, ifp->ifr_name, sizeof(ifp->ifr_name)); + + /* + * Check the status of the interface... + */ + + if (ioctl(sock, SIOCGIFFLAGS, &request) < 0) + continue; + + /* + * Allocate memory for a single interface record... + */ + + if ((temp = calloc(1, sizeof(struct ifaddrs))) == NULL) + { + /* + * Unable to allocate memory... + */ + + close(sock); + return (-1); + } + + /* + * Add this record to the front of the list and copy the name, flags, + * and network address... + */ + + temp->ifa_next = *addrs; + *addrs = temp; + temp->ifa_name = strdup(ifp->ifr_name); + temp->ifa_flags = request.ifr_flags; + if ((temp->ifa_addr = calloc(1, sockaddr_len(&(ifp->ifr_addr)))) != NULL) + memcpy(temp->ifa_addr, &(ifp->ifr_addr), sockaddr_len(&(ifp->ifr_addr))); + + /* + * Try to get the netmask for the interface... + */ + + if (!ioctl(sock, SIOCGIFNETMASK, &request)) + { + /* + * Got it, make a copy... + */ + + if ((temp->ifa_netmask = calloc(1, sizeof(request.ifr_netmask))) != NULL) + memcpy(temp->ifa_netmask, &(request.ifr_netmask), + sizeof(request.ifr_netmask)); + } + + /* + * Then get the broadcast or point-to-point (destination) address, + * if applicable... + */ + + if (temp->ifa_flags & IFF_BROADCAST) + { + /* + * Have a broadcast address, so get it! + */ + + if (!ioctl(sock, SIOCGIFBRDADDR, &request)) + { + /* + * Got it, make a copy... + */ + + if ((temp->ifa_broadaddr = + calloc(1, sizeof(request.ifr_broadaddr))) != NULL) + memcpy(temp->ifa_broadaddr, &(request.ifr_broadaddr), + sizeof(request.ifr_broadaddr)); + } + } + else if (temp->ifa_flags & IFF_POINTOPOINT) + { + /* + * Point-to-point interface; grab the remote address... + */ + + if (!ioctl(sock, SIOCGIFDSTADDR, &request)) + { + temp->ifa_dstaddr = malloc(sizeof(request.ifr_dstaddr)); + memcpy(temp->ifa_dstaddr, &(request.ifr_dstaddr), + sizeof(request.ifr_dstaddr)); + } + } + } + + /* + * OK, we're done with the socket, close it and return 0... + */ + + close(sock); + + return (0); +} + + +/* + * '_cups_freeifaddrs()' - Free an interface list... + */ + +void +_cups_freeifaddrs(struct ifaddrs *addrs)/* I - Interface list to free */ +{ + struct ifaddrs *next; /* Next interface in list */ + + + while (addrs != NULL) + { + /* + * Make a copy of the next interface pointer... + */ + + next = addrs->ifa_next; + + /* + * Free data values as needed... + */ + + if (addrs->ifa_name) + { + free(addrs->ifa_name); + addrs->ifa_name = NULL; + } + + if (addrs->ifa_addr) + { + free(addrs->ifa_addr); + addrs->ifa_addr = NULL; + } + + if (addrs->ifa_netmask) + { + free(addrs->ifa_netmask); + addrs->ifa_netmask = NULL; + } + + if (addrs->ifa_dstaddr) + { + free(addrs->ifa_dstaddr); + addrs->ifa_dstaddr = NULL; + } + + /* + * Free this node and continue to the next... + */ + + free(addrs); + + addrs = next; + } +} +#endif /* !HAVE_GETIFADDRS */ + + +/* + * End of "$Id: getifaddrs.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/cups/getputfile.c b/cups/getputfile.c new file mode 100644 index 0000000..7e12fe9 --- /dev/null +++ b/cups/getputfile.c @@ -0,0 +1,502 @@ +/* + * "$Id: getputfile.c 7359 2008-02-29 19:01:35Z mike $" + * + * Get/put file functions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * cupsGetFd() - Get a file from the server. + * cupsGetFile() - Get a file from the server. + * cupsPutFd() - Put a file on the server. + * cupsPutFile() - Put a file on the server. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#if defined(WIN32) || defined(__EMX__) +# include +#else +# include +#endif /* WIN32 || __EMX__ */ + + +/* + * 'cupsGetFd()' - Get a file from the server. + * + * This function returns @code HTTP_OK@ when the file is successfully retrieved. + * + * @since CUPS 1.1.20/OS X 10.4@ + */ + +http_status_t /* O - HTTP status */ +cupsGetFd(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *resource, /* I - Resource name */ + int fd) /* I - File descriptor */ +{ + int bytes; /* Number of bytes read */ + char buffer[8192]; /* Buffer for file */ + http_status_t status; /* HTTP status from server */ + char if_modified_since[HTTP_MAX_VALUE]; + /* If-Modified-Since header */ + + + /* + * Range check input... + */ + + DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)", http, + resource, fd)); + + if (!resource || fd < 0) + { + if (http) + http->error = EINVAL; + + return (HTTP_ERROR); + } + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (HTTP_SERVICE_UNAVAILABLE); + + /* + * Then send GET requests to the HTTP server... + */ + + strlcpy(if_modified_since, httpGetField(http, HTTP_FIELD_IF_MODIFIED_SINCE), + sizeof(if_modified_since)); + + do + { + httpClearFields(http); + httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); + httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE, if_modified_since); + + if (httpGet(http, resource)) + { + if (httpReconnect(http)) + { + status = HTTP_ERROR; + break; + } + else + { + status = HTTP_UNAUTHORIZED; + continue; + } + } + + while ((status = httpUpdate(http)) == HTTP_CONTINUE); + + if (status == HTTP_UNAUTHORIZED) + { + /* + * Flush any error message... + */ + + httpFlush(http); + + /* + * See if we can do authentication... + */ + + if (cupsDoAuthentication(http, "GET", resource)) + { + status = HTTP_AUTHORIZATION_CANCELED; + break; + } + + if (httpReconnect(http)) + { + status = HTTP_ERROR; + break; + } + + continue; + } +#ifdef HAVE_SSL + else if (status == HTTP_UPGRADE_REQUIRED) + { + /* Flush any error message... */ + httpFlush(http); + + /* Reconnect... */ + if (httpReconnect(http)) + { + status = HTTP_ERROR; + break; + } + + /* Upgrade with encryption... */ + httpEncryption(http, HTTP_ENCRYPT_REQUIRED); + + /* Try again, this time with encryption enabled... */ + continue; + } +#endif /* HAVE_SSL */ + } + while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED); + + /* + * See if we actually got the file or an error... + */ + + if (status == HTTP_OK) + { + /* + * Yes, copy the file... + */ + + while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) + write(fd, buffer, bytes); + } + else + { + _cupsSetHTTPError(status); + httpFlush(http); + } + + /* + * Return the request status... + */ + + DEBUG_printf(("1cupsGetFd: Returning %d...", status)); + + return (status); +} + + +/* + * 'cupsGetFile()' - Get a file from the server. + * + * This function returns @code HTTP_OK@ when the file is successfully retrieved. + * + * @since CUPS 1.1.20/OS X 10.4@ + */ + +http_status_t /* O - HTTP status */ +cupsGetFile(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *resource, /* I - Resource name */ + const char *filename) /* I - Filename */ +{ + int fd; /* File descriptor */ + http_status_t status; /* Status */ + + + /* + * Range check input... + */ + + if (!http || !resource || !filename) + { + if (http) + http->error = EINVAL; + + return (HTTP_ERROR); + } + + /* + * Create the file... + */ + + if ((fd = open(filename, O_WRONLY | O_EXCL | O_TRUNC)) < 0) + { + /* + * Couldn't open the file! + */ + + http->error = errno; + + return (HTTP_ERROR); + } + + /* + * Get the file... + */ + + status = cupsGetFd(http, resource, fd); + + /* + * If the file couldn't be gotten, then remove the file... + */ + + close(fd); + + if (status != HTTP_OK) + unlink(filename); + + /* + * Return the HTTP status code... + */ + + return (status); +} + + +/* + * 'cupsPutFd()' - Put a file on the server. + * + * This function returns @code HTTP_CREATED@ when the file is stored + * successfully. + * + * @since CUPS 1.1.20/OS X 10.4@ + */ + +http_status_t /* O - HTTP status */ +cupsPutFd(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *resource, /* I - Resource name */ + int fd) /* I - File descriptor */ +{ + int bytes, /* Number of bytes read */ + retries; /* Number of retries */ + char buffer[8192]; /* Buffer for file */ + http_status_t status; /* HTTP status from server */ + + + /* + * Range check input... + */ + + DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)", http, + resource, fd)); + + if (!resource || fd < 0) + { + if (http) + http->error = EINVAL; + + return (HTTP_ERROR); + } + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (HTTP_SERVICE_UNAVAILABLE); + + /* + * Then send PUT requests to the HTTP server... + */ + + retries = 0; + + do + { + DEBUG_printf(("2cupsPutFd: starting attempt, authstring=\"%s\"...", + http->authstring)); + + httpClearFields(http); + httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); + httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked"); + httpSetExpect(http, HTTP_CONTINUE); + + if (httpPut(http, resource)) + { + if (httpReconnect(http)) + { + status = HTTP_ERROR; + break; + } + else + { + status = HTTP_UNAUTHORIZED; + continue; + } + } + + /* + * Wait up to 1 second for a 100-continue response... + */ + + if (httpWait(http, 1000)) + status = httpUpdate(http); + else + status = HTTP_CONTINUE; + + if (status == HTTP_CONTINUE) + { + /* + * Copy the file... + */ + + lseek(fd, 0, SEEK_SET); + + while ((bytes = read(fd, buffer, sizeof(buffer))) > 0) + if (httpCheck(http)) + { + if ((status = httpUpdate(http)) != HTTP_CONTINUE) + break; + } + else + httpWrite2(http, buffer, bytes); + } + + if (status == HTTP_CONTINUE) + { + httpWrite2(http, buffer, 0); + + while ((status = httpUpdate(http)) == HTTP_CONTINUE); + } + + if (status == HTTP_ERROR && !retries) + { + DEBUG_printf(("2cupsPutFd: retry on status %d", status)); + + retries ++; + + /* Flush any error message... */ + httpFlush(http); + + /* Reconnect... */ + if (httpReconnect(http)) + { + status = HTTP_ERROR; + break; + } + + /* Try again... */ + continue; + } + + DEBUG_printf(("2cupsPutFd: status=%d", status)); + + if (status == HTTP_UNAUTHORIZED) + { + /* + * Flush any error message... + */ + + httpFlush(http); + + /* + * See if we can do authentication... + */ + + if (cupsDoAuthentication(http, "PUT", resource)) + { + status = HTTP_AUTHORIZATION_CANCELED; + break; + } + + if (httpReconnect(http)) + { + status = HTTP_ERROR; + break; + } + + continue; + } +#ifdef HAVE_SSL + else if (status == HTTP_UPGRADE_REQUIRED) + { + /* Flush any error message... */ + httpFlush(http); + + /* Reconnect... */ + if (httpReconnect(http)) + { + status = HTTP_ERROR; + break; + } + + /* Upgrade with encryption... */ + httpEncryption(http, HTTP_ENCRYPT_REQUIRED); + + /* Try again, this time with encryption enabled... */ + continue; + } +#endif /* HAVE_SSL */ + } + while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED || + (status == HTTP_ERROR && retries < 2)); + + /* + * See if we actually put the file or an error... + */ + + if (status != HTTP_CREATED) + { + _cupsSetHTTPError(status); + httpFlush(http); + } + + DEBUG_printf(("1cupsPutFd: Returning %d...", status)); + + return (status); +} + + +/* + * 'cupsPutFile()' - Put a file on the server. + * + * This function returns @code HTTP_CREATED@ when the file is stored + * successfully. + * + * @since CUPS 1.1.20/OS X 10.4@ + */ + +http_status_t /* O - HTTP status */ +cupsPutFile(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *resource, /* I - Resource name */ + const char *filename) /* I - Filename */ +{ + int fd; /* File descriptor */ + http_status_t status; /* Status */ + + + /* + * Range check input... + */ + + if (!http || !resource || !filename) + { + if (http) + http->error = EINVAL; + + return (HTTP_ERROR); + } + + /* + * Open the local file... + */ + + if ((fd = open(filename, O_RDONLY)) < 0) + { + /* + * Couldn't open the file! + */ + + http->error = errno; + + return (HTTP_ERROR); + } + + /* + * Put the file... + */ + + status = cupsPutFd(http, resource, fd); + + close(fd); + + return (status); +} + + +/* + * End of "$Id: getputfile.c 7359 2008-02-29 19:01:35Z mike $". + */ diff --git a/cups/globals.c b/cups/globals.c new file mode 100644 index 0000000..418452f --- /dev/null +++ b/cups/globals.c @@ -0,0 +1,384 @@ +/* + * "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $" + * + * Global variable access routines for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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. + * + * Contents: + * + * _cupsGlobalLock() - Lock the global mutex. + * _cupsGlobals() - Return a pointer to thread local storage + * _cupsGlobalUnlock() - Unlock the global mutex. + * DllMain() - Main entry for library. + * cups_fix_path() - Fix a file path to use forward slashes consistently. + * cups_globals_alloc() - Allocate and initialize global data. + * cups_globals_free() - Free global data. + * cups_globals_init() - Initialize environment variables. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * Local globals... + */ + + +static _cups_threadkey_t cups_globals_key = _CUPS_THREADKEY_INITIALIZER; + /* Thread local storage key */ +#ifdef HAVE_PTHREAD_H +static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT; + /* One-time initialization object */ +#endif /* HAVE_PTHREAD_H */ +#if defined(HAVE_PTHREAD_H) || defined(WIN32) +static _cups_mutex_t cups_global_mutex = _CUPS_MUTEX_INITIALIZER; + /* Global critical section */ +#endif /* HAVE_PTHREAD_H || WIN32 */ + + +/* + * Local functions... + */ + +#ifdef WIN32 +static void cups_fix_path(char *path); +#endif /* WIN32 */ +static _cups_globals_t *cups_globals_alloc(void); +#if defined(HAVE_PTHREAD_H) || defined(WIN32) +static void cups_globals_free(_cups_globals_t *g); +#endif /* HAVE_PTHREAD_H || WIN32 */ +#ifdef HAVE_PTHREAD_H +static void cups_globals_init(void); +#endif /* HAVE_PTHREAD_H */ + + +/* + * '_cupsGlobalLock()' - Lock the global mutex. + */ + +void +_cupsGlobalLock(void) +{ +#ifdef HAVE_PTHREAD_H + pthread_mutex_lock(&cups_global_mutex); +#elif defined(WIN32) + EnterCriticalSection(&cups_global_mutex.m_criticalSection); +#endif /* HAVE_PTHREAD_H */ +} + + +/* + * '_cupsGlobals()' - Return a pointer to thread local storage + */ + +_cups_globals_t * /* O - Pointer to global data */ +_cupsGlobals(void) +{ + _cups_globals_t *cg; /* Pointer to global data */ + + +#ifdef HAVE_PTHREAD_H + /* + * Initialize the global data exactly once... + */ + + pthread_once(&cups_globals_key_once, cups_globals_init); +#endif /* HAVE_PTHREAD_H */ + + /* + * See if we have allocated the data yet... + */ + + if ((cg = (_cups_globals_t *)_cupsThreadGetData(cups_globals_key)) == NULL) + { + /* + * No, allocate memory as set the pointer for the key... + */ + + if ((cg = cups_globals_alloc()) != NULL) + _cupsThreadSetData(cups_globals_key, cg); + } + + /* + * Return the pointer to the data... + */ + + return (cg); +} + + +/* + * '_cupsGlobalUnlock()' - Unlock the global mutex. + */ + +void +_cupsGlobalUnlock(void) +{ +#ifdef HAVE_PTHREAD_H + pthread_mutex_unlock(&cups_global_mutex); +#elif defined(WIN32) + LeaveCriticalSection(&cups_global_mutex.m_criticalSection); +#endif /* HAVE_PTHREAD_H */ +} + + +#ifdef WIN32 +/* + * 'DllMain()' - Main entry for library. + */ + +BOOL WINAPI /* O - Success/failure */ +DllMain(HINSTANCE hinst, /* I - DLL module handle */ + DWORD reason, /* I - Reason */ + LPVOID reserved) /* I - Unused */ +{ + _cups_globals_t *cg; /* Global data */ + + + (void)hinst; + (void)reserved; + + switch (reason) + { + case DLL_PROCESS_ATTACH : /* Called on library initialization */ + InitializeCriticalSection(&cups_global_mutex.m_criticalSection); + + if ((cups_globals_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) + return (FALSE); + break; + + case DLL_THREAD_DETACH : /* Called when a thread terminates */ + if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL) + cups_globals_free(cg); + break; + + case DLL_PROCESS_DETACH : /* Called when library is unloaded */ + if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL) + cups_globals_free(cg); + + TlsFree(cups_globals_key); + DeleteCriticalSection(&cups_global_mutex.m_criticalSection); + break; + + default: + break; + } + + return (TRUE); +} +#endif /* WIN32 */ + + +/* + * 'cups_globals_alloc()' - Allocate and initialize global data. + */ + +static _cups_globals_t * /* O - Pointer to global data */ +cups_globals_alloc(void) +{ + _cups_globals_t *cg = malloc(sizeof(_cups_globals_t)); + /* Pointer to global data */ +#ifdef WIN32 + HKEY key; /* Registry key */ + DWORD size; /* Size of string */ + static char installdir[1024] = "", /* Install directory */ + confdir[1024] = "", /* Server root directory */ + localedir[1024] = ""; /* Locale directory */ +#endif /* WIN32 */ + + + if (!cg) + return (NULL); + + /* + * Clear the global storage and set the default encryption and password + * callback values... + */ + + memset(cg, 0, sizeof(_cups_globals_t)); + cg->encryption = (http_encryption_t)-1; + cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; + cg->any_root = 1; + cg->expired_certs = 1; + cg->expired_root = 1; + + /* + * Then set directories as appropriate... + */ + +#ifdef WIN32 + if (!installdir[0]) + { + /* + * Open the registry... + */ + + strcpy(installdir, "C:/Program Files/cups.org"); + + if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, + &key)) + { + /* + * Grab the installation directory... + */ + + char *ptr; /* Pointer into installdir */ + + size = sizeof(installdir); + RegQueryValueEx(key, "installdir", NULL, NULL, installdir, &size); + RegCloseKey(key); + + for (ptr = installdir; *ptr;) + { + if (*ptr == '\\') + { + if (ptr[1]) + *ptr++ = '/'; + else + *ptr = '\0'; /* Strip trailing \ */ + } + else if (*ptr == '/' && !ptr[1]) + *ptr = '\0'; /* Strip trailing / */ + else + ptr ++; + } + } + + snprintf(confdir, sizeof(confdir), "%s/conf", installdir); + snprintf(localedir, sizeof(localedir), "%s/locale", installdir); + } + + if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL) + cg->cups_datadir = installdir; + + if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) + cg->cups_serverbin = installdir; + + if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) + cg->cups_serverroot = confdir; + + if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) + cg->cups_statedir = confdir; + + if ((cg->localedir = getenv("LOCALEDIR")) == NULL) + cg->localedir = localedir; + +#else +# ifdef HAVE_GETEUID + if ((geteuid() != getuid() && getuid()) || getegid() != getgid()) +# else + if (!getuid()) +# endif /* HAVE_GETEUID */ + { + /* + * When running setuid/setgid, don't allow environment variables to override + * the directories... + */ + + cg->cups_datadir = CUPS_DATADIR; + cg->cups_serverbin = CUPS_SERVERBIN; + cg->cups_serverroot = CUPS_SERVERROOT; + cg->cups_statedir = CUPS_STATEDIR; + cg->localedir = CUPS_LOCALEDIR; + } + else + { + /* + * Allow directories to be overridden by environment variables. + */ + + if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL) + cg->cups_datadir = CUPS_DATADIR; + + if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) + cg->cups_serverbin = CUPS_SERVERBIN; + + if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) + cg->cups_serverroot = CUPS_SERVERROOT; + + if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) + cg->cups_statedir = CUPS_STATEDIR; + + if ((cg->localedir = getenv("LOCALEDIR")) == NULL) + cg->localedir = CUPS_LOCALEDIR; + } +#endif /* WIN32 */ + + return (cg); +} + + +/* + * 'cups_globals_free()' - Free global data. + */ + +#if defined(HAVE_PTHREAD_H) || defined(WIN32) +static void +cups_globals_free(_cups_globals_t *cg) /* I - Pointer to global data */ +{ + _cups_buffer_t *buffer, /* Current read/write buffer */ + *next; /* Next buffer */ + + + if (cg->last_status_message) + _cupsStrFree(cg->last_status_message); + + for (buffer = cg->cups_buffers; buffer; buffer = next) + { + next = buffer->next; + free(buffer); + } + + cupsArrayDelete(cg->leg_size_lut); + cupsArrayDelete(cg->ppd_size_lut); + cupsArrayDelete(cg->pwg_size_lut); + + httpClose(cg->http); + + _httpFreeCredentials(cg->tls_credentials); + + cupsFileClose(cg->stdio_files[0]); + cupsFileClose(cg->stdio_files[1]); + cupsFileClose(cg->stdio_files[2]); + + cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings); + + free(cg); +} +#endif /* HAVE_PTHREAD_H || WIN32 */ + + +#ifdef HAVE_PTHREAD_H +/* + * 'cups_globals_init()' - Initialize environment variables. + */ + +static void +cups_globals_init(void) +{ + /* + * Register the global data for this thread... + */ + + pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free); +} +#endif /* HAVE_PTHREAD_H */ + + +/* + * End of "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $". + */ diff --git a/cups/http-addr.c b/cups/http-addr.c new file mode 100644 index 0000000..4b09d8e --- /dev/null +++ b/cups/http-addr.c @@ -0,0 +1,703 @@ +/* + * "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $" + * + * HTTP address routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products, all rights reserved. + * + * 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/". + * + * Contents: + * + * httpAddrAny() - Check for the "any" address. + * httpAddrEqual() - Compare two addresses. + * httpAddrLocalhost() - Check for the local loopback address. + * httpAddrLookup() - Lookup the hostname associated with the address. + * _httpAddrPort() - Get the port number associated with an address. + * _httpAddrSetPort() - Set the port number associated with an address. + * httpAddrString() - Convert an IP address to a dotted string. + * httpGetHostByName() - Lookup a hostname or IP address, and return + * address records for the specified name. + * httpGetHostname() - Get the FQDN for the local system. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#ifdef HAVE_RESOLV_H +# include +#endif /* HAVE_RESOLV_H */ +#ifdef __APPLE__ +# include +# include +#endif /* __APPLE__ */ + + +/* + * 'httpAddrAny()' - Check for the "any" address. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 if "any", 0 otherwise */ +httpAddrAny(const http_addr_t *addr) /* I - Address to check */ +{ + if (!addr) + return (0); + +#ifdef AF_INET6 + if (addr->addr.sa_family == AF_INET6 && + IN6_IS_ADDR_UNSPECIFIED(&(addr->ipv6.sin6_addr))) + return (1); +#endif /* AF_INET6 */ + + if (addr->addr.sa_family == AF_INET && + ntohl(addr->ipv4.sin_addr.s_addr) == 0x00000000) + return (1); + + return (0); +} + + +/* + * 'httpAddrEqual()' - Compare two addresses. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 if equal, 0 if not */ +httpAddrEqual(const http_addr_t *addr1, /* I - First address */ + const http_addr_t *addr2) /* I - Second address */ +{ + if (!addr1 && !addr2) + return (1); + + if (!addr1 || !addr2) + return (0); + + if (addr1->addr.sa_family != addr2->addr.sa_family) + return (0); + +#ifdef AF_LOCAL + if (addr1->addr.sa_family == AF_LOCAL) + return (!strcmp(addr1->un.sun_path, addr2->un.sun_path)); +#endif /* AF_LOCAL */ + +#ifdef AF_INET6 + if (addr1->addr.sa_family == AF_INET6) + return (!memcmp(&(addr1->ipv6.sin6_addr), &(addr2->ipv6.sin6_addr), 16)); +#endif /* AF_INET6 */ + + return (addr1->ipv4.sin_addr.s_addr == addr2->ipv4.sin_addr.s_addr); +} + + +/* + * 'httpAddrLength()' - Return the length of the address in bytes. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Length in bytes */ +httpAddrLength(const http_addr_t *addr) /* I - Address */ +{ + if (!addr) + return (0); + +#ifdef AF_INET6 + if (addr->addr.sa_family == AF_INET6) + return (sizeof(addr->ipv6)); + else +#endif /* AF_INET6 */ +#ifdef AF_LOCAL + if (addr->addr.sa_family == AF_LOCAL) + return (offsetof(struct sockaddr_un, sun_path) + + strlen(addr->un.sun_path) + 1); + else +#endif /* AF_LOCAL */ + if (addr->addr.sa_family == AF_INET) + return (sizeof(addr->ipv4)); + else + return (0); + +} + + +/* + * 'httpAddrLocalhost()' - Check for the local loopback address. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 if local host, 0 otherwise */ +httpAddrLocalhost( + const http_addr_t *addr) /* I - Address to check */ +{ + if (!addr) + return (1); + +#ifdef AF_INET6 + if (addr->addr.sa_family == AF_INET6 && + IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr))) + return (1); +#endif /* AF_INET6 */ + +#ifdef AF_LOCAL + if (addr->addr.sa_family == AF_LOCAL) + return (1); +#endif /* AF_LOCAL */ + + if (addr->addr.sa_family == AF_INET && + (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000) + return (1); + + return (0); +} + + +#ifdef __sgi +# define ADDR_CAST (struct sockaddr *) +#else +# define ADDR_CAST (char *) +#endif /* __sgi */ + + +/* + * 'httpAddrLookup()' - Lookup the hostname associated with the address. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - Host name */ +httpAddrLookup( + const http_addr_t *addr, /* I - Address to lookup */ + char *name, /* I - Host name buffer */ + int namelen) /* I - Size of name buffer */ +{ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name, + namelen)); + + /* + * Range check input... + */ + + if (!addr || !name || namelen <= 2) + { + if (name && namelen >= 1) + *name = '\0'; + + return (NULL); + } + +#ifdef AF_LOCAL + if (addr->addr.sa_family == AF_LOCAL) + { + strlcpy(name, addr->un.sun_path, namelen); + return (name); + } +#endif /* AF_LOCAL */ + + /* + * Optimize lookups for localhost/loopback addresses... + */ + + if (httpAddrLocalhost(addr)) + { + strlcpy(name, "localhost", namelen); + return (name); + } + +#ifdef HAVE_RES_INIT + /* + * STR #2920: Initialize resolver after failure in cups-polld + * + * If the previous lookup failed, re-initialize the resolver to prevent + * temporary network errors from persisting. This *should* be handled by + * the resolver libraries, but apparently the glibc folks do not agree. + * + * We set a flag at the end of this function if we encounter an error that + * requires reinitialization of the resolver functions. We then call + * res_init() if the flag is set on the next call here or in httpAddrLookup(). + */ + + if (cg->need_res_init) + { + res_init(); + + cg->need_res_init = 0; + } +#endif /* HAVE_RES_INIT */ + +#ifdef HAVE_GETNAMEINFO + { + /* + * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN + * + * FWIW, I think this is really a bug in the implementation of + * getnameinfo(), but falling back on httpAddrString() is easy to + * do... + */ + + int error = getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen, + NULL, 0, 0); + + if (error) + { + if (error == EAI_FAIL) + cg->need_res_init = 1; + + return (httpAddrString(addr, name, namelen)); + } + } +#else + { + struct hostent *host; /* Host from name service */ + + +# ifdef AF_INET6 + if (addr->addr.sa_family == AF_INET6) + host = gethostbyaddr(ADDR_CAST &(addr->ipv6.sin6_addr), + sizeof(struct in_addr), AF_INET6); + else +# endif /* AF_INET6 */ + host = gethostbyaddr(ADDR_CAST &(addr->ipv4.sin_addr), + sizeof(struct in_addr), AF_INET); + + if (host == NULL) + { + /* + * No hostname, so return the raw address... + */ + + if (h_errno == NO_RECOVERY) + cg->need_res_init = 1; + + return (httpAddrString(addr, name, namelen)); + } + + strlcpy(name, host->h_name, namelen); + } +#endif /* HAVE_GETNAMEINFO */ + + DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name)); + + return (name); +} + + +/* + * '_httpAddrPort()' - Get the port number associated with an address. + */ + +int /* O - Port number */ +_httpAddrPort(http_addr_t *addr) /* I - Address */ +{ + if (!addr) + return (ippPort()); +#ifdef AF_INET6 + else if (addr->addr.sa_family == AF_INET6) + return (ntohs(addr->ipv6.sin6_port)); +#endif /* AF_INET6 */ + else if (addr->addr.sa_family == AF_INET) + return (ntohs(addr->ipv4.sin_port)); + else + return (ippPort()); +} + + +/* + * '_httpAddrSetPort()' - Set the port number associated with an address. + */ + +void +_httpAddrSetPort(http_addr_t *addr, /* I - Address */ + int port) /* I - Port */ +{ + if (!addr || port <= 0) + return; + +#ifdef AF_INET6 + if (addr->addr.sa_family == AF_INET6) + addr->ipv6.sin6_port = htons(port); + else +#endif /* AF_INET6 */ + if (addr->addr.sa_family == AF_INET) + addr->ipv4.sin_port = htons(port); +} + + +/* + * 'httpAddrString()' - Convert an address to a numeric string. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - Numeric address string */ +httpAddrString(const http_addr_t *addr, /* I - Address to convert */ + char *s, /* I - String buffer */ + int slen) /* I - Length of string */ +{ + DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen)); + + /* + * Range check input... + */ + + if (!addr || !s || slen <= 2) + { + if (s && slen >= 1) + *s = '\0'; + + return (NULL); + } + +#ifdef AF_LOCAL + if (addr->addr.sa_family == AF_LOCAL) + { + if (addr->un.sun_path[0] == '/') + strlcpy(s, addr->un.sun_path, slen); + else + strlcpy(s, "localhost", slen); + } + else +#endif /* AF_LOCAL */ + if (addr->addr.sa_family == AF_INET) + { + unsigned temp; /* Temporary address */ + + + temp = ntohl(addr->ipv4.sin_addr.s_addr); + + snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255, + (temp >> 16) & 255, (temp >> 8) & 255, temp & 255); + } +#ifdef AF_INET6 + else if (addr->addr.sa_family == AF_INET6) + { + char *sptr, /* Pointer into string */ + temps[64]; /* Temporary string for address */ + +# ifdef HAVE_GETNAMEINFO + if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps), + NULL, 0, NI_NUMERICHOST)) + { + /* + * If we get an error back, then the address type is not supported + * and we should zero out the buffer... + */ + + s[0] = '\0'; + + return (NULL); + } + else if ((sptr = strchr(temps, '%')) != NULL) + { + /* + * Convert "%zone" to "+zone" to match URI form... + */ + + *sptr = '+'; + } + +# else + int i; /* Looping var */ + unsigned temp; /* Current value */ + const char *prefix; /* Prefix for address */ + + + prefix = ""; + for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++) + { + temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); + + snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, + (temp >> 16) & 0xffff); + prefix = ":"; + sptr += strlen(sptr); + + temp &= 0xffff; + + if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1]) + { + snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp); + sptr += strlen(sptr); + } + } + + if (i < 4) + { + while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i]) + i ++; + + if (i < 4) + { + snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix); + prefix = ":"; + sptr += strlen(sptr); + + for (; i < 4; i ++) + { + temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); + + if ((temp & 0xffff0000) || + (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1])) + { + snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, + (temp >> 16) & 0xffff); + sptr += strlen(sptr); + } + + snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, + temp & 0xffff); + sptr += strlen(sptr); + } + } + else if (sptr == s) + { + /* + * Empty address... + */ + + strlcpy(temps, "::", sizeof(temps)); + } + else + { + /* + * Empty at end... + */ + + strlcpy(sptr, "::", sizeof(temps) - (sptr - temps)); + } + } +# endif /* HAVE_GETNAMEINFO */ + + /* + * Add "[v1." and "]" around IPv6 address to convert to URI form. + */ + + snprintf(s, slen, "[v1.%s]", temps); + } +#endif /* AF_INET6 */ + else + strlcpy(s, "UNKNOWN", slen); + + DEBUG_printf(("1httpAddrString: returning \"%s\"...", s)); + + return (s); +} + + +/* + * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return + * address records for the specified name. + * + * @deprecated@ + */ + +struct hostent * /* O - Host entry */ +httpGetHostByName(const char *name) /* I - Hostname or IP address */ +{ + const char *nameptr; /* Pointer into name */ + unsigned ip[4]; /* IP address components */ + _cups_globals_t *cg = _cupsGlobals(); + /* Pointer to library globals */ + + + DEBUG_printf(("httpGetHostByName(name=\"%s\")", name)); + + /* + * Avoid lookup delays and configuration problems when connecting + * to the localhost address... + */ + + if (!strcmp(name, "localhost")) + name = "127.0.0.1"; + + /* + * This function is needed because some operating systems have a + * buggy implementation of gethostbyname() that does not support + * IP addresses. If the first character of the name string is a + * number, then sscanf() is used to extract the IP components. + * We then pack the components into an IPv4 address manually, + * since the inet_aton() function is deprecated. We use the + * htonl() macro to get the right byte order for the address. + * + * We also support domain sockets when supported by the underlying + * OS... + */ + +#ifdef AF_LOCAL + if (name[0] == '/') + { + /* + * A domain socket address, so make an AF_LOCAL entry and return it... + */ + + cg->hostent.h_name = (char *)name; + cg->hostent.h_aliases = NULL; + cg->hostent.h_addrtype = AF_LOCAL; + cg->hostent.h_length = strlen(name) + 1; + cg->hostent.h_addr_list = cg->ip_ptrs; + cg->ip_ptrs[0] = (char *)name; + cg->ip_ptrs[1] = NULL; + + DEBUG_puts("1httpGetHostByName: returning domain socket address..."); + + return (&cg->hostent); + } +#endif /* AF_LOCAL */ + + for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++); + + if (!*nameptr) + { + /* + * We have an IPv4 address; break it up and provide the host entry + * to the caller. + */ + + if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) + return (NULL); /* Must have 4 numbers */ + + if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255) + return (NULL); /* Invalid byte ranges! */ + + cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) | + ip[3])); + + /* + * Fill in the host entry and return it... + */ + + cg->hostent.h_name = (char *)name; + cg->hostent.h_aliases = NULL; + cg->hostent.h_addrtype = AF_INET; + cg->hostent.h_length = 4; + cg->hostent.h_addr_list = cg->ip_ptrs; + cg->ip_ptrs[0] = (char *)&(cg->ip_addr); + cg->ip_ptrs[1] = NULL; + + DEBUG_puts("1httpGetHostByName: returning IPv4 address..."); + + return (&cg->hostent); + } + else + { + /* + * Use the gethostbyname() function to get the IPv4 address for + * the name... + */ + + DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)..."); + + return (gethostbyname(name)); + } +} + + +/* + * 'httpGetHostname()' - Get the FQDN for the connection or local system. + * + * When "http" points to a connected socket, return the hostname or + * address that was used in the call to httpConnect() or httpConnectEncrypt(). + * Otherwise, return the FQDN for the local system using both gethostname() + * and gethostbyname() to get the local hostname with domain. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +const char * /* O - FQDN for connection or system */ +httpGetHostname(http_t *http, /* I - HTTP connection or NULL */ + char *s, /* I - String buffer for name */ + int slen) /* I - Size of buffer */ +{ + if (!s || slen <= 1) + return (NULL); + + if (http) + { + if (http->hostname[0] == '/') + strlcpy(s, "localhost", slen); + else + strlcpy(s, http->hostname, slen); + } + else + { + /* + * Get the hostname... + */ + + if (gethostname(s, slen) < 0) + strlcpy(s, "localhost", slen); + + if (!strchr(s, '.')) + { +#ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME + /* + * The hostname is not a FQDN, so use the local hostname from the + * SystemConfiguration framework... + */ + + SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault, + CFSTR("libcups"), NULL, NULL); + /* System configuration data */ + CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL; + /* Local host name */ + char localStr[1024]; /* Local host name C string */ + + if (local && CFStringGetCString(local, localStr, sizeof(localStr), + kCFStringEncodingUTF8)) + { + /* + * Append ".local." to the hostname we get... + */ + + snprintf(s, slen, "%s.local.", localStr); + } + + if (local) + CFRelease(local); + if (sc) + CFRelease(sc); + +#else + /* + * The hostname is not a FQDN, so look it up... + */ + + struct hostent *host; /* Host entry to get FQDN */ + + if ((host = gethostbyname(s)) != NULL && host->h_name) + { + /* + * Use the resolved hostname... + */ + + strlcpy(s, host->h_name, slen); + } +#endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */ + } + } + + /* + * Return the hostname with as much domain info as we have... + */ + + return (s); +} + + +/* + * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $". + */ diff --git a/cups/http-addrlist.c b/cups/http-addrlist.c new file mode 100644 index 0000000..f9cd1f3 --- /dev/null +++ b/cups/http-addrlist.c @@ -0,0 +1,845 @@ +/* + * "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $" + * + * HTTP address list routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * Contents: + * + * httpAddrConnect() - Connect to any of the addresses in the list. + * httpAddrConnect2() - Connect to any of the addresses in the list with a + * timeout and optional cancel. + * httpAddrFreeList() - Free an address list. + * httpAddrGetList() - Get a list of addresses for a hostname. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#ifdef HAVE_RESOLV_H +# include +#endif /* HAVE_RESOLV_H */ +#ifdef HAVE_POLL +# include +#endif /* HAVE_POLL */ +#ifndef WIN32 +# include +#endif /* WIN32 */ + + +/* + * 'httpAddrConnect()' - Connect to any of the addresses in the list. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +http_addrlist_t * /* O - Connected address or NULL on failure */ +httpAddrConnect( + http_addrlist_t *addrlist, /* I - List of potential addresses */ + int *sock) /* O - Socket */ +{ + DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)", addrlist, sock)); + + return (httpAddrConnect2(addrlist, sock, 30000, NULL)); +} + + +/* + * 'httpAddrConnect2()' - Connect to any of the addresses in the list with a + * timeout and optional cancel. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +http_addrlist_t * /* O - Connected address or NULL on failure */ +httpAddrConnect2( + http_addrlist_t *addrlist, /* I - List of potential addresses */ + int *sock, /* O - Socket */ + int msec, /* I - Timeout in milliseconds */ + int *cancel) /* I - Pointer to "cancel" variable */ +{ + int val; /* Socket option value */ +#ifdef O_NONBLOCK + socklen_t len; /* Length of value */ + http_addr_t peer; /* Peer address */ + int flags, /* Socket flags */ + remaining; /* Remaining timeout */ +# ifdef HAVE_POLL + struct pollfd pfd; /* Polled file descriptor */ +# else + fd_set input_set, /* select() input set */ + output_set; /* select() output set */ + struct timeval timeout; /* Timeout */ +# endif /* HAVE_POLL */ + int nfds; /* Result from select()/poll() */ +#endif /* O_NONBLOCK */ +#ifdef DEBUG + char temp[256]; /* Temporary address string */ +#endif /* DEBUG */ + + + DEBUG_printf(("httpAddrConnect2(addrlist=%p, sock=%p, msec=%d, cancel=%p)", + addrlist, sock, msec, cancel)); + + if (!sock) + { + errno = EINVAL; + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + return (NULL); + } + + if (cancel && *cancel) + return (NULL); + + if (msec <= 0 || getenv("CUPS_DISABLE_ASYNC_CONNECT")) + msec = INT_MAX; + + /* + * Loop through each address until we connect or run out of addresses... + */ + + while (addrlist) + { + if (cancel && *cancel) + return (NULL); + + /* + * Create the socket... + */ + + DEBUG_printf(("2httpAddrConnect2: Trying %s:%d...", + httpAddrString(&(addrlist->addr), temp, sizeof(temp)), + _httpAddrPort(&(addrlist->addr)))); + + if ((*sock = (int)socket(_httpAddrFamily(&(addrlist->addr)), SOCK_STREAM, + 0)) < 0) + { + /* + * Don't abort yet, as this could just be an issue with the local + * system not being configured with IPv4/IPv6/domain socket enabled... + */ + + addrlist = addrlist->next; + continue; + } + + /* + * Set options... + */ + + val = 1; +#ifdef WIN32 + setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, + sizeof(val)); +#else + setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); +#endif /* WIN32 */ + +#ifdef SO_REUSEPORT + val = 1; + setsockopt(*sock, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)); +#endif /* SO_REUSEPORT */ + +#ifdef SO_NOSIGPIPE + val = 1; + setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val)); +#endif /* SO_NOSIGPIPE */ + + /* + * Using TCP_NODELAY improves responsiveness, especially on systems + * with a slow loopback interface... + */ + + val = 1; +#ifdef WIN32 + setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&val, + sizeof(val)); +#else + setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); +#endif /* WIN32 */ + +#ifdef FD_CLOEXEC + /* + * Close this socket when starting another process... + */ + + fcntl(*sock, F_SETFD, FD_CLOEXEC); +#endif /* FD_CLOEXEC */ + +#ifdef O_NONBLOCK + /* + * Do an asynchronous connect by setting the socket non-blocking... + */ + + DEBUG_printf(("httpAddrConnect2: Setting non-blocking connect()")); + + flags = fcntl(*sock, F_GETFL, 0); + if (msec != INT_MAX) + { + DEBUG_puts("httpAddrConnect2: Setting non-blocking connect()"); + + fcntl(*sock, F_SETFL, flags | O_NONBLOCK); + } +#endif /* O_NONBLOCK */ + + /* + * Then connect... + */ + + if (!connect(*sock, &(addrlist->addr.addr), + httpAddrLength(&(addrlist->addr)))) + { + DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...", + httpAddrString(&(addrlist->addr), temp, sizeof(temp)), + _httpAddrPort(&(addrlist->addr)))); + +#ifdef O_NONBLOCK + fcntl(*sock, F_SETFL, flags); +#endif /* O_NONBLOCK */ + + return (addrlist); + } + +#ifdef O_NONBLOCK +# ifdef WIN32 + if (WSAGetLastError() == WSAEINPROGRESS || + WSAGetLastError() == WSAEWOULDBLOCK) +# else + if (errno == EINPROGRESS || errno == EWOULDBLOCK) +# endif /* WIN32 */ + { + DEBUG_puts("1httpAddrConnect2: Finishing async connect()"); + + fcntl(*sock, F_SETFL, flags); + + for (remaining = msec; remaining > 0; remaining -= 250) + { + do + { + if (cancel && *cancel) + { + /* + * Close this socket and return... + */ + + DEBUG_puts("1httpAddrConnect2: Canceled connect()"); + +# ifdef WIN32 + closesocket(*sock); +# else + close(*sock); +# endif /* WIN32 */ + + *sock = -1; + + return (NULL); + } + +# ifdef HAVE_POLL + pfd.fd = *sock; + pfd.events = POLLIN | POLLOUT; + + nfds = poll(&pfd, 1, remaining > 250 ? 250 : remaining); + + DEBUG_printf(("1httpAddrConnect2: poll() returned %d (%d)", nfds, + errno)); + +# else + FD_ZERO(&input_set); + FD_SET(*sock, &input_set); + output_set = input_set; + + timeout.tv_sec = 0; + timeout.tv_usec = (remaining > 250 ? 250 : remaining) * 1000; + + nfds = select(*sock + 1, &input_set, &output_set, NULL, &timeout); + + DEBUG_printf(("1httpAddrConnect2: select() returned %d (%d)", nfds, + errno)); +# endif /* HAVE_POLL */ + } +# ifdef WIN32 + while (nfds < 0 && (WSAGetLastError() == WSAEINTR || + WSAGetLastError() == WSAEWOULDBLOCK)); +# else + while (nfds < 0 && (errno == EINTR || errno == EAGAIN)); +# endif /* WIN32 */ + + if (nfds > 0) + { + len = sizeof(peer); + if (!getpeername(*sock, (struct sockaddr *)&peer, &len)) + { + DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...", + httpAddrString(&peer, temp, sizeof(temp)), + _httpAddrPort(&peer))); + + return (addrlist); + } + + break; + } + } + } +#endif /* O_NONBLOCK */ + + DEBUG_printf(("1httpAddrConnect2: Unable to connect to %s:%d: %s", + httpAddrString(&(addrlist->addr), temp, sizeof(temp)), + _httpAddrPort(&(addrlist->addr)), strerror(errno))); + +#ifndef WIN32 + if (errno == EINPROGRESS) + errno = ETIMEDOUT; +#endif /* !WIN32 */ + + /* + * Close this socket and move to the next address... + */ + +#ifdef WIN32 + closesocket(*sock); +#else + close(*sock); +#endif /* WIN32 */ + + *sock = -1; + addrlist = addrlist->next; + } + + if (!addrlist) +#ifdef WIN32 + _cupsSetError(IPP_SERVICE_UNAVAILABLE, "Connection failed", 0); +#else + _cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno), 0); +#endif /* WIN32 */ + + return (addrlist); +} + + +/* + * 'httpAddrFreeList()' - Free an address list. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +httpAddrFreeList( + http_addrlist_t *addrlist) /* I - Address list to free */ +{ + http_addrlist_t *next; /* Next address in list */ + + + /* + * Free each address in the list... + */ + + while (addrlist) + { + next = addrlist->next; + + free(addrlist); + + addrlist = next; + } +} + + +/* + * 'httpAddrGetList()' - Get a list of addresses for a hostname. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +http_addrlist_t * /* O - List of addresses or NULL */ +httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for passive listen address */ + int family, /* I - Address family or AF_UNSPEC */ + const char *service) /* I - Service name or port number */ +{ + http_addrlist_t *first, /* First address in list */ + *addr, /* Current address in list */ + *temp; /* New address */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + +#ifdef DEBUG + _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, " + "service=\"%s\")\n", + hostname ? hostname : "(nil)", + family == AF_UNSPEC ? "UNSPEC" : +# ifdef AF_LOCAL + family == AF_LOCAL ? "LOCAL" : +# endif /* AF_LOCAL */ +# ifdef AF_INET6 + family == AF_INET6 ? "INET6" : +# endif /* AF_INET6 */ + family == AF_INET ? "INET" : "???", service); +#endif /* DEBUG */ + +#ifdef HAVE_RES_INIT + /* + * STR #2920: Initialize resolver after failure in cups-polld + * + * If the previous lookup failed, re-initialize the resolver to prevent + * temporary network errors from persisting. This *should* be handled by + * the resolver libraries, but apparently the glibc folks do not agree. + * + * We set a flag at the end of this function if we encounter an error that + * requires reinitialization of the resolver functions. We then call + * res_init() if the flag is set on the next call here or in httpAddrLookup(). + */ + + if (cg->need_res_init) + { + res_init(); + + cg->need_res_init = 0; + } +#endif /* HAVE_RES_INIT */ + + /* + * Lookup the address the best way we can... + */ + + first = addr = NULL; + +#ifdef AF_LOCAL + if (hostname && hostname[0] == '/') + { + /* + * Domain socket address... + */ + + if ((first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t))) != NULL) + { + first->addr.un.sun_family = AF_LOCAL; + strlcpy(first->addr.un.sun_path, hostname, sizeof(first->addr.un.sun_path)); + } + } + else +#endif /* AF_LOCAL */ + if (!hostname || _cups_strcasecmp(hostname, "localhost")) + { +#ifdef HAVE_GETADDRINFO + struct addrinfo hints, /* Address lookup hints */ + *results, /* Address lookup results */ + *current; /* Current result */ + char ipv6[64], /* IPv6 address */ + *ipv6zone; /* Pointer to zone separator */ + int ipv6len; /* Length of IPv6 address */ + int error; /* getaddrinfo() error */ + + + /* + * Lookup the address as needed... + */ + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = family; + hints.ai_flags = hostname ? 0 : AI_PASSIVE; + hints.ai_socktype = SOCK_STREAM; + + if (hostname && *hostname == '[') + { + /* + * Remove brackets from numeric IPv6 address... + */ + + if (!strncmp(hostname, "[v1.", 4)) + { + /* + * Copy the newer address format which supports link-local addresses... + */ + + strlcpy(ipv6, hostname + 4, sizeof(ipv6)); + if ((ipv6len = (int)strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']') + { + ipv6[ipv6len] = '\0'; + hostname = ipv6; + + /* + * Convert "+zone" in address to "%zone"... + */ + + if ((ipv6zone = strrchr(ipv6, '+')) != NULL) + *ipv6zone = '%'; + } + } + else + { + /* + * Copy the regular non-link-local IPv6 address... + */ + + strlcpy(ipv6, hostname + 1, sizeof(ipv6)); + if ((ipv6len = (int)strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']') + { + ipv6[ipv6len] = '\0'; + hostname = ipv6; + } + } + } + + if ((error = getaddrinfo(hostname, service, &hints, &results)) == 0) + { + /* + * Copy the results to our own address list structure... + */ + + for (current = results; current; current = current->ai_next) + if (current->ai_family == AF_INET || current->ai_family == AF_INET6) + { + /* + * Copy the address over... + */ + + temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); + if (!temp) + { + httpAddrFreeList(first); + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + return (NULL); + } + + if (current->ai_family == AF_INET6) + memcpy(&(temp->addr.ipv6), current->ai_addr, + sizeof(temp->addr.ipv6)); + else + memcpy(&(temp->addr.ipv4), current->ai_addr, + sizeof(temp->addr.ipv4)); + + /* + * Append the address to the list... + */ + + if (!first) + first = temp; + + if (addr) + addr->next = temp; + + addr = temp; + } + + /* + * Free the results from getaddrinfo()... + */ + + freeaddrinfo(results); + } + else + { + if (error == EAI_FAIL) + cg->need_res_init = 1; + + _cupsSetError(IPP_INTERNAL_ERROR, gai_strerror(error), 0); + } + +#else + if (hostname) + { + int i; /* Looping vars */ + unsigned ip[4]; /* IPv4 address components */ + const char *ptr; /* Pointer into hostname */ + struct hostent *host; /* Result of lookup */ + struct servent *port; /* Port number for service */ + int portnum; /* Port number */ + + + /* + * Lookup the service... + */ + + if (!service) + portnum = 0; + else if (isdigit(*service & 255)) + portnum = atoi(service); + else if ((port = getservbyname(service, NULL)) != NULL) + portnum = ntohs(port->s_port); + else if (!strcmp(service, "http")) + portnum = 80; + else if (!strcmp(service, "https")) + portnum = 443; + else if (!strcmp(service, "ipp") || !strcmp(service, "ipps")) + portnum = 631; + else if (!strcmp(service, "lpd")) + portnum = 515; + else if (!strcmp(service, "socket")) + portnum = 9100; + else + return (NULL); + + /* + * This code is needed because some operating systems have a + * buggy implementation of gethostbyname() that does not support + * IPv4 addresses. If the hostname string is an IPv4 address, then + * sscanf() is used to extract the IPv4 components. We then pack + * the components into an IPv4 address manually, since the + * inet_aton() function is deprecated. We use the htonl() macro + * to get the right byte order for the address. + */ + + for (ptr = hostname; isdigit(*ptr & 255) || *ptr == '.'; ptr ++); + + if (!*ptr) + { + /* + * We have an IPv4 address; break it up and create an IPv4 address... + */ + + if (sscanf(hostname, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) == 4 && + ip[0] <= 255 && ip[1] <= 255 && ip[2] <= 255 && ip[3] <= 255) + { + first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); + if (!first) + return (NULL); + + first->addr.ipv4.sin_family = AF_INET; + first->addr.ipv4.sin_addr.s_addr = htonl(((((((ip[0] << 8) | + ip[1]) << 8) | + ip[2]) << 8) | ip[3])); + first->addr.ipv4.sin_port = htons(portnum); + } + } + else if ((host = gethostbyname(hostname)) != NULL && +# ifdef AF_INET6 + (host->h_addrtype == AF_INET || host->h_addrtype == AF_INET6)) +# else + host->h_addrtype == AF_INET) +# endif /* AF_INET6 */ + { + for (i = 0; host->h_addr_list[i]; i ++) + { + /* + * Copy the address over... + */ + + temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); + if (!temp) + { + httpAddrFreeList(first); + return (NULL); + } + +# ifdef AF_INET6 + if (host->h_addrtype == AF_INET6) + { + temp->addr.ipv6.sin6_family = AF_INET6; + memcpy(&(temp->addr.ipv6.sin6_addr), host->h_addr_list[i], + sizeof(temp->addr.ipv6)); + temp->addr.ipv6.sin6_port = htons(portnum); + } + else +# endif /* AF_INET6 */ + { + temp->addr.ipv4.sin_family = AF_INET; + memcpy(&(temp->addr.ipv4.sin_addr), host->h_addr_list[i], + sizeof(temp->addr.ipv4)); + temp->addr.ipv4.sin_port = htons(portnum); + } + + /* + * Append the address to the list... + */ + + if (!first) + first = temp; + + if (addr) + addr->next = temp; + + addr = temp; + } + } + else + { + if (h_errno == NO_RECOVERY) + cg->need_res_init = 1; + + _cupsSetError(IPP_INTERNAL_ERROR, hstrerror(h_errno), 0); + } + } +#endif /* HAVE_GETADDRINFO */ + } + + /* + * Detect some common errors and handle them sanely... + */ + + if (!addr && (!hostname || !_cups_strcasecmp(hostname, "localhost"))) + { + struct servent *port; /* Port number for service */ + int portnum; /* Port number */ + + + /* + * Lookup the service... + */ + + if (!service) + portnum = 0; + else if (isdigit(*service & 255)) + portnum = atoi(service); + else if ((port = getservbyname(service, NULL)) != NULL) + portnum = ntohs(port->s_port); + else if (!strcmp(service, "http")) + portnum = 80; + else if (!strcmp(service, "https")) + portnum = 443; + else if (!strcmp(service, "ipp") || !strcmp(service, "ipps")) + portnum = 631; + else if (!strcmp(service, "lpd")) + portnum = 515; + else if (!strcmp(service, "socket")) + portnum = 9100; + else + { + httpAddrFreeList(first); + + _cupsSetError(IPP_INTERNAL_ERROR, _("Unknown service name."), 1); + return (NULL); + } + + if (hostname && !_cups_strcasecmp(hostname, "localhost")) + { + /* + * Unfortunately, some users ignore all of the warnings in the + * /etc/hosts file and delete "localhost" from it. If we get here + * then we were unable to resolve the name, so use the IPv6 and/or + * IPv4 loopback interface addresses... + */ + +#ifdef AF_INET6 + if (family != AF_INET) + { + /* + * Add [::1] to the address list... + */ + + temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); + if (!temp) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + httpAddrFreeList(first); + return (NULL); + } + + temp->addr.ipv6.sin6_family = AF_INET6; + temp->addr.ipv6.sin6_port = htons(portnum); +# ifdef WIN32 + temp->addr.ipv6.sin6_addr.u.Byte[15] = 1; +# else + temp->addr.ipv6.sin6_addr.s6_addr32[3] = htonl(1); +# endif /* WIN32 */ + + if (!first) + first = temp; + + addr = temp; + } + + if (family != AF_INET6) +#endif /* AF_INET6 */ + { + /* + * Add 127.0.0.1 to the address list... + */ + + temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); + if (!temp) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + httpAddrFreeList(first); + return (NULL); + } + + temp->addr.ipv4.sin_family = AF_INET; + temp->addr.ipv4.sin_port = htons(portnum); + temp->addr.ipv4.sin_addr.s_addr = htonl(0x7f000001); + + if (!first) + first = temp; + + if (addr) + addr->next = temp; + } + } + else if (!hostname) + { + /* + * Provide one or more passive listening addresses... + */ + +#ifdef AF_INET6 + if (family != AF_INET) + { + /* + * Add [::] to the address list... + */ + + temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); + if (!temp) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + httpAddrFreeList(first); + return (NULL); + } + + temp->addr.ipv6.sin6_family = AF_INET6; + temp->addr.ipv6.sin6_port = htons(portnum); + + if (!first) + first = temp; + + addr = temp; + } + + if (family != AF_INET6) +#endif /* AF_INET6 */ + { + /* + * Add 0.0.0.0 to the address list... + */ + + temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); + if (!temp) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + httpAddrFreeList(first); + return (NULL); + } + + temp->addr.ipv4.sin_family = AF_INET; + temp->addr.ipv4.sin_port = htons(portnum); + + if (!first) + first = temp; + + if (addr) + addr->next = temp; + } + } + } + + /* + * Return the address list... + */ + + return (first); +} + + +/* + * End of "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $". + */ diff --git a/cups/http-private.h b/cups/http-private.h new file mode 100644 index 0000000..ec58de0 --- /dev/null +++ b/cups/http-private.h @@ -0,0 +1,394 @@ +/* + * "$Id: http-private.h 7850 2008-08-20 00:07:25Z mike $" + * + * Private HTTP definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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. + */ + +#ifndef _CUPS_HTTP_PRIVATE_H_ +# define _CUPS_HTTP_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include "config.h" +# include +# include + +# ifdef __sun +# include +# endif /* __sun */ + +# include +# ifdef WIN32 +# include +# include +# else +# include +# include +# include +# define closesocket(f) close(f) +# endif /* WIN32 */ + +# ifdef HAVE_GSSAPI +# ifdef HAVE_GSS_GSSAPI_H +# include +# elif defined(HAVE_GSSAPI_GSSAPI_H) +# include +# elif defined(HAVE_GSSAPI_H) +# include +# endif /* HAVE_GSS_GSSAPI_H */ +# ifndef HAVE_GSS_C_NT_HOSTBASED_SERVICE +# define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name +# endif /* !HAVE_GSS_C_NT_HOSTBASED_SERVICE */ +# endif /* HAVE_GSSAPI */ + +# ifdef HAVE_AUTHORIZATION_H +# include +# endif /* HAVE_AUTHORIZATION_H */ + +# if defined(__sgi) || (defined(__APPLE__) && !defined(_SOCKLEN_T)) +/* + * IRIX and MacOS X 10.2.x do not define socklen_t, and in fact use an int instead of + * unsigned type for length values... + */ + +typedef int socklen_t; +# endif /* __sgi || (__APPLE__ && !_SOCKLEN_T) */ + +# include +# include "md5-private.h" +# include "ipp-private.h" + +# if defined HAVE_LIBSSL +# include +# include +# include +# elif defined HAVE_GNUTLS +# include +# include +# include +# elif defined(HAVE_CDSASSL) +# include +# include +# include +# ifdef HAVE_SECURETRANSPORTPRIV_H +# include +# endif /* HAVE_SECURETRANSPORTPRIV_H */ +# ifdef HAVE_SECITEM_H +# include +# endif /* HAVE_SECITEM_H */ +# ifdef HAVE_SECBASEPRIV_H +# include +# endif /* HAVE_SECBASEPRIV_H */ +# ifdef HAVE_SECCERTIFICATE_H +# include +# include +# endif /* HAVE_SECCERTIFICATE_H */ +# ifdef HAVE_SECITEMPRIV_H +# include +# endif /* HAVE_SECITEMPRIV_H */ +# ifdef HAVE_SECIDENTITYSEARCHPRIV_H +# include +# endif /* HAVE_SECIDENTITYSEARCHPRIV_H */ +# ifdef HAVE_SECPOLICYPRIV_H +# include +# endif /* HAVE_SECPOLICYPRIV_H */ +# elif defined(HAVE_SSPISSL) +# include "sspi-private.h" +# endif /* HAVE_LIBSSL */ + +# ifndef WIN32 +# include +# ifdef HAVE_GETIFADDRS +# include +# else +# include +# ifdef HAVE_SYS_SOCKIO_H +# include +# endif /* HAVE_SYS_SOCKIO_H */ +# endif /* HAVE_GETIFADDRS */ +# endif /* !WIN32 */ + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Constants... + */ + + +#define _HTTP_RESOLVE_DEFAULT 0 /* Just resolve with default options */ +#define _HTTP_RESOLVE_STDERR 1 /* Log resolve progress to stderr */ +#define _HTTP_RESOLVE_FQDN 2 /* Resolve to a FQDN */ +#define _HTTP_RESOLVE_FAXOUT 4 /* Resolve FaxOut service? */ + + +/* + * Types and functions for SSL support... + */ + +# if defined HAVE_LIBSSL +/* + * The OpenSSL library provides its own SSL/TLS context structure for its + * IO and protocol management. However, we need to provide our own BIO + * (basic IO) implementation to do timeouts... + */ + +typedef SSL *http_tls_t; +typedef void *http_tls_credentials_t; + +extern BIO_METHOD *_httpBIOMethods(void); + +# elif defined HAVE_GNUTLS +/* + * The GNU TLS library is more of a "bare metal" SSL/TLS library... + */ + +typedef gnutls_session http_tls_t; +typedef void *http_tls_credentials_t; + +extern ssize_t _httpReadGNUTLS(gnutls_transport_ptr ptr, void *data, + size_t length); +extern ssize_t _httpWriteGNUTLS(gnutls_transport_ptr ptr, const void *data, + size_t length); + +# elif defined(HAVE_CDSASSL) +/* + * Darwin's Security framework provides its own SSL/TLS context structure + * for its IO and protocol management... + */ + +# if !defined(HAVE_SECBASEPRIV_H) && defined(HAVE_CSSMERRORSTRING) /* Declare prototype for function in that header... */ +extern const char *cssmErrorString(int error); +# endif /* !HAVE_SECBASEPRIV_H && HAVE_CSSMERRORSTRING */ +# ifndef HAVE_SECITEMPRIV_H /* Declare constants from that header... */ +extern const CFTypeRef kSecClassCertificate; +extern const CFTypeRef kSecClassIdentity; +# endif /* !HAVE_SECITEMPRIV_H */ +# if !defined(HAVE_SECIDENTITYSEARCHPRIV_H) && defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY) /* Declare prototype for function in that header... */ +extern OSStatus SecIdentitySearchCreateWithPolicy(SecPolicyRef policy, + CFStringRef idString, CSSM_KEYUSE keyUsage, + CFTypeRef keychainOrArray, + Boolean returnOnlyValidIdentities, + SecIdentitySearchRef* searchRef); +# endif /* !HAVE_SECIDENTITYSEARCHPRIV_H && HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */ +# if !defined(HAVE_SECPOLICYPRIV_H) && defined(HAVE_SECPOLICYSETVALUE) /* Declare prototype for function in that header... */ +extern OSStatus SecPolicySetValue(SecPolicyRef policyRef, + const CSSM_DATA *value); +# endif /* !HAVE_SECPOLICYPRIV_H && HAVE_SECPOLICYSETVALUE */ + +typedef SSLContextRef http_tls_t; +typedef CFArrayRef http_tls_credentials_t; + +extern OSStatus _httpReadCDSA(SSLConnectionRef connection, void *data, + size_t *dataLength); +extern OSStatus _httpWriteCDSA(SSLConnectionRef connection, const void *data, + size_t *dataLength); + +# elif defined(HAVE_SSPISSL) +/* + * Windows' SSPI library gets a CUPS wrapper... + */ + +typedef _sspi_struct_t * http_tls_t; +typedef void *http_tls_credentials_t; + +# else +/* + * Otherwise define stub types since we have no SSL support... + */ + +typedef void *http_tls_t; +typedef void *http_tls_credentials_t; +# endif /* HAVE_LIBSSL */ + +struct _http_s /**** HTTP connection structure. ****/ +{ + int fd; /* File descriptor for this socket */ + int blocking; /* To block or not to block */ + int error; /* Last error on read */ + time_t activity; /* Time since last read/write */ + http_state_t state; /* State of client */ + http_status_t status; /* Status of last request */ + http_version_t version; /* Protocol version */ + http_keepalive_t keep_alive; /* Keep-alive supported? */ + struct sockaddr_in _hostaddr; /* Address of connected host (deprecated) */ + char hostname[HTTP_MAX_HOST], + /* Name of connected host */ + fields[HTTP_FIELD_MAX][HTTP_MAX_VALUE]; + /* Field values */ + char *data; /* Pointer to data buffer */ + http_encoding_t data_encoding; /* Chunked or not */ + int _data_remaining;/* Number of bytes left (deprecated) */ + int used; /* Number of bytes used in buffer */ + char buffer[HTTP_MAX_BUFFER]; + /* Buffer for incoming data */ + int auth_type; /* Authentication in use */ + _cups_md5_state_t md5_state; /* MD5 state */ + char nonce[HTTP_MAX_VALUE]; + /* Nonce value */ + int nonce_count; /* Nonce count */ + http_tls_t tls; /* TLS state information */ + http_encryption_t encryption; /* Encryption requirements */ + /**** New in CUPS 1.1.19 ****/ + fd_set *input_set; /* select() set for httpWait() (deprecated) */ + http_status_t expect; /* Expect: header */ + char *cookie; /* Cookie value(s) */ + /**** New in CUPS 1.1.20 ****/ + char _authstring[HTTP_MAX_VALUE], + /* Current Authentication value (deprecated) */ + userpass[HTTP_MAX_VALUE]; + /* Username:password string */ + int digest_tries; /* Number of tries for digest auth */ + /**** New in CUPS 1.2 ****/ + off_t data_remaining; /* Number of bytes left */ + http_addr_t *hostaddr; /* Current host address and port */ + http_addrlist_t *addrlist; /* List of valid addresses */ + char wbuffer[HTTP_MAX_BUFFER]; + /* Buffer for outgoing data */ + int wused; /* Write buffer bytes used */ + /**** New in CUPS 1.3 ****/ + char *field_authorization; + /* Authorization field */ + char *authstring; /* Current authorization field */ +# ifdef HAVE_GSSAPI + gss_OID gssmech; /* Authentication mechanism */ + gss_ctx_id_t gssctx; /* Authentication context */ + gss_name_t gssname; /* Authentication server name */ +# endif /* HAVE_GSSAPI */ +# ifdef HAVE_AUTHORIZATION_H + AuthorizationRef auth_ref; /* Authorization ref */ +# endif /* HAVE_AUTHORIZATION_H */ + /**** New in CUPS 1.5 ****/ + http_tls_credentials_t tls_credentials; + /* TLS credentials */ + http_timeout_cb_t timeout_cb; /* Timeout callback */ + void *timeout_data; /* User data pointer */ + double timeout_value; /* Timeout in seconds */ + int wait_value; /* httpWait value for timeout */ +# ifdef HAVE_GSSAPI + char gsshost[256]; /* Hostname for Kerberos */ +# endif /* HAVE_GSSAPI */ +}; + + +/* + * Some OS's don't have hstrerror(), most notably Solaris... + */ + +# ifndef HAVE_HSTRERROR +extern const char *_cups_hstrerror(int error); +# define hstrerror _cups_hstrerror +# elif defined(_AIX) || defined(__osf__) +/* + * AIX and Tru64 UNIX don't provide a prototype but do provide the function... + */ +extern const char *hstrerror(int error); +# endif /* !HAVE_HSTRERROR */ + + +/* + * Some OS's don't have getifaddrs() and freeifaddrs()... + */ + +# if !defined(WIN32) && !defined(HAVE_GETIFADDRS) +# ifdef ifa_dstaddr +# undef ifa_dstaddr +# endif /* ifa_dstaddr */ +# ifndef ifr_netmask +# define ifr_netmask ifr_addr +# endif /* !ifr_netmask */ + +struct ifaddrs /**** Interface Structure ****/ +{ + struct ifaddrs *ifa_next; /* Next interface in list */ + char *ifa_name; /* Name of interface */ + unsigned int ifa_flags; /* Flags (up, point-to-point, etc.) */ + struct sockaddr *ifa_addr, /* Network address */ + *ifa_netmask; /* Address mask */ + union + { + struct sockaddr *ifu_broadaddr; /* Broadcast address of this interface. */ + struct sockaddr *ifu_dstaddr; /* Point-to-point destination address. */ + } ifa_ifu; + + void *ifa_data; /* Interface statistics */ +}; + +# ifndef ifa_broadaddr +# define ifa_broadaddr ifa_ifu.ifu_broadaddr +# endif /* !ifa_broadaddr */ +# ifndef ifa_dstaddr +# define ifa_dstaddr ifa_ifu.ifu_dstaddr +# endif /* !ifa_dstaddr */ + +extern int _cups_getifaddrs(struct ifaddrs **addrs); +# define getifaddrs _cups_getifaddrs +extern void _cups_freeifaddrs(struct ifaddrs *addrs); +# define freeifaddrs _cups_freeifaddrs +# endif /* !WIN32 && !HAVE_GETIFADDRS */ + + +/* + * Prototypes... + */ + +#define _httpAddrFamily(addrp) (addrp)->addr.sa_family +extern int _httpAddrPort(http_addr_t *addr); +extern void _httpAddrSetPort(http_addr_t *addr, int port); +extern char *_httpAssembleUUID(const char *server, int port, + const char *name, int number, + char *buffer, size_t bufsize); +extern http_t *_httpCreate(const char *host, int port, + http_addrlist_t *addrlist, + http_encryption_t encryption, + int family); +extern http_tls_credentials_t + _httpCreateCredentials(cups_array_t *credentials); +extern char *_httpDecodeURI(char *dst, const char *src, + size_t dstsize); +extern void _httpDisconnect(http_t *http); +extern char *_httpEncodeURI(char *dst, const char *src, + size_t dstsize); +extern void _httpFreeCredentials(http_tls_credentials_t credentials); +extern ssize_t _httpPeek(http_t *http, char *buffer, size_t length); +extern const char *_httpResolveURI(const char *uri, char *resolved_uri, + size_t resolved_size, int options, + int (*cb)(void *context), + void *context); +extern int _httpUpdate(http_t *http, http_status_t *status); +extern int _httpWait(http_t *http, int msec, int usessl); + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_HTTP_PRIVATE_H_ */ + +/* + * End of "$Id: http-private.h 7850 2008-08-20 00:07:25Z mike $". + */ diff --git a/cups/http-support.c b/cups/http-support.c new file mode 100644 index 0000000..146719d --- /dev/null +++ b/cups/http-support.c @@ -0,0 +1,2364 @@ +/* + * "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $" + * + * HTTP support routines for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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. + * + * Contents: + * + * httpAssembleURI() - Assemble a uniform resource identifier from its + * components. + * httpAssembleURIf() - Assemble a uniform resource identifier from its + * components with a formatted resource. + * _httpAssembleUUID() - Make a UUID URI conforming to RFC 4122. + * httpDecode64() - Base64-decode a string. + * httpDecode64_2() - Base64-decode a string. + * httpEncode64() - Base64-encode a string. + * httpEncode64_2() - Base64-encode a string. + * httpGetDateString() - Get a formatted date/time string from a time value. + * httpGetDateString2() - Get a formatted date/time string from a time value. + * httpGetDateTime() - Get a time value from a formatted date/time string. + * httpSeparate() - Separate a Universal Resource Identifier into its + * components. + * httpSeparate2() - Separate a Universal Resource Identifier into its + * components. + * httpSeparateURI() - Separate a Universal Resource Identifier into its + * components. + * httpStatus() - Return a short string describing a HTTP status + * code. + * _cups_hstrerror() - hstrerror() emulation function for Solaris and + * others. + * _httpDecodeURI() - Percent-decode a HTTP request URI. + * _httpEncodeURI() - Percent-encode a HTTP request URI. + * _httpResolveURI() - Resolve a DNS-SD URI. + * http_client_cb() - Client callback for resolving URI. + * http_copy_decode() - Copy and decode a URI. + * http_copy_encode() - Copy and encode a URI. + * http_poll_cb() - Wait for input on the specified file descriptors. + * http_resolve_cb() - Build a device URI for the given service name. + * http_resolve_cb() - Build a device URI for the given service name. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#ifdef HAVE_DNSSD +# include +# ifdef WIN32 +# include +# elif defined(HAVE_POLL) +# include +# else +# include +# endif /* WIN32 */ +#elif defined(HAVE_AVAHI) +# include +# include +# include +#endif /* HAVE_DNSSD */ + + +/* + * Local types... + */ + +typedef struct _http_uribuf_s /* URI buffer */ +{ +#ifdef HAVE_AVAHI + AvahiSimplePoll *poll; /* Poll state */ +#endif /* HAVE_AVAHI */ + char *buffer; /* Pointer to buffer */ + size_t bufsize; /* Size of buffer */ + int options; /* Options passed to _httpResolveURI */ + const char *resource; /* Resource from URI */ +} _http_uribuf_t; + + +/* + * Local globals... + */ + +static const char * const http_days[7] = + { + "Sun", + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat" + }; +static const char * const http_months[12] = + { + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" + }; + + +/* + * Local functions... + */ + +static const char *http_copy_decode(char *dst, const char *src, + int dstsize, const char *term, + int decode); +static char *http_copy_encode(char *dst, const char *src, + char *dstend, const char *reserved, + const char *term, int encode); +#ifdef HAVE_DNSSD +static void DNSSD_API http_resolve_cb(DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *fullName, + const char *hostTarget, + uint16_t port, uint16_t txtLen, + const unsigned char *txtRecord, + void *context); +#endif /* HAVE_DNSSD */ + +#ifdef HAVE_AVAHI +static void http_client_cb(AvahiClient *client, + AvahiClientState state, void *simple_poll); +static int http_poll_cb(struct pollfd *pollfds, unsigned int num_pollfds, + int timeout, void *context); +static void http_resolve_cb(AvahiServiceResolver *resolver, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiResolverEvent event, + const char *name, const char *type, + const char *domain, const char *host_name, + const AvahiAddress *address, uint16_t port, + AvahiStringList *txt, + AvahiLookupResultFlags flags, void *context); +#endif /* HAVE_AVAHI */ + + +/* + * 'httpAssembleURI()' - Assemble a uniform resource identifier from its + * components. + * + * This function escapes reserved characters in the URI depending on the + * value of the "encoding" argument. You should use this function in + * place of traditional string functions whenever you need to create a + * URI string. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +http_uri_status_t /* O - URI status */ +httpAssembleURI( + http_uri_coding_t encoding, /* I - Encoding flags */ + char *uri, /* I - URI buffer */ + int urilen, /* I - Size of URI buffer */ + const char *scheme, /* I - Scheme name */ + const char *username, /* I - Username */ + const char *host, /* I - Hostname or address */ + int port, /* I - Port number */ + const char *resource) /* I - Resource */ +{ + char *ptr, /* Pointer into URI buffer */ + *end; /* End of URI buffer */ + + + /* + * Range check input... + */ + + if (!uri || urilen < 1 || !scheme || port < 0) + { + if (uri) + *uri = '\0'; + + return (HTTP_URI_BAD_ARGUMENTS); + } + + /* + * Assemble the URI starting with the scheme... + */ + + end = uri + urilen - 1; + ptr = http_copy_encode(uri, scheme, end, NULL, NULL, 0); + + if (!ptr) + goto assemble_overflow; + + if (!strcmp(scheme, "mailto")) + { + /* + * mailto: only has :, no //... + */ + + if (ptr < end) + *ptr++ = ':'; + else + goto assemble_overflow; + } + else + { + /* + * Schemes other than mailto: all have //... + */ + + if ((ptr + 2) < end) + { + *ptr++ = ':'; + *ptr++ = '/'; + *ptr++ = '/'; + } + else + goto assemble_overflow; + } + + /* + * Next the username and hostname, if any... + */ + + if (host) + { + const char *hostptr; /* Pointer into hostname */ + int have_ipv6; /* Do we have an IPv6 address? */ + + if (username && *username) + { + /* + * Add username@ first... + */ + + ptr = http_copy_encode(ptr, username, end, "/?#[]@", NULL, + encoding & HTTP_URI_CODING_USERNAME); + + if (!ptr) + goto assemble_overflow; + + if (ptr < end) + *ptr++ = '@'; + else + goto assemble_overflow; + } + + /* + * Then add the hostname. Since IPv6 is a particular pain to deal + * with, we have several special cases to deal with. If we get + * an IPv6 address with brackets around it, assume it is already in + * URI format. Since DNS-SD service names can sometimes look like + * raw IPv6 addresses, we specifically look for "._tcp" in the name, + * too... + */ + + for (hostptr = host, + have_ipv6 = strchr(host, ':') && !strstr(host, "._tcp"); + *hostptr && have_ipv6; + hostptr ++) + if (*hostptr != ':' && !isxdigit(*hostptr & 255)) + { + have_ipv6 = *hostptr == '%'; + break; + } + + if (have_ipv6) + { + /* + * We have a raw IPv6 address... + */ + + if (strchr(host, '%') && !(encoding & HTTP_URI_CODING_RFC6874)) + { + /* + * We have a link-local address, add "[v1." prefix... + */ + + if ((ptr + 4) < end) + { + *ptr++ = '['; + *ptr++ = 'v'; + *ptr++ = '1'; + *ptr++ = '.'; + } + else + goto assemble_overflow; + } + else + { + /* + * We have a normal (or RFC 6874 link-local) address, add "[" prefix... + */ + + if (ptr < end) + *ptr++ = '['; + else + goto assemble_overflow; + } + + /* + * Copy the rest of the IPv6 address, and terminate with "]". + */ + + while (ptr < end && *host) + { + if (*host == '%') + { + /* + * Convert/encode zone separator + */ + + if (encoding & HTTP_URI_CODING_RFC6874) + { + if (ptr >= (end - 2)) + goto assemble_overflow; + + *ptr++ = '%'; + *ptr++ = '2'; + *ptr++ = '5'; + } + else + *ptr++ = '+'; + + host ++; + } + else + *ptr++ = *host++; + } + + if (*host) + goto assemble_overflow; + + if (ptr < end) + *ptr++ = ']'; + else + goto assemble_overflow; + } + else + { + /* + * Otherwise, just copy the host string (the extra chars are not in the + * "reg-name" ABNF rule; anything <= SP or >= DEL plus % gets automatically + * percent-encoded. + */ + + ptr = http_copy_encode(ptr, host, end, "\"#/:<>?@[\\]^`{|}", NULL, + encoding & HTTP_URI_CODING_HOSTNAME); + + if (!ptr) + goto assemble_overflow; + } + + /* + * Finish things off with the port number... + */ + + if (port > 0) + { + snprintf(ptr, end - ptr + 1, ":%d", port); + ptr += strlen(ptr); + + if (ptr >= end) + goto assemble_overflow; + } + } + + /* + * Last but not least, add the resource string... + */ + + if (resource) + { + char *query; /* Pointer to query string */ + + + /* + * Copy the resource string up to the query string if present... + */ + + query = strchr(resource, '?'); + ptr = http_copy_encode(ptr, resource, end, NULL, "?", + encoding & HTTP_URI_CODING_RESOURCE); + if (!ptr) + goto assemble_overflow; + + if (query) + { + /* + * Copy query string without encoding... + */ + + ptr = http_copy_encode(ptr, query, end, NULL, NULL, + encoding & HTTP_URI_CODING_QUERY); + if (!ptr) + goto assemble_overflow; + } + } + else if (ptr < end) + *ptr++ = '/'; + else + goto assemble_overflow; + + /* + * Nul-terminate the URI buffer and return with no errors... + */ + + *ptr = '\0'; + + return (HTTP_URI_OK); + + /* + * Clear the URI string and return an overflow error; I don't usually + * like goto's, but in this case it makes sense... + */ + + assemble_overflow: + + *uri = '\0'; + return (HTTP_URI_OVERFLOW); +} + + +/* + * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its + * components with a formatted resource. + * + * This function creates a formatted version of the resource string + * argument "resourcef" and escapes reserved characters in the URI + * depending on the value of the "encoding" argument. You should use + * this function in place of traditional string functions whenever + * you need to create a URI string. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +http_uri_status_t /* O - URI status */ +httpAssembleURIf( + http_uri_coding_t encoding, /* I - Encoding flags */ + char *uri, /* I - URI buffer */ + int urilen, /* I - Size of URI buffer */ + const char *scheme, /* I - Scheme name */ + const char *username, /* I - Username */ + const char *host, /* I - Hostname or address */ + int port, /* I - Port number */ + const char *resourcef, /* I - Printf-style resource */ + ...) /* I - Additional arguments as needed */ +{ + va_list ap; /* Pointer to additional arguments */ + char resource[1024]; /* Formatted resource string */ + int bytes; /* Bytes in formatted string */ + + + /* + * Range check input... + */ + + if (!uri || urilen < 1 || !scheme || port < 0 || !resourcef) + { + if (uri) + *uri = '\0'; + + return (HTTP_URI_BAD_ARGUMENTS); + } + + /* + * Format the resource string and assemble the URI... + */ + + va_start(ap, resourcef); + bytes = vsnprintf(resource, sizeof(resource), resourcef, ap); + va_end(ap); + + if (bytes >= sizeof(resource)) + { + *uri = '\0'; + return (HTTP_URI_OVERFLOW); + } + else + return (httpAssembleURI(encoding, uri, urilen, scheme, username, host, + port, resource)); +} + + +/* + * '_httpAssembleUUID()' - Make a UUID URI conforming to RFC 4122. + * + * The buffer needs to be at least 46 bytes in size. + */ + +char * /* I - UUID string */ +_httpAssembleUUID(const char *server, /* I - Server name */ + int port, /* I - Port number */ + const char *name, /* I - Object name or NULL */ + int number, /* I - Object number or 0 */ + char *buffer, /* I - String buffer */ + size_t bufsize) /* I - Size of buffer */ +{ + char data[1024]; /* Source string for MD5 */ + _cups_md5_state_t md5state; /* MD5 state */ + unsigned char md5sum[16]; /* MD5 digest/sum */ + + + /* + * Build a version 3 UUID conforming to RFC 4122. + * + * Start with the MD5 sum of the server, port, object name and + * number, and some random data on the end. + */ + + snprintf(data, sizeof(data), "%s:%d:%s:%d:%04x:%04x", server, + port, name ? name : server, number, + (unsigned)CUPS_RAND() & 0xffff, (unsigned)CUPS_RAND() & 0xffff); + + _cupsMD5Init(&md5state); + _cupsMD5Append(&md5state, (unsigned char *)data, strlen(data)); + _cupsMD5Finish(&md5state, md5sum); + + /* + * Generate the UUID from the MD5... + */ + + snprintf(buffer, bufsize, + "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" + "%02x%02x%02x%02x%02x%02x", + md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], + (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40, + md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13], + md5sum[14], md5sum[15]); + + return (buffer); +} + + +/* + * 'httpDecode64()' - Base64-decode a string. + * + * This function is deprecated. Use the httpDecode64_2() function instead + * which provides buffer length arguments. + * + * @deprecated@ + */ + +char * /* O - Decoded string */ +httpDecode64(char *out, /* I - String to write to */ + const char *in) /* I - String to read from */ +{ + int outlen; /* Output buffer length */ + + + /* + * Use the old maximum buffer size for binary compatibility... + */ + + outlen = 512; + + return (httpDecode64_2(out, &outlen, in)); +} + + +/* + * 'httpDecode64_2()' - Base64-decode a string. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +char * /* O - Decoded string */ +httpDecode64_2(char *out, /* I - String to write to */ + int *outlen, /* IO - Size of output string */ + const char *in) /* I - String to read from */ +{ + int pos, /* Bit position */ + base64; /* Value of this character */ + char *outptr, /* Output pointer */ + *outend; /* End of output buffer */ + + + /* + * Range check input... + */ + + if (!out || !outlen || *outlen < 1 || !in) + return (NULL); + + if (!*in) + { + *out = '\0'; + *outlen = 0; + + return (out); + } + + /* + * Convert from base-64 to bytes... + */ + + for (outptr = out, outend = out + *outlen - 1, pos = 0; *in != '\0'; in ++) + { + /* + * Decode this character into a number from 0 to 63... + */ + + if (*in >= 'A' && *in <= 'Z') + base64 = *in - 'A'; + else if (*in >= 'a' && *in <= 'z') + base64 = *in - 'a' + 26; + else if (*in >= '0' && *in <= '9') + base64 = *in - '0' + 52; + else if (*in == '+') + base64 = 62; + else if (*in == '/') + base64 = 63; + else if (*in == '=') + break; + else + continue; + + /* + * Store the result in the appropriate chars... + */ + + switch (pos) + { + case 0 : + if (outptr < outend) + *outptr = base64 << 2; + pos ++; + break; + case 1 : + if (outptr < outend) + *outptr++ |= (base64 >> 4) & 3; + if (outptr < outend) + *outptr = (base64 << 4) & 255; + pos ++; + break; + case 2 : + if (outptr < outend) + *outptr++ |= (base64 >> 2) & 15; + if (outptr < outend) + *outptr = (base64 << 6) & 255; + pos ++; + break; + case 3 : + if (outptr < outend) + *outptr++ |= base64; + pos = 0; + break; + } + } + + *outptr = '\0'; + + /* + * Return the decoded string and size... + */ + + *outlen = (int)(outptr - out); + + return (out); +} + + +/* + * 'httpEncode64()' - Base64-encode a string. + * + * This function is deprecated. Use the httpEncode64_2() function instead + * which provides buffer length arguments. + * + * @deprecated@ + */ + +char * /* O - Encoded string */ +httpEncode64(char *out, /* I - String to write to */ + const char *in) /* I - String to read from */ +{ + return (httpEncode64_2(out, 512, in, (int)strlen(in))); +} + + +/* + * 'httpEncode64_2()' - Base64-encode a string. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +char * /* O - Encoded string */ +httpEncode64_2(char *out, /* I - String to write to */ + int outlen, /* I - Size of output string */ + const char *in, /* I - String to read from */ + int inlen) /* I - Size of input string */ +{ + char *outptr, /* Output pointer */ + *outend; /* End of output buffer */ + static const char base64[] = /* Base64 characters... */ + { + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/" + }; + + + /* + * Range check input... + */ + + if (!out || outlen < 1 || !in) + return (NULL); + + /* + * Convert bytes to base-64... + */ + + for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --) + { + /* + * Encode the up to 3 characters as 4 Base64 numbers... + */ + + if (outptr < outend) + *outptr ++ = base64[(in[0] & 255) >> 2]; + + if (outptr < outend) + { + if (inlen > 1) + *outptr ++ = base64[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63]; + else + *outptr ++ = base64[((in[0] & 255) << 4) & 63]; + } + + in ++; + inlen --; + if (inlen <= 0) + { + if (outptr < outend) + *outptr ++ = '='; + if (outptr < outend) + *outptr ++ = '='; + break; + } + + if (outptr < outend) + { + if (inlen > 1) + *outptr ++ = base64[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63]; + else + *outptr ++ = base64[((in[0] & 255) << 2) & 63]; + } + + in ++; + inlen --; + if (inlen <= 0) + { + if (outptr < outend) + *outptr ++ = '='; + break; + } + + if (outptr < outend) + *outptr ++ = base64[in[0] & 63]; + } + + *outptr = '\0'; + + /* + * Return the encoded string... + */ + + return (out); +} + + +/* + * 'httpGetDateString()' - Get a formatted date/time string from a time value. + * + * @deprecated@ + */ + +const char * /* O - Date/time string */ +httpGetDateString(time_t t) /* I - UNIX time */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + return (httpGetDateString2(t, cg->http_date, sizeof(cg->http_date))); +} + + +/* + * 'httpGetDateString2()' - Get a formatted date/time string from a time value. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +const char * /* O - Date/time string */ +httpGetDateString2(time_t t, /* I - UNIX time */ + char *s, /* I - String buffer */ + int slen) /* I - Size of string buffer */ +{ + struct tm *tdate; /* UNIX date/time data */ + + + tdate = gmtime(&t); + if (tdate) + snprintf(s, slen, "%s, %02d %s %d %02d:%02d:%02d GMT", + http_days[tdate->tm_wday], tdate->tm_mday, + http_months[tdate->tm_mon], tdate->tm_year + 1900, + tdate->tm_hour, tdate->tm_min, tdate->tm_sec); + else + s[0] = '\0'; + + return (s); +} + + +/* + * 'httpGetDateTime()' - Get a time value from a formatted date/time string. + */ + +time_t /* O - UNIX time */ +httpGetDateTime(const char *s) /* I - Date/time string */ +{ + int i; /* Looping var */ + char mon[16]; /* Abbreviated month name */ + int day, year; /* Day of month and year */ + int hour, min, sec; /* Time */ + int days; /* Number of days since 1970 */ + static const int normal_days[] = /* Days to a month, normal years */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; + static const int leap_days[] = /* Days to a month, leap years */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; + + + DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s)); + + /* + * Extract the date and time from the formatted string... + */ + + if (sscanf(s, "%*s%d%15s%d%d:%d:%d", &day, mon, &year, &hour, &min, &sec) < 6) + return (0); + + DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, " + "min=%d, sec=%d", day, mon, year, hour, min, sec)); + + /* + * Convert the month name to a number from 0 to 11. + */ + + for (i = 0; i < 12; i ++) + if (!_cups_strcasecmp(mon, http_months[i])) + break; + + if (i >= 12) + return (0); + + DEBUG_printf(("4httpGetDateTime: i=%d", i)); + + /* + * Now convert the date and time to a UNIX time value in seconds since + * 1970. We can't use mktime() since the timezone may not be UTC but + * the date/time string *is* UTC. + */ + + if ((year & 3) == 0 && ((year % 100) != 0 || (year % 400) == 0)) + days = leap_days[i] + day - 1; + else + days = normal_days[i] + day - 1; + + DEBUG_printf(("4httpGetDateTime: days=%d", days)); + + days += (year - 1970) * 365 + /* 365 days per year (normally) */ + ((year - 1) / 4 - 492) - /* + leap days */ + ((year - 1) / 100 - 19) + /* - 100 year days */ + ((year - 1) / 400 - 4); /* + 400 year days */ + + DEBUG_printf(("4httpGetDateTime: days=%d\n", days)); + + return (days * 86400 + hour * 3600 + min * 60 + sec); +} + + +/* + * 'httpSeparate()' - Separate a Universal Resource Identifier into its + * components. + * + * This function is deprecated; use the httpSeparateURI() function instead. + * + * @deprecated@ + */ + +void +httpSeparate(const char *uri, /* I - Universal Resource Identifier */ + char *scheme, /* O - Scheme [32] (http, https, etc.) */ + char *username, /* O - Username [1024] */ + char *host, /* O - Hostname [1024] */ + int *port, /* O - Port number to use */ + char *resource) /* O - Resource/filename [1024] */ +{ + httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, 32, username, + HTTP_MAX_URI, host, HTTP_MAX_URI, port, resource, + HTTP_MAX_URI); +} + + +/* + * 'httpSeparate2()' - Separate a Universal Resource Identifier into its + * components. + * + * This function is deprecated; use the httpSeparateURI() function instead. + * + * @since CUPS 1.1.21/OS X 10.4@ + * @deprecated@ + */ + +void +httpSeparate2(const char *uri, /* I - Universal Resource Identifier */ + char *scheme, /* O - Scheme (http, https, etc.) */ + int schemelen, /* I - Size of scheme buffer */ + char *username, /* O - Username */ + int usernamelen, /* I - Size of username buffer */ + char *host, /* O - Hostname */ + int hostlen, /* I - Size of hostname buffer */ + int *port, /* O - Port number to use */ + char *resource, /* O - Resource/filename */ + int resourcelen) /* I - Size of resource buffer */ +{ + httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, schemelen, username, + usernamelen, host, hostlen, port, resource, resourcelen); +} + + +/* + * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its + * components. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +http_uri_status_t /* O - Result of separation */ +httpSeparateURI( + http_uri_coding_t decoding, /* I - Decoding flags */ + const char *uri, /* I - Universal Resource Identifier */ + char *scheme, /* O - Scheme (http, https, etc.) */ + int schemelen, /* I - Size of scheme buffer */ + char *username, /* O - Username */ + int usernamelen, /* I - Size of username buffer */ + char *host, /* O - Hostname */ + int hostlen, /* I - Size of hostname buffer */ + int *port, /* O - Port number to use */ + char *resource, /* O - Resource/filename */ + int resourcelen) /* I - Size of resource buffer */ +{ + char *ptr, /* Pointer into string... */ + *end; /* End of string */ + const char *sep; /* Separator character */ + http_uri_status_t status; /* Result of separation */ + + + /* + * Initialize everything to blank... + */ + + if (scheme && schemelen > 0) + *scheme = '\0'; + + if (username && usernamelen > 0) + *username = '\0'; + + if (host && hostlen > 0) + *host = '\0'; + + if (port) + *port = 0; + + if (resource && resourcelen > 0) + *resource = '\0'; + + /* + * Range check input... + */ + + if (!uri || !port || !scheme || schemelen <= 0 || !username || + usernamelen <= 0 || !host || hostlen <= 0 || !resource || + resourcelen <= 0) + return (HTTP_URI_BAD_ARGUMENTS); + + if (!*uri) + return (HTTP_URI_BAD_URI); + + /* + * Grab the scheme portion of the URI... + */ + + status = HTTP_URI_OK; + + if (!strncmp(uri, "//", 2)) + { + /* + * Workaround for HP IPP client bug... + */ + + strlcpy(scheme, "ipp", schemelen); + status = HTTP_URI_MISSING_SCHEME; + } + else if (*uri == '/') + { + /* + * Filename... + */ + + strlcpy(scheme, "file", schemelen); + status = HTTP_URI_MISSING_SCHEME; + } + else + { + /* + * Standard URI with scheme... + */ + + for (ptr = scheme, end = scheme + schemelen - 1; + *uri && *uri != ':' && ptr < end;) + if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789-+.", *uri) != NULL) + *ptr++ = *uri++; + else + break; + + *ptr = '\0'; + + if (*uri != ':') + { + *scheme = '\0'; + return (HTTP_URI_BAD_SCHEME); + } + + uri ++; + } + + /* + * Set the default port number... + */ + + if (!strcmp(scheme, "http")) + *port = 80; + else if (!strcmp(scheme, "https")) + *port = 443; + else if (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) + *port = 631; + else if (!_cups_strcasecmp(scheme, "lpd")) + *port = 515; + else if (!strcmp(scheme, "socket")) /* Not yet registered with IANA... */ + *port = 9100; + else if (strcmp(scheme, "file") && strcmp(scheme, "mailto")) + status = HTTP_URI_UNKNOWN_SCHEME; + + /* + * Now see if we have a hostname... + */ + + if (!strncmp(uri, "//", 2)) + { + /* + * Yes, extract it... + */ + + uri += 2; + + /* + * Grab the username, if any... + */ + + if ((sep = strpbrk(uri, "@/")) != NULL && *sep == '@') + { + /* + * Get a username:password combo... + */ + + uri = http_copy_decode(username, uri, usernamelen, "@", + decoding & HTTP_URI_CODING_USERNAME); + + if (!uri) + { + *username = '\0'; + return (HTTP_URI_BAD_USERNAME); + } + + uri ++; + } + + /* + * Then the hostname/IP address... + */ + + if (*uri == '[') + { + /* + * Grab IPv6 address... + */ + + uri ++; + if (*uri == 'v') + { + /* + * Skip IPvFuture ("vXXXX.") prefix... + */ + + uri ++; + + while (isxdigit(*uri & 255)) + uri ++; + + if (*uri != '.') + { + *host = '\0'; + return (HTTP_URI_BAD_HOSTNAME); + } + + uri ++; + } + + uri = http_copy_decode(host, uri, hostlen, "]", + decoding & HTTP_URI_CODING_HOSTNAME); + + if (!uri) + { + *host = '\0'; + return (HTTP_URI_BAD_HOSTNAME); + } + + /* + * Validate value... + */ + + if (*uri != ']') + { + *host = '\0'; + return (HTTP_URI_BAD_HOSTNAME); + } + + uri ++; + + for (ptr = host; *ptr; ptr ++) + if (*ptr == '+') + { + /* + * Convert zone separator to % and stop here... + */ + + *ptr = '%'; + break; + } + else if (*ptr == '%') + { + /* + * Stop at zone separator (RFC 6874) + */ + + break; + } + else if (*ptr != ':' && *ptr != '.' && !isxdigit(*ptr & 255)) + { + *host = '\0'; + return (HTTP_URI_BAD_HOSTNAME); + } + } + else + { + /* + * Validate the hostname or IPv4 address first... + */ + + for (ptr = (char *)uri; *ptr; ptr ++) + if (strchr(":?/", *ptr)) + break; + else if (!strchr("abcdefghijklmnopqrstuvwxyz" /* unreserved */ + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* unreserved */ + "0123456789" /* unreserved */ + "-._~" /* unreserved */ + "%" /* pct-encoded */ + "!$&'()*+,;=" /* sub-delims */ + "\\", *ptr)) /* SMB domain */ + { + *host = '\0'; + return (HTTP_URI_BAD_HOSTNAME); + } + + /* + * Then copy the hostname or IPv4 address to the buffer... + */ + + uri = http_copy_decode(host, uri, hostlen, ":?/", + decoding & HTTP_URI_CODING_HOSTNAME); + + if (!uri) + { + *host = '\0'; + return (HTTP_URI_BAD_HOSTNAME); + } + } + + /* + * Validate hostname for file scheme - only empty and localhost are + * acceptable. + */ + + if (!strcmp(scheme, "file") && strcmp(host, "localhost") && host[0]) + { + *host = '\0'; + return (HTTP_URI_BAD_HOSTNAME); + } + + /* + * See if we have a port number... + */ + + if (*uri == ':') + { + /* + * Yes, collect the port number... + */ + + if (!isdigit(uri[1] & 255)) + { + *port = 0; + return (HTTP_URI_BAD_PORT); + } + + *port = strtol(uri + 1, (char **)&uri, 10); + + if (*uri != '/' && *uri) + { + *port = 0; + return (HTTP_URI_BAD_PORT); + } + } + } + + /* + * The remaining portion is the resource string... + */ + + if (*uri == '?' || !*uri) + { + /* + * Hostname but no path... + */ + + status = HTTP_URI_MISSING_RESOURCE; + *resource = '/'; + + /* + * Copy any query string... + */ + + if (*uri == '?') + uri = http_copy_decode(resource + 1, uri, resourcelen - 1, NULL, + decoding & HTTP_URI_CODING_QUERY); + else + resource[1] = '\0'; + } + else + { + uri = http_copy_decode(resource, uri, resourcelen, "?", + decoding & HTTP_URI_CODING_RESOURCE); + + if (uri && *uri == '?') + { + /* + * Concatenate any query string... + */ + + char *resptr = resource + strlen(resource); + + uri = http_copy_decode(resptr, uri, + resourcelen - (int)(resptr - resource), NULL, + decoding & HTTP_URI_CODING_QUERY); + } + } + + if (!uri) + { + *resource = '\0'; + return (HTTP_URI_BAD_RESOURCE); + } + + /* + * Return the URI separation status... + */ + + return (status); +} + + +/* + * 'httpStatus()' - Return a short string describing a HTTP status code. + * + * The returned string is localized to the current POSIX locale and is based + * on the status strings defined in RFC 2616. + */ + +const char * /* O - Localized status string */ +httpStatus(http_status_t status) /* I - HTTP status code */ +{ + const char *s; /* Status string */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + switch (status) + { + case HTTP_ERROR : + s = strerror(errno); + break; + case HTTP_CONTINUE : + s = _("Continue"); + break; + case HTTP_SWITCHING_PROTOCOLS : + s = _("Switching Protocols"); + break; + case HTTP_OK : + s = _("OK"); + break; + case HTTP_CREATED : + s = _("Created"); + break; + case HTTP_ACCEPTED : + s = _("Accepted"); + break; + case HTTP_NO_CONTENT : + s = _("No Content"); + break; + case HTTP_MOVED_PERMANENTLY : + s = _("Moved Permanently"); + break; + case HTTP_SEE_OTHER : + s = _("See Other"); + break; + case HTTP_NOT_MODIFIED : + s = _("Not Modified"); + break; + case HTTP_BAD_REQUEST : + s = _("Bad Request"); + break; + case HTTP_UNAUTHORIZED : + case HTTP_AUTHORIZATION_CANCELED : + s = _("Unauthorized"); + break; + case HTTP_FORBIDDEN : + s = _("Forbidden"); + break; + case HTTP_NOT_FOUND : + s = _("Not Found"); + break; + case HTTP_REQUEST_TOO_LARGE : + s = _("Request Entity Too Large"); + break; + case HTTP_URI_TOO_LONG : + s = _("URI Too Long"); + break; + case HTTP_UPGRADE_REQUIRED : + s = _("Upgrade Required"); + break; + case HTTP_NOT_IMPLEMENTED : + s = _("Not Implemented"); + break; + case HTTP_NOT_SUPPORTED : + s = _("Not Supported"); + break; + case HTTP_EXPECTATION_FAILED : + s = _("Expectation Failed"); + break; + case HTTP_SERVICE_UNAVAILABLE : + s = _("Service Unavailable"); + break; + case HTTP_SERVER_ERROR : + s = _("Internal Server Error"); + break; + case HTTP_PKI_ERROR : + s = _("SSL/TLS Negotiation Error"); + break; + case HTTP_WEBIF_DISABLED : + s = _("Web Interface is Disabled"); + break; + + default : + s = _("Unknown"); + break; + } + + return (_cupsLangString(cg->lang_default, s)); +} + + +#ifndef HAVE_HSTRERROR +/* + * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others. + */ + +const char * /* O - Error string */ +_cups_hstrerror(int error) /* I - Error number */ +{ + static const char * const errors[] = /* Error strings */ + { + "OK", + "Host not found.", + "Try again.", + "Unrecoverable lookup error.", + "No data associated with name." + }; + + + if (error < 0 || error > 4) + return ("Unknown hostname lookup error."); + else + return (errors[error]); +} +#endif /* !HAVE_HSTRERROR */ + + +/* + * '_httpDecodeURI()' - Percent-decode a HTTP request URI. + */ + +char * /* O - Decoded URI or NULL on error */ +_httpDecodeURI(char *dst, /* I - Destination buffer */ + const char *src, /* I - Source URI */ + size_t dstsize) /* I - Size of destination buffer */ +{ + if (http_copy_decode(dst, src, (int)dstsize, NULL, 1)) + return (dst); + else + return (NULL); +} + + +/* + * '_httpEncodeURI()' - Percent-encode a HTTP request URI. + */ + +char * /* O - Encoded URI */ +_httpEncodeURI(char *dst, /* I - Destination buffer */ + const char *src, /* I - Source URI */ + size_t dstsize) /* I - Size of destination buffer */ +{ + http_copy_encode(dst, src, dst + dstsize - 1, NULL, NULL, 1); + return (dst); +} + + +/* + * '_httpResolveURI()' - Resolve a DNS-SD URI. + */ + +const char * /* O - Resolved URI */ +_httpResolveURI( + const char *uri, /* I - DNS-SD URI */ + char *resolved_uri, /* I - Buffer for resolved URI */ + size_t resolved_size, /* I - Size of URI buffer */ + int options, /* I - Resolve options */ + int (*cb)(void *context), /* I - Continue callback function */ + void *context) /* I - Context pointer for callback */ +{ + char scheme[32], /* URI components... */ + userpass[256], + hostname[1024], + resource[1024]; + int port; +#ifdef DEBUG + http_uri_status_t status; /* URI decode status */ +#endif /* DEBUG */ + + + DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, " + "resolved_size=" CUPS_LLFMT ")", uri, resolved_uri, + CUPS_LLCAST resolved_size)); + + /* + * Get the device URI... + */ + +#ifdef DEBUG + if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, + sizeof(scheme), userpass, sizeof(userpass), + hostname, sizeof(hostname), &port, resource, + sizeof(resource))) < HTTP_URI_OK) +#else + if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, + sizeof(scheme), userpass, sizeof(userpass), + hostname, sizeof(hostname), &port, resource, + sizeof(resource)) < HTTP_URI_OK) +#endif /* DEBUG */ + { + if (options & _HTTP_RESOLVE_STDERR) + _cupsLangPrintFilter(stderr, "ERROR", _("Bad device-uri \"%s\"."), uri); + + DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status)); + DEBUG_puts("5_httpResolveURI: Returning NULL"); + return (NULL); + } + + /* + * Resolve it as needed... + */ + + if (strstr(hostname, "._tcp")) + { +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) + char *regtype, /* Pointer to type in hostname */ + *domain; /* Pointer to domain in hostname */ + _http_uribuf_t uribuf; /* URI buffer */ + int offline = 0; /* offline-report state set? */ +# ifdef HAVE_DNSSD +# ifdef WIN32 +# pragma comment(lib, "dnssd.lib") +# endif /* WIN32 */ + DNSServiceRef ref, /* DNS-SD master service reference */ + domainref, /* DNS-SD service reference for domain */ + localref; /* DNS-SD service reference for .local */ + int domainsent = 0; /* Send the domain resolve? */ +# ifdef HAVE_POLL + struct pollfd polldata; /* Polling data */ +# else /* select() */ + fd_set input_set; /* Input set for select() */ + struct timeval stimeout; /* Timeout value for select() */ +# endif /* HAVE_POLL */ +# elif defined(HAVE_AVAHI) + AvahiClient *client; /* Client information */ + int error; /* Status */ +# endif /* HAVE_DNSSD */ + + if (options & _HTTP_RESOLVE_STDERR) + fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname); + + /* + * Separate the hostname into service name, registration type, and domain... + */ + + for (regtype = strstr(hostname, "._tcp") - 2; + regtype > hostname; + regtype --) + if (regtype[0] == '.' && regtype[1] == '_') + { + /* + * Found ._servicetype in front of ._tcp... + */ + + *regtype++ = '\0'; + break; + } + + if (regtype <= hostname) + { + DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL"); + return (NULL); + } + + for (domain = strchr(regtype, '.'); + domain; + domain = strchr(domain + 1, '.')) + if (domain[1] != '_') + break; + + if (domain) + *domain++ = '\0'; + + uribuf.buffer = resolved_uri; + uribuf.bufsize = resolved_size; + uribuf.options = options; + uribuf.resource = resource; + + resolved_uri[0] = '\0'; + + DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", " + "domain=\"%s\"\n", hostname, regtype, domain)); + if (options & _HTTP_RESOLVE_STDERR) + { + fputs("STATE: +connecting-to-device\n", stderr); + fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"%s\", " + "domain=\"local.\"...\n", hostname, regtype); + } + + uri = NULL; + +# ifdef HAVE_DNSSD + if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError) + { + localref = ref; + if (DNSServiceResolve(&localref, + kDNSServiceFlagsShareConnection, 0, hostname, regtype, + "local.", http_resolve_cb, + &uribuf) == kDNSServiceErr_NoError) + { + int fds; /* Number of ready descriptors */ + time_t timeout, /* Poll timeout */ + start_time = time(NULL),/* Start time */ + end_time = start_time + 90; + /* End time */ + + while (time(NULL) < end_time) + { + if (options & _HTTP_RESOLVE_STDERR) + _cupsLangPrintFilter(stderr, "INFO", _("Looking for printer.")); + + if (cb && !(*cb)(context)) + { + DEBUG_puts("5_httpResolveURI: callback returned 0 (stop)"); + break; + } + + /* + * Wakeup every 2 seconds to emit a "looking for printer" message... + */ + + if ((timeout = end_time - time(NULL)) > 2) + timeout = 2; + +# ifdef HAVE_POLL + polldata.fd = DNSServiceRefSockFD(ref); + polldata.events = POLLIN; + + fds = poll(&polldata, 1, 1000 * timeout); + +# else /* select() */ + FD_ZERO(&input_set); + FD_SET(DNSServiceRefSockFD(ref), &input_set); + +# ifdef WIN32 + stimeout.tv_sec = (long)timeout; +# else + stimeout.tv_sec = timeout; +# endif /* WIN32 */ + stimeout.tv_usec = 0; + + fds = select(DNSServiceRefSockFD(ref)+1, &input_set, NULL, NULL, + &stimeout); +# endif /* HAVE_POLL */ + + if (fds < 0) + { + if (errno != EINTR && errno != EAGAIN) + { + DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno))); + break; + } + } + else if (fds == 0) + { + /* + * Wait 2 seconds for a response to the local resolve; if nothing + * comes in, do an additional domain resolution... + */ + + if (domainsent == 0 && domain && _cups_strcasecmp(domain, "local.")) + { + if (options & _HTTP_RESOLVE_STDERR) + fprintf(stderr, + "DEBUG: Resolving \"%s\", regtype=\"%s\", " + "domain=\"%s\"...\n", hostname, regtype, + domain ? domain : ""); + + domainref = ref; + if (DNSServiceResolve(&domainref, + kDNSServiceFlagsShareConnection, + 0, hostname, regtype, domain, + http_resolve_cb, + &uribuf) == kDNSServiceErr_NoError) + domainsent = 1; + } + + /* + * If it hasn't resolved within 5 seconds set the offline-report + * printer-state-reason... + */ + + if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 && + time(NULL) > (start_time + 5)) + { + fputs("STATE: +offline-report\n", stderr); + offline = 1; + } + } + else + { + if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError) + { + uri = resolved_uri; + break; + } + } + } + + if (domainsent) + DNSServiceRefDeallocate(domainref); + + DNSServiceRefDeallocate(localref); + } + + DNSServiceRefDeallocate(ref); + } +# else /* HAVE_AVAHI */ + if ((uribuf.poll = avahi_simple_poll_new()) != NULL) + { + avahi_simple_poll_set_func(uribuf.poll, http_poll_cb, NULL); + + if ((client = avahi_client_new(avahi_simple_poll_get(uribuf.poll), + 0, http_client_cb, + &uribuf, &error)) != NULL) + { + if (avahi_service_resolver_new(client, AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, hostname, + regtype, "local.", AVAHI_PROTO_UNSPEC, 0, + http_resolve_cb, &uribuf) != NULL) + { + time_t start_time = time(NULL), + /* Start time */ + end_time = start_time + 90; + /* End time */ + int pstatus; /* Poll status */ + + pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000); + + if (pstatus == 0 && !resolved_uri[0] && domain && + _cups_strcasecmp(domain, "local.")) + { + /* + * Resolve for .local hasn't returned anything, try the listed + * domain... + */ + + avahi_service_resolver_new(client, AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, hostname, + regtype, domain, AVAHI_PROTO_UNSPEC, 0, + http_resolve_cb, &uribuf); + } + + while (!pstatus && !resolved_uri[0] && time(NULL) < end_time) + { + if ((pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000)) != 0) + break; + + /* + * If it hasn't resolved within 5 seconds set the offline-report + * printer-state-reason... + */ + + if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 && + time(NULL) > (start_time + 5)) + { + fputs("STATE: +offline-report\n", stderr); + offline = 1; + } + } + + /* + * Collect the result (if we got one). + */ + + if (resolved_uri[0]) + uri = resolved_uri; + } + + avahi_client_free(client); + } + + avahi_simple_poll_free(uribuf.poll); + } +# endif /* HAVE_DNSSD */ + + if (options & _HTTP_RESOLVE_STDERR) + { + if (uri) + { + fprintf(stderr, "DEBUG: Resolved as \"%s\"...\n", uri); + fputs("STATE: -connecting-to-device,offline-report\n", stderr); + } + else + { + fputs("DEBUG: Unable to resolve URI\n", stderr); + fputs("STATE: -connecting-to-device\n", stderr); + } + } + +#else /* HAVE_DNSSD || HAVE_AVAHI */ + /* + * No DNS-SD support... + */ + + uri = NULL; +#endif /* HAVE_DNSSD || HAVE_AVAHI */ + + if ((options & _HTTP_RESOLVE_STDERR) && !uri) + _cupsLangPrintFilter(stderr, "ERROR", _("Unable to find printer.")); + } + else + { + /* + * Nothing more to do... + */ + + strlcpy(resolved_uri, uri, resolved_size); + uri = resolved_uri; + } + + DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri)); + + return (uri); +} + + +#ifdef HAVE_AVAHI +/* + * 'http_client_cb()' - Client callback for resolving URI. + */ + +static void +http_client_cb( + AvahiClient *client, /* I - Client information */ + AvahiClientState state, /* I - Current state */ + void *context) /* I - Pointer to URI buffer */ +{ + DEBUG_printf(("7http_client_cb(client=%p, state=%d, context=%p)", client, + state, context)); + + /* + * If the connection drops, quit. + */ + + if (state == AVAHI_CLIENT_FAILURE) + { + _http_uribuf_t *uribuf = (_http_uribuf_t *)context; + /* URI buffer */ + + avahi_simple_poll_quit(uribuf->poll); + } +} +#endif /* HAVE_AVAHI */ + + +/* + * 'http_copy_decode()' - Copy and decode a URI. + */ + +static const char * /* O - New source pointer or NULL on error */ +http_copy_decode(char *dst, /* O - Destination buffer */ + const char *src, /* I - Source pointer */ + int dstsize, /* I - Destination size */ + const char *term, /* I - Terminating characters */ + int decode) /* I - Decode %-encoded values */ +{ + char *ptr, /* Pointer into buffer */ + *end; /* End of buffer */ + int quoted; /* Quoted character */ + + + /* + * Copy the src to the destination until we hit a terminating character + * or the end of the string. + */ + + for (ptr = dst, end = dst + dstsize - 1; + *src && (!term || !strchr(term, *src)); + src ++) + if (ptr < end) + { + if (*src == '%' && decode) + { + if (isxdigit(src[1] & 255) && isxdigit(src[2] & 255)) + { + /* + * Grab a hex-encoded character... + */ + + src ++; + if (isalpha(*src)) + quoted = (tolower(*src) - 'a' + 10) << 4; + else + quoted = (*src - '0') << 4; + + src ++; + if (isalpha(*src)) + quoted |= tolower(*src) - 'a' + 10; + else + quoted |= *src - '0'; + + *ptr++ = quoted; + } + else + { + /* + * Bad hex-encoded character... + */ + + *ptr = '\0'; + return (NULL); + } + } + else if ((*src & 255) <= 0x20 || (*src & 255) >= 0x7f) + { + *ptr = '\0'; + return (NULL); + } + else + *ptr++ = *src; + } + + *ptr = '\0'; + + return (src); +} + + +/* + * 'http_copy_encode()' - Copy and encode a URI. + */ + +static char * /* O - End of current URI */ +http_copy_encode(char *dst, /* O - Destination buffer */ + const char *src, /* I - Source pointer */ + char *dstend, /* I - End of destination buffer */ + const char *reserved, /* I - Extra reserved characters */ + const char *term, /* I - Terminating characters */ + int encode) /* I - %-encode reserved chars? */ +{ + static const char hex[] = "0123456789ABCDEF"; + + + while (*src && dst < dstend) + { + if (term && *src == *term) + return (dst); + + if (encode && (*src == '%' || *src <= ' ' || *src & 128 || + (reserved && strchr(reserved, *src)))) + { + /* + * Hex encode reserved characters... + */ + + if ((dst + 2) >= dstend) + break; + + *dst++ = '%'; + *dst++ = hex[(*src >> 4) & 15]; + *dst++ = hex[*src & 15]; + + src ++; + } + else + *dst++ = *src++; + } + + *dst = '\0'; + + if (*src) + return (NULL); + else + return (dst); +} + + +#ifdef HAVE_DNSSD +/* + * 'http_resolve_cb()' - Build a device URI for the given service name. + */ + +static void DNSSD_API +http_resolve_cb( + DNSServiceRef sdRef, /* I - Service reference */ + DNSServiceFlags flags, /* I - Results flags */ + uint32_t interfaceIndex, /* I - Interface number */ + DNSServiceErrorType errorCode, /* I - Error, if any */ + const char *fullName, /* I - Full service name */ + const char *hostTarget, /* I - Hostname */ + uint16_t port, /* I - Port number */ + uint16_t txtLen, /* I - Length of TXT record */ + const unsigned char *txtRecord, /* I - TXT record data */ + void *context) /* I - Pointer to URI buffer */ +{ + _http_uribuf_t *uribuf = (_http_uribuf_t *)context; + /* URI buffer */ + const char *scheme, /* URI scheme */ + *hostptr, /* Pointer into hostTarget */ + *reskey, /* "rp" or "rfo" */ + *resdefault; /* Default path */ + char resource[257], /* Remote path */ + fqdn[256]; /* FQDN of the .local name */ + const void *value; /* Value from TXT record */ + uint8_t valueLen; /* Length of value */ + + + DEBUG_printf(("7http_resolve_cb(sdRef=%p, flags=%x, interfaceIndex=%u, " + "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, " + "txtLen=%u, txtRecord=%p, context=%p)", sdRef, flags, + interfaceIndex, errorCode, fullName, hostTarget, port, txtLen, + txtRecord, context)); + + /* + * Figure out the scheme from the full name... + */ + + if (strstr(fullName, "._ipps") || strstr(fullName, "._ipp-tls")) + scheme = "ipps"; + else if (strstr(fullName, "._ipp") || strstr(fullName, "._fax-ipp")) + scheme = "ipp"; + else if (strstr(fullName, "._http.")) + scheme = "http"; + else if (strstr(fullName, "._https.")) + scheme = "https"; + else if (strstr(fullName, "._printer.")) + scheme = "lpd"; + else if (strstr(fullName, "._pdl-datastream.")) + scheme = "socket"; + else + scheme = "riousbprint"; + + /* + * Extract the "remote printer" key from the TXT record... + */ + + if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) && + (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps"))) + { + reskey = "rfo"; + resdefault = "/ipp/faxout"; + } + else + { + reskey = "rp"; + resdefault = "/"; + } + + if ((value = TXTRecordGetValuePtr(txtLen, txtRecord, reskey, + &valueLen)) != NULL) + { + if (((char *)value)[0] == '/') + { + /* + * Value (incorrectly) has a leading slash already... + */ + + memcpy(resource, value, valueLen); + resource[valueLen] = '\0'; + } + else + { + /* + * Convert to resource by concatenating with a leading "/"... + */ + + resource[0] = '/'; + memcpy(resource + 1, value, valueLen); + resource[valueLen + 1] = '\0'; + } + } + else + { + /* + * Use the default value... + */ + + strlcpy(resource, resdefault, sizeof(resource)); + } + + /* + * Lookup the FQDN if needed... + */ + + if ((uribuf->options & _HTTP_RESOLVE_FQDN) && + (hostptr = hostTarget + strlen(hostTarget) - 7) > hostTarget && + !_cups_strcasecmp(hostptr, ".local.")) + { + /* + * OK, we got a .local name but the caller needs a real domain. Start by + * getting the IP address of the .local name and then do reverse-lookups... + */ + + http_addrlist_t *addrlist, /* List of addresses */ + *addr; /* Current address */ + + DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget)); + + snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port)); + if ((addrlist = httpAddrGetList(hostTarget, AF_UNSPEC, fqdn)) != NULL) + { + for (addr = addrlist; addr; addr = addr->next) + { + int error = getnameinfo(&(addr->addr.addr), + httpAddrLength(&(addr->addr)), + fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD); + + if (!error) + { + DEBUG_printf(("8http_resolve_cb: Found \"%s\".", fqdn)); + + if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn || + _cups_strcasecmp(hostptr, ".local")) + { + hostTarget = fqdn; + break; + } + } +#ifdef DEBUG + else + DEBUG_printf(("8http_resolve_cb: \"%s\" did not resolve: %d", + httpAddrString(&(addr->addr), fqdn, sizeof(fqdn)), + error)); +#endif /* DEBUG */ + } + + httpAddrFreeList(addrlist); + } + } + + /* + * Assemble the final device URI... + */ + + if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) && + !strcmp(uribuf->resource, "/cups")) + httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, + scheme, NULL, hostTarget, ntohs(port), "%s?snmp=false", + resource); + else + httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, + scheme, NULL, hostTarget, ntohs(port), resource); + + DEBUG_printf(("8http_resolve_cb: Resolved URI is \"%s\"...", uribuf->buffer)); +} + +#elif defined(HAVE_AVAHI) +/* + * 'http_poll_cb()' - Wait for input on the specified file descriptors. + * + * Note: This function is needed because avahi_simple_poll_iterate is broken + * and always uses a timeout of 0 (!) milliseconds. + * (Avahi Ticket #364) + */ + +static int /* O - Number of file descriptors matching */ +http_poll_cb( + struct pollfd *pollfds, /* I - File descriptors */ + unsigned int num_pollfds, /* I - Number of file descriptors */ + int timeout, /* I - Timeout in milliseconds (used) */ + void *context) /* I - User data (unused) */ +{ + (void)timeout; + (void)context; + + return (poll(pollfds, num_pollfds, 2000)); +} + + +/* + * 'http_resolve_cb()' - Build a device URI for the given service name. + */ + +static void +http_resolve_cb( + AvahiServiceResolver *resolver, /* I - Resolver (unused) */ + AvahiIfIndex interface, /* I - Interface index (unused) */ + AvahiProtocol protocol, /* I - Network protocol (unused) */ + AvahiResolverEvent event, /* I - Event (found, etc.) */ + const char *name, /* I - Service name */ + const char *type, /* I - Registration type */ + const char *domain, /* I - Domain (unused) */ + const char *hostTarget, /* I - Hostname */ + const AvahiAddress *address, /* I - Address (unused) */ + uint16_t port, /* I - Port number */ + AvahiStringList *txt, /* I - TXT record */ + AvahiLookupResultFlags flags, /* I - Lookup flags (unused) */ + void *context) /* I - Pointer to URI buffer */ +{ + _http_uribuf_t *uribuf = (_http_uribuf_t *)context; + /* URI buffer */ + const char *scheme, /* URI scheme */ + *hostptr, /* Pointer into hostTarget */ + *reskey, /* "rp" or "rfo" */ + *resdefault; /* Default path */ + char resource[257], /* Remote path */ + fqdn[256]; /* FQDN of the .local name */ + AvahiStringList *pair; /* Current TXT record key/value pair */ + char *value; /* Value for "rp" key */ + size_t valueLen = 0; /* Length of "rp" key */ + + + DEBUG_printf(("7http_resolve_cb(resolver=%p, " + "interface=%d, protocol=%d, event=%d, name=\"%s\", " + "type=\"%s\", domain=\"%s\", hostTarget=\"%s\", address=%p, " + "port=%d, txt=%p, flags=%d, context=%p)", + resolver, interface, protocol, event, name, type, domain, + hostTarget, address, port, txt, flags, context)); + + if (event != AVAHI_RESOLVER_FOUND) + { + avahi_service_resolver_free(resolver); + avahi_simple_poll_quit(uribuf->poll); + return; + } + + /* + * Figure out the scheme from the full name... + */ + + if (strstr(type, "_ipp.")) + scheme = "ipp"; + else if (strstr(type, "_printer.")) + scheme = "lpd"; + else if (strstr(type, "_pdl-datastream.")) + scheme = "socket"; + else + scheme = "riousbprint"; + + if (!strncmp(type, "_ipps.", 6) || !strncmp(type, "_ipp-tls.", 9)) + scheme = "ipps"; + else if (!strncmp(type, "_ipp.", 5) || !strncmp(type, "_fax-ipp.", 9)) + scheme = "ipp"; + else if (!strncmp(type, "_http.", 6)) + scheme = "http"; + else if (!strncmp(type, "_https.", 7)) + scheme = "https"; + else if (!strncmp(type, "_printer.", 9)) + scheme = "lpd"; + else if (!strncmp(type, "_pdl-datastream.", 16)) + scheme = "socket"; + else + { + avahi_service_resolver_free(resolver); + avahi_simple_poll_quit(uribuf->poll); + return; + } + + /* + * Extract the remote resource key from the TXT record... + */ + + if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) && + (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps"))) + { + reskey = "rfo"; + resdefault = "/ipp/faxout"; + } + else + { + reskey = "rp"; + resdefault = "/"; + } + + if ((pair = avahi_string_list_find(txt, reskey)) != NULL) + { + avahi_string_list_get_pair(pair, NULL, &value, &valueLen); + + if (value[0] == '/') + { + /* + * Value (incorrectly) has a leading slash already... + */ + + memcpy(resource, value, valueLen); + resource[valueLen] = '\0'; + } + else + { + /* + * Convert to resource by concatenating with a leading "/"... + */ + + resource[0] = '/'; + memcpy(resource + 1, value, valueLen); + resource[valueLen + 1] = '\0'; + } + } + else + { + /* + * Use the default value... + */ + + strlcpy(resource, resdefault, sizeof(resource)); + } + + /* + * Lookup the FQDN if needed... + */ + + if ((uribuf->options & _HTTP_RESOLVE_FQDN) && + (hostptr = hostTarget + strlen(hostTarget) - 6) > hostTarget && + !_cups_strcasecmp(hostptr, ".local")) + { + /* + * OK, we got a .local name but the caller needs a real domain. Start by + * getting the IP address of the .local name and then do reverse-lookups... + */ + + http_addrlist_t *addrlist, /* List of addresses */ + *addr; /* Current address */ + + DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget)); + + snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port)); + if ((addrlist = httpAddrGetList(hostTarget, AF_UNSPEC, fqdn)) != NULL) + { + for (addr = addrlist; addr; addr = addr->next) + { + int error = getnameinfo(&(addr->addr.addr), + httpAddrLength(&(addr->addr)), + fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD); + + if (!error) + { + DEBUG_printf(("8http_resolve_cb: Found \"%s\".", fqdn)); + + if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn || + _cups_strcasecmp(hostptr, ".local")) + { + hostTarget = fqdn; + break; + } + } +#ifdef DEBUG + else + DEBUG_printf(("8http_resolve_cb: \"%s\" did not resolve: %d", + httpAddrString(&(addr->addr), fqdn, sizeof(fqdn)), + error)); +#endif /* DEBUG */ + } + + httpAddrFreeList(addrlist); + } + } + + /* + * Assemble the final device URI using the resolved hostname... + */ + + httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme, + NULL, hostTarget, port, resource); + DEBUG_printf(("8http_resolve_cb: Resolved URI is \"%s\".", uribuf->buffer)); + + avahi_simple_poll_quit(uribuf->poll); +} +#endif /* HAVE_DNSSD */ + + +/* + * End of "$Id: http-support.c 7952 2008-09-17 00:56:20Z mike $". + */ diff --git a/cups/http.c b/cups/http.c new file mode 100644 index 0000000..ec948f6 --- /dev/null +++ b/cups/http.c @@ -0,0 +1,4778 @@ +/* + * "$Id: http.c 7850 2008-08-20 00:07:25Z mike $" + * + * HTTP routines for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * This file contains Kerberos support code, copyright 2006 by + * Jelmer Vernooij. + * + * 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. + * + * Contents: + * + * httpAddCredential() - Allocates and adds a single credential to an + * array. + * _httpBIOMethods() - Get the OpenSSL BIO methods for HTTP + * connections. + * httpBlocking() - Set blocking/non-blocking behavior on a + * connection. + * httpCheck() - Check to see if there is a pending response + * from the server. + * httpClearCookie() - Clear the cookie value(s). + * httpClearFields() - Clear HTTP request fields. + * httpClose() - Close an HTTP connection. + * httpConnect() - Connect to a HTTP server. + * httpConnectEncrypt() - Connect to a HTTP server using encryption. + * httpCopyCredentials() - Copy the credentials associated with an + * encrypted connection. + * _httpCreate() - Create an unconnected HTTP connection. + * _httpCreateCredentials() - Create credentials in the internal format. + * httpDelete() - Send a DELETE request to the server. + * _httpDisconnect() - Disconnect a HTTP connection. + * httpEncryption() - Set the required encryption on the link. + * httpError() - Get the last error on a connection. + * httpFlush() - Flush data from a HTTP connection. + * httpFlushWrite() - Flush data in write buffer. + * _httpFreeCredentials() - Free internal credentials. + * httpFreeCredentials() - Free an array of credentials. + * httpGet() - Send a GET request to the server. + * httpGetAuthString() - Get the current authorization string. + * httpGetBlocking() - Get the blocking/non-block state of a + * connection. + * httpGetCookie() - Get any cookie data from the response. + * httpGetFd() - Get the file descriptor associated with a + * connection. + * httpGetField() - Get a field value from a request/response. + * httpGetLength() - Get the amount of data remaining from the + * content-length or transfer-encoding fields. + * httpGetLength2() - Get the amount of data remaining from the + * content-length or transfer-encoding fields. + * httpGets() - Get a line of text from a HTTP connection. + * httpGetState() - Get the current state of the HTTP request. + * httpGetStatus() - Get the status of the last HTTP request. + * httpGetSubField() - Get a sub-field value. + * httpGetSubField2() - Get a sub-field value. + * httpGetVersion() - Get the HTTP version at the other end. + * httpHead() - Send a HEAD request to the server. + * httpInitialize() - Initialize the HTTP interface library and set + * the default HTTP proxy (if any). + * httpOptions() - Send an OPTIONS request to the server. + * _httpPeek() - Peek at data from a HTTP connection. + * httpPost() - Send a POST request to the server. + * httpPrintf() - Print a formatted string to a HTTP connection. + * httpPut() - Send a PUT request to the server. + * httpRead() - Read data from a HTTP connection. + * httpRead2() - Read data from a HTTP connection. + * _httpReadCDSA() - Read function for the CDSA library. + * _httpReadGNUTLS() - Read function for the GNU TLS library. + * httpReconnect() - Reconnect to a HTTP server. + * httpReconnect2() - Reconnect to a HTTP server with timeout and + * optional cancel. + * httpSetAuthString() - Set the current authorization string. + * httpSetCredentials() - Set the credentials associated with an + * encrypted connection. + * httpSetCookie() - Set the cookie value(s). + * httpSetExpect() - Set the Expect: header in a request. + * httpSetField() - Set the value of an HTTP header. + * httpSetLength() - Set the content-length and content-encoding. + * httpSetTimeout() - Set read/write timeouts and an optional + * callback. + * httpTrace() - Send an TRACE request to the server. + * _httpUpdate() - Update the current HTTP status for incoming + * data. + * httpUpdate() - Update the current HTTP state for incoming + * data. + * _httpWait() - Wait for data available on a connection (no + * flush). + * httpWait() - Wait for data available on a connection. + * httpWrite() - Write data to a HTTP connection. + * httpWrite2() - Write data to a HTTP connection. + * _httpWriteCDSA() - Write function for the CDSA library. + * _httpWriteGNUTLS() - Write function for the GNU TLS library. + * http_bio_ctrl() - Control the HTTP connection. + * http_bio_free() - Free OpenSSL data. + * http_bio_new() - Initialize an OpenSSL BIO structure. + * http_bio_puts() - Send a string for OpenSSL. + * http_bio_read() - Read data for OpenSSL. + * http_bio_write() - Write data for OpenSSL. + * http_debug_hex() - Do a hex dump of a buffer. + * http_field() - Return the field index for a field name. + * http_read_ssl() - Read from a SSL/TLS connection. + * http_send() - Send a request with all fields and the trailing + * blank line. + * http_set_credentials() - Set the SSL/TLS credentials. + * http_set_timeout() - Set the socket timeout values. + * http_set_wait() - Set the default wait value for reads. + * http_setup_ssl() - Set up SSL/TLS support on a connection. + * http_shutdown_ssl() - Shut down SSL/TLS on a connection. + * http_upgrade() - Force upgrade to TLS encryption. + * http_write() - Write a buffer to a HTTP connection. + * http_write_chunk() - Write a chunked buffer. + * http_write_ssl() - Write to a SSL/TLS connection. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#ifdef WIN32 +# include +#else +# include +# include +# include +# include +#endif /* WIN32 */ +#ifdef HAVE_POLL +# include +#endif /* HAVE_POLL */ + + +/* + * Local functions... + */ + +#ifdef DEBUG +static void http_debug_hex(const char *prefix, const char *buffer, + int bytes); +#endif /* DEBUG */ +static http_field_t http_field(const char *name); +static int http_send(http_t *http, http_state_t request, + const char *uri); +static int http_write(http_t *http, const char *buffer, + int length); +static int http_write_chunk(http_t *http, const char *buffer, + int length); +#ifdef HAVE_SSL +static int http_read_ssl(http_t *http, char *buf, int len); +# if defined(HAVE_CDSASSL) && defined(HAVE_SECCERTIFICATECOPYDATA) +static int http_set_credentials(http_t *http); +# endif /* HAVE_CDSASSL ** HAVE_SECCERTIFICATECOPYDATA */ +#endif /* HAVE_SSL */ +static void http_set_timeout(int fd, double timeout); +static void http_set_wait(http_t *http); +#ifdef HAVE_SSL +static int http_setup_ssl(http_t *http); +static void http_shutdown_ssl(http_t *http); +static int http_upgrade(http_t *http); +static int http_write_ssl(http_t *http, const char *buf, int len); +#endif /* HAVE_SSL */ + + +/* + * Local globals... + */ + +static const char * const http_fields[] = + { + "Accept-Language", + "Accept-Ranges", + "Authorization", + "Connection", + "Content-Encoding", + "Content-Language", + "Content-Length", + "Content-Location", + "Content-MD5", + "Content-Range", + "Content-Type", + "Content-Version", + "Date", + "Host", + "If-Modified-Since", + "If-Unmodified-since", + "Keep-Alive", + "Last-Modified", + "Link", + "Location", + "Range", + "Referer", + "Retry-After", + "Transfer-Encoding", + "Upgrade", + "User-Agent", + "WWW-Authenticate" + }; +#ifdef DEBUG +static const char * const http_states[] = + { + "HTTP_WAITING", + "HTTP_OPTIONS", + "HTTP_GET", + "HTTP_GET_SEND", + "HTTP_HEAD", + "HTTP_POST", + "HTTP_POST_RECV", + "HTTP_POST_SEND", + "HTTP_PUT", + "HTTP_PUT_RECV", + "HTTP_DELETE", + "HTTP_TRACE", + "HTTP_CLOSE", + "HTTP_STATUS" + }; +#endif /* DEBUG */ + + +#if defined(HAVE_SSL) && defined(HAVE_LIBSSL) +/* + * BIO methods for OpenSSL... + */ + +static int http_bio_write(BIO *h, const char *buf, int num); +static int http_bio_read(BIO *h, char *buf, int size); +static int http_bio_puts(BIO *h, const char *str); +static long http_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2); +static int http_bio_new(BIO *h); +static int http_bio_free(BIO *data); + +static BIO_METHOD http_bio_methods = + { + BIO_TYPE_SOCKET, + "http", + http_bio_write, + http_bio_read, + http_bio_puts, + NULL, /* http_bio_gets, */ + http_bio_ctrl, + http_bio_new, + http_bio_free, + NULL, + }; +#endif /* HAVE_SSL && HAVE_LIBSSL */ + + +/* + * 'httpAddCredential()' - Allocates and adds a single credential to an array. + * + * Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +int /* O - 0 on success, -1 on error */ +httpAddCredential( + cups_array_t *credentials, /* I - Credentials array */ + const void *data, /* I - PEM-encoded X.509 data */ + size_t datalen) /* I - Length of data */ +{ + http_credential_t *credential; /* Credential data */ + + + if ((credential = malloc(sizeof(http_credential_t))) != NULL) + { + credential->datalen = datalen; + + if ((credential->data = malloc(datalen)) != NULL) + { + memcpy(credential->data, data, datalen); + cupsArrayAdd(credentials, credential); + return (0); + } + + free(credential); + } + + return (-1); +} + + +#if defined(HAVE_SSL) && defined(HAVE_LIBSSL) +/* + * '_httpBIOMethods()' - Get the OpenSSL BIO methods for HTTP connections. + */ + +BIO_METHOD * /* O - BIO methods for OpenSSL */ +_httpBIOMethods(void) +{ + return (&http_bio_methods); +} +#endif /* HAVE_SSL && HAVE_LIBSSL */ + + +/* + * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection. + */ + +void +httpBlocking(http_t *http, /* I - Connection to server */ + int b) /* I - 1 = blocking, 0 = non-blocking */ +{ + if (http) + { + http->blocking = b; + http_set_wait(http); + } +} + + +/* + * 'httpCheck()' - Check to see if there is a pending response from the server. + */ + +int /* O - 0 = no data, 1 = data available */ +httpCheck(http_t *http) /* I - Connection to server */ +{ + return (httpWait(http, 0)); +} + + +/* + * 'httpClearCookie()' - Clear the cookie value(s). + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +void +httpClearCookie(http_t *http) /* I - Connection to server */ +{ + if (!http) + return; + + if (http->cookie) + { + free(http->cookie); + http->cookie = NULL; + } +} + + +/* + * 'httpClearFields()' - Clear HTTP request fields. + */ + +void +httpClearFields(http_t *http) /* I - Connection to server */ +{ + if (http) + { + // 07/22/2016 Mopria-notice: preserve User Agent + char buf[HTTP_MAX_VALUE]; + + memcpy(buf, http->fields[HTTP_FIELD_USER_AGENT], HTTP_MAX_VALUE); + memset(http->fields, 0, sizeof(http->fields)); + + // 07/22/2016 Mopria-notice: restore User Agent + if (buf[0]) httpSetField(http, HTTP_FIELD_USER_AGENT, buf); + + if (http->hostname[0] == '/') + httpSetField(http, HTTP_FIELD_HOST, "localhost"); + else + httpSetField(http, HTTP_FIELD_HOST, http->hostname); + + if (http->field_authorization) + { + free(http->field_authorization); + http->field_authorization = NULL; + } + + http->expect = (http_status_t)0; + } +} + + +/* + * 'httpClose()' - Close an HTTP connection. + */ + +void +httpClose(http_t *http) /* I - Connection to server */ +{ +#ifdef HAVE_GSSAPI + OM_uint32 minor_status; /* Minor status code */ +#endif /* HAVE_GSSAPI */ + + + DEBUG_printf(("httpClose(http=%p)", http)); + + /* + * Range check input... + */ + + if (!http) + return; + + /* + * Close any open connection... + */ + + _httpDisconnect(http); + + /* + * Free memory used... + */ + + httpAddrFreeList(http->addrlist); + + if (http->cookie) + free(http->cookie); + +#ifdef HAVE_GSSAPI + if (http->gssctx != GSS_C_NO_CONTEXT) + gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER); + + if (http->gssname != GSS_C_NO_NAME) + gss_release_name(&minor_status, &http->gssname); +#endif /* HAVE_GSSAPI */ + +#ifdef HAVE_AUTHORIZATION_H + if (http->auth_ref) + AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); +#endif /* HAVE_AUTHORIZATION_H */ + + httpClearFields(http); + + if (http->authstring && http->authstring != http->_authstring) + free(http->authstring); + + free(http); +} + + +/* + * 'httpConnect()' - Connect to a HTTP server. + * + * This function is deprecated - use @link httpConnectEncrypt@ instead. + * + * @deprecated@ + */ + +http_t * /* O - New HTTP connection */ +httpConnect(const char *host, /* I - Host to connect to */ + int port) /* I - Port number */ +{ + return (httpConnectEncrypt(host, port, HTTP_ENCRYPT_IF_REQUESTED)); +} + + +/* + * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption. + */ + +http_t * /* O - New HTTP connection */ +httpConnectEncrypt( + const char *host, /* I - Host to connect to */ + int port, /* I - Port number */ + http_encryption_t encryption) /* I - Type of encryption to use */ +{ + http_t *http; /* New HTTP connection */ + + + DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)", + host, port, encryption)); + + /* + * Create the HTTP structure... + */ + + if ((http = _httpCreate(host, port, NULL, encryption, AF_UNSPEC)) == NULL) + return (NULL); + + /* + * Connect to the remote system... + */ + + if (!httpReconnect(http)) + return (http); + + /* + * Could not connect to any known address - bail out! + */ + + httpAddrFreeList(http->addrlist); + + free(http); + + return (NULL); +} + + +/* + * 'httpCopyCredentials()' - Copy the credentials associated with an encrypted + * connection. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +int /* O - Status of call (0 = success) */ +httpCopyCredentials( + http_t *http, /* I - Connection to server */ + cups_array_t **credentials) /* O - Array of credentials */ +{ +# ifdef HAVE_LIBSSL +# elif defined(HAVE_GNUTLS) +# elif defined(HAVE_CDSASSL) && defined(HAVE_SECCERTIFICATECOPYDATA) + OSStatus error; /* Error code */ + CFIndex count; /* Number of credentials */ + CFArrayRef peerCerts; /* Peer certificates */ + SecCertificateRef secCert; /* Certificate reference */ + CFDataRef data; /* Certificate data */ + int i; /* Looping var */ +# elif defined(HAVE_SSPISSL) +# endif /* HAVE_LIBSSL */ + + + if (credentials) + *credentials = NULL; + + if (!http || !http->tls || !credentials) + return (-1); + +# ifdef HAVE_LIBSSL + return (-1); + +# elif defined(HAVE_GNUTLS) + return (-1); + +# elif defined(HAVE_CDSASSL) && defined(HAVE_SECCERTIFICATECOPYDATA) + if (!(error = SSLCopyPeerCertificates(http->tls, &peerCerts)) && peerCerts) + { + if ((*credentials = cupsArrayNew(NULL, NULL)) != NULL) + { + for (i = 0, count = CFArrayGetCount(peerCerts); i < count; i++) + { + secCert = (SecCertificateRef)CFArrayGetValueAtIndex(peerCerts, i); + if ((data = SecCertificateCopyData(secCert))) + { + httpAddCredential(*credentials, CFDataGetBytePtr(data), + CFDataGetLength(data)); + CFRelease(data); + } + } + } + + CFRelease(peerCerts); + } + + return (error); + +# elif defined(HAVE_SSPISSL) + return (-1); + +# else + return (-1); +# endif /* HAVE_LIBSSL */ +} + + +/* + * '_httpCreate()' - Create an unconnected HTTP connection. + */ + +http_t * /* O - HTTP connection */ +_httpCreate( + const char *host, /* I - Hostname */ + int port, /* I - Port number */ + http_addrlist_t *addrlist, /* I - Address list or NULL */ + http_encryption_t encryption, /* I - Encryption to use */ + int family) /* I - Address family or AF_UNSPEC */ +{ + http_t *http; /* New HTTP connection */ + char service[255]; /* Service name */ + + + DEBUG_printf(("4_httpCreate(host=\"%s\", port=%d, encryption=%d)", + host, port, encryption)); + + if (!host) + return (NULL); + + httpInitialize(); + + /* + * Lookup the host... + */ + + sprintf(service, "%d", port); + + if (!addrlist) + if ((addrlist = httpAddrGetList(host, family, service)) == NULL) + return (NULL); + + /* + * Allocate memory for the structure... + */ + + if ((http = calloc(sizeof(http_t), 1)) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + httpAddrFreeList(addrlist); + return (NULL); + } + + /* + * Initialize the HTTP data... + */ + + http->activity = time(NULL); + http->addrlist = addrlist; + http->blocking = 1; + http->fd = -1; +#ifdef HAVE_GSSAPI + http->gssctx = GSS_C_NO_CONTEXT; + http->gssname = GSS_C_NO_NAME; +#endif /* HAVE_GSSAPI */ + http->version = HTTP_1_1; + + strlcpy(http->hostname, host, sizeof(http->hostname)); + + if (port == 443) /* Always use encryption for https */ + http->encryption = HTTP_ENCRYPT_ALWAYS; + else + http->encryption = encryption; + + http_set_wait(http); + + /* + * Return the new structure... + */ + + return (http); +} + + +/* + * '_httpCreateCredentials()' - Create credentials in the internal format. + */ + +http_tls_credentials_t /* O - Internal credentials */ +_httpCreateCredentials( + cups_array_t *credentials) /* I - Array of credentials */ +{ + if (!credentials) + return (NULL); + +# ifdef HAVE_LIBSSL + return (NULL); + +# elif defined(HAVE_GNUTLS) + return (NULL); + +# elif defined(HAVE_CDSASSL) && defined(HAVE_SECCERTIFICATECOPYDATA) + CFMutableArrayRef peerCerts; /* Peer credentials reference */ + SecCertificateRef secCert; /* Certificate reference */ + CFDataRef data; /* Credential data reference */ + http_credential_t *credential; /* Credential data */ + + + if ((peerCerts = CFArrayCreateMutable(kCFAllocatorDefault, + cupsArrayCount(credentials), + &kCFTypeArrayCallBacks)) == NULL) + return (NULL); + + for (credential = (http_credential_t *)cupsArrayFirst(credentials); + credential; + credential = (http_credential_t *)cupsArrayNext(credentials)) + { + if ((data = CFDataCreate(kCFAllocatorDefault, credential->data, + credential->datalen))) + { + if ((secCert = SecCertificateCreateWithData(kCFAllocatorDefault, data)) + != NULL) + { + CFArrayAppendValue(peerCerts, secCert); + CFRelease(secCert); + } + + CFRelease(data); + } + } + + return (peerCerts); + +# elif defined(HAVE_SSPISSL) + return (NULL); + +# else + return (NULL); +# endif /* HAVE_LIBSSL */ +} + + +/* + * 'httpDelete()' - Send a DELETE request to the server. + */ + +int /* O - Status of call (0 = success) */ +httpDelete(http_t *http, /* I - Connection to server */ + const char *uri) /* I - URI to delete */ +{ + return (http_send(http, HTTP_DELETE, uri)); +} + + +/* + * '_httpDisconnect()' - Disconnect a HTTP connection. + */ + +void +_httpDisconnect(http_t *http) /* I - Connection to server */ +{ +#ifdef HAVE_SSL + if (http->tls) + http_shutdown_ssl(http); +#endif /* HAVE_SSL */ + +#ifdef WIN32 + closesocket(http->fd); +#else + close(http->fd); +#endif /* WIN32 */ + + http->fd = -1; +} + + +/* + * 'httpEncryption()' - Set the required encryption on the link. + */ + +int /* O - -1 on error, 0 on success */ +httpEncryption(http_t *http, /* I - Connection to server */ + http_encryption_t e) /* I - New encryption preference */ +{ + DEBUG_printf(("httpEncryption(http=%p, e=%d)", http, e)); + +#ifdef HAVE_SSL + if (!http) + return (0); + + http->encryption = e; + + if ((http->encryption == HTTP_ENCRYPT_ALWAYS && !http->tls) || + (http->encryption == HTTP_ENCRYPT_NEVER && http->tls)) + return (httpReconnect(http)); + else if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls) + return (http_upgrade(http)); + else + return (0); +#else + if (e == HTTP_ENCRYPT_ALWAYS || e == HTTP_ENCRYPT_REQUIRED) + return (-1); + else + return (0); +#endif /* HAVE_SSL */ +} + + +/* + * 'httpError()' - Get the last error on a connection. + */ + +int /* O - Error code (errno) value */ +httpError(http_t *http) /* I - Connection to server */ +{ + if (http) + return (http->error); + else + return (EINVAL); +} + + +/* + * 'httpFlush()' - Flush data from a HTTP connection. + */ + +void +httpFlush(http_t *http) /* I - Connection to server */ +{ + char buffer[8192]; /* Junk buffer */ + int blocking; /* To block or not to block */ + http_state_t oldstate; /* Old state */ + + + DEBUG_printf(("httpFlush(http=%p), state=%s", http, + http_states[http->state])); + + /* + * Temporarily set non-blocking mode so we don't get stuck in httpRead()... + */ + + blocking = http->blocking; + http->blocking = 0; + + /* + * Read any data we can... + */ + + oldstate = http->state; + while (httpRead2(http, buffer, sizeof(buffer)) > 0); + + /* + * Restore blocking and reset the connection if we didn't get all of + * the remaining data... + */ + + http->blocking = blocking; + + if (http->state == oldstate && http->state != HTTP_WAITING && http->fd >= 0) + { + /* + * Didn't get the data back, so close the current connection. + */ + + http->state = HTTP_WAITING; + +#ifdef HAVE_SSL + if (http->tls) + http_shutdown_ssl(http); +#endif /* HAVE_SSL */ + +#ifdef WIN32 + closesocket(http->fd); +#else + close(http->fd); +#endif /* WIN32 */ + + http->fd = -1; + } +} + + +/* + * 'httpFlushWrite()' - Flush data in write buffer. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - Bytes written or -1 on error */ +httpFlushWrite(http_t *http) /* I - Connection to server */ +{ + int bytes; /* Bytes written */ + + + DEBUG_printf(("httpFlushWrite(http=%p)", http)); + + if (!http || !http->wused) + { + DEBUG_puts(http ? "1httpFlushWrite: Write buffer is empty." : + "1httpFlushWrite: No connection."); + return (0); + } + + if (http->data_encoding == HTTP_ENCODE_CHUNKED) + bytes = http_write_chunk(http, http->wbuffer, http->wused); + else + bytes = http_write(http, http->wbuffer, http->wused); + + http->wused = 0; + + DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", bytes, errno)); + + return (bytes); +} + + +/* + * '_httpFreeCredentials()' - Free internal credentials. + */ + +void +_httpFreeCredentials( + http_tls_credentials_t credentials) /* I - Internal credentials */ +{ + if (!credentials) + return; + +#ifdef HAVE_LIBSSL + (void)credentials; + +#elif defined(HAVE_GNUTLS) + (void)credentials; + +#elif defined(HAVE_CDSASSL) + CFRelease(credentials); + +#elif defined(HAVE_SSPISSL) + (void)credentials; + +#endif /* HAVE_LIBSSL */ +} + + +/* + * 'httpFreeCredentials()' - Free an array of credentials. + */ + +void +httpFreeCredentials( + cups_array_t *credentials) /* I - Array of credentials */ +{ + http_credential_t *credential; /* Credential */ + + + for (credential = (http_credential_t *)cupsArrayFirst(credentials); + credential; + credential = (http_credential_t *)cupsArrayNext(credentials)) + { + cupsArrayRemove(credentials, credential); + free((void *)credential->data); + free(credential); + } + + cupsArrayDelete(credentials); +} + + +/* + * 'httpGet()' - Send a GET request to the server. + */ + +int /* O - Status of call (0 = success) */ +httpGet(http_t *http, /* I - Connection to server */ + const char *uri) /* I - URI to get */ +{ + return (http_send(http, HTTP_GET, uri)); +} + + +/* + * 'httpGetAuthString()' - Get the current authorization string. + * + * The authorization string is set by cupsDoAuthentication() and + * httpSetAuthString(). Use httpGetAuthString() to retrieve the + * string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION + * value. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +char * /* O - Authorization string */ +httpGetAuthString(http_t *http) /* I - Connection to server */ +{ + if (http) + return (http->authstring); + else + return (NULL); +} + + +/* + * 'httpGetBlocking()' - Get the blocking/non-block state of a connection. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 1 if blocking, 0 if non-blocking */ +httpGetBlocking(http_t *http) /* I - Connection to server */ +{ + return (http ? http->blocking : 0); +} + + +/* + * 'httpGetCookie()' - Get any cookie data from the response. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +const char * /* O - Cookie data or NULL */ +httpGetCookie(http_t *http) /* I - HTTP connecion */ +{ + return (http ? http->cookie : NULL); +} + + +/* + * 'httpGetFd()' - Get the file descriptor associated with a connection. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - File descriptor or -1 if none */ +httpGetFd(http_t *http) /* I - Connection to server */ +{ + return (http ? http->fd : -1); +} + + +/* + * 'httpGetField()' - Get a field value from a request/response. + */ + +const char * /* O - Field value */ +httpGetField(http_t *http, /* I - Connection to server */ + http_field_t field) /* I - Field to get */ +{ + if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) + return (NULL); + else if (field == HTTP_FIELD_AUTHORIZATION && + http->field_authorization) + { + /* + * Special case for WWW-Authenticate: as its contents can be + * longer than HTTP_MAX_VALUE... + */ + + return (http->field_authorization); + } + else + return (http->fields[field]); +} + + +/* + * 'httpGetLength()' - Get the amount of data remaining from the + * content-length or transfer-encoding fields. + * + * This function is deprecated and will not return lengths larger than + * 2^31 - 1; use httpGetLength2() instead. + * + * @deprecated@ + */ + +int /* O - Content length */ +httpGetLength(http_t *http) /* I - Connection to server */ +{ + /* + * Get the read content length and return the 32-bit value. + */ + + if (http) + { + httpGetLength2(http); + + return (http->_data_remaining); + } + else + return (-1); +} + + +/* + * 'httpGetLength2()' - Get the amount of data remaining from the + * content-length or transfer-encoding fields. + * + * This function returns the complete content length, even for + * content larger than 2^31 - 1. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +off_t /* O - Content length */ +httpGetLength2(http_t *http) /* I - Connection to server */ +{ + DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http, + http_states[http->state])); + + if (!http) + return (-1); + + if (!_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked")) + { + DEBUG_puts("4httpGetLength2: chunked request!"); + + http->data_encoding = HTTP_ENCODE_CHUNKED; + http->data_remaining = 0; + } + else + { + http->data_encoding = HTTP_ENCODE_LENGTH; + + /* + * The following is a hack for HTTP servers that don't send a + * content-length or transfer-encoding field... + * + * If there is no content-length then the connection must close + * after the transfer is complete... + */ + + if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0]) + { + /* + * Default content length is 0 for errors and 2^31-1 for other + * successful requests... + */ + + if (http->status >= HTTP_MULTIPLE_CHOICES) + http->data_remaining = 0; + else + http->data_remaining = 2147483647; + } + else + http->data_remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH], + NULL, 10); + + DEBUG_printf(("4httpGetLength2: content_length=" CUPS_LLFMT, + CUPS_LLCAST http->data_remaining)); + } + + if (http->data_remaining <= INT_MAX) + http->_data_remaining = (int)http->data_remaining; + else + http->_data_remaining = INT_MAX; + + return (http->data_remaining); +} + + +/* + * 'httpGets()' - Get a line of text from a HTTP connection. + */ + +char * /* O - Line or NULL */ +httpGets(char *line, /* I - Line to read into */ + int length, /* I - Max length of buffer */ + http_t *http) /* I - Connection to server */ +{ + char *lineptr, /* Pointer into line */ + *lineend, /* End of line */ + *bufptr, /* Pointer into input buffer */ + *bufend; /* Pointer to end of buffer */ + int bytes, /* Number of bytes read */ + eol; /* End-of-line? */ + + + DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", line, length, http)); + + if (http == NULL || line == NULL) + return (NULL); + + /* + * Read a line from the buffer... + */ + + http->error = 0; + lineptr = line; + lineend = line + length - 1; + eol = 0; + + while (lineptr < lineend) + { + /* + * Pre-load the buffer as needed... + */ + +#ifdef WIN32 + WSASetLastError(0); +#else + errno = 0; +#endif /* WIN32 */ + + while (http->used == 0) + { + /* + * No newline; see if there is more data to be read... + */ + + while (!_httpWait(http, http->wait_value, 1)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + DEBUG_puts("3httpGets: Timed out!"); +#ifdef WIN32 + http->error = WSAETIMEDOUT; +#else + http->error = ETIMEDOUT; +#endif /* WIN32 */ + return (NULL); + } + +#ifdef HAVE_SSL + if (http->tls) + bytes = http_read_ssl(http, http->buffer + http->used, + HTTP_MAX_BUFFER - http->used); + else +#endif /* HAVE_SSL */ + bytes = recv(http->fd, http->buffer + http->used, + HTTP_MAX_BUFFER - http->used, 0); + + DEBUG_printf(("4httpGets: read %d bytes...", bytes)); + +#ifdef DEBUG + http_debug_hex("httpGets", http->buffer + http->used, bytes); +#endif /* DEBUG */ + + if (bytes < 0) + { + /* + * Nope, can't get a line this time... + */ + +#ifdef WIN32 + DEBUG_printf(("3httpGets: recv() error %d!", WSAGetLastError())); + + if (WSAGetLastError() == WSAEINTR) + continue; + else if (WSAGetLastError() == WSAEWOULDBLOCK) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + http->error = WSAGetLastError(); + } + else if (WSAGetLastError() != http->error) + { + http->error = WSAGetLastError(); + continue; + } + +#else + DEBUG_printf(("3httpGets: recv() error %d!", errno)); + + if (errno == EINTR) + continue; + else if (errno == EWOULDBLOCK || errno == EAGAIN) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + else if (!http->timeout_cb && errno == EAGAIN) + continue; + + http->error = errno; + } + else if (errno != http->error) + { + http->error = errno; + continue; + } +#endif /* WIN32 */ + + return (NULL); + } + else if (bytes == 0) + { + http->error = EPIPE; + + return (NULL); + } + + /* + * Yup, update the amount used... + */ + + http->used += bytes; + } + + /* + * Now copy as much of the current line as possible... + */ + + for (bufptr = http->buffer, bufend = http->buffer + http->used; + lineptr < lineend && bufptr < bufend;) + { + if (*bufptr == 0x0a) + { + eol = 1; + bufptr ++; + break; + } + else if (*bufptr == 0x0d) + bufptr ++; + else + *lineptr++ = *bufptr++; + } + + http->used -= (int)(bufptr - http->buffer); + if (http->used > 0) + memmove(http->buffer, bufptr, http->used); + + if (eol) + { + /* + * End of line... + */ + + http->activity = time(NULL); + + *lineptr = '\0'; + + DEBUG_printf(("3httpGets: Returning \"%s\"", line)); + + return (line); + } + } + + DEBUG_puts("3httpGets: No new line available!"); + + return (NULL); +} + + +/* + * 'httpGetState()' - Get the current state of the HTTP request. + */ + +http_state_t /* O - HTTP state */ +httpGetState(http_t *http) /* I - Connection to server */ +{ + return (http ? http->state : HTTP_ERROR); +} + + +/* + * 'httpGetStatus()' - Get the status of the last HTTP request. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +http_status_t /* O - HTTP status */ +httpGetStatus(http_t *http) /* I - Connection to server */ +{ + return (http ? http->status : HTTP_ERROR); +} + + +/* + * 'httpGetSubField()' - Get a sub-field value. + * + * @deprecated@ + */ + +char * /* O - Value or NULL */ +httpGetSubField(http_t *http, /* I - Connection to server */ + http_field_t field, /* I - Field index */ + const char *name, /* I - Name of sub-field */ + char *value) /* O - Value string */ +{ + return (httpGetSubField2(http, field, name, value, HTTP_MAX_VALUE)); +} + + +/* + * 'httpGetSubField2()' - Get a sub-field value. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - Value or NULL */ +httpGetSubField2(http_t *http, /* I - Connection to server */ + http_field_t field, /* I - Field index */ + const char *name, /* I - Name of sub-field */ + char *value, /* O - Value string */ + int valuelen) /* I - Size of value buffer */ +{ + const char *fptr; /* Pointer into field */ + char temp[HTTP_MAX_VALUE], /* Temporary buffer for name */ + *ptr, /* Pointer into string buffer */ + *end; /* End of value buffer */ + + DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, " + "valuelen=%d)", http, field, name, value, valuelen)); + + if (!http || !name || !value || valuelen < 2 || + field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) + return (NULL); + + end = value + valuelen - 1; + + for (fptr = http->fields[field]; *fptr;) + { + /* + * Skip leading whitespace... + */ + + while (_cups_isspace(*fptr)) + fptr ++; + + if (*fptr == ',') + { + fptr ++; + continue; + } + + /* + * Get the sub-field name... + */ + + for (ptr = temp; + *fptr && *fptr != '=' && !_cups_isspace(*fptr) && + ptr < (temp + sizeof(temp) - 1); + *ptr++ = *fptr++); + + *ptr = '\0'; + + DEBUG_printf(("4httpGetSubField2: name=\"%s\"", temp)); + + /* + * Skip trailing chars up to the '='... + */ + + while (_cups_isspace(*fptr)) + fptr ++; + + if (!*fptr) + break; + + if (*fptr != '=') + continue; + + /* + * Skip = and leading whitespace... + */ + + fptr ++; + + while (_cups_isspace(*fptr)) + fptr ++; + + if (*fptr == '\"') + { + /* + * Read quoted string... + */ + + for (ptr = value, fptr ++; + *fptr && *fptr != '\"' && ptr < end; + *ptr++ = *fptr++); + + *ptr = '\0'; + + while (*fptr && *fptr != '\"') + fptr ++; + + if (*fptr) + fptr ++; + } + else + { + /* + * Read unquoted string... + */ + + for (ptr = value; + *fptr && !_cups_isspace(*fptr) && *fptr != ',' && ptr < end; + *ptr++ = *fptr++); + + *ptr = '\0'; + + while (*fptr && !_cups_isspace(*fptr) && *fptr != ',') + fptr ++; + } + + DEBUG_printf(("4httpGetSubField2: value=\"%s\"", value)); + + /* + * See if this is the one... + */ + + if (!strcmp(name, temp)) + { + DEBUG_printf(("3httpGetSubField2: Returning \"%s\"", value)); + return (value); + } + } + + value[0] = '\0'; + + DEBUG_puts("3httpGetSubField2: Returning NULL"); + + return (NULL); +} + + +/* + * 'httpGetVersion()' - Get the HTTP version at the other end. + */ + +http_version_t /* O - Version number */ +httpGetVersion(http_t *http) /* I - Connection to server */ +{ + return (http ? http->version : HTTP_1_0); +} + + +/* + * 'httpHead()' - Send a HEAD request to the server. + */ + +int /* O - Status of call (0 = success) */ +httpHead(http_t *http, /* I - Connection to server */ + const char *uri) /* I - URI for head */ +{ + DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", http, uri)); + return (http_send(http, HTTP_HEAD, uri)); +} + + +/* + * 'httpInitialize()' - Initialize the HTTP interface library and set the + * default HTTP proxy (if any). + */ + +void +httpInitialize(void) +{ + static int initialized = 0; /* Have we been called before? */ +#ifdef WIN32 + WSADATA winsockdata; /* WinSock data */ +#endif /* WIN32 */ +#ifdef HAVE_LIBSSL + int i; /* Looping var */ + unsigned char data[1024]; /* Seed data */ +#endif /* HAVE_LIBSSL */ + + + _cupsGlobalLock(); + if (initialized) + { + _cupsGlobalUnlock(); + return; + } + +#ifdef WIN32 + WSAStartup(MAKEWORD(2,2), &winsockdata); + +#elif !defined(SO_NOSIGPIPE) + /* + * Ignore SIGPIPE signals... + */ + +# ifdef HAVE_SIGSET + sigset(SIGPIPE, SIG_IGN); + +# elif defined(HAVE_SIGACTION) + struct sigaction action; /* POSIX sigaction data */ + + + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_IGN; + sigaction(SIGPIPE, &action, NULL); + +# else + signal(SIGPIPE, SIG_IGN); +# endif /* !SO_NOSIGPIPE */ +#endif /* WIN32 */ + +#ifdef HAVE_GNUTLS + /* + * Initialize GNU TLS... + */ + + gnutls_global_init(); + +#elif defined(HAVE_LIBSSL) + /* + * Initialize OpenSSL... + */ + + SSL_load_error_strings(); + SSL_library_init(); + + /* + * Using the current time is a dubious random seed, but on some systems + * it is the best we can do (on others, this seed isn't even used...) + */ + + CUPS_SRAND(time(NULL)); + + for (i = 0; i < sizeof(data); i ++) + data[i] = CUPS_RAND(); + + RAND_seed(data, sizeof(data)); +#endif /* HAVE_GNUTLS */ + + initialized = 1; + _cupsGlobalUnlock(); +} + + +/* + * 'httpOptions()' - Send an OPTIONS request to the server. + */ + +int /* O - Status of call (0 = success) */ +httpOptions(http_t *http, /* I - Connection to server */ + const char *uri) /* I - URI for options */ +{ + return (http_send(http, HTTP_OPTIONS, uri)); +} + + +/* + * '_httpPeek()' - Peek at data from a HTTP connection. + * + * This function copies available data from the given HTTP connection, reading + * a buffer as needed. The data is still available for reading using + * @link httpRead@ or @link httpRead2@. + * + * For non-blocking connections the usual timeouts apply. + */ + +ssize_t /* O - Number of bytes copied */ +_httpPeek(http_t *http, /* I - Connection to server */ + char *buffer, /* I - Buffer for data */ + size_t length) /* I - Maximum number of bytes */ +{ + ssize_t bytes; /* Bytes read */ + char len[32]; /* Length string */ + + + DEBUG_printf(("_httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT ")", + http, buffer, CUPS_LLCAST length)); + + if (http == NULL || buffer == NULL) + return (-1); + + http->activity = time(NULL); + http->error = 0; + + if (length <= 0) + return (0); + + if (http->data_encoding == HTTP_ENCODE_CHUNKED && + http->data_remaining <= 0) + { + DEBUG_puts("2_httpPeek: Getting chunk length..."); + + if (httpGets(len, sizeof(len), http) == NULL) + { + DEBUG_puts("1_httpPeek: Could not get length!"); + return (0); + } + + http->data_remaining = strtoll(len, NULL, 16); + if (http->data_remaining < 0) + { + DEBUG_puts("1_httpPeek: Negative chunk length!"); + return (0); + } + } + + DEBUG_printf(("2_httpPeek: data_remaining=" CUPS_LLFMT, + CUPS_LLCAST http->data_remaining)); + + if (http->data_remaining <= 0) + { + /* + * A zero-length chunk ends a transfer; unless we are reading POST + * data, go idle... + */ + + if (http->data_encoding == HTTP_ENCODE_CHUNKED) + httpGets(len, sizeof(len), http); + + if (http->state == HTTP_POST_RECV) + http->state ++; + else + http->state = HTTP_WAITING; + + /* + * Prevent future reads for this request... + */ + + http->data_encoding = HTTP_ENCODE_LENGTH; + + return (0); + } + else if (length > (size_t)http->data_remaining) + length = (size_t)http->data_remaining; + + if (http->used == 0) + { + /* + * Buffer small reads for better performance... + */ + + ssize_t buflen; /* Length of read for buffer */ + + if (!http->blocking) + { + while (!httpWait(http, http->wait_value)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + return (0); + } + } + + if (http->data_remaining > sizeof(http->buffer)) + buflen = sizeof(http->buffer); + else + buflen = http->data_remaining; + + DEBUG_printf(("2_httpPeek: Reading %d bytes into buffer.", (int)buflen)); + + do + { +#ifdef HAVE_SSL + if (http->tls) + bytes = http_read_ssl(http, http->buffer, buflen); + else +#endif /* HAVE_SSL */ + bytes = recv(http->fd, http->buffer, buflen, 0); + + if (bytes < 0) + { +#ifdef WIN32 + if (WSAGetLastError() != WSAEINTR) + { + http->error = WSAGetLastError(); + return (-1); + } + else if (WSAGetLastError() == WSAEWOULDBLOCK) + { + if (!http->timeout_cb || + !(*http->timeout_cb)(http, http->timeout_data)) + { + http->error = WSAEWOULDBLOCK; + return (-1); + } + } +#else + if (errno == EWOULDBLOCK || errno == EAGAIN) + { + if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) + { + http->error = errno; + return (-1); + } + else if (!http->timeout_cb && errno != EAGAIN) + { + http->error = errno; + return (-1); + } + } + else if (errno != EINTR) + { + http->error = errno; + return (-1); + } +#endif /* WIN32 */ + } + } + while (bytes < 0); + + DEBUG_printf(("2_httpPeek: Read " CUPS_LLFMT " bytes into buffer.", + CUPS_LLCAST bytes)); +#ifdef DEBUG + http_debug_hex("_httpPeek", http->buffer, (int)bytes); +#endif /* DEBUG */ + + http->used = bytes; + } + + if (http->used > 0) + { + if (length > (size_t)http->used) + length = (size_t)http->used; + + bytes = (ssize_t)length; + + DEBUG_printf(("2_httpPeek: grabbing %d bytes from input buffer...", + (int)bytes)); + + memcpy(buffer, http->buffer, length); + } + else + bytes = 0; + + if (bytes < 0) + { +#ifdef WIN32 + if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK) + bytes = 0; + else + http->error = WSAGetLastError(); +#else + if (errno == EINTR || errno == EAGAIN) + bytes = 0; + else + http->error = errno; +#endif /* WIN32 */ + } + else if (bytes == 0) + { + http->error = EPIPE; + return (0); + } + +#ifdef DEBUG + http_debug_hex("_httpPeek", buffer, (int)bytes); +#endif /* DEBUG */ + + return (bytes); +} + + +/* + * 'httpPost()' - Send a POST request to the server. + */ + +int /* O - Status of call (0 = success) */ +httpPost(http_t *http, /* I - Connection to server */ + const char *uri) /* I - URI for post */ +{ + return (http_send(http, HTTP_POST, uri)); +} + + +/* + * 'httpPrintf()' - Print a formatted string to a HTTP connection. + * + * @private@ + */ + +int /* O - Number of bytes written */ +httpPrintf(http_t *http, /* I - Connection to server */ + const char *format, /* I - printf-style format string */ + ...) /* I - Additional args as needed */ +{ + int bytes; /* Number of bytes to write */ + char buf[16384]; /* Buffer for formatted string */ + va_list ap; /* Variable argument pointer */ + + + DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", http, format)); + + va_start(ap, format); + bytes = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + + DEBUG_printf(("3httpPrintf: %s", buf)); + + if (http->data_encoding == HTTP_ENCODE_FIELDS) + return (httpWrite2(http, buf, bytes)); + else + { + if (http->wused) + { + DEBUG_puts("4httpPrintf: flushing existing data..."); + + if (httpFlushWrite(http) < 0) + return (-1); + } + + return (http_write(http, buf, bytes)); + } +} + + +/* + * 'httpPut()' - Send a PUT request to the server. + */ + +int /* O - Status of call (0 = success) */ +httpPut(http_t *http, /* I - Connection to server */ + const char *uri) /* I - URI to put */ +{ + DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", http, uri)); + return (http_send(http, HTTP_PUT, uri)); +} + + +/* + * 'httpRead()' - Read data from a HTTP connection. + * + * This function is deprecated. Use the httpRead2() function which can + * read more than 2GB of data. + * + * @deprecated@ + */ + +int /* O - Number of bytes read */ +httpRead(http_t *http, /* I - Connection to server */ + char *buffer, /* I - Buffer for data */ + int length) /* I - Maximum number of bytes */ +{ + return ((int)httpRead2(http, buffer, length)); +} + + +/* + * 'httpRead2()' - Read data from a HTTP connection. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ssize_t /* O - Number of bytes read */ +httpRead2(http_t *http, /* I - Connection to server */ + char *buffer, /* I - Buffer for data */ + size_t length) /* I - Maximum number of bytes */ +{ + ssize_t bytes; /* Bytes read */ + char len[32]; /* Length string */ + + + DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT ")", + http, buffer, CUPS_LLCAST length)); + + if (http == NULL || buffer == NULL) + return (-1); + + http->activity = time(NULL); + http->error = 0; + + if (length <= 0) + return (0); + + if (http->data_encoding == HTTP_ENCODE_CHUNKED && + http->data_remaining <= 0) + { + DEBUG_puts("2httpRead2: Getting chunk length..."); + + if (httpGets(len, sizeof(len), http) == NULL) + { + DEBUG_puts("1httpRead2: Could not get length!"); + return (0); + } + + http->data_remaining = strtoll(len, NULL, 16); + if (http->data_remaining < 0) + { + DEBUG_puts("1httpRead2: Negative chunk length!"); + return (0); + } + } + + DEBUG_printf(("2httpRead2: data_remaining=" CUPS_LLFMT, + CUPS_LLCAST http->data_remaining)); + + if (http->data_remaining <= 0) + { + /* + * A zero-length chunk ends a transfer; unless we are reading POST + * data, go idle... + */ + + if (http->data_encoding == HTTP_ENCODE_CHUNKED) + httpGets(len, sizeof(len), http); + + if (http->state == HTTP_POST_RECV) + http->state ++; + else + http->state = HTTP_WAITING; + + /* + * Prevent future reads for this request... + */ + + http->data_encoding = HTTP_ENCODE_LENGTH; + + return (0); + } + else if (length > (size_t)http->data_remaining) + length = (size_t)http->data_remaining; + + if (http->used == 0 && length <= 256) + { + /* + * Buffer small reads for better performance... + */ + + ssize_t buflen; /* Length of read for buffer */ + + if (!http->blocking) + { + while (!httpWait(http, http->wait_value)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + return (0); + } + } + + if (http->data_remaining > sizeof(http->buffer)) + buflen = sizeof(http->buffer); + else + buflen = http->data_remaining; + + DEBUG_printf(("2httpRead2: Reading %d bytes into buffer.", (int)buflen)); + + do + { +#ifdef HAVE_SSL + if (http->tls) + bytes = http_read_ssl(http, http->buffer, buflen); + else +#endif /* HAVE_SSL */ + bytes = recv(http->fd, http->buffer, buflen, 0); + + if (bytes < 0) + { +#ifdef WIN32 + if (WSAGetLastError() != WSAEINTR) + { + http->error = WSAGetLastError(); + return (-1); + } + else if (WSAGetLastError() == WSAEWOULDBLOCK) + { + if (!http->timeout_cb || + !(*http->timeout_cb)(http, http->timeout_data)) + { + http->error = WSAEWOULDBLOCK; + return (-1); + } + } +#else + if (errno == EWOULDBLOCK || errno == EAGAIN) + { + if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) + { + http->error = errno; + return (-1); + } + else if (!http->timeout_cb && errno != EAGAIN) + { + http->error = errno; + return (-1); + } + } + else if (errno != EINTR) + { + http->error = errno; + return (-1); + } +#endif /* WIN32 */ + } + } + while (bytes < 0); + + DEBUG_printf(("2httpRead2: Read " CUPS_LLFMT " bytes into buffer.", + CUPS_LLCAST bytes)); +#ifdef DEBUG + http_debug_hex("httpRead2", http->buffer, (int)bytes); +#endif /* DEBUG */ + + http->used = bytes; + } + + if (http->used > 0) + { + if (length > (size_t)http->used) + length = (size_t)http->used; + + bytes = (ssize_t)length; + + DEBUG_printf(("2httpRead2: grabbing %d bytes from input buffer...", + (int)bytes)); + + memcpy(buffer, http->buffer, length); + http->used -= (int)length; + + if (http->used > 0) + memmove(http->buffer, http->buffer + length, http->used); + } +#ifdef HAVE_SSL + else if (http->tls) + { + if (!http->blocking) + { + while (!httpWait(http, http->wait_value)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + return (0); + } + } + + while ((bytes = (ssize_t)http_read_ssl(http, buffer, (int)length)) < 0) + { +#ifdef WIN32 + if (WSAGetLastError() == WSAEWOULDBLOCK) + { + if (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data)) + break; + } + else if (WSAGetLastError() != WSAEINTR) + break; +#else + if (errno == EWOULDBLOCK || errno == EAGAIN) + { + if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) + break; + else if (!http->timeout_cb && errno != EAGAIN) + break; + } + else if (errno != EINTR) + break; +#endif /* WIN32 */ + } + } +#endif /* HAVE_SSL */ + else + { + if (!http->blocking) + { + while (!httpWait(http, http->wait_value)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + return (0); + } + } + + DEBUG_printf(("2httpRead2: reading " CUPS_LLFMT " bytes from socket...", + CUPS_LLCAST length)); + +#ifdef WIN32 + while ((bytes = (ssize_t)recv(http->fd, buffer, (int)length, 0)) < 0) + { + if (WSAGetLastError() == WSAEWOULDBLOCK) + { + if (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data)) + break; + } + else if (WSAGetLastError() != WSAEINTR) + break; + } +#else + while ((bytes = recv(http->fd, buffer, length, 0)) < 0) + { + if (errno == EWOULDBLOCK || errno == EAGAIN) + { + if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) + break; + else if (!http->timeout_cb && errno != EAGAIN) + break; + } + else if (errno != EINTR) + break; + } +#endif /* WIN32 */ + + DEBUG_printf(("2httpRead2: read " CUPS_LLFMT " bytes from socket...", + CUPS_LLCAST bytes)); +#ifdef DEBUG + http_debug_hex("httpRead2", buffer, (int)bytes); +#endif /* DEBUG */ + } + + if (bytes > 0) + { + http->data_remaining -= bytes; + + if (http->data_remaining <= INT_MAX) + http->_data_remaining = (int)http->data_remaining; + else + http->_data_remaining = INT_MAX; + } + else if (bytes < 0) + { +#ifdef WIN32 + if (WSAGetLastError() == WSAEINTR) + bytes = 0; + else + http->error = WSAGetLastError(); +#else + if (errno == EINTR || (errno == EAGAIN && !http->timeout_cb)) + bytes = 0; + else + http->error = errno; +#endif /* WIN32 */ + } + else + { + http->error = EPIPE; + return (0); + } + + if (http->data_remaining == 0) + { + if (http->data_encoding == HTTP_ENCODE_CHUNKED) + httpGets(len, sizeof(len), http); + else if (http->state == HTTP_POST_RECV) + http->state ++; + else + http->state = HTTP_WAITING; + } + + return (bytes); +} + + +#if defined(HAVE_SSL) && defined(HAVE_CDSASSL) +/* + * '_httpReadCDSA()' - Read function for the CDSA library. + */ + +OSStatus /* O - -1 on error, 0 on success */ +_httpReadCDSA( + SSLConnectionRef connection, /* I - SSL/TLS connection */ + void *data, /* I - Data buffer */ + size_t *dataLength) /* IO - Number of bytes */ +{ + OSStatus result; /* Return value */ + ssize_t bytes; /* Number of bytes read */ + http_t *http; /* HTTP connection */ + + + http = (http_t *)connection; + + if (!http->blocking) + { + /* + * Make sure we have data before we read... + */ + + while (!_httpWait(http, http->wait_value, 0)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + http->error = ETIMEDOUT; + return (-1); + } + } + + do + { + bytes = recv(http->fd, data, *dataLength, 0); + } + while (bytes == -1 && (errno == EINTR || errno == EAGAIN)); + + if (bytes == *dataLength) + { + result = 0; + } + else if (bytes > 0) + { + *dataLength = bytes; + result = errSSLWouldBlock; + } + else + { + *dataLength = 0; + + if (bytes == 0) + result = errSSLClosedGraceful; + else if (errno == EAGAIN) + result = errSSLWouldBlock; + else + result = errSSLClosedAbort; + } + + return (result); +} +#endif /* HAVE_SSL && HAVE_CDSASSL */ + + +#if defined(HAVE_SSL) && defined(HAVE_GNUTLS) +/* + * '_httpReadGNUTLS()' - Read function for the GNU TLS library. + */ + +ssize_t /* O - Number of bytes read or -1 on error */ +_httpReadGNUTLS( + gnutls_transport_ptr ptr, /* I - Connection to server */ + void *data, /* I - Buffer */ + size_t length) /* I - Number of bytes to read */ +{ + http_t *http; /* HTTP connection */ + ssize_t bytes; /* Bytes read */ + + + DEBUG_printf(("6_httpReadGNUTLS(ptr=%p, data=%p, length=%d)", ptr, data, (int)length)); + + http = (http_t *)ptr; + + if (!http->blocking) + { + /* + * Make sure we have data before we read... + */ + + while (!_httpWait(http, http->wait_value, 0)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + http->error = ETIMEDOUT; + return (-1); + } + } + + bytes = recv(http->fd, data, length, 0); + DEBUG_printf(("6_httpReadGNUTLS: bytes=%d", (int)bytes)); + return (bytes); +} +#endif /* HAVE_SSL && HAVE_GNUTLS */ + + +/* + * 'httpReconnect()' - Reconnect to a HTTP server. + */ + +int /* O - 0 on success, non-zero on failure */ +httpReconnect(http_t *http) /* I - Connection to server */ +{ + DEBUG_printf(("httpReconnect(http=%p)", http)); + + return (httpReconnect2(http, 30000, NULL)); +} + + +/* + * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional + * cancel. + */ + +int /* O - 0 on success, non-zero on failure */ +httpReconnect2(http_t *http, /* I - Connection to server */ + int msec, /* I - Timeout in milliseconds */ + int *cancel) /* I - Pointer to "cancel" variable */ +{ + http_addrlist_t *addr; /* Connected address */ +#ifdef DEBUG + http_addrlist_t *current; /* Current address */ + char temp[256]; /* Temporary address string */ +#endif /* DEBUG */ + + + DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", http, msec, + cancel)); + + if (!http) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (-1); + } + +#ifdef HAVE_SSL + if (http->tls) + { + DEBUG_puts("2httpReconnect2: Shutting down SSL/TLS..."); + http_shutdown_ssl(http); + } +#endif /* HAVE_SSL */ + + /* + * Close any previously open socket... + */ + + if (http->fd >= 0) + { + DEBUG_printf(("2httpReconnect2: Closing socket %d...", http->fd)); + +#ifdef WIN32 + closesocket(http->fd); +#else + close(http->fd); +#endif /* WIN32 */ + + http->fd = -1; + } + + /* + * Reset all state (except fields, which may be reused)... + */ + + http->state = HTTP_WAITING; + http->status = HTTP_CONTINUE; + http->version = HTTP_1_1; + http->keep_alive = HTTP_KEEPALIVE_OFF; + memset(&http->_hostaddr, 0, sizeof(http->_hostaddr)); + http->data_encoding = HTTP_ENCODE_LENGTH; + http->_data_remaining = 0; + http->used = 0; + http->expect = 0; + http->data_remaining = 0; + http->hostaddr = NULL; + http->wused = 0; + + /* + * Connect to the server... + */ + +#ifdef DEBUG + for (current = http->addrlist; current; current = current->next) + DEBUG_printf(("2httpReconnect2: Address %s:%d", + httpAddrString(&(current->addr), temp, sizeof(temp)), + _httpAddrPort(&(current->addr)))); +#endif /* DEBUG */ + + if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec, + cancel)) == NULL) + { + /* + * Unable to connect... + */ + +#ifdef WIN32 + http->error = WSAGetLastError(); +#else + http->error = errno; +#endif /* WIN32 */ + http->status = HTTP_ERROR; + + DEBUG_printf(("1httpReconnect2: httpAddrConnect failed: %s", + strerror(http->error))); + + return (-1); + } + + DEBUG_printf(("2httpReconnect2: New socket=%d", http->fd)); + + if (http->timeout_value > 0) + http_set_timeout(http->fd, http->timeout_value); + + http->hostaddr = &(addr->addr); + http->error = 0; + +#ifdef HAVE_SSL + if (http->encryption == HTTP_ENCRYPT_ALWAYS) + { + /* + * Always do encryption via SSL. + */ + + if (http_setup_ssl(http) != 0) + { +# ifdef WIN32 + closesocket(http->fd); +# else + close(http->fd); +# endif /* WIN32 */ + + return (-1); + } + } + else if (http->encryption == HTTP_ENCRYPT_REQUIRED) + return (http_upgrade(http)); +#endif /* HAVE_SSL */ + + DEBUG_printf(("1httpReconnect2: Connected to %s:%d...", + httpAddrString(http->hostaddr, temp, sizeof(temp)), + _httpAddrPort(http->hostaddr))); + + return (0); +} + + +/* + * 'httpSetAuthString()' - Set the current authorization string. + * + * This function just stores a copy of the current authorization string in + * the HTTP connection object. You must still call httpSetField() to set + * HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(), + * httpHead(), httpOptions(), httpPost, or httpPut(). + * + * @since CUPS 1.3/OS X 10.5@ + */ + +void +httpSetAuthString(http_t *http, /* I - Connection to server */ + const char *scheme, /* I - Auth scheme (NULL to clear it) */ + const char *data) /* I - Auth data (NULL for none) */ +{ + /* + * Range check input... + */ + + if (!http) + return; + + if (http->authstring && http->authstring != http->_authstring) + free(http->authstring); + + http->authstring = http->_authstring; + + if (scheme) + { + /* + * Set the current authorization string... + */ + + int len = (int)strlen(scheme) + (data ? (int)strlen(data) + 1 : 0) + 1; + char *temp; + + if (len > (int)sizeof(http->_authstring)) + { + if ((temp = malloc(len)) == NULL) + len = sizeof(http->_authstring); + else + http->authstring = temp; + } + + if (data) + snprintf(http->authstring, len, "%s %s", scheme, data); + else + strlcpy(http->authstring, scheme, len); + } + else + { + /* + * Clear the current authorization string... + */ + + http->_authstring[0] = '\0'; + } +} + + +/* + * 'httpSetCredentials()' - Set the credentials associated with an encrypted + * connection. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +int /* O - Status of call (0 = success) */ +httpSetCredentials(http_t *http, /* I - Connection to server */ + cups_array_t *credentials) /* I - Array of credentials */ +{ + if (!http || cupsArrayCount(credentials) < 1) + return (-1); + + _httpFreeCredentials(http->tls_credentials); + + http->tls_credentials = _httpCreateCredentials(credentials); + + return (http->tls_credentials ? 0 : -1); +} + + +/* + * 'httpSetCookie()' - Set the cookie value(s). + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +void +httpSetCookie(http_t *http, /* I - Connection */ + const char *cookie) /* I - Cookie string */ +{ + if (!http) + return; + + if (http->cookie) + free(http->cookie); + + if (cookie) + http->cookie = strdup(cookie); + else + http->cookie = NULL; +} + + +/* + * 'httpSetExpect()' - Set the Expect: header in a request. + * + * Currently only HTTP_CONTINUE is supported for the "expect" argument. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +httpSetExpect(http_t *http, /* I - Connection to server */ + http_status_t expect) /* I - HTTP status to expect (HTTP_CONTINUE) */ +{ + if (http) + http->expect = expect; +} + + +/* + * 'httpSetField()' - Set the value of an HTTP header. + */ + +void +httpSetField(http_t *http, /* I - Connection to server */ + http_field_t field, /* I - Field index */ + const char *value) /* I - Value */ +{ + if (http == NULL || + field < HTTP_FIELD_ACCEPT_LANGUAGE || + field > HTTP_FIELD_WWW_AUTHENTICATE || + value == NULL) + return; + + strlcpy(http->fields[field], value, HTTP_MAX_VALUE); + + if (field == HTTP_FIELD_AUTHORIZATION) + { + /* + * Special case for Authorization: as its contents can be + * longer than HTTP_MAX_VALUE + */ + + if (http->field_authorization) + free(http->field_authorization); + + http->field_authorization = strdup(value); + } + else if (field == HTTP_FIELD_HOST) + { + /* + * Special-case for Host: as we don't want a trailing "." on the hostname and + * need to bracket IPv6 numeric addresses. + */ + + char *ptr = strchr(value, ':'); + + if (value[0] != '[' && ptr && strchr(ptr + 1, ':')) + { + /* + * Bracket IPv6 numeric addresses... + * + * This is slightly inefficient (basically copying twice), but is an edge + * case and not worth optimizing... + */ + + snprintf(http->fields[HTTP_FIELD_HOST], + sizeof(http->fields[HTTP_FIELD_HOST]), "[%s]", value); + } + else + { + /* + * Check for a trailing dot on the hostname... + */ + + ptr = http->fields[HTTP_FIELD_HOST]; + + if (*ptr) + { + ptr += strlen(ptr) - 1; + + if (*ptr == '.') + *ptr = '\0'; + } + } + } +} + + +/* + * 'httpSetLength()' - Set the content-length and content-encoding. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +void +httpSetLength(http_t *http, /* I - Connection to server */ + size_t length) /* I - Length (0 for chunked) */ +{ + if (!http) + return; + + if (!length) + { + strcpy(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked"); + http->fields[HTTP_FIELD_CONTENT_LENGTH][0] = '\0'; + } + else + { + http->fields[HTTP_FIELD_TRANSFER_ENCODING][0] = '\0'; + snprintf(http->fields[HTTP_FIELD_CONTENT_LENGTH], HTTP_MAX_VALUE, + CUPS_LLFMT, CUPS_LLCAST length); + } +} + + +/* + * 'httpSetTimeout()' - Set read/write timeouts and an optional callback. + * + * The optional timeout callback receives both the HTTP connection and a user + * data pointer and must return 1 to continue or 0 to error (time) out. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +void +httpSetTimeout( + http_t *http, /* I - Connection to server */ + double timeout, /* I - Number of seconds for timeout, + must be greater than 0 */ + http_timeout_cb_t cb, /* I - Callback function or NULL */ + void *user_data) /* I - User data pointer */ +{ + if (!http || timeout <= 0.0) + return; + + http->timeout_cb = cb; + http->timeout_data = user_data; + http->timeout_value = timeout; + + if (http->fd >= 0) + http_set_timeout(http->fd, timeout); + + http_set_wait(http); +} + + +/* + * 'httpTrace()' - Send an TRACE request to the server. + */ + +int /* O - Status of call (0 = success) */ +httpTrace(http_t *http, /* I - Connection to server */ + const char *uri) /* I - URI for trace */ +{ + return (http_send(http, HTTP_TRACE, uri)); +} + + +/* + * '_httpUpdate()' - Update the current HTTP status for incoming data. + * + * Note: Unlike httpUpdate(), this function does not flush pending write data + * and only retrieves a single status line from the HTTP connection. + */ + +int /* O - 1 to continue, 0 to stop */ +_httpUpdate(http_t *http, /* I - Connection to server */ + http_status_t *status) /* O - Current HTTP status */ +{ + char line[32768], /* Line from connection... */ + *value; /* Pointer to value on line */ + http_field_t field; /* Field index */ + int major, minor; /* HTTP version numbers */ + + + DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", http, status, + http_states[http->state])); + + /* + * Grab a single line from the connection... + */ + + if (!httpGets(line, sizeof(line), http)) + { + *status = HTTP_ERROR; + return (0); + } + + DEBUG_printf(("2_httpUpdate: Got \"%s\"", line)); + + if (line[0] == '\0') + { + /* + * Blank line means the start of the data section (if any). Return + * the result code, too... + * + * If we get status 100 (HTTP_CONTINUE), then we *don't* change states. + * Instead, we just return HTTP_CONTINUE to the caller and keep on + * tryin'... + */ + + if (http->status == HTTP_CONTINUE) + { + *status = http->status; + return (0); + } + + if (http->status < HTTP_BAD_REQUEST) + http->digest_tries = 0; + +#ifdef HAVE_SSL + if (http->status == HTTP_SWITCHING_PROTOCOLS && !http->tls) + { + if (http_setup_ssl(http) != 0) + { +# ifdef WIN32 + closesocket(http->fd); +# else + close(http->fd); +# endif /* WIN32 */ + + *status = http->status = HTTP_ERROR; + return (0); + } + + *status = HTTP_CONTINUE; + return (0); + } +#endif /* HAVE_SSL */ + + httpGetLength2(http); + + switch (http->state) + { + case HTTP_GET : + case HTTP_POST : + case HTTP_POST_RECV : + case HTTP_PUT : + http->state ++; + case HTTP_POST_SEND : + case HTTP_HEAD : + break; + + default : + http->state = HTTP_WAITING; + break; + } + + *status = http->status; + return (0); + } + else if (!strncmp(line, "HTTP/", 5)) + { + /* + * Got the beginning of a response... + */ + + int intstatus; /* Status value as an integer */ + + if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &intstatus) != 3) + { + *status = http->status = HTTP_ERROR; + return (0); + } + + http->version = (http_version_t)(major * 100 + minor); + *status = http->status = (http_status_t)intstatus; + } + else if ((value = strchr(line, ':')) != NULL) + { + /* + * Got a value... + */ + + *value++ = '\0'; + while (_cups_isspace(*value)) + value ++; + + /* + * Be tolerants of servers that send unknown attribute fields... + */ + + if (!_cups_strcasecmp(line, "expect")) + { + /* + * "Expect: 100-continue" or similar... + */ + + http->expect = (http_status_t)atoi(value); + } + else if (!_cups_strcasecmp(line, "cookie")) + { + /* + * "Cookie: name=value[; name=value ...]" - replaces previous cookies... + */ + + httpSetCookie(http, value); + } + else if ((field = http_field(line)) != HTTP_FIELD_UNKNOWN) + httpSetField(http, field, value); +#ifdef DEBUG + else + DEBUG_printf(("1_httpUpdate: unknown field %s seen!", line)); +#endif /* DEBUG */ + } + else + { + DEBUG_printf(("1_httpUpdate: Bad response line \"%s\"!", line)); + *status = http->status = HTTP_ERROR; + return (0); + } + + return (1); +} + + +/* + * 'httpUpdate()' - Update the current HTTP state for incoming data. + */ + +http_status_t /* O - HTTP status */ +httpUpdate(http_t *http) /* I - Connection to server */ +{ + http_status_t status; /* Request status */ + + + DEBUG_printf(("httpUpdate(http=%p), state=%s", http, + http_states[http->state])); + + /* + * Flush pending data, if any... + */ + + if (http->wused) + { + DEBUG_puts("2httpUpdate: flushing buffer..."); + + if (httpFlushWrite(http) < 0) + return (HTTP_ERROR); + } + + /* + * If we haven't issued any commands, then there is nothing to "update"... + */ + + if (http->state == HTTP_WAITING) + return (HTTP_CONTINUE); + + /* + * Grab all of the lines we can from the connection... + */ + + while (_httpUpdate(http, &status)); + + /* + * See if there was an error... + */ + + if (http->error == EPIPE && http->status > HTTP_CONTINUE) + { + DEBUG_printf(("1httpUpdate: Returning status %d...", http->status)); + return (http->status); + } + + if (http->error) + { + DEBUG_printf(("1httpUpdate: socket error %d - %s", http->error, + strerror(http->error))); + http->status = HTTP_ERROR; + return (HTTP_ERROR); + } + + /* + * Return the current status... + */ + + return (status); +} + + +/* + * '_httpWait()' - Wait for data available on a connection (no flush). + */ + +int /* O - 1 if data is available, 0 otherwise */ +_httpWait(http_t *http, /* I - Connection to server */ + int msec, /* I - Milliseconds to wait */ + int usessl) /* I - Use SSL context? */ +{ +#ifdef HAVE_POLL + struct pollfd pfd; /* Polled file descriptor */ +#else + fd_set input_set; /* select() input set */ + struct timeval timeout; /* Timeout */ +#endif /* HAVE_POLL */ + int nfds; /* Result from select()/poll() */ + + + DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", http, msec, usessl)); + + if (http->fd < 0) + { + DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http->fd)); + return (0); + } + + /* + * Check the SSL/TLS buffers for data first... + */ + +#ifdef HAVE_SSL + if (http->tls && usessl) + { +# ifdef HAVE_LIBSSL + if (SSL_pending(http->tls)) + { + DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data."); + return (1); + } + +# elif defined(HAVE_GNUTLS) + if (gnutls_record_check_pending(http->tls)) + { + DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data."); + return (1); + } + +# elif defined(HAVE_CDSASSL) + size_t bytes; /* Bytes that are available */ + + if (!SSLGetBufferedReadSize(http->tls, &bytes) && + bytes > 0) + { + DEBUG_puts("5_httpWait: Return 1 since there is pending SSL data."); + return (1); + } +# endif /* HAVE_LIBSSL */ + } +#endif /* HAVE_SSL */ + + /* + * Then try doing a select() or poll() to poll the socket... + */ + +#ifdef HAVE_POLL + pfd.fd = http->fd; + pfd.events = POLLIN; + + do + { + nfds = poll(&pfd, 1, msec); + } + while (nfds < 0 && (errno == EINTR || errno == EAGAIN)); + +#else + do + { + FD_ZERO(&input_set); + FD_SET(http->fd, &input_set); + + DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec, http->fd)); + + if (msec >= 0) + { + timeout.tv_sec = msec / 1000; + timeout.tv_usec = (msec % 1000) * 1000; + + nfds = select(http->fd + 1, &input_set, NULL, NULL, &timeout); + } + else + nfds = select(http->fd + 1, &input_set, NULL, NULL, NULL); + + DEBUG_printf(("6_httpWait: select() returned %d...", nfds)); + } +# ifdef WIN32 + while (nfds < 0 && (WSAGetLastError() == WSAEINTR || + WSAGetLastError() == WSAEWOULDBLOCK)); +# else + while (nfds < 0 && (errno == EINTR || errno == EAGAIN)); +# endif /* WIN32 */ +#endif /* HAVE_POLL */ + + DEBUG_printf(("5_httpWait: returning with nfds=%d, errno=%d...", nfds, + errno)); + + return (nfds > 0); +} + + +/* + * 'httpWait()' - Wait for data available on a connection. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +int /* O - 1 if data is available, 0 otherwise */ +httpWait(http_t *http, /* I - Connection to server */ + int msec) /* I - Milliseconds to wait */ +{ + /* + * First see if there is data in the buffer... + */ + + DEBUG_printf(("2httpWait(http=%p, msec=%d)", http, msec)); + + if (http == NULL) + return (0); + + if (http->used) + { + DEBUG_puts("3httpWait: Returning 1 since there is buffered data ready."); + return (1); + } + + /* + * Flush pending data, if any... + */ + + if (http->wused) + { + DEBUG_puts("3httpWait: Flushing write buffer."); + + if (httpFlushWrite(http) < 0) + return (0); + } + + /* + * If not, check the SSL/TLS buffers and do a select() on the connection... + */ + + return (_httpWait(http, msec, 1)); +} + + +/* + * 'httpWrite()' - Write data to a HTTP connection. + * + * This function is deprecated. Use the httpWrite2() function which can + * write more than 2GB of data. + * + * @deprecated@ + */ + +int /* O - Number of bytes written */ +httpWrite(http_t *http, /* I - Connection to server */ + const char *buffer, /* I - Buffer for data */ + int length) /* I - Number of bytes to write */ +{ + return ((int)httpWrite2(http, buffer, length)); +} + + +/* + * 'httpWrite2()' - Write data to a HTTP connection. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ssize_t /* O - Number of bytes written */ +httpWrite2(http_t *http, /* I - Connection to server */ + const char *buffer, /* I - Buffer for data */ + size_t length) /* I - Number of bytes to write */ +{ + ssize_t bytes; /* Bytes written */ + + + DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT ")", http, + buffer, CUPS_LLCAST length)); + + /* + * Range check input... + */ + + if (http == NULL || buffer == NULL) + return (-1); + + /* + * Mark activity on the connection... + */ + + http->activity = time(NULL); + + /* + * Buffer small writes for better performance... + */ + + if (length > 0) + { + if (http->wused && (length + http->wused) > sizeof(http->wbuffer)) + { + DEBUG_printf(("2httpWrite2: Flushing buffer (wused=%d, length=" + CUPS_LLFMT ")", http->wused, CUPS_LLCAST length)); + + httpFlushWrite(http); + } + + if ((length + http->wused) <= sizeof(http->wbuffer) && + length < sizeof(http->wbuffer)) + { + /* + * Write to buffer... + */ + + DEBUG_printf(("2httpWrite2: Copying " CUPS_LLFMT " bytes to wbuffer...", + CUPS_LLCAST length)); + + memcpy(http->wbuffer + http->wused, buffer, length); + http->wused += (int)length; + bytes = (ssize_t)length; + } + else + { + /* + * Otherwise write the data directly... + */ + + DEBUG_printf(("2httpWrite2: Writing " CUPS_LLFMT " bytes to socket...", + CUPS_LLCAST length)); + + if (http->data_encoding == HTTP_ENCODE_CHUNKED) + bytes = (ssize_t)http_write_chunk(http, buffer, (int)length); + else + bytes = (ssize_t)http_write(http, buffer, (int)length); + + DEBUG_printf(("2httpWrite2: Wrote " CUPS_LLFMT " bytes...", + CUPS_LLCAST bytes)); + } + + if (http->data_encoding == HTTP_ENCODE_LENGTH) + http->data_remaining -= bytes; + } + else + bytes = 0; + + /* + * Handle end-of-request processing... + */ + + if ((http->data_encoding == HTTP_ENCODE_CHUNKED && length == 0) || + (http->data_encoding == HTTP_ENCODE_LENGTH && http->data_remaining == 0)) + { + /* + * Finished with the transfer; unless we are sending POST or PUT + * data, go idle... + */ + + DEBUG_puts("2httpWrite: changing states..."); + + if (http->wused) + httpFlushWrite(http); + + if (http->data_encoding == HTTP_ENCODE_CHUNKED) + { + /* + * Send a 0-length chunk at the end of the request... + */ + + http_write(http, "0\r\n\r\n", 5); + + /* + * Reset the data state... + */ + + http->data_encoding = HTTP_ENCODE_LENGTH; + http->data_remaining = 0; + } + } + + return (bytes); +} + + +#if defined(HAVE_SSL) && defined(HAVE_CDSASSL) +/* + * '_httpWriteCDSA()' - Write function for the CDSA library. + */ + +OSStatus /* O - -1 on error, 0 on success */ +_httpWriteCDSA( + SSLConnectionRef connection, /* I - SSL/TLS connection */ + const void *data, /* I - Data buffer */ + size_t *dataLength) /* IO - Number of bytes */ +{ + OSStatus result; /* Return value */ + ssize_t bytes; /* Number of bytes read */ + http_t *http; /* HTTP connection */ + + + http = (http_t *)connection; + + do + { + bytes = write(http->fd, data, *dataLength); + } + while (bytes == -1 && (errno == EINTR || errno == EAGAIN)); + + if (bytes == *dataLength) + { + result = 0; + } + else if (bytes >= 0) + { + *dataLength = bytes; + result = errSSLWouldBlock; + } + else + { + *dataLength = 0; + + if (errno == EAGAIN) + result = errSSLWouldBlock; + else + result = errSSLClosedAbort; + } + + return (result); +} +#endif /* HAVE_SSL && HAVE_CDSASSL */ + + +#if defined(HAVE_SSL) && defined(HAVE_GNUTLS) +/* + * '_httpWriteGNUTLS()' - Write function for the GNU TLS library. + */ + +ssize_t /* O - Number of bytes written or -1 on error */ +_httpWriteGNUTLS( + gnutls_transport_ptr ptr, /* I - Connection to server */ + const void *data, /* I - Data buffer */ + size_t length) /* I - Number of bytes to write */ +{ + ssize_t bytes; /* Bytes written */ + + + DEBUG_printf(("6_httpWriteGNUTLS(ptr=%p, data=%p, length=%d)", ptr, data, + (int)length)); +#ifdef DEBUG + http_debug_hex("_httpWriteGNUTLS", data, (int)length); +#endif /* DEBUG */ + + bytes = send(((http_t *)ptr)->fd, data, length, 0); + DEBUG_printf(("_httpWriteGNUTLS: bytes=%d", (int)bytes)); + + return (bytes); +} +#endif /* HAVE_SSL && HAVE_GNUTLS */ + + +#if defined(HAVE_SSL) && defined(HAVE_LIBSSL) +/* + * 'http_bio_ctrl()' - Control the HTTP connection. + */ + +static long /* O - Result/data */ +http_bio_ctrl(BIO *h, /* I - BIO data */ + int cmd, /* I - Control command */ + long arg1, /* I - First argument */ + void *arg2) /* I - Second argument */ +{ + switch (cmd) + { + default : + return (0); + + case BIO_CTRL_RESET : + h->ptr = NULL; + return (0); + + case BIO_C_SET_FILE_PTR : + h->ptr = arg2; + h->init = 1; + return (1); + + case BIO_C_GET_FILE_PTR : + if (arg2) + { + *((void **)arg2) = h->ptr; + return (1); + } + else + return (0); + + case BIO_CTRL_DUP : + case BIO_CTRL_FLUSH : + return (1); + } +} + + +/* + * 'http_bio_free()' - Free OpenSSL data. + */ + +static int /* O - 1 on success, 0 on failure */ +http_bio_free(BIO *h) /* I - BIO data */ +{ + if (!h) + return (0); + + if (h->shutdown) + { + h->init = 0; + h->flags = 0; + } + + return (1); +} + + +/* + * 'http_bio_new()' - Initialize an OpenSSL BIO structure. + */ + +static int /* O - 1 on success, 0 on failure */ +http_bio_new(BIO *h) /* I - BIO data */ +{ + if (!h) + return (0); + + h->init = 0; + h->num = 0; + h->ptr = NULL; + h->flags = 0; + + return (1); +} + + +/* + * 'http_bio_puts()' - Send a string for OpenSSL. + */ + +static int /* O - Bytes written */ +http_bio_puts(BIO *h, /* I - BIO data */ + const char *str) /* I - String to write */ +{ +#ifdef WIN32 + return (send(((http_t *)h->ptr)->fd, str, (int)strlen(str), 0)); +#else + return (send(((http_t *)h->ptr)->fd, str, strlen(str), 0)); +#endif /* WIN32 */ +} + + +/* + * 'http_bio_read()' - Read data for OpenSSL. + */ + +static int /* O - Bytes read */ +http_bio_read(BIO *h, /* I - BIO data */ + char *buf, /* I - Buffer */ + int size) /* I - Number of bytes to read */ +{ + http_t *http; /* HTTP connection */ + + + http = (http_t *)h->ptr; + + if (!http->blocking) + { + /* + * Make sure we have data before we read... + */ + + while (!_httpWait(http, http->wait_value, 0)) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + +#ifdef WIN32 + http->error = WSAETIMEDOUT; +#else + http->error = ETIMEDOUT; +#endif /* WIN32 */ + + return (-1); + } + } + + return (recv(http->fd, buf, size, 0)); +} + + +/* + * 'http_bio_write()' - Write data for OpenSSL. + */ + +static int /* O - Bytes written */ +http_bio_write(BIO *h, /* I - BIO data */ + const char *buf, /* I - Buffer to write */ + int num) /* I - Number of bytes to write */ +{ + return (send(((http_t *)h->ptr)->fd, buf, num, 0)); +} +#endif /* HAVE_SSL && HAVE_LIBSSL */ + + +#ifdef DEBUG +/* + * 'http_debug_hex()' - Do a hex dump of a buffer. + */ + +static void +http_debug_hex(const char *prefix, /* I - Prefix for line */ + const char *buffer, /* I - Buffer to dump */ + int bytes) /* I - Bytes to dump */ +{ + int i, j, /* Looping vars */ + ch; /* Current character */ + char line[255], /* Line buffer */ + *start, /* Start of line after prefix */ + *ptr; /* Pointer into line */ + + + if (_cups_debug_fd < 0 || _cups_debug_level < 6) + return; + + DEBUG_printf(("6%s: %d bytes:", prefix, bytes)); + + snprintf(line, sizeof(line), "6%s: ", prefix); + start = line + strlen(line); + + for (i = 0; i < bytes; i += 16) + { + for (j = 0, ptr = start; j < 16 && (i + j) < bytes; j ++, ptr += 2) + sprintf(ptr, "%02X", buffer[i + j] & 255); + + while (j < 16) + { + strcpy(ptr, " "); + ptr += 2; + j ++; + } + + strcpy(ptr, " "); + ptr += 2; + + for (j = 0; j < 16 && (i + j) < bytes; j ++) + { + ch = buffer[i + j] & 255; + + if (ch < ' ' || ch >= 127) + ch = '.'; + + *ptr++ = ch; + } + + *ptr = '\0'; + DEBUG_puts(line); + } +} +#endif /* DEBUG */ + + +/* + * 'http_field()' - Return the field index for a field name. + */ + +static http_field_t /* O - Field index */ +http_field(const char *name) /* I - String name */ +{ + int i; /* Looping var */ + + + for (i = 0; i < HTTP_FIELD_MAX; i ++) + if (_cups_strcasecmp(name, http_fields[i]) == 0) + return ((http_field_t)i); + + return (HTTP_FIELD_UNKNOWN); +} + + +#ifdef HAVE_SSL +/* + * 'http_read_ssl()' - Read from a SSL/TLS connection. + */ + +static int /* O - Bytes read */ +http_read_ssl(http_t *http, /* I - Connection to server */ + char *buf, /* I - Buffer to store data */ + int len) /* I - Length of buffer */ +{ +# if defined(HAVE_LIBSSL) + return (SSL_read((SSL *)(http->tls), buf, len)); + +# elif defined(HAVE_GNUTLS) + ssize_t result; /* Return value */ + + + result = gnutls_record_recv(http->tls, buf, len); + + if (result < 0 && !errno) + { + /* + * Convert GNU TLS error to errno value... + */ + + switch (result) + { + case GNUTLS_E_INTERRUPTED : + errno = EINTR; + break; + + case GNUTLS_E_AGAIN : + errno = EAGAIN; + break; + + default : + errno = EPIPE; + break; + } + + result = -1; + } + + return ((int)result); + +# elif defined(HAVE_CDSASSL) + int result; /* Return value */ + OSStatus error; /* Error info */ + size_t processed; /* Number of bytes processed */ + + + error = SSLRead(http->tls, buf, len, &processed); + DEBUG_printf(("6http_read_ssl: error=%d, processed=%d", (int)error, + (int)processed)); + switch (error) + { + case 0 : + result = (int)processed; + break; + + case errSSLWouldBlock : + if (processed) + result = (int)processed; + else + { + result = -1; + errno = EINTR; + } + break; + + case errSSLClosedGraceful : + default : + if (processed) + result = (int)processed; + else + { + result = -1; + errno = EPIPE; + } + break; + } + + return (result); + +# elif defined(HAVE_SSPISSL) + return _sspiRead((_sspi_struct_t*) http->tls, buf, len); +# endif /* HAVE_LIBSSL */ +} +#endif /* HAVE_SSL */ + + +/* + * 'http_send()' - Send a request with all fields and the trailing blank line. + */ + +static int /* O - 0 on success, non-zero on error */ +http_send(http_t *http, /* I - Connection to server */ + http_state_t request, /* I - Request code */ + const char *uri) /* I - URI */ +{ + int i; /* Looping var */ + char buf[1024]; /* Encoded URI buffer */ + static const char * const codes[] = + { /* Request code strings */ + NULL, + "OPTIONS", + "GET", + NULL, + "HEAD", + "POST", + NULL, + NULL, + "PUT", + NULL, + "DELETE", + "TRACE", + "CLOSE" + }; + + + DEBUG_printf(("7http_send(http=%p, request=HTTP_%s, uri=\"%s\")", + http, codes[request], uri)); + + if (http == NULL || uri == NULL) + return (-1); + + /* + * Set the User-Agent field if it isn't already... + */ + + if (!http->fields[HTTP_FIELD_USER_AGENT][0]) + { +#ifdef WIN32 + SYSTEM_INFO sysinfo; /* System information */ + OSVERSIONEX version; /* OS version info */ + + version.dwOSVersionInfoSize = sizeof(OSVERSIONEX); + GetVersionInfoEx(&version); + GetNativeSystemInfo(&sysinfo); + + snprintf(buf, sizeof(buf), CUPS_MINIMAL " (Windows %d.%d; %s) IPP/2.0", + version.major, version.minor, + sysinfo.wProcessorArchitecture + == PROCESSOR_ARCHITECTURE_AMD64 ? "amd64" : + sysinfo.wProcessorArchitecture + == PROCESSOR_ARCHITECTURE_ARM ? "arm" : + sysinfo.wProcessorArchitecture + == PROCESSOR_ARCHITECTURE_IA64 ? "ia64" : + sysinfo.wProcessorArchitecture + == PROCESSOR_ARCHITECTURE_INTEL ? "intel" : + "unknown"); + +#else + struct utsname name; /* uname info */ + + uname(&name); + + snprintf(buf, sizeof(buf), CUPS_MINIMAL " (%s %s; %s) IPP/2.0", + name.sysname, name.release, name.machine); +#endif /* WIN32 */ + + DEBUG_printf(("8http_send: Default User-Agent: %s", buf)); + httpSetField(http, HTTP_FIELD_USER_AGENT, buf); + } + + /* + * Encode the URI as needed... + */ + + _httpEncodeURI(buf, uri, sizeof(buf)); + + /* + * See if we had an error the last time around; if so, reconnect... + */ + + if (http->status == HTTP_ERROR || http->status >= HTTP_BAD_REQUEST) + if (httpReconnect(http)) + return (-1); + + /* + * Flush any written data that is pending... + */ + + if (http->wused) + { + if (httpFlushWrite(http) < 0) + if (httpReconnect(http)) + return (-1); + } + + /* + * Send the request header... + */ + + http->state = request; + http->data_encoding = HTTP_ENCODE_FIELDS; + + if (request == HTTP_POST || request == HTTP_PUT) + http->state ++; + + http->status = HTTP_CONTINUE; + +#ifdef HAVE_SSL + if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls) + { + httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); + httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.0,SSL/2.0,SSL/3.0"); + } +#endif /* HAVE_SSL */ + + if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1) + { + http->status = HTTP_ERROR; + return (-1); + } + + for (i = 0; i < HTTP_FIELD_MAX; i ++) + if (http->fields[i][0] != '\0') + { + DEBUG_printf(("9http_send: %s: %s", http_fields[i], + httpGetField(http, i))); + + if (i == HTTP_FIELD_HOST) + { + if (httpPrintf(http, "Host: %s:%d\r\n", httpGetField(http, i), + _httpAddrPort(http->hostaddr)) < 1) + { + http->status = HTTP_ERROR; + return (-1); + } + } + else if (httpPrintf(http, "%s: %s\r\n", http_fields[i], + httpGetField(http, i)) < 1) + { + http->status = HTTP_ERROR; + return (-1); + } + } + + if (http->cookie) + if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1) + { + http->status = HTTP_ERROR; + return (-1); + } + + if (http->expect == HTTP_CONTINUE && + (http->state == HTTP_POST_RECV || http->state == HTTP_PUT_RECV)) + if (httpPrintf(http, "Expect: 100-continue\r\n") < 1) + { + http->status = HTTP_ERROR; + return (-1); + } + + if (httpPrintf(http, "\r\n") < 1) + { + http->status = HTTP_ERROR; + return (-1); + } + + if (httpFlushWrite(http) < 0) + return (-1); + + httpGetLength2(http); + httpClearFields(http); + + /* + * The Kerberos and AuthRef authentication strings can only be used once... + */ + + if (http->field_authorization && http->authstring && + (!strncmp(http->authstring, "Negotiate", 9) || + !strncmp(http->authstring, "AuthRef", 7))) + { + http->_authstring[0] = '\0'; + + if (http->authstring != http->_authstring) + free(http->authstring); + + http->authstring = http->_authstring; + } + + return (0); +} + + +#ifdef HAVE_SSL +# if defined(HAVE_CDSASSL) && defined(HAVE_SECCERTIFICATECOPYDATA) +/* + * 'http_set_credentials()' - Set the SSL/TLS credentials. + */ + +static int /* O - Status of connection */ +http_set_credentials(http_t *http) /* I - Connection to server */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + OSStatus error = 0; /* Error code */ + http_tls_credentials_t credentials = NULL; + /* TLS credentials */ + + + DEBUG_printf(("7http_set_credentials(%p)", http)); + + /* + * Prefer connection specific credentials... + */ + + if ((credentials = http->tls_credentials) == NULL) + credentials = cg->tls_credentials; + + if (credentials) + { + error = SSLSetCertificate(http->tls, credentials); + DEBUG_printf(("4http_set_credentials: SSLSetCertificate, error=%d", + (int)error)); + } + else + DEBUG_puts("4http_set_credentials: No credentials to set."); + + return (error); +} +# endif /* HAVE_CDSASSL && HAVE_SECCERTIFICATECOPYDATA */ +#endif /* HAVE_SSL */ + + +/* + * 'http_set_timeout()' - Set the socket timeout values. + */ + +static void +http_set_timeout(int fd, /* I - File descriptor */ + double timeout) /* I - Timeout in seconds */ +{ +#ifdef WIN32 + DWORD tv = (DWORD)(timeout * 1000); + /* Timeout in milliseconds */ + + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); + +#else + struct timeval tv; /* Timeout in secs and usecs */ + + tv.tv_sec = (int)timeout; + tv.tv_usec = (int)(1000000 * fmod(timeout, 1.0)); + + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); +#endif /* WIN32 */ +} + + +/* + * 'http_set_wait()' - Set the default wait value for reads. + */ + +static void +http_set_wait(http_t *http) /* I - Connection to server */ +{ + if (http->blocking) + { + http->wait_value = (int)(http->timeout_value * 1000); + + if (http->wait_value <= 0) + http->wait_value = 60000; + } + else + http->wait_value = 10000; +} + + +#ifdef HAVE_SSL +/* + * 'http_setup_ssl()' - Set up SSL/TLS support on a connection. + */ + +static int /* O - 0 on success, -1 on failure */ +http_setup_ssl(http_t *http) /* I - Connection to server */ +{ + int any_root; /* Allow any root */ + char hostname[256], /* Hostname */ + *hostptr; /* Pointer into hostname */ + _cups_globals_t *cg = _cupsGlobals(); + /* Pointer to library globals */ +# ifdef HAVE_LIBSSL + SSL_CTX *context; /* Context for encryption */ + BIO *bio; /* BIO data */ + const char *message = NULL;/* Error message */ +# elif defined(HAVE_GNUTLS) + int status; /* Status of handshake */ + gnutls_certificate_client_credentials *credentials; + /* TLS credentials */ +# elif defined(HAVE_CDSASSL) + OSStatus error; /* Error code */ + const char *message = NULL;/* Error message */ +# ifdef HAVE_SECCERTIFICATECOPYDATA + cups_array_t *credentials; /* Credentials array */ + cups_array_t *names; /* CUPS distinguished names */ + CFArrayRef dn_array; /* CF distinguished names array */ + CFIndex count; /* Number of credentials */ + CFDataRef data; /* Certificate data */ + int i; /* Looping var */ + http_credential_t *credential; /* Credential data */ +# endif /* HAVE_SECCERTIFICATECOPYDATA */ +# elif defined(HAVE_SSPISSL) + TCHAR username[256]; /* Username returned from GetUserName() */ + TCHAR commonName[256];/* Common name for certificate */ + DWORD dwSize; /* 32 bit size */ +# endif /* HAVE_LIBSSL */ + + + DEBUG_printf(("7http_setup_ssl(http=%p)", http)); + + /* + * Always allow self-signed certificates for the local loopback address... + */ + + if (httpAddrLocalhost(http->hostaddr)) + { + any_root = 1; + strlcpy(hostname, "localhost", sizeof(hostname)); + } + else + { + /* + * Otherwise use the system-wide setting and make sure the hostname we have + * does not end in a trailing dot. + */ + + any_root = cg->any_root; + + strlcpy(hostname, http->hostname, sizeof(hostname)); + if ((hostptr = hostname + strlen(hostname) - 1) >= hostname && + *hostptr == '.') + *hostptr = '\0'; + } + +# ifdef HAVE_LIBSSL + (void)any_root; + + context = SSL_CTX_new(SSLv23_client_method()); + // 07/22/2016 Mopria-notice: disabling SSLv3 + SSL_CTX_set_options(context, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 ); /* Only use TLS */ + + bio = BIO_new(_httpBIOMethods()); + BIO_ctrl(bio, BIO_C_SET_FILE_PTR, 0, (char *)http); + + http->tls = SSL_new(context); + SSL_set_bio(http->tls, bio, bio); + +# ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME + SSL_set_tlsext_host_name(http->tls, hostname); +# endif /* HAVE_SSL_SET_TLSEXT_HOST_NAME */ + + if (SSL_connect(http->tls) != 1) + { + unsigned long error; /* Error code */ + + while ((error = ERR_get_error()) != 0) + { + message = ERR_error_string(error, NULL); + DEBUG_printf(("8http_setup_ssl: %s", message)); + } + + SSL_CTX_free(context); + SSL_free(http->tls); + http->tls = NULL; + +# ifdef WIN32 + http->error = WSAGetLastError(); +# else + http->error = errno; +# endif /* WIN32 */ + http->status = HTTP_ERROR; + + if (!message) + message = _("Unable to establish a secure connection to host."); + + _cupsSetError(IPP_PKI_ERROR, message, 1); + + return (-1); + } + +# elif defined(HAVE_GNUTLS) + (void)any_root; + + credentials = (gnutls_certificate_client_credentials *) + malloc(sizeof(gnutls_certificate_client_credentials)); + if (credentials == NULL) + { + DEBUG_printf(("8http_setup_ssl: Unable to allocate credentials: %s", + strerror(errno))); + http->error = errno; + http->status = HTTP_ERROR; + _cupsSetHTTPError(HTTP_ERROR); + + return (-1); + } + + gnutls_certificate_allocate_credentials(credentials); + + gnutls_init(&http->tls, GNUTLS_CLIENT); + gnutls_set_default_priority(http->tls); + gnutls_server_name_set(http->tls, GNUTLS_NAME_DNS, hostname, + strlen(hostname)); + gnutls_credentials_set(http->tls, GNUTLS_CRD_CERTIFICATE, *credentials); + gnutls_transport_set_ptr(http->tls, (gnutls_transport_ptr)http); + gnutls_transport_set_pull_function(http->tls, _httpReadGNUTLS); + gnutls_transport_set_push_function(http->tls, _httpWriteGNUTLS); + + while ((status = gnutls_handshake(http->tls)) != GNUTLS_E_SUCCESS) + { + DEBUG_printf(("8http_setup_ssl: gnutls_handshake returned %d (%s)", + status, gnutls_strerror(status))); + + if (gnutls_error_is_fatal(status)) + { + http->error = EIO; + http->status = HTTP_ERROR; + + _cupsSetError(IPP_PKI_ERROR, gnutls_strerror(status), 0); + + gnutls_deinit(http->tls); + gnutls_certificate_free_credentials(*credentials); + free(credentials); + http->tls = NULL; + + return (-1); + } + } + + http->tls_credentials = credentials; + +# elif defined(HAVE_CDSASSL) + if ((error = SSLNewContext(false, &http->tls))) + { + http->error = errno; + http->status = HTTP_ERROR; + _cupsSetHTTPError(HTTP_ERROR); + + return (-1); + } + + error = SSLSetConnection(http->tls, http); + DEBUG_printf(("4http_setup_ssl: SSLSetConnection, error=%d", (int)error)); + + if (!error) + { + error = SSLSetIOFuncs(http->tls, _httpReadCDSA, _httpWriteCDSA); + DEBUG_printf(("4http_setup_ssl: SSLSetIOFuncs, error=%d", (int)error)); + } + + if (!error) + { + error = SSLSetAllowsAnyRoot(http->tls, any_root); + DEBUG_printf(("4http_setup_ssl: SSLSetAllowsAnyRoot(%d), error=%d", + any_root, (int)error)); + } + + if (!error) + { + error = SSLSetAllowsExpiredCerts(http->tls, cg->expired_certs); + DEBUG_printf(("4http_setup_ssl: SSLSetAllowsExpiredCerts(%d), error=%d", + cg->expired_certs, (int)error)); + } + + if (!error) + { + error = SSLSetAllowsExpiredRoots(http->tls, cg->expired_root); + DEBUG_printf(("4http_setup_ssl: SSLSetAllowsExpiredRoots(%d), error=%d", + cg->expired_root, (int)error)); + } + + /* + * In general, don't verify certificates since things like the common name + * often do not match... + */ + + if (!error) + { + error = SSLSetEnableCertVerify(http->tls, false); + DEBUG_printf(("4http_setup_ssl: SSLSetEnableCertVerify, error=%d", + (int)error)); + } + +# ifdef HAVE_SECCERTIFICATECOPYDATA + if (!error) + { + if (cg->client_cert_cb) + { + error = SSLSetSessionOption(http->tls, + kSSLSessionOptionBreakOnCertRequested, true); + DEBUG_printf(("4http_setup_ssl: kSSLSessionOptionBreakOnCertRequested, " + "error=%d", (int)error)); + } + else + { + error = http_set_credentials(http); + DEBUG_printf(("4http_setup_ssl: http_set_credentials, error=%d", + (int)error)); + } + } + + /* + * If there's a server certificate callback installed let it evaluate the + * certificate(s) during the handshake... + */ + + if (!error && cg->server_cert_cb != NULL) + { + error = SSLSetSessionOption(http->tls, + kSSLSessionOptionBreakOnServerAuth, true); + DEBUG_printf(("4http_setup_ssl: kSSLSessionOptionBreakOnServerAuth, " + "error=%d", (int)error)); + } +# endif /* HAVE_SECCERTIFICATECOPYDATA */ + + /* + * Let the server know which hostname/domain we are trying to connect to + * in case it wants to serve up a certificate with a matching common name. + */ + + if (!error) + { + error = SSLSetPeerDomainName(http->tls, hostname, strlen(hostname)); + + DEBUG_printf(("4http_setup_ssl: SSLSetPeerDomainName, error=%d", + (int)error)); + } + + if (!error) + { + int done = 0; /* Are we done yet? */ + + while (!error && !done) + { + error = SSLHandshake(http->tls); + + DEBUG_printf(("4http_setup_ssl: SSLHandshake returned %d.", (int)error)); + + switch (error) + { + case noErr : + done = 1; + break; + + case errSSLWouldBlock : + error = noErr; /* Force a retry */ + usleep(1000); /* in 1 millisecond */ + break; + +# ifdef HAVE_SECCERTIFICATECOPYDATA + case errSSLServerAuthCompleted : + error = 0; + if (cg->server_cert_cb) + { + error = httpCopyCredentials(http, &credentials); + if (!error) + { + error = (cg->server_cert_cb)(http, http->tls, credentials, + cg->server_cert_data); + httpFreeCredentials(credentials); + } + + DEBUG_printf(("4http_setup_ssl: Server certificate callback " + "returned %d.", (int)error)); + } + break; + + case errSSLClientCertRequested : + error = 0; + + if (cg->client_cert_cb) + { + names = NULL; + if (!(error = SSLCopyDistinguishedNames(http->tls, &dn_array)) && + dn_array) + { + if ((names = cupsArrayNew(NULL, NULL)) != NULL) + { + for (i = 0, count = CFArrayGetCount(dn_array); i < count; i++) + { + data = (CFDataRef)CFArrayGetValueAtIndex(dn_array, i); + + if ((credential = malloc(sizeof(*credential))) != NULL) + { + credential->datalen = CFDataGetLength(data); + if ((credential->data = malloc(credential->datalen))) + { + memcpy((void *)credential->data, CFDataGetBytePtr(data), + credential->datalen); + cupsArrayAdd(names, credential); + } + else + free(credential); + } + } + } + + CFRelease(dn_array); + } + + if (!error) + { + error = (cg->client_cert_cb)(http, http->tls, names, + cg->client_cert_data); + + DEBUG_printf(("4http_setup_ssl: Client certificate callback " + "returned %d.", (int)error)); + } + + httpFreeCredentials(names); + } + break; +# endif /* HAVE_SECCERTIFICATECOPYDATA */ + + case errSSLUnknownRootCert : + message = _("Unable to establish a secure connection to host " + "(untrusted certificate)."); + break; + + case errSSLNoRootCert : + message = _("Unable to establish a secure connection to host " + "(self-signed certificate)."); + break; + + case errSSLCertExpired : + message = _("Unable to establish a secure connection to host " + "(expired certificate)."); + break; + + case errSSLCertNotYetValid : + message = _("Unable to establish a secure connection to host " + "(certificate not yet valid)."); + break; + + case errSSLHostNameMismatch : + message = _("Unable to establish a secure connection to host " + "(host name mismatch)."); + break; + + case errSSLXCertChainInvalid : + message = _("Unable to establish a secure connection to host " + "(certificate chain invalid)."); + break; + + case errSSLConnectionRefused : + message = _("Unable to establish a secure connection to host " + "(peer dropped connection before responding)."); + break; + + default : + break; + } + } + } + + if (error) + { + http->error = error; + http->status = HTTP_ERROR; + errno = ECONNREFUSED; + + SSLDisposeContext(http->tls); + http->tls = NULL; + + /* + * If an error string wasn't set by the callbacks use a generic one... + */ + + if (!message) +#ifdef HAVE_CSSMERRORSTRING + message = cssmErrorString(error); +#else + message = _("Unable to establish a secure connection to host."); +#endif /* HAVE_CSSMERRORSTRING */ + + _cupsSetError(IPP_PKI_ERROR, message, 1); + + return (-1); + } + +# elif defined(HAVE_SSPISSL) + http->tls = _sspiAlloc(); + + if (!http->tls) + { + _cupsSetHTTPError(HTTP_ERROR); + return (-1); + } + + http->tls->sock = http->fd; + dwSize = sizeof(username) / sizeof(TCHAR); + GetUserName(username, &dwSize); + _sntprintf_s(commonName, sizeof(commonName) / sizeof(TCHAR), + sizeof(commonName) / sizeof(TCHAR), TEXT("CN=%s"), username); + + if (!_sspiGetCredentials(http->tls_credentials, L"ClientContainer", + commonName, FALSE)) + { + _sspiFree(http->tls_credentials); + http->tls_credentials = NULL; + + http->error = EIO; + http->status = HTTP_ERROR; + + _cupsSetError(IPP_PKI_ERROR, + _("Unable to establish a secure connection to host."), 1); + + return (-1); + } + + _sspiSetAllowsAnyRoot(http->tls_credentials, any_root); + _sspiSetAllowsExpiredCerts(http->tls_credentials, TRUE); + + if (!_sspiConnect(http->tls_credentials, hostname)) + { + _sspiFree(http->tls_credentials); + http->tls_credentials = NULL; + + http->error = EIO; + http->status = HTTP_ERROR; + + _cupsSetError(IPP_PKI_ERROR, + _("Unable to establish a secure connection to host."), 1); + + return (-1); + } +# endif /* HAVE_CDSASSL */ + + return (0); +} + + +/* + * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection. + */ + +static void +http_shutdown_ssl(http_t *http) /* I - Connection to server */ +{ +# ifdef HAVE_LIBSSL + SSL_CTX *context; /* Context for encryption */ + + context = SSL_get_SSL_CTX(http->tls); + + SSL_shutdown(http->tls); + SSL_CTX_free(context); + SSL_free(http->tls); + +# elif defined(HAVE_GNUTLS) + gnutls_certificate_client_credentials *credentials; + /* TLS credentials */ + + credentials = (gnutls_certificate_client_credentials *)(http->tls_credentials); + + gnutls_bye(http->tls, GNUTLS_SHUT_RDWR); + gnutls_deinit(http->tls); + gnutls_certificate_free_credentials(*credentials); + free(credentials); + +# elif defined(HAVE_CDSASSL) + while (SSLClose(http->tls) == errSSLWouldBlock) + usleep(1000); + + SSLDisposeContext(http->tls); + + if (http->tls_credentials) + CFRelease(http->tls_credentials); + +# elif defined(HAVE_SSPISSL) + _sspiFree(http->tls_credentials); +# endif /* HAVE_LIBSSL */ + + http->tls = NULL; + http->tls_credentials = NULL; +} +#endif /* HAVE_SSL */ + + +#ifdef HAVE_SSL +/* + * 'http_upgrade()' - Force upgrade to TLS encryption. + */ + +static int /* O - Status of connection */ +http_upgrade(http_t *http) /* I - Connection to server */ +{ + int ret; /* Return value */ + http_t myhttp; /* Local copy of HTTP data */ + + + DEBUG_printf(("7http_upgrade(%p)", http)); + + /* + * Flush the connection to make sure any previous "Upgrade" message + * has been read. + */ + + httpFlush(http); + + /* + * Copy the HTTP data to a local variable so we can do the OPTIONS + * request without interfering with the existing request data... + */ + + memcpy(&myhttp, http, sizeof(myhttp)); + + /* + * Send an OPTIONS request to the server, requiring SSL or TLS + * encryption on the link... + */ + + http->field_authorization = NULL; /* Don't free the auth string */ + + httpClearFields(http); + httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade"); + httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2, TLS/1.1, TLS/1.0, SSL/3.0"); + + if ((ret = httpOptions(http, "*")) == 0) + { + /* + * Wait for the secure connection... + */ + + while (httpUpdate(http) == HTTP_CONTINUE); + } + + /* + * Restore the HTTP request data... + */ + + memcpy(http->fields, myhttp.fields, sizeof(http->fields)); + http->data_encoding = myhttp.data_encoding; + http->data_remaining = myhttp.data_remaining; + http->_data_remaining = myhttp._data_remaining; + http->expect = myhttp.expect; + http->field_authorization = myhttp.field_authorization; + http->digest_tries = myhttp.digest_tries; + + /* + * See if we actually went secure... + */ + + if (!http->tls) + { + /* + * Server does not support HTTP upgrade... + */ + + DEBUG_puts("8http_upgrade: Server does not support HTTP upgrade!"); + +# ifdef WIN32 + closesocket(http->fd); +# else + close(http->fd); +# endif + + http->fd = -1; + + return (-1); + } + else + return (ret); +} +#endif /* HAVE_SSL */ + + +/* + * 'http_write()' - Write a buffer to a HTTP connection. + */ + +static int /* O - Number of bytes written */ +http_write(http_t *http, /* I - Connection to server */ + const char *buffer, /* I - Buffer for data */ + int length) /* I - Number of bytes to write */ +{ + int tbytes, /* Total bytes sent */ + bytes; /* Bytes sent */ + + + DEBUG_printf(("2http_write(http=%p, buffer=%p, length=%d)", http, buffer, + length)); + http->error = 0; + tbytes = 0; + + while (length > 0) + { + DEBUG_printf(("3http_write: About to write %d bytes.", (int)length)); + + if (http->timeout_cb) + { +#ifdef HAVE_POLL + struct pollfd pfd; /* Polled file descriptor */ +#else + fd_set output_set; /* Output ready for write? */ + struct timeval timeout; /* Timeout value */ +#endif /* HAVE_POLL */ + int nfds; /* Result from select()/poll() */ + + do + { +#ifdef HAVE_POLL + pfd.fd = http->fd; + pfd.events = POLLOUT; + + while ((nfds = poll(&pfd, 1, http->wait_value)) < 0 && + (errno == EINTR || errno == EAGAIN)) + /* do nothing */; + +#else + do + { + FD_ZERO(&output_set); + FD_SET(http->fd, &output_set); + + timeout.tv_sec = http->wait_value / 1000; + timeout.tv_usec = 1000 * (http->wait_value % 1000); + + nfds = select(http->fd + 1, NULL, &output_set, NULL, &timeout); + } +# ifdef WIN32 + while (nfds < 0 && (WSAGetLastError() == WSAEINTR || + WSAGetLastError() == WSAEWOULDBLOCK)); +# else + while (nfds < 0 && (errno == EINTR || errno == EAGAIN)); +# endif /* WIN32 */ +#endif /* HAVE_POLL */ + + if (nfds < 0) + { + http->error = errno; + return (-1); + } + else if (nfds == 0 && !(*http->timeout_cb)(http, http->timeout_data)) + { +#ifdef WIN32 + http->error = WSAEWOULDBLOCK; +#else + http->error = EWOULDBLOCK; +#endif /* WIN32 */ + return (-1); + } + } + while (nfds <= 0); + } + +#ifdef HAVE_SSL + if (http->tls) + bytes = http_write_ssl(http, buffer, length); + else +#endif /* HAVE_SSL */ + bytes = send(http->fd, buffer, length, 0); + + DEBUG_printf(("3http_write: Write of %d bytes returned %d.", (int)length, + (int)bytes)); + + if (bytes < 0) + { +#ifdef WIN32 + if (WSAGetLastError() == WSAEINTR) + continue; + else if (WSAGetLastError() == WSAEWOULDBLOCK) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + + http->error = WSAGetLastError(); + } + else if (WSAGetLastError() != http->error && + WSAGetLastError() != WSAECONNRESET) + { + http->error = WSAGetLastError(); + continue; + } + +#else + if (errno == EINTR) + continue; + else if (errno == EWOULDBLOCK || errno == EAGAIN) + { + if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) + continue; + else if (!http->timeout_cb && errno == EAGAIN) + continue; + + http->error = errno; + } + else if (errno != http->error && errno != ECONNRESET) + { + http->error = errno; + continue; + } +#endif /* WIN32 */ + + DEBUG_printf(("3http_write: error writing data (%s).", + strerror(http->error))); + + return (-1); + } + + buffer += bytes; + tbytes += bytes; + length -= bytes; + } + +#ifdef DEBUG + http_debug_hex("http_write", buffer - tbytes, tbytes); +#endif /* DEBUG */ + + DEBUG_printf(("3http_write: Returning %d.", tbytes)); + + return (tbytes); +} + + +/* + * 'http_write_chunk()' - Write a chunked buffer. + */ + +static int /* O - Number bytes written */ +http_write_chunk(http_t *http, /* I - Connection to server */ + const char *buffer, /* I - Buffer to write */ + int length) /* I - Length of buffer */ +{ + char header[255]; /* Chunk header */ + int bytes; /* Bytes written */ + + + DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=%d)", + http, buffer, length)); + + /* + * Write the chunk header, data, and trailer. + */ + + sprintf(header, "%x\r\n", length); + if (http_write(http, header, (int)strlen(header)) < 0) + { + DEBUG_puts("8http_write_chunk: http_write of length failed!"); + return (-1); + } + + if ((bytes = http_write(http, buffer, length)) < 0) + { + DEBUG_puts("8http_write_chunk: http_write of buffer failed!"); + return (-1); + } + + if (http_write(http, "\r\n", 2) < 0) + { + DEBUG_puts("8http_write_chunk: http_write of CR LF failed!"); + return (-1); + } + + return (bytes); +} + + +#ifdef HAVE_SSL +/* + * 'http_write_ssl()' - Write to a SSL/TLS connection. + */ + +static int /* O - Bytes written */ +http_write_ssl(http_t *http, /* I - Connection to server */ + const char *buf, /* I - Buffer holding data */ + int len) /* I - Length of buffer */ +{ + ssize_t result; /* Return value */ + + + DEBUG_printf(("2http_write_ssl(http=%p, buf=%p, len=%d)", http, buf, len)); + +# if defined(HAVE_LIBSSL) + result = SSL_write((SSL *)(http->tls), buf, len); + +# elif defined(HAVE_GNUTLS) + result = gnutls_record_send(http->tls, buf, len); + + if (result < 0 && !errno) + { + /* + * Convert GNU TLS error to errno value... + */ + + switch (result) + { + case GNUTLS_E_INTERRUPTED : + errno = EINTR; + break; + + case GNUTLS_E_AGAIN : + errno = EAGAIN; + break; + + default : + errno = EPIPE; + break; + } + + result = -1; + } + +# elif defined(HAVE_CDSASSL) + OSStatus error; /* Error info */ + size_t processed; /* Number of bytes processed */ + + + error = SSLWrite(http->tls, buf, len, &processed); + + switch (error) + { + case 0 : + result = (int)processed; + break; + + case errSSLWouldBlock : + if (processed) + result = (int)processed; + else + { + result = -1; + errno = EINTR; + } + break; + + case errSSLClosedGraceful : + default : + if (processed) + result = (int)processed; + else + { + result = -1; + errno = EPIPE; + } + break; + } +# elif defined(HAVE_SSPISSL) + return _sspiWrite((_sspi_struct_t *)http->tls, (void *)buf, len); +# endif /* HAVE_LIBSSL */ + + DEBUG_printf(("3http_write_ssl: Returning %d.", (int)result)); + + return ((int)result); +} +#endif /* HAVE_SSL */ + + +/* + * End of "$Id: http.c 7850 2008-08-20 00:07:25Z mike $". + */ diff --git a/cups/http.h b/cups/http.h new file mode 100644 index 0000000..3f259d3 --- /dev/null +++ b/cups/http.h @@ -0,0 +1,487 @@ +/* + * "$Id: http.h 7026 2007-10-19 00:57:45Z mike $" + * + * Hyper-Text Transport Protocol definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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. + */ + +#ifndef _CUPS_HTTP_H_ +# define _CUPS_HTTP_H_ + +/* + * Include necessary headers... + */ + +# include "versioning.h" +# include "array.h" +# include +# include +# include +# ifdef WIN32 +# ifndef __CUPS_SSIZE_T_DEFINED +# define __CUPS_SSIZE_T_DEFINED +/* Windows does not support the ssize_t type, so map it to off_t... */ +typedef off_t ssize_t; /* @private@ */ +# endif /* !__CUPS_SSIZE_T_DEFINED */ +# include +# include +# else +# ifdef __sgi +# define INET6 /* IRIX IPv6 support... */ +# endif /* __sgi */ +# include +# include +# include +# include +# include +# include +# include +# include +# if !defined(__APPLE__) || !defined(TCP_NODELAY) +# include +# endif /* !__APPLE__ || !TCP_NODELAY */ +# if defined(AF_UNIX) && !defined(AF_LOCAL) +# define AF_LOCAL AF_UNIX /* Older UNIX's have old names... */ +# endif /* AF_UNIX && !AF_LOCAL */ +# ifdef AF_LOCAL +# include +# endif /* AF_LOCAL */ +# if defined(LOCAL_PEERCRED) && !defined(SO_PEERCRED) +# define SO_PEERCRED LOCAL_PEERCRED +# endif /* LOCAL_PEERCRED && !SO_PEERCRED */ +# endif /* WIN32 */ + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Oh, the wonderful world of IPv6 compatibility. Apparently some + * implementations expose the (more logical) 32-bit address parts + * to everyone, while others only expose it to kernel code... To + * make supporting IPv6 even easier, each vendor chose different + * core structure and union names, so the same defines or code + * can't be used on all platforms. + * + * The following will likely need tweaking on new platforms that + * support IPv6 - the "s6_addr32" define maps to the 32-bit integer + * array in the in6_addr union, which is named differently on various + * platforms. + */ + +#if defined(AF_INET6) && !defined(s6_addr32) +# if defined(__sun) +# define s6_addr32 _S6_un._S6_u32 +# elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)|| defined(__DragonFly__) +# define s6_addr32 __u6_addr.__u6_addr32 +# elif defined(__osf__) +# define s6_addr32 s6_un.sa6_laddr +# elif defined(WIN32) +/* + * Windows only defines byte and 16-bit word members of the union and + * requires special casing of all raw address code... + */ +# define s6_addr32 error_need_win32_specific_code +# endif /* __sun */ +#endif /* AF_INET6 && !s6_addr32 */ + + +/* + * Limits... + */ + +# define HTTP_MAX_URI 1024 /* Max length of URI string */ +# define HTTP_MAX_HOST 256 /* Max length of hostname string */ +# define HTTP_MAX_BUFFER 2048 /* Max length of data buffer */ +# define HTTP_MAX_VALUE 256 /* Max header field value length */ + + +/* + * Types and structures... + */ + +typedef enum http_auth_e /**** HTTP authentication types ****/ +{ + HTTP_AUTH_NONE, /* No authentication in use */ + HTTP_AUTH_BASIC, /* Basic authentication in use */ + HTTP_AUTH_MD5, /* Digest authentication in use */ + HTTP_AUTH_MD5_SESS, /* MD5-session authentication in use */ + HTTP_AUTH_MD5_INT, /* Digest authentication in use for body */ + HTTP_AUTH_MD5_SESS_INT, /* MD5-session authentication in use for body */ + HTTP_AUTH_NEGOTIATE /* GSSAPI authentication in use @since CUPS 1.3/OS X 10.5@ */ +} http_auth_t; + +typedef enum http_encoding_e /**** HTTP transfer encoding values ****/ +{ + HTTP_ENCODE_LENGTH, /* Data is sent with Content-Length */ + HTTP_ENCODE_CHUNKED, /* Data is chunked */ + HTTP_ENCODE_FIELDS /* Sending HTTP fields */ +} http_encoding_t; + +typedef enum http_encryption_e /**** HTTP encryption values ****/ +{ + HTTP_ENCRYPT_IF_REQUESTED, /* Encrypt if requested (TLS upgrade) */ + HTTP_ENCRYPT_NEVER, /* Never encrypt */ + HTTP_ENCRYPT_REQUIRED, /* Encryption is required (TLS upgrade) */ + HTTP_ENCRYPT_ALWAYS /* Always encrypt (SSL) */ +} http_encryption_t; + +typedef enum http_field_e /**** HTTP field names ****/ +{ + HTTP_FIELD_UNKNOWN = -1, /* Unknown field */ + HTTP_FIELD_ACCEPT_LANGUAGE, /* Accept-Language field */ + HTTP_FIELD_ACCEPT_RANGES, /* Accept-Ranges field */ + HTTP_FIELD_AUTHORIZATION, /* Authorization field */ + HTTP_FIELD_CONNECTION, /* Connection field */ + HTTP_FIELD_CONTENT_ENCODING, /* Content-Encoding field */ + HTTP_FIELD_CONTENT_LANGUAGE, /* Content-Language field */ + HTTP_FIELD_CONTENT_LENGTH, /* Content-Length field */ + HTTP_FIELD_CONTENT_LOCATION, /* Content-Location field */ + HTTP_FIELD_CONTENT_MD5, /* Content-MD5 field */ + HTTP_FIELD_CONTENT_RANGE, /* Content-Range field */ + HTTP_FIELD_CONTENT_TYPE, /* Content-Type field */ + HTTP_FIELD_CONTENT_VERSION, /* Content-Version field */ + HTTP_FIELD_DATE, /* Date field */ + HTTP_FIELD_HOST, /* Host field */ + HTTP_FIELD_IF_MODIFIED_SINCE, /* If-Modified-Since field */ + HTTP_FIELD_IF_UNMODIFIED_SINCE, /* If-Unmodified-Since field */ + HTTP_FIELD_KEEP_ALIVE, /* Keep-Alive field */ + HTTP_FIELD_LAST_MODIFIED, /* Last-Modified field */ + HTTP_FIELD_LINK, /* Link field */ + HTTP_FIELD_LOCATION, /* Location field */ + HTTP_FIELD_RANGE, /* Range field */ + HTTP_FIELD_REFERER, /* Referer field */ + HTTP_FIELD_RETRY_AFTER, /* Retry-After field */ + HTTP_FIELD_TRANSFER_ENCODING, /* Transfer-Encoding field */ + HTTP_FIELD_UPGRADE, /* Upgrade field */ + HTTP_FIELD_USER_AGENT, /* User-Agent field */ + HTTP_FIELD_WWW_AUTHENTICATE, /* WWW-Authenticate field */ + HTTP_FIELD_MAX /* Maximum field index */ +} http_field_t; + +typedef enum http_keepalive_e /**** HTTP keep-alive values ****/ +{ + HTTP_KEEPALIVE_OFF = 0, /* No keep alive support */ + HTTP_KEEPALIVE_ON /* Use keep alive */ +} http_keepalive_t; + +typedef enum http_state_e /**** HTTP state values; states + **** are server-oriented... + ****/ +{ + HTTP_WAITING, /* Waiting for command */ + HTTP_OPTIONS, /* OPTIONS command, waiting for blank line */ + HTTP_GET, /* GET command, waiting for blank line */ + HTTP_GET_SEND, /* GET command, sending data */ + HTTP_HEAD, /* HEAD command, waiting for blank line */ + HTTP_POST, /* POST command, waiting for blank line */ + HTTP_POST_RECV, /* POST command, receiving data */ + HTTP_POST_SEND, /* POST command, sending data */ + HTTP_PUT, /* PUT command, waiting for blank line */ + HTTP_PUT_RECV, /* PUT command, receiving data */ + HTTP_DELETE, /* DELETE command, waiting for blank line */ + HTTP_TRACE, /* TRACE command, waiting for blank line */ + HTTP_CLOSE, /* CLOSE command, waiting for blank line */ + HTTP_STATUS /* Command complete, sending status */ +} http_state_t; + +typedef enum http_status_e /**** HTTP status codes ****/ +{ + HTTP_ERROR = -1, /* An error response from httpXxxx() */ + + HTTP_CONTINUE = 100, /* Everything OK, keep going... */ + HTTP_SWITCHING_PROTOCOLS, /* HTTP upgrade to TLS/SSL */ + + HTTP_OK = 200, /* OPTIONS/GET/HEAD/POST/TRACE command was successful */ + HTTP_CREATED, /* PUT command was successful */ + HTTP_ACCEPTED, /* DELETE command was successful */ + HTTP_NOT_AUTHORITATIVE, /* Information isn't authoritative */ + HTTP_NO_CONTENT, /* Successful command, no new data */ + HTTP_RESET_CONTENT, /* Content was reset/recreated */ + HTTP_PARTIAL_CONTENT, /* Only a partial file was recieved/sent */ + + HTTP_MULTIPLE_CHOICES = 300, /* Multiple files match request */ + HTTP_MOVED_PERMANENTLY, /* Document has moved permanently */ + HTTP_MOVED_TEMPORARILY, /* Document has moved temporarily */ + HTTP_SEE_OTHER, /* See this other link... */ + HTTP_NOT_MODIFIED, /* File not modified */ + HTTP_USE_PROXY, /* Must use a proxy to access this URI */ + + HTTP_BAD_REQUEST = 400, /* Bad request */ + HTTP_UNAUTHORIZED, /* Unauthorized to access host */ + HTTP_PAYMENT_REQUIRED, /* Payment required */ + HTTP_FORBIDDEN, /* Forbidden to access this URI */ + HTTP_NOT_FOUND, /* URI was not found */ + HTTP_METHOD_NOT_ALLOWED, /* Method is not allowed */ + HTTP_NOT_ACCEPTABLE, /* Not Acceptable */ + HTTP_PROXY_AUTHENTICATION, /* Proxy Authentication is Required */ + HTTP_REQUEST_TIMEOUT, /* Request timed out */ + HTTP_CONFLICT, /* Request is self-conflicting */ + HTTP_GONE, /* Server has gone away */ + HTTP_LENGTH_REQUIRED, /* A content length or encoding is required */ + HTTP_PRECONDITION, /* Precondition failed */ + HTTP_REQUEST_TOO_LARGE, /* Request entity too large */ + HTTP_URI_TOO_LONG, /* URI too long */ + HTTP_UNSUPPORTED_MEDIATYPE, /* The requested media type is unsupported */ + HTTP_REQUESTED_RANGE, /* The requested range is not satisfiable */ + HTTP_EXPECTATION_FAILED, /* The expectation given in an Expect header field was not met */ + HTTP_UPGRADE_REQUIRED = 426, /* Upgrade to SSL/TLS required */ + + HTTP_SERVER_ERROR = 500, /* Internal server error */ + HTTP_NOT_IMPLEMENTED, /* Feature not implemented */ + HTTP_BAD_GATEWAY, /* Bad gateway */ + HTTP_SERVICE_UNAVAILABLE, /* Service is unavailable */ + HTTP_GATEWAY_TIMEOUT, /* Gateway connection timed out */ + HTTP_NOT_SUPPORTED, /* HTTP version not supported */ + + HTTP_AUTHORIZATION_CANCELED = 1000, /* User canceled authorization @since CUPS 1.4@ */ + HTTP_PKI_ERROR, /* Error negotiating a secure connection @since CUPS 1.5/OS X 10.7@ */ + HTTP_WEBIF_DISABLED /* Web interface is disabled @private@ */ +} http_status_t; + +typedef enum http_uri_status_e /**** URI separation status @since CUPS 1.2@ ****/ +{ + HTTP_URI_OVERFLOW = -8, /* URI buffer for httpAssembleURI is too small */ + HTTP_URI_BAD_ARGUMENTS = -7, /* Bad arguments to function (error) */ + HTTP_URI_BAD_RESOURCE = -6, /* Bad resource in URI (error) */ + HTTP_URI_BAD_PORT = -5, /* Bad port number in URI (error) */ + HTTP_URI_BAD_HOSTNAME = -4, /* Bad hostname in URI (error) */ + HTTP_URI_BAD_USERNAME = -3, /* Bad username in URI (error) */ + HTTP_URI_BAD_SCHEME = -2, /* Bad scheme in URI (error) */ + HTTP_URI_BAD_URI = -1, /* Bad/empty URI (error) */ + HTTP_URI_OK = 0, /* URI decoded OK */ + HTTP_URI_MISSING_SCHEME, /* Missing scheme in URI (warning) */ + HTTP_URI_UNKNOWN_SCHEME, /* Unknown scheme in URI (warning) */ + HTTP_URI_MISSING_RESOURCE /* Missing resource in URI (warning) */ +} http_uri_status_t; + +typedef enum http_uri_coding_e /**** URI en/decode flags ****/ +{ + HTTP_URI_CODING_NONE = 0, /* Don't en/decode anything */ + HTTP_URI_CODING_USERNAME = 1, /* En/decode the username portion */ + HTTP_URI_CODING_HOSTNAME = 2, /* En/decode the hostname portion */ + HTTP_URI_CODING_RESOURCE = 4, /* En/decode the resource portion */ + HTTP_URI_CODING_MOST = 7, /* En/decode all but the query */ + HTTP_URI_CODING_QUERY = 8, /* En/decode the query portion */ + HTTP_URI_CODING_ALL = 15, /* En/decode everything */ + HTTP_URI_CODING_RFC6874 = 16 /* Use RFC 6874 address format */ +} http_uri_coding_t; + +typedef enum http_version_e /**** HTTP version numbers ****/ +{ + HTTP_0_9 = 9, /* HTTP/0.9 */ + HTTP_1_0 = 100, /* HTTP/1.0 */ + HTTP_1_1 = 101 /* HTTP/1.1 */ +} http_version_t; + +typedef union _http_addr_u /**** Socket address union, which + **** makes using IPv6 and other + **** address types easier and + **** more portable. @since CUPS 1.2/OS X 10.5@ + ****/ +{ + struct sockaddr addr; /* Base structure for family value */ + struct sockaddr_in ipv4; /* IPv4 address */ +#ifdef AF_INET6 + struct sockaddr_in6 ipv6; /* IPv6 address */ +#endif /* AF_INET6 */ +#ifdef AF_LOCAL + struct sockaddr_un un; /* Domain socket file */ +#endif /* AF_LOCAL */ + char pad[256]; /* Padding to ensure binary compatibility */ +} http_addr_t; + +typedef struct http_addrlist_s /**** Socket address list, which is + **** used to enumerate all of the + **** addresses that are associated + **** with a hostname. @since CUPS 1.2/OS X 10.5@ + ****/ +{ + struct http_addrlist_s *next; /* Pointer to next address in list */ + http_addr_t addr; /* Address */ +} http_addrlist_t; + +typedef struct _http_s http_t; /**** HTTP connection type ****/ + +typedef struct http_credential_s /**** HTTP credential data @since CUPS 1.5/OS X 10.7@ ****/ +{ + void *data; /* Pointer to credential data */ + size_t datalen; /* Credential length */ +} http_credential_t; + +typedef int (*http_timeout_cb_t)(http_t *http, void *user_data); + /**** HTTP timeout callback @since CUPS 1.5/OS X 10.7@ ****/ + + + +/* + * Prototypes... + */ + +extern void httpBlocking(http_t *http, int b); +extern int httpCheck(http_t *http); +extern void httpClearFields(http_t *http); +extern void httpClose(http_t *http); +extern http_t *httpConnect(const char *host, int port); +extern http_t *httpConnectEncrypt(const char *host, int port, + http_encryption_t encryption); +extern int httpDelete(http_t *http, const char *uri); +extern int httpEncryption(http_t *http, http_encryption_t e); +extern int httpError(http_t *http); +extern void httpFlush(http_t *http); +extern int httpGet(http_t *http, const char *uri); +extern char *httpGets(char *line, int length, http_t *http); +extern const char *httpGetDateString(time_t t); +extern time_t httpGetDateTime(const char *s); +extern const char *httpGetField(http_t *http, http_field_t field); +extern struct hostent *httpGetHostByName(const char *name); +extern char *httpGetSubField(http_t *http, http_field_t field, + const char *name, char *value); +extern int httpHead(http_t *http, const char *uri); +extern void httpInitialize(void); +extern int httpOptions(http_t *http, const char *uri); +extern int httpPost(http_t *http, const char *uri); +extern int httpPrintf(http_t *http, const char *format, ...) + __attribute__ ((__format__ (__printf__, 2, 3))); +extern int httpPut(http_t *http, const char *uri); +extern int httpRead(http_t *http, char *buffer, int length) _CUPS_DEPRECATED; +extern int httpReconnect(http_t *http); +extern void httpSeparate(const char *uri, char *method, + char *username, char *host, int *port, + char *resource) _CUPS_DEPRECATED; +extern void httpSetField(http_t *http, http_field_t field, + const char *value); +extern const char *httpStatus(http_status_t status); +extern int httpTrace(http_t *http, const char *uri); +extern http_status_t httpUpdate(http_t *http); +extern int httpWrite(http_t *http, const char *buffer, int length) _CUPS_DEPRECATED; +extern char *httpEncode64(char *out, const char *in) _CUPS_DEPRECATED; +extern char *httpDecode64(char *out, const char *in) _CUPS_DEPRECATED; +extern int httpGetLength(http_t *http) _CUPS_DEPRECATED; +extern char *httpMD5(const char *, const char *, const char *, + char [33]); +extern char *httpMD5Final(const char *, const char *, const char *, + char [33]); +extern char *httpMD5String(const unsigned char *, char [33]); + +/**** New in CUPS 1.1.19 ****/ +extern void httpClearCookie(http_t *http) _CUPS_API_1_1_19; +extern const char *httpGetCookie(http_t *http) _CUPS_API_1_1_19; +extern void httpSetCookie(http_t *http, const char *cookie) _CUPS_API_1_1_19; +extern int httpWait(http_t *http, int msec) _CUPS_API_1_1_19; + +/**** New in CUPS 1.1.21 ****/ +extern char *httpDecode64_2(char *out, int *outlen, const char *in) _CUPS_API_1_1_21; +extern char *httpEncode64_2(char *out, int outlen, const char *in, + int inlen) _CUPS_API_1_1_21; +extern void httpSeparate2(const char *uri, + char *method, int methodlen, + char *username, int usernamelen, + char *host, int hostlen, int *port, + char *resource, int resourcelen) _CUPS_DEPRECATED; + +/**** New in CUPS 1.2/OS X 10.5 ****/ +extern int httpAddrAny(const http_addr_t *addr) _CUPS_API_1_2; +extern http_addrlist_t *httpAddrConnect(http_addrlist_t *addrlist, int *sock) _CUPS_API_1_2; +extern int httpAddrEqual(const http_addr_t *addr1, + const http_addr_t *addr2) _CUPS_API_1_2; +extern void httpAddrFreeList(http_addrlist_t *addrlist) _CUPS_API_1_2; +extern http_addrlist_t *httpAddrGetList(const char *hostname, int family, + const char *service) _CUPS_API_1_2; +extern int httpAddrLength(const http_addr_t *addr) _CUPS_API_1_2; +extern int httpAddrLocalhost(const http_addr_t *addr) _CUPS_API_1_2; +extern char *httpAddrLookup(const http_addr_t *addr, + char *name, int namelen) _CUPS_API_1_2; +extern char *httpAddrString(const http_addr_t *addr, + char *s, int slen) _CUPS_API_1_2; +extern http_uri_status_t httpAssembleURI(http_uri_coding_t encoding, + char *uri, int urilen, + const char *scheme, + const char *username, + const char *host, int port, + const char *resource) _CUPS_API_1_2; +extern http_uri_status_t httpAssembleURIf(http_uri_coding_t encoding, + char *uri, int urilen, + const char *scheme, + const char *username, + const char *host, int port, + const char *resourcef, ...) _CUPS_API_1_2; +extern int httpFlushWrite(http_t *http) _CUPS_API_1_2; +extern int httpGetBlocking(http_t *http) _CUPS_API_1_2; +extern const char *httpGetDateString2(time_t t, char *s, int slen) _CUPS_API_1_2; +extern int httpGetFd(http_t *http) _CUPS_API_1_2; +extern const char *httpGetHostname(http_t *http, char *s, int slen) _CUPS_API_1_2; +extern off_t httpGetLength2(http_t *http) _CUPS_API_1_2; +extern http_status_t httpGetStatus(http_t *http) _CUPS_API_1_2; +extern char *httpGetSubField2(http_t *http, http_field_t field, + const char *name, char *value, + int valuelen) _CUPS_API_1_2; +extern ssize_t httpRead2(http_t *http, char *buffer, size_t length) _CUPS_API_1_2; +extern http_uri_status_t httpSeparateURI(http_uri_coding_t decoding, + const char *uri, + char *scheme, int schemelen, + char *username, int usernamelen, + char *host, int hostlen, int *port, + char *resource, int resourcelen) _CUPS_API_1_2; +extern void httpSetExpect(http_t *http, http_status_t expect) _CUPS_API_1_2; +extern void httpSetLength(http_t *http, size_t length) _CUPS_API_1_2; +extern ssize_t httpWrite2(http_t *http, const char *buffer, + size_t length) _CUPS_API_1_2; + +/**** New in CUPS 1.3/OS X 10.5 ****/ +extern char *httpGetAuthString(http_t *http) _CUPS_API_1_3; +extern void httpSetAuthString(http_t *http, const char *scheme, + const char *data) _CUPS_API_1_3; + +/**** New in CUPS 1.5/OS X 10.7 ****/ +extern int httpAddCredential(cups_array_t *credentials, + const void *data, size_t datalen) + _CUPS_API_1_5; +extern int httpCopyCredentials(http_t *http, + cups_array_t **credentials) + _CUPS_API_1_5; +extern void httpFreeCredentials(cups_array_t *certs) _CUPS_API_1_5; +extern int httpSetCredentials(http_t *http, cups_array_t *certs) + _CUPS_API_1_5; +extern void httpSetTimeout(http_t *http, double timeout, + http_timeout_cb_t cb, void *user_data) + _CUPS_API_1_5; + +/**** New in CUPS 1.6/OS X 10.8 ****/ +extern http_addrlist_t *httpAddrConnect2(http_addrlist_t *addrlist, int *sock, + int msec, int *cancel) + _CUPS_API_1_6; +extern http_state_t httpGetState(http_t *http) _CUPS_API_1_6; +extern http_version_t httpGetVersion(http_t *http) _CUPS_API_1_6; +extern int httpReconnect2(http_t *http, int msec, int *cancel) + _CUPS_API_1_6; + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_HTTP_H_ */ + +/* + * End of "$Id: http.h 7026 2007-10-19 00:57:45Z mike $". + */ diff --git a/cups/ipp-private.h b/cups/ipp-private.h new file mode 100644 index 0000000..1d3003c --- /dev/null +++ b/cups/ipp-private.h @@ -0,0 +1,78 @@ +/* + * "$Id: ipp-private.h 7259 2008-01-28 22:26:04Z mike $" + * + * Private IPP definitions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + */ + +#ifndef _CUPS_IPP_PRIVATE_H_ +# define _CUPS_IPP_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Constants... + */ + +# define IPP_BUF_SIZE (IPP_MAX_LENGTH + 2) + /* Size of buffer */ + + +/* + * Structures... + */ + +typedef struct /**** Attribute mapping data ****/ +{ + int multivalue; /* Option has multiple values? */ + const char *name; /* Option/attribute name */ + ipp_tag_t value_tag; /* Value tag for this attribute */ + ipp_tag_t group_tag; /* Group tag for this attribute */ + ipp_tag_t alt_group_tag; /* Alternate group tag for this + * attribute */ +} _ipp_option_t; + + +/* + * Prototypes for private functions... + */ + +extern _ipp_option_t *_ippFindOption(const char *name); + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_IPP_H_ */ + +/* + * End of "$Id: ipp-private.h 7259 2008-01-28 22:26:04Z mike $". + */ diff --git a/cups/ipp-support.c b/cups/ipp-support.c new file mode 100644 index 0000000..96cd149 --- /dev/null +++ b/cups/ipp-support.c @@ -0,0 +1,1090 @@ +/* + * "$Id: ipp-support.c 9371 2010-11-17 06:21:32Z mike $" + * + * Internet Printing Protocol support functions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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. + * + * Contents: + * + * ippAttributeString() - Convert the attribute's value to a string. + * ippEnumString() - Return a string corresponding to the enum value. + * ippEnumValue() - Return the value associated with a given enum + * string. + * ippErrorString() - Return a name for the given status code. + * ippErrorValue() - Return a status code for the given name. + * ippOpString() - Return a name for the given operation id. + * ippOpValue() - Return an operation id for the given name. + * ippPort() - Return the default IPP port number. + * ippSetPort() - Set the default port number. + * ippTagString() - Return the tag name corresponding to a tag value. + * ippTagValue() - Return the tag value corresponding to a tag name. + * ipp_col_string() - Convert a collection to a string. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * Local globals... + */ + +static const char * const ipp_status_oks[] = /* "OK" status codes */ + { /* (name) = abandoned standard value */ + "successful-ok", + "successful-ok-ignored-or-substituted-attributes", + "successful-ok-conflicting-attributes", + "successful-ok-ignored-subscriptions", + "(successful-ok-ignored-notifications)", + "successful-ok-too-many-events", + "(successful-ok-but-cancel-subscription)", + "successful-ok-events-complete" + }, + * const ipp_status_400s[] = /* Client errors */ + { /* (name) = abandoned standard value */ + "client-error-bad-request", + "client-error-forbidden", + "client-error-not-authenticated", + "client-error-not-authorized", + "client-error-not-possible", + "client-error-timeout", + "client-error-not-found", + "client-error-gone", + "client-error-request-entity-too-large", + "client-error-request-value-too-long", + "client-error-document-format-not-supported", + "client-error-attributes-or-values-not-supported", + "client-error-uri-scheme-not-supported", + "client-error-charset-not-supported", + "client-error-conflicting-attributes", + "client-error-compression-not-supported", + "client-error-compression-error", + "client-error-document-format-error", + "client-error-document-access-error", + "client-error-attributes-not-settable", + "client-error-ignored-all-subscriptions", + "client-error-too-many-subscriptions", + "(client-error-ignored-all-notifications)", + "(client-error-client-print-support-file-not-found)", + "client-error-document-password-error", + "client-error-document-permission-error", + "client-error-document-security-error", + "client-error-document-unprintable-error" + }, + * const ipp_status_500s[] = /* Server errors */ + { + "server-error-internal-error", + "server-error-operation-not-supported", + "server-error-service-unavailable", + "server-error-version-not-supported", + "server-error-device-error", + "server-error-temporary-error", + "server-error-not-accepting-jobs", + "server-error-busy", + "server-error-job-canceled", + "server-error-multiple-document-jobs-not-supported", + "server-error-printer-is-deactivated", + "server-error-too-many-jobs", + "server-error-too-many-documents" + }, + * const ipp_status_1000s[] = /* CUPS internal */ + { + "cups-authorization-canceled", + "cups-pki-error", + "cups-upgrade-required" + }; +static const char * const ipp_std_ops[] = + { + /* 0x0000 - 0x000f */ + "0x00", + "0x01", + "Print-Job", + "Print-URI", + "Validate-Job", + "Create-Job", + "Send-Document", + "Send-URI", + "Cancel-Job", + "Get-Job-Attributes", + "Get-Jobs", + "Get-Printer-Attributes", + "Hold-Job", + "Release-Job", + "Restart-Job", + "0x0f", + + /* 0x0010 - 0x001f */ + "Pause-Printer", + "Resume-Printer", + "Purge-Jobs", + "Set-Printer-Attributes", + "Set-Job-Attributes", + "Get-Printer-Supported-Values", + "Create-Printer-Subscription", + "Create-Job-Subscription", + "Get-Subscription-Attributes", + "Get-Subscriptions", + "Renew-Subscription", + "Cancel-Subscription", + "Get-Notifications", + "(Send-Notifications)", + "(Get-Resource-Attributes)", + "(Get-Resource-Data)", + + /* 0x0020 - 0x002f */ + "(Get-Resources)", + "(Get-Printer-Support-Files)", + "Enable-Printer", + "Disable-Printer", + "Pause-Printer-After-Current-Job", + "Hold-New-Jobs", + "Release-Held-New-Jobs", + "Deactivate-Printer", + "Activate-Printer", + "Restart-Printer", + "Shutdown-Printer", + "Startup-Printer", + "Reprocess-Job", + "Cancel-Current-Job", + "Suspend-Current-Job", + "Resume-Job", + + /* 0x0030 - 0x003d */ + "Promote-Job", + "Schedule-Job-After", + "0x32", + "Cancel-Document", + "Get-Document-Attributes", + "Get-Documents", + "Delete-Document", + "Set-Document-Attributes", + "Cancel-Jobs", + "Cancel-My-Jobs", + "Resubmit-Job", + "Close-Job", + "Identify-Printer", + "Validate-Document" + }, + * const ipp_cups_ops[] = + { + "CUPS-Get-Default", + "CUPS-Get-Printers", + "CUPS-Add-Modify-Printer", + "CUPS-Delete-Printer", + "CUPS-Get-Classes", + "CUPS-Add-Modify-Class", + "CUPS-Delete-Class", + "CUPS-Accept-Jobs", + "CUPS-Reject-Jobs", + "CUPS-Set-Default", + "CUPS-Get-Devices", + "CUPS-Get-PPDs", + "CUPS-Move-Job", + "CUPS-Authenticate-Job", + "CUPS-Get-PPD" + }, + * const ipp_cups_ops2[] = + { + "CUPS-Get-Document" + }, + * const ipp_tag_names[] = + { /* Value/group tag names */ + "zero", /* 0x00 */ + "operation-attributes-tag", + /* 0x01 */ + "job-attributes-tag", /* 0x02 */ + "end-of-attributes-tag", + /* 0x03 */ + "printer-attributes-tag", + /* 0x04 */ + "unsupported-attributes-tag", + /* 0x05 */ + "subscription-attributes-tag", + /* 0x06 */ + "event-notification-attributes-tag", + /* 0x07 */ + "(resource-attributes-tag)", + /* 0x08 */ + "document-attributes-tag", + /* 0x09 */ + "0x0a", /* 0x0a */ + "0x0b", /* 0x0b */ + "0x0c", /* 0x0c */ + "0x0d", /* 0x0d */ + "0x0e", /* 0x0e */ + "0x0f", /* 0x0f */ + "unsupported", /* 0x10 */ + "default", /* 0x11 */ + "unknown", /* 0x12 */ + "no-value", /* 0x13 */ + "0x14", /* 0x14 */ + "not-settable", /* 0x15 */ + "delete-attribute", /* 0x16 */ + "admin-define", /* 0x17 */ + "0x18", /* 0x18 */ + "0x19", /* 0x19 */ + "0x1a", /* 0x1a */ + "0x1b", /* 0x1b */ + "0x1c", /* 0x1c */ + "0x1d", /* 0x1d */ + "0x1e", /* 0x1e */ + "0x1f", /* 0x1f */ + "0x20", /* 0x20 */ + "integer", /* 0x21 */ + "boolean", /* 0x22 */ + "enum", /* 0x23 */ + "0x24", /* 0x24 */ + "0x25", /* 0x25 */ + "0x26", /* 0x26 */ + "0x27", /* 0x27 */ + "0x28", /* 0x28 */ + "0x29", /* 0x29 */ + "0x2a", /* 0x2a */ + "0x2b", /* 0x2b */ + "0x2c", /* 0x2c */ + "0x2d", /* 0x2d */ + "0x2e", /* 0x2e */ + "0x2f", /* 0x2f */ + "octetString", /* 0x30 */ + "dateTime", /* 0x31 */ + "resolution", /* 0x32 */ + "rangeOfInteger", /* 0x33 */ + "collection", /* 0x34 */ + "textWithLanguage", /* 0x35 */ + "nameWithLanguage", /* 0x36 */ + "endCollection", /* 0x37 */ + "0x38", /* 0x38 */ + "0x39", /* 0x39 */ + "0x3a", /* 0x3a */ + "0x3b", /* 0x3b */ + "0x3c", /* 0x3c */ + "0x3d", /* 0x3d */ + "0x3e", /* 0x3e */ + "0x3f", /* 0x3f */ + "0x40", /* 0x40 */ + "textWithoutLanguage",/* 0x41 */ + "nameWithoutLanguage",/* 0x42 */ + "0x43", /* 0x43 */ + "keyword", /* 0x44 */ + "uri", /* 0x45 */ + "uriScheme", /* 0x46 */ + "charset", /* 0x47 */ + "naturalLanguage", /* 0x48 */ + "mimeMediaType", /* 0x49 */ + "memberAttrName" /* 0x4a */ + }; +static const char * const ipp_document_states[] = + { /* document-state-enums */ + "pending", + "4", + "processing", + "6", + "canceled", + "aborted", + "completed" + }, + * const ipp_finishings[] = + { /* finishings enums */ + "none", + "staple", + "punch", + "cover", + "bind", + "saddle-stitch", + "edge-stitch", + "fold", + "trim", + "bale", + "booklet-maker", + "jog-offset", + "15", + "16", + "17", + "18", + "19", + "staple-top-left", + "staple-bottom-left", + "staple-top-right", + "staple-bottom-right", + "edge-stitch-left", + "edge-stitch-top", + "edge-stitch-right", + "edge-stitch-bottom", + "staple-dual-left", + "staple-dual-top", + "staple-dual-right", + "staple-dual-bottom", + "32", + "33", + "34", + "35", + "36", + "37", + "38", + "39", + "40", + "41", + "42", + "43", + "44", + "45", + "46", + "47", + "48", + "49", + "bind-left", + "bind-top", + "bind-right", + "bind-bottom", + "54", + "55", + "56", + "57", + "58", + "59", + "trim-after-pages", + "trim-after-documents", + "trim-after-copies", + "trim-after-job" + }, + * const ipp_job_collation_types[] = + { /* job-collation-type enums */ + "uncollated-sheets", + "collated-documents", + "uncollated-documents" + }, + * const ipp_job_states[] = + { /* job-state enums */ + "pending", + "pending-held", + "processing", + "processing-stopped", + "canceled", + "aborted", + "completed" + }, + * const ipp_orientation_requesteds[] = + { /* orientation-requested enums */ + "portrait", + "landscape", + "reverse-landscape", + "reverse-portrait" + }, + * const ipp_print_qualities[] = + { /* print-quality enums */ + "draft", + "normal", + "high" + }, + * const ipp_printer_states[] = + { /* printer-state enums */ + "idle", + "processing", + "stopped", + }; + + +/* + * Local functions... + */ + +static size_t ipp_col_string(ipp_t *col, char *buffer, size_t bufsize); + + +/* + * 'ippAttributeString()' - Convert the attribute's value to a string. + * + * Returns the number of bytes that would be written, not including the + * trailing nul. The buffer pointer can be NULL to get the required length, + * just like (v)snprintf. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +size_t /* O - Number of bytes less nul */ +ippAttributeString( + ipp_attribute_t *attr, /* I - Attribute */ + char *buffer, /* I - String buffer or NULL */ + size_t bufsize) /* I - Size of string buffer */ +{ + int i; /* Looping var */ + char *bufptr, /* Pointer into buffer */ + *bufend, /* End of buffer */ + temp[256]; /* Temporary string */ + const char *ptr; /* Pointer into string */ + _ipp_value_t *val; /* Current value */ + + + if (!attr || !attr->name) + { + if (buffer) + *buffer = '\0'; + + return (0); + } + + bufptr = buffer; + if (buffer) + bufend = buffer + bufsize - 1; + else + bufend = NULL; + + for (i = attr->num_values, val = attr->values; i > 0; i --, val ++) + { + if (val > attr->values) + { + if (buffer && bufptr < bufend) + *bufptr++ = ','; + else + bufptr ++; + } + + switch (attr->value_tag & ~IPP_TAG_COPY) + { + case IPP_TAG_ENUM : + ptr = ippEnumString(attr->name, val->integer); + + if (buffer && bufptr < bufend) + strlcpy(bufptr, ptr, bufend - bufptr + 1); + + bufptr += strlen(ptr); + break; + + case IPP_TAG_INTEGER : + if (buffer && bufptr < bufend) + bufptr += snprintf(bufptr, bufend - bufptr + 1, "%d", val->integer); + else + bufptr += snprintf(temp, sizeof(temp), "%d", val->integer); + break; + + case IPP_TAG_BOOLEAN : + if (buffer && bufptr < bufend) + strlcpy(bufptr, val->boolean ? "true" : "false", + bufend - bufptr + 1); + + bufptr += val->boolean ? 4 : 5; + break; + + case IPP_TAG_RANGE : + if (buffer && bufptr < bufend) + bufptr += snprintf(bufptr, bufend - bufptr + 1, "%d-%d", + val->range.lower, val->range.upper); + else + bufptr += snprintf(temp, sizeof(temp), "%d-%d", val->range.lower, + val->range.upper); + break; + + case IPP_TAG_RESOLUTION : + if (buffer && bufptr < bufend) + bufptr += snprintf(bufptr, bufend - bufptr + 1, "%dx%d%s", + val->resolution.xres, val->resolution.yres, + val->resolution.units == IPP_RES_PER_INCH ? + "dpi" : "dpcm"); + else + bufptr += snprintf(temp, sizeof(temp), "%dx%d%s", + val->resolution.xres, val->resolution.yres, + val->resolution.units == IPP_RES_PER_INCH ? + "dpi" : "dpcm"); + break; + + case IPP_TAG_DATE : + { + unsigned year; /* Year */ + + year = (val->date[0] << 8) + val->date[1]; + + if (val->date[9] == 0 && val->date[10] == 0) + snprintf(temp, sizeof(temp), "%04u-%02u-%02uT%02u:%02u:%02uZ", + year, val->date[2], val->date[3], val->date[4], + val->date[5], val->date[6]); + else + snprintf(temp, sizeof(temp), + "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u", + year, val->date[2], val->date[3], val->date[4], + val->date[5], val->date[6], val->date[8], val->date[9], + val->date[10]); + + if (buffer && bufptr < bufend) + strlcpy(bufptr, temp, bufend - bufptr + 1); + + bufptr += strlen(temp); + } + break; + + case IPP_TAG_TEXT : + case IPP_TAG_NAME : + case IPP_TAG_KEYWORD : + case IPP_TAG_CHARSET : + case IPP_TAG_URI : + case IPP_TAG_URISCHEME : + case IPP_TAG_MIMETYPE : + case IPP_TAG_LANGUAGE : + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + if (!val->string.text) + break; + + for (ptr = val->string.text; *ptr; ptr ++) + { + if (*ptr == '\\' || *ptr == '\"' || *ptr == '[') + { + if (buffer && bufptr < bufend) + *bufptr = '\\'; + bufptr ++; + } + + if (buffer && bufptr < bufend) + *bufptr = *ptr; + bufptr ++; + } + + if (val->string.language) + { + /* + * Add "[language]" to end of string... + */ + + if (buffer && bufptr < bufend) + *bufptr = '['; + bufptr ++; + + if (buffer && bufptr < bufend) + strlcpy(bufptr, val->string.language, bufend - bufptr); + bufptr += strlen(val->string.language); + + if (buffer && bufptr < bufend) + *bufptr = ']'; + bufptr ++; + } + break; + + case IPP_TAG_BEGIN_COLLECTION : + if (buffer && bufptr < bufend) + bufptr += ipp_col_string(val->collection, bufptr, + bufend - bufptr + 1); + else + bufptr += ipp_col_string(val->collection, NULL, 0); + break; + + case IPP_TAG_STRING : + for (ptr = val->string.text; *ptr; ptr ++) + { + if (*ptr == '\\' || _cups_isspace(*ptr)) + { + if (buffer && bufptr < bufend) + *bufptr = '\\'; + bufptr ++; + + if (buffer && bufptr < bufend) + *bufptr = *ptr; + bufptr ++; + } + else if (!isprint(*ptr & 255)) + { + if (buffer && bufptr < bufend) + bufptr += snprintf(bufptr, bufend - bufptr + 1, "\\%03o", + *ptr & 255); + else + bufptr += snprintf(temp, sizeof(temp), "\\%03o", + *ptr & 255); + } + else + { + if (buffer && bufptr < bufend) + *bufptr = *ptr; + bufptr ++; + } + } + break; + + default : + ptr = ippTagString(attr->value_tag); + if (buffer && bufptr < bufend) + strlcpy(bufptr, ptr, bufend - bufptr + 1); + bufptr += strlen(ptr); + break; + } + } + + if (buffer && bufptr < bufend) + *bufptr = '\0'; + else if (bufend) + *bufend = '\0'; + + return (bufptr - buffer); +} + + +/* + * 'ippEnumString()' - Return a string corresponding to the enum value. + */ + +const char * /* O - Enum string */ +ippEnumString(const char *attrname, /* I - Attribute name */ + int enumvalue) /* I - Enum value */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * Check for standard enum values... + */ + + if (!strcmp(attrname, "document-state") && + enumvalue >= 3 && + enumvalue < (3 + (int)(sizeof(ipp_document_states) / + sizeof(ipp_document_states[0])))) + return (ipp_document_states[enumvalue - 3]); + else if ((!strcmp(attrname, "finishings") || + !strcmp(attrname, "finishings-actual") || + !strcmp(attrname, "finishings-default") || + !strcmp(attrname, "finishings-ready") || + !strcmp(attrname, "finishings-supported")) && + enumvalue >= 3 && + enumvalue < (3 + (int)(sizeof(ipp_finishings) / sizeof(ipp_finishings[0])))) + return (ipp_finishings[enumvalue - 3]); + else if ((!strcmp(attrname, "job-collation-type") || + !strcmp(attrname, "job-collation-type-actual")) && + enumvalue >= 3 && + enumvalue < (3 + (int)(sizeof(ipp_job_collation_types) / + sizeof(ipp_job_collation_types[0])))) + return (ipp_job_collation_types[enumvalue - 3]); + else if (!strcmp(attrname, "job-state") && + enumvalue >= IPP_JOB_PENDING && enumvalue <= IPP_JOB_COMPLETED) + return (ipp_job_states[enumvalue - IPP_JOB_PENDING]); + else if (!strcmp(attrname, "operations-supported")) + return (ippOpString((ipp_op_t)enumvalue)); + else if ((!strcmp(attrname, "orientation-requested") || + !strcmp(attrname, "orientation-requested-actual") || + !strcmp(attrname, "orientation-requested-default") || + !strcmp(attrname, "orientation-requested-supported")) && + enumvalue >= 3 && + enumvalue < (3 + (int)(sizeof(ipp_orientation_requesteds) / + sizeof(ipp_orientation_requesteds[0])))) + return (ipp_orientation_requesteds[enumvalue - 3]); + else if ((!strcmp(attrname, "print-quality") || + !strcmp(attrname, "print-quality-actual") || + !strcmp(attrname, "print-quality-default") || + !strcmp(attrname, "print-quality-supported")) && + enumvalue >= 3 && + enumvalue < (3 + (int)(sizeof(ipp_print_qualities) / + sizeof(ipp_print_qualities[0])))) + return (ipp_print_qualities[enumvalue - 3]); + else if (!strcmp(attrname, "printer-state") && + enumvalue >= IPP_PRINTER_IDLE && enumvalue <= IPP_PRINTER_STOPPED) + return (ipp_printer_states[enumvalue - IPP_PRINTER_IDLE]); + + /* + * Not a standard enum value, just return the decimal equivalent... + */ + + snprintf(cg->ipp_unknown, sizeof(cg->ipp_unknown), "%d", enumvalue); + return (cg->ipp_unknown); +} + + +/* + * 'ippEnumValue()' - Return the value associated with a given enum string. + */ + +int /* O - Enum value or -1 if unknown */ +ippEnumValue(const char *attrname, /* I - Attribute name */ + const char *enumstring) /* I - Enum string */ +{ + int i, /* Looping var */ + num_strings; /* Number of strings to compare */ + const char * const *strings; /* Strings to compare */ + + + /* + * If the string is just a number, return it... + */ + + if (isdigit(*enumstring & 255)) + return (strtol(enumstring, NULL, 0)); + + /* + * Otherwise look up the string... + */ + + if (!strcmp(attrname, "document-state")) + { + num_strings = (int)(sizeof(ipp_document_states) / sizeof(ipp_document_states[0])); + strings = ipp_document_states; + } + else if (!strcmp(attrname, "finishings") || + !strcmp(attrname, "finishings-actual") || + !strcmp(attrname, "finishings-default") || + !strcmp(attrname, "finishings-ready") || + !strcmp(attrname, "finishings-supported")) + { + num_strings = (int)(sizeof(ipp_finishings) / sizeof(ipp_finishings[0])); + strings = ipp_finishings; + } + else if (!strcmp(attrname, "job-collation-type") || + !strcmp(attrname, "job-collation-type-actual")) + { + num_strings = (int)(sizeof(ipp_job_collation_types) / + sizeof(ipp_job_collation_types[0])); + strings = ipp_job_collation_types; + } + else if (!strcmp(attrname, "job-state")) + { + num_strings = (int)(sizeof(ipp_job_states) / sizeof(ipp_job_states[0])); + strings = ipp_job_states; + } + else if (!strcmp(attrname, "operations-supported")) + return (ippOpValue(enumstring)); + else if (!strcmp(attrname, "orientation-requested") || + !strcmp(attrname, "orientation-requested-actual") || + !strcmp(attrname, "orientation-requested-default") || + !strcmp(attrname, "orientation-requested-supported")) + { + num_strings = (int)(sizeof(ipp_orientation_requesteds) / + sizeof(ipp_orientation_requesteds[0])); + strings = ipp_orientation_requesteds; + } + else if (!strcmp(attrname, "print-quality") || + !strcmp(attrname, "print-quality-actual") || + !strcmp(attrname, "print-quality-default") || + !strcmp(attrname, "print-quality-supported")) + { + num_strings = (int)(sizeof(ipp_print_qualities) / sizeof(ipp_print_qualities[0])); + strings = ipp_print_qualities; + } + else if (!strcmp(attrname, "printer-state")) + { + num_strings = (int)(sizeof(ipp_printer_states) / sizeof(ipp_printer_states[0])); + strings = ipp_printer_states; + } + else + return (-1); + + for (i = 0; i < num_strings; i ++) + if (!strcmp(enumstring, strings[i])) + return (i + 3); + + return (-1); +} + + +/* + * 'ippErrorString()' - Return a name for the given status code. + */ + +const char * /* O - Text string */ +ippErrorString(ipp_status_t error) /* I - Error status */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * See if the error code is a known value... + */ + + if (error >= IPP_OK && error <= IPP_OK_EVENTS_COMPLETE) + return (ipp_status_oks[error]); + else if (error == IPP_REDIRECTION_OTHER_SITE) + return ("redirection-other-site"); + else if (error == CUPS_SEE_OTHER) + return ("cups-see-other"); + else if (error >= IPP_BAD_REQUEST && error <= IPP_PRINT_SUPPORT_FILE_NOT_FOUND) + return (ipp_status_400s[error - IPP_BAD_REQUEST]); + else if (error >= IPP_INTERNAL_ERROR && error <= IPP_PRINTER_IS_DEACTIVATED) + return (ipp_status_500s[error - IPP_INTERNAL_ERROR]); + else if (error >= IPP_AUTHENTICATION_CANCELED && error <= IPP_UPGRADE_REQUIRED) + return (ipp_status_1000s[error - IPP_AUTHENTICATION_CANCELED]); + + /* + * No, build an "0xxxxx" error string... + */ + + sprintf(cg->ipp_unknown, "0x%04x", error); + + return (cg->ipp_unknown); +} + + +/* + * 'ippErrorValue()' - Return a status code for the given name. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ipp_status_t /* O - IPP status code */ +ippErrorValue(const char *name) /* I - Name */ +{ + int i; + + + for (i = 0; i < (sizeof(ipp_status_oks) / sizeof(ipp_status_oks[0])); i ++) + if (!_cups_strcasecmp(name, ipp_status_oks[i])) + return ((ipp_status_t)i); + + if (!_cups_strcasecmp(name, "redirection-other-site")) + return (IPP_REDIRECTION_OTHER_SITE); + + if (!_cups_strcasecmp(name, "cups-see-other")) + return (CUPS_SEE_OTHER); + + for (i = 0; i < (sizeof(ipp_status_400s) / sizeof(ipp_status_400s[0])); i ++) + if (!_cups_strcasecmp(name, ipp_status_400s[i])) + return ((ipp_status_t)(i + 0x400)); + + for (i = 0; i < (sizeof(ipp_status_500s) / sizeof(ipp_status_500s[0])); i ++) + if (!_cups_strcasecmp(name, ipp_status_500s[i])) + return ((ipp_status_t)(i + 0x500)); + + for (i = 0; i < (sizeof(ipp_status_1000s) / sizeof(ipp_status_1000s[0])); i ++) + if (!_cups_strcasecmp(name, ipp_status_1000s[i])) + return ((ipp_status_t)(i + 0x1000)); + + return ((ipp_status_t)-1); +} + + +/* + * 'ippOpString()' - Return a name for the given operation id. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +const char * /* O - Name */ +ippOpString(ipp_op_t op) /* I - Operation ID */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * See if the operation ID is a known value... + */ + + if (op >= IPP_PRINT_JOB && op <= IPP_CLOSE_JOB) + return (ipp_std_ops[op]); + else if (op == IPP_PRIVATE) + return ("windows-ext"); + else if (op >= CUPS_GET_DEFAULT && op <= CUPS_GET_PPD) + return (ipp_cups_ops[op - CUPS_GET_DEFAULT]); + else if (op == CUPS_GET_DOCUMENT) + return (ipp_cups_ops2[0]); + + /* + * No, build an "0xxxxx" operation string... + */ + + sprintf(cg->ipp_unknown, "0x%04x", op); + + return (cg->ipp_unknown); +} + + +/* + * 'ippOpValue()' - Return an operation id for the given name. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ipp_op_t /* O - Operation ID */ +ippOpValue(const char *name) /* I - Textual name */ +{ + int i; + + + if (!strncmp(name, "0x", 2)) + return ((ipp_op_t)strtol(name + 2, NULL, 16)); + + for (i = 0; i < (sizeof(ipp_std_ops) / sizeof(ipp_std_ops[0])); i ++) + if (!_cups_strcasecmp(name, ipp_std_ops[i])) + return ((ipp_op_t)i); + + if (!_cups_strcasecmp(name, "windows-ext")) + return (IPP_PRIVATE); + + for (i = 0; i < (sizeof(ipp_cups_ops) / sizeof(ipp_cups_ops[0])); i ++) + if (!_cups_strcasecmp(name, ipp_cups_ops[i])) + return ((ipp_op_t)(i + 0x4001)); + + for (i = 0; i < (sizeof(ipp_cups_ops2) / sizeof(ipp_cups_ops2[0])); i ++) + if (!_cups_strcasecmp(name, ipp_cups_ops2[i])) + return ((ipp_op_t)(i + 0x4027)); + + if (!_cups_strcasecmp(name, "CUPS-Add-Class")) + return (CUPS_ADD_MODIFY_CLASS); + + if (!_cups_strcasecmp(name, "CUPS-Add-Printer")) + return (CUPS_ADD_MODIFY_PRINTER); + + return ((ipp_op_t)-1); +} + + +/* + * 'ippPort()' - Return the default IPP port number. + */ + +int /* O - Port number */ +ippPort(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + DEBUG_puts("ippPort()"); + + if (!cg->ipp_port) + _cupsSetDefaults(); + + DEBUG_printf(("1ippPort: Returning %d...", cg->ipp_port)); + + return (cg->ipp_port); +} + + +/* + * 'ippSetPort()' - Set the default port number. + */ + +void +ippSetPort(int p) /* I - Port number to use */ +{ + DEBUG_printf(("ippSetPort(p=%d)", p)); + + _cupsGlobals()->ipp_port = p; +} + + +/* + * 'ippTagString()' - Return the tag name corresponding to a tag value. + * + * The returned names are defined in RFC 2911 and 3382. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +const char * /* O - Tag name */ +ippTagString(ipp_tag_t tag) /* I - Tag value */ +{ + tag &= IPP_TAG_MASK; + + if (tag < (ipp_tag_t)(sizeof(ipp_tag_names) / sizeof(ipp_tag_names[0]))) + return (ipp_tag_names[tag]); + else + return ("UNKNOWN"); +} + + +/* + * 'ippTagValue()' - Return the tag value corresponding to a tag name. + * + * The tag names are defined in RFC 2911 and 3382. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +ipp_tag_t /* O - Tag value */ +ippTagValue(const char *name) /* I - Tag name */ +{ + int i; /* Looping var */ + + + for (i = 0; i < (sizeof(ipp_tag_names) / sizeof(ipp_tag_names[0])); i ++) + if (!_cups_strcasecmp(name, ipp_tag_names[i])) + return ((ipp_tag_t)i); + + if (!_cups_strcasecmp(name, "operation")) + return (IPP_TAG_OPERATION); + else if (!_cups_strcasecmp(name, "job")) + return (IPP_TAG_JOB); + else if (!_cups_strcasecmp(name, "printer")) + return (IPP_TAG_PRINTER); + else if (!_cups_strcasecmp(name, "unsupported")) + return (IPP_TAG_UNSUPPORTED_GROUP); + else if (!_cups_strcasecmp(name, "subscription")) + return (IPP_TAG_SUBSCRIPTION); + else if (!_cups_strcasecmp(name, "event")) + return (IPP_TAG_EVENT_NOTIFICATION); + else if (!_cups_strcasecmp(name, "language")) + return (IPP_TAG_LANGUAGE); + else if (!_cups_strcasecmp(name, "mimetype")) + return (IPP_TAG_MIMETYPE); + else if (!_cups_strcasecmp(name, "name")) + return (IPP_TAG_NAME); + else if (!_cups_strcasecmp(name, "text")) + return (IPP_TAG_TEXT); + else if (!_cups_strcasecmp(name, "begCollection")) + return (IPP_TAG_BEGIN_COLLECTION); + else + return (IPP_TAG_ZERO); +} + + +/* + * 'ipp_col_string()' - Convert a collection to a string. + */ + +static size_t /* O - Number of bytes */ +ipp_col_string(ipp_t *col, /* I - Collection attribute */ + char *buffer, /* I - Buffer or NULL */ + size_t bufsize) /* I - Size of buffer */ +{ + char *bufptr, /* Position in buffer */ + *bufend, /* End of buffer */ + prefix = '{', /* Prefix character */ + temp[256]; /* Temporary string */ + ipp_attribute_t *attr; /* Current member attribute */ + + + bufptr = buffer; + bufend = buffer + bufsize - 1; + + for (attr = col->attrs; attr; attr = attr->next) + { + if (!attr->name) + continue; + + if (buffer && bufptr < bufend) + *bufptr = prefix; + bufptr ++; + prefix = ' '; + + if (buffer && bufptr < bufend) + bufptr += snprintf(bufptr, bufend - bufptr + 1, "%s=", attr->name); + else + bufptr += strlen(attr->name) + 1; + + if (buffer && bufptr < bufend) + bufptr += ippAttributeString(attr, bufptr, bufend - bufptr + 1); + else + bufptr += ippAttributeString(attr, temp, sizeof(temp)); + } + + if (prefix == '{') + { + if (buffer && bufptr < bufend) + *bufptr = prefix; + bufptr ++; + } + + if (buffer && bufptr < bufend) + *bufptr = '}'; + bufptr ++; + + return (bufptr - buffer); +} + + +/* + * End of "$Id: ipp-support.c 9371 2010-11-17 06:21:32Z mike $". + */ diff --git a/cups/ipp.c b/cups/ipp.c new file mode 100644 index 0000000..0384792 --- /dev/null +++ b/cups/ipp.c @@ -0,0 +1,5574 @@ +/* + * "$Id: ipp.c 10102 2011-11-02 23:52:39Z mike $" + * + * Internet Printing Protocol functions for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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. + * + * Contents: + * + * _cupsBufferGet() - Get a read/write buffer. + * _cupsBufferRelease() - Release a read/write buffer. + * ippAddBoolean() - Add a boolean attribute to an IPP message. + * ippAddBooleans() - Add an array of boolean values. + * ippAddCollection() - Add a collection value. + * ippAddCollections() - Add an array of collection values. + * ippAddDate() - Add a date attribute to an IPP message. + * ippAddInteger() - Add a integer attribute to an IPP message. + * ippAddIntegers() - Add an array of integer values. + * ippAddOctetString() - Add an octetString value to an IPP message. + * ippAddOutOfBand() - Add an out-of-band value to an IPP message. + * ippAddRange() - Add a range of values to an IPP message. + * ippAddRanges() - Add ranges of values to an IPP message. + * ippAddResolution() - Add a resolution value to an IPP message. + * ippAddResolutions() - Add resolution values to an IPP message. + * ippAddSeparator() - Add a group separator to an IPP message. + * ippAddString() - Add a language-encoded string to an IPP message. + * ippAddStrings() - Add language-encoded strings to an IPP message. + * ippCopyAttribute() - Copy an attribute. + * ippCopyAttributes() - Copy attributes from one IPP message to another. + * ippDateToTime() - Convert from RFC 1903 Date/Time format to UNIX + * time in seconds. + * ippDelete() - Delete an IPP message. + * ippDeleteAttribute() - Delete a single attribute in an IPP message. + * ippDeleteValues() - Delete values in an attribute. + * ippFindAttribute() - Find a named attribute in a request. + * ippFindNextAttribute() - Find the next named attribute in a request. + * ippFirstAttribute() - Return the first attribute in the message. + * ippGetBoolean() - Get a boolean value for an attribute. + * ippGetCollection() - Get a collection value for an attribute. + * ippGetCount() - Get the number of values in an attribute. + * ippGetDate() - Get a date value for an attribute. + * ippGetGroupTag() - Get the group associated with an attribute. + * ippGetInteger() - Get the integer/enum value for an attribute. + * ippGetName() - Get the attribute name. + * ippGetOperation() - Get the operation ID in an IPP message. + * ippGetRange() - Get a rangeOfInteger value from an attribute. + * ippGetRequestId() - Get the request ID from an IPP message. + * ippGetResolution() - Get a resolution value for an attribute. + * ippGetStatusCode() - Get the status code from an IPP response or event + * message. + * ippGetString() - Get the string and optionally the language code + * for an attribute. + * ippGetValueTag() - Get the value tag for an attribute. + * ippGetVersion() - Get the major and minor version number from an + * IPP message. + * ippLength() - Compute the length of an IPP message. + * ippNextAttribute() - Return the next attribute in the message. + * ippNew() - Allocate a new IPP message. + * ippNewRequest() - Allocate a new IPP request message. + * ippRead() - Read data for an IPP message from a HTTP + * connection. + * ippReadFile() - Read data for an IPP message from a file. + * ippReadIO() - Read data for an IPP message. + * ippSetBoolean() - Set a boolean value in an attribute. + * ippSetCollection() - Set a collection value in an attribute. + * ippSetDate() - Set a date value in an attribute. + * ippSetGroupTag() - Set the group tag of an attribute. + * ippSetInteger() - Set an integer or enum value in an attribute. + * ippSetName() - Set the name of an attribute. + * ippSetOperation() - Set the operation ID in an IPP request message. + * ippSetRange() - Set a rangeOfInteger value in an attribute. + * ippSetRequestId() - Set the request ID in an IPP message. + * ippSetResolution() - Set a resolution value in an attribute. + * ippSetState() - Set the current state of the IPP message. + * ippSetStatusCode() - Set the status code in an IPP response or event + * message. + * ippSetString() - Set a string value in an attribute. + * ippSetValueTag() - Set the value tag of an attribute. + * ippSetVersion() - Set the version number in an IPP message. + * ippTimeToDate() - Convert from UNIX time to RFC 1903 format. + * ippWrite() - Write data for an IPP message to a HTTP + * connection. + * ippWriteFile() - Write data for an IPP message to a file. + * ippWriteIO() - Write data for an IPP message. + * ipp_add_attr() - Add a new attribute to the message. + * ipp_free_values() - Free attribute values. + * ipp_get_code() - Convert a C locale/charset name into an IPP + * language/charset code. + * ipp_lang_code() - Convert a C locale name into an IPP language + * code. + * ipp_length() - Compute the length of an IPP message or + * collection value. + * ipp_read_http() - Semi-blocking read on a HTTP connection... + * ipp_read_file() - Read IPP data from a file. + * ipp_set_value() - Get the value element from an attribute, + * expanding it as needed. + * ipp_write_file() - Write IPP data to a file. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#ifdef WIN32 +# include +#endif /* WIN32 */ + + +/* + * Local functions... + */ + +static ipp_attribute_t *ipp_add_attr(ipp_t *ipp, const char *name, + ipp_tag_t group_tag, ipp_tag_t value_tag, + int num_values); +static void ipp_free_values(ipp_attribute_t *attr, int element, + int count); +static char *ipp_get_code(const char *locale, char *buffer, + size_t bufsize) + __attribute__((nonnull(1,2))); +static char *ipp_lang_code(const char *locale, char *buffer, + size_t bufsize) + __attribute__((nonnull(1,2))); +static size_t ipp_length(ipp_t *ipp, int collection); +static ssize_t ipp_read_http(http_t *http, ipp_uchar_t *buffer, + size_t length); +static ssize_t ipp_read_file(int *fd, ipp_uchar_t *buffer, + size_t length); +static _ipp_value_t *ipp_set_value(ipp_t *ipp, ipp_attribute_t **attr, + int element); +static ssize_t ipp_write_file(int *fd, ipp_uchar_t *buffer, + size_t length); + + +/* + * '_cupsBufferGet()' - Get a read/write buffer. + */ + +char * /* O - Buffer */ +_cupsBufferGet(size_t size) /* I - Size required */ +{ + _cups_buffer_t *buffer; /* Current buffer */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + for (buffer = cg->cups_buffers; buffer; buffer = buffer->next) + if (!buffer->used && buffer->size >= size) + break; + + if (!buffer) + { + if ((buffer = malloc(sizeof(_cups_buffer_t) + size - 1)) == NULL) + return (NULL); + + buffer->next = cg->cups_buffers; + buffer->size = size; + cg->cups_buffers = buffer; + } + + buffer->used = 1; + + return (buffer->d); +} + + +/* + * '_cupsBufferRelease()' - Release a read/write buffer. + */ + +void +_cupsBufferRelease(char *b) /* I - Buffer to release */ +{ + _cups_buffer_t *buffer; /* Buffer */ + + + /* + * Mark this buffer as unused... + */ + + buffer = (_cups_buffer_t *)(b - offsetof(_cups_buffer_t, d)); + buffer->used = 0; +} + + +/* + * 'ippAddBoolean()' - Add a boolean attribute to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddBoolean(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + char value) /* I - Value of attribute */ +{ + ipp_attribute_t *attr; /* New attribute */ + + + DEBUG_printf(("ippAddBoolean(ipp=%p, group=%02x(%s), name=\"%s\", value=%d)", + ipp, group, ippTagString(group), name, value)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, 1)) == NULL) + return (NULL); + + attr->values[0].boolean = value; + + return (attr); +} + + +/* + * 'ippAddBooleans()' - Add an array of boolean values. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddBooleans(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + int num_values, /* I - Number of values */ + const char *values) /* I - Values */ +{ + int i; /* Looping var */ + ipp_attribute_t *attr; /* New attribute */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("ippAddBooleans(ipp=%p, group=%02x(%s), name=\"%s\", " + "num_values=%d, values=%p)", ipp, group, ippTagString(group), + name, num_values, values)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + num_values < 1) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, num_values)) == NULL) + return (NULL); + + if (values) + { + for (i = num_values, value = attr->values; + i > 0; + i --, value ++) + value->boolean = *values++; + } + + return (attr); +} + + +/* + * 'ippAddCollection()' - Add a collection value. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddCollection(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + ipp_t *value) /* I - Value */ +{ + ipp_attribute_t *attr; /* New attribute */ + + + DEBUG_printf(("ippAddCollection(ipp=%p, group=%02x(%s), name=\"%s\", " + "value=%p)", ipp, group, ippTagString(group), name, value)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION, 1)) == NULL) + return (NULL); + + attr->values[0].collection = value; + + if (value) + value->use ++; + + return (attr); +} + + +/* + * 'ippAddCollections()' - Add an array of collection values. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddCollections( + ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + int num_values, /* I - Number of values */ + const ipp_t **values) /* I - Values */ +{ + int i; /* Looping var */ + ipp_attribute_t *attr; /* New attribute */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("ippAddCollections(ipp=%p, group=%02x(%s), name=\"%s\", " + "num_values=%d, values=%p)", ipp, group, ippTagString(group), + name, num_values, values)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + num_values < 1) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION, + num_values)) == NULL) + return (NULL); + + if (values) + { + for (i = num_values, value = attr->values; + i > 0; + i --, value ++) + { + value->collection = (ipp_t *)*values++; + value->collection->use ++; + } + } + + return (attr); +} + + +/* + * 'ippAddDate()' - Add a date attribute to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddDate(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + const ipp_uchar_t *value) /* I - Value */ +{ + ipp_attribute_t *attr; /* New attribute */ + + + DEBUG_printf(("ippAddDate(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)", + ipp, group, ippTagString(group), name, value)); + + /* + * Range check input... + */ + + if (!ipp || !name || !value || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_DATE, 1)) == NULL) + return (NULL); + + memcpy(attr->values[0].date, value, 11); + + return (attr); +} + + +/* + * 'ippAddInteger()' - Add a integer attribute to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * Supported values include enum (@code IPP_TAG_ENUM@) and integer + * (@code IPP_TAG_INTEGER@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddInteger(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + ipp_tag_t value_tag, /* I - Type of attribute */ + const char *name, /* I - Name of attribute */ + int value) /* I - Value of attribute */ +{ + ipp_attribute_t *attr; /* New attribute */ + + + DEBUG_printf(("ippAddInteger(ipp=%p, group=%02x(%s), type=%02x(%s), " + "name=\"%s\", value=%d)", ipp, group, ippTagString(group), + value_tag, ippTagString(value_tag), name, value)); + + value_tag &= IPP_TAG_MASK; + + /* + * Special-case for legacy usage: map out-of-band attributes to new ippAddOutOfBand + * function... + */ + + if (value_tag >= IPP_TAG_UNSUPPORTED_VALUE && value_tag <= IPP_TAG_ADMINDEFINE) + return (ippAddOutOfBand(ipp, group, value_tag, name)); + + /* + * Range check input... + */ + +#if 0 + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + (value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM)) + return (NULL); +#else + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE) + return (NULL); +#endif /* 0 */ + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL) + return (NULL); + + attr->values[0].integer = value; + + return (attr); +} + + +/* + * 'ippAddIntegers()' - Add an array of integer values. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * Supported values include enum (@code IPP_TAG_ENUM@) and integer + * (@code IPP_TAG_INTEGER@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddIntegers(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + ipp_tag_t value_tag, /* I - Type of attribute */ + const char *name, /* I - Name of attribute */ + int num_values, /* I - Number of values */ + const int *values) /* I - Values */ +{ + int i; /* Looping var */ + ipp_attribute_t *attr; /* New attribute */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("ippAddIntegers(ipp=%p, group=%02x(%s), type=%02x(%s), " + "name=\"%s\", num_values=%d, values=%p)", ipp, + group, ippTagString(group), value_tag, ippTagString(value_tag), name, + num_values, values)); + + value_tag &= IPP_TAG_MASK; + + /* + * Range check input... + */ + +#if 0 + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + (value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM) || + num_values < 1) + return (NULL); +#else + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + num_values < 1) + return (NULL); +#endif /* 0 */ + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL) + return (NULL); + + if (values) + { + for (i = num_values, value = attr->values; + i > 0; + i --, value ++) + value->integer = *values++; + } + + return (attr); +} + + +/* + * 'ippAddOctetString()' - Add an octetString value to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddOctetString(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + const void *data, /* I - octetString data */ + int datalen) /* I - Length of data in bytes */ +{ + ipp_attribute_t *attr; /* New attribute */ + + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE) + return (NULL); + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_STRING, 1)) == NULL) + return (NULL); + + /* + * Initialize the attribute data... + */ + + attr->values[0].unknown.length = datalen; + + if (data) + { + if ((attr->values[0].unknown.data = malloc(datalen)) == NULL) + { + ippDeleteAttribute(ipp, attr); + return (NULL); + } + + memcpy(attr->values[0].unknown.data, data, datalen); + } + + /* + * Return the new attribute... + */ + + return (attr); +} + + +/* + * 'ippAddOutOfBand()' - Add an out-of-band value to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * Supported out-of-band values include unsupported-value + * (@code IPP_TAG_UNSUPPORTED_VALUE@), default (@code IPP_TAG_DEFAULT@), unknown + * (@code IPP_TAG_UNKNOWN@), no-value (@code IPP_TAG_NOVALUE@), not-settable + * (@code IPP_TAG_NOTSETTABLE@), delete-attribute (@code IPP_TAG_DELETEATTR@), and + * admin-define (@code IPP_TAG_ADMINDEFINE@). + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddOutOfBand(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + ipp_tag_t value_tag, /* I - Type of attribute */ + const char *name) /* I - Name of attribute */ +{ + DEBUG_printf(("ippAddOutOfBand(ipp=%p, group=%02x(%s), value_tag=%02x(%s), " + "name=\"%s\")", ipp, group, ippTagString(group), value_tag, + ippTagString(value_tag), name)); + + value_tag &= IPP_TAG_MASK; + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + (value_tag != IPP_TAG_UNSUPPORTED_VALUE && + value_tag != IPP_TAG_DEFAULT && + value_tag != IPP_TAG_UNKNOWN && + value_tag != IPP_TAG_NOVALUE && + value_tag != IPP_TAG_NOTSETTABLE && + value_tag != IPP_TAG_DELETEATTR && + value_tag != IPP_TAG_ADMINDEFINE)) + return (NULL); + + /* + * Create the attribute... + */ + + return (ipp_add_attr(ipp, name, group, value_tag, 1)); +} + + +/* + * 'ippAddRange()' - Add a range of values to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * The @code lower@ parameter must be less than or equal to the @code upper@ parameter. + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddRange(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + int lower, /* I - Lower value */ + int upper) /* I - Upper value */ +{ + ipp_attribute_t *attr; /* New attribute */ + + + DEBUG_printf(("ippAddRange(ipp=%p, group=%02x(%s), name=\"%s\", lower=%d, " + "upper=%d)", ipp, group, ippTagString(group), name, lower, + upper)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, 1)) == NULL) + return (NULL); + + attr->values[0].range.lower = lower; + attr->values[0].range.upper = upper; + + return (attr); +} + + +/* + * 'ippAddRanges()' - Add ranges of values to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddRanges(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + int num_values, /* I - Number of values */ + const int *lower, /* I - Lower values */ + const int *upper) /* I - Upper values */ +{ + int i; /* Looping var */ + ipp_attribute_t *attr; /* New attribute */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("ippAddRanges(ipp=%p, group=%02x(%s), name=\"%s\", " + "num_values=%d, lower=%p, upper=%p)", ipp, group, + ippTagString(group), name, num_values, lower, upper)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + num_values < 1) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, num_values)) == NULL) + return (NULL); + + if (lower && upper) + { + for (i = num_values, value = attr->values; + i > 0; + i --, value ++) + { + value->range.lower = *lower++; + value->range.upper = *upper++; + } + } + + return (attr); +} + + +/* + * 'ippAddResolution()' - Add a resolution value to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddResolution(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + ipp_res_t units, /* I - Units for resolution */ + int xres, /* I - X resolution */ + int yres) /* I - Y resolution */ +{ + ipp_attribute_t *attr; /* New attribute */ + + + DEBUG_printf(("ippAddResolution(ipp=%p, group=%02x(%s), name=\"%s\", " + "units=%d, xres=%d, yres=%d)", ipp, group, + ippTagString(group), name, units, xres, yres)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM || + xres < 0 || yres < 0) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, 1)) == NULL) + return (NULL); + + attr->values[0].resolution.xres = xres; + attr->values[0].resolution.yres = yres; + attr->values[0].resolution.units = units; + + return (attr); +} + + +/* + * 'ippAddResolutions()' - Add resolution values to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddResolutions(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + const char *name, /* I - Name of attribute */ + int num_values,/* I - Number of values */ + ipp_res_t units, /* I - Units for resolution */ + const int *xres, /* I - X resolutions */ + const int *yres) /* I - Y resolutions */ +{ + int i; /* Looping var */ + ipp_attribute_t *attr; /* New attribute */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("ippAddResolutions(ipp=%p, group=%02x(%s), name=\"%s\", " + "num_value=%d, units=%d, xres=%p, yres=%p)", ipp, group, + ippTagString(group), name, num_values, units, xres, yres)); + + /* + * Range check input... + */ + + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + num_values < 1 || + units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM) + return (NULL); + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, num_values)) == NULL) + return (NULL); + + if (xres && yres) + { + for (i = num_values, value = attr->values; + i > 0; + i --, value ++) + { + value->resolution.xres = *xres++; + value->resolution.yres = *yres++; + value->resolution.units = units; + } + } + + return (attr); +} + + +/* + * 'ippAddSeparator()' - Add a group separator to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddSeparator(ipp_t *ipp) /* I - IPP message */ +{ + DEBUG_printf(("ippAddSeparator(ipp=%p)", ipp)); + + /* + * Range check input... + */ + + if (!ipp) + return (NULL); + + /* + * Create the attribute... + */ + + return (ipp_add_attr(ipp, NULL, IPP_TAG_ZERO, IPP_TAG_ZERO, 0)); +} + + +/* + * 'ippAddString()' - Add a language-encoded string to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * Supported string values include charset (@code IPP_TAG_CHARSET@), keyword + * (@code IPP_TAG_KEYWORD@), language (@code IPP_TAG_LANGUAGE@), mimeMediaType + * (@code IPP_TAG_MIMETYPE@), name (@code IPP_TAG_NAME@), nameWithLanguage + * (@code IPP_TAG_NAMELANG), text (@code IPP_TAG_TEXT@), textWithLanguage + * (@code IPP_TAG_TEXTLANG@), uri (@code IPP_TAG_URI@), and uriScheme + * (@code IPP_TAG_URISCHEME@). + * + * The @code language@ parameter must be non-@code NULL@ for nameWithLanguage and + * textWithLanguage string values and must be @code NULL@ for all other string values. + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddString(ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + ipp_tag_t value_tag, /* I - Type of attribute */ + const char *name, /* I - Name of attribute */ + const char *language, /* I - Language code */ + const char *value) /* I - Value */ +{ + ipp_tag_t temp_tag; /* Temporary value tag (masked) */ + ipp_attribute_t *attr; /* New attribute */ + char code[32]; /* Charset/language code buffer */ + + + DEBUG_printf(("ippAddString(ipp=%p, group=%02x(%s), value_tag=%02x(%s), " + "name=\"%s\", language=\"%s\", value=\"%s\")", ipp, + group, ippTagString(group), value_tag, ippTagString(value_tag), name, + language, value)); + + /* + * Range check input... + */ + + temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_MASK); + +#if 0 + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + (temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG && + temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE) + return (NULL); + + if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG) + != (language != NULL)) + return (NULL); +#else + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE) + return (NULL); +#endif /* 0 */ + + /* + * See if we need to map charset, language, or locale values... + */ + + if (language && ((int)value_tag & IPP_TAG_COPY) && + strcmp(language, ipp_lang_code(language, code, sizeof(code)))) + value_tag = temp_tag; /* Don't do a fast copy */ + else if (value && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_COPY) && + strcmp(value, ipp_get_code(value, code, sizeof(code)))) + value_tag = temp_tag; /* Don't do a fast copy */ + else if (value && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_COPY) && + strcmp(value, ipp_lang_code(value, code, sizeof(code)))) + value_tag = temp_tag; /* Don't do a fast copy */ + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL) + return (NULL); + + /* + * Initialize the attribute data... + */ + + if ((int)value_tag & IPP_TAG_COPY) + { + attr->values[0].string.language = (char *)language; + attr->values[0].string.text = (char *)value; + } + else + { + if (language) + attr->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language, code, + sizeof(code))); + + if (value) + { + if (value_tag == IPP_TAG_CHARSET) + attr->values[0].string.text = _cupsStrAlloc(ipp_get_code(value, code, + sizeof(code))); + else if (value_tag == IPP_TAG_LANGUAGE) + attr->values[0].string.text = _cupsStrAlloc(ipp_lang_code(value, code, + sizeof(code))); + else + attr->values[0].string.text = _cupsStrAlloc(value); + } + } + + return (attr); +} + + +/* + * 'ippAddStrings()' - Add language-encoded strings to an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * Supported string values include charset (@code IPP_TAG_CHARSET@), keyword + * (@code IPP_TAG_KEYWORD@), language (@code IPP_TAG_LANGUAGE@), mimeMediaType + * (@code IPP_TAG_MIMETYPE@), name (@code IPP_TAG_NAME@), nameWithLanguage + * (@code IPP_TAG_NAMELANG), text (@code IPP_TAG_TEXT@), textWithLanguage + * (@code IPP_TAG_TEXTLANG@), uri (@code IPP_TAG_URI@), and uriScheme + * (@code IPP_TAG_URISCHEME@). + * + * The @code language@ parameter must be non-@code NULL@ for nameWithLanguage and + * textWithLanguage string values and must be @code NULL@ for all other string values. + */ + +ipp_attribute_t * /* O - New attribute */ +ippAddStrings( + ipp_t *ipp, /* I - IPP message */ + ipp_tag_t group, /* I - IPP group */ + ipp_tag_t value_tag, /* I - Type of attribute */ + const char *name, /* I - Name of attribute */ + int num_values, /* I - Number of values */ + const char *language, /* I - Language code (@code NULL@ for default) */ + const char * const *values) /* I - Values */ +{ + int i; /* Looping var */ + ipp_tag_t temp_tag; /* Temporary value tag (masked) */ + ipp_attribute_t *attr; /* New attribute */ + _ipp_value_t *value; /* Current value */ + char code[32]; /* Language/charset value buffer */ + + + DEBUG_printf(("ippAddStrings(ipp=%p, group=%02x(%s), value_tag=%02x(%s), " + "name=\"%s\", num_values=%d, language=\"%s\", values=%p)", ipp, + group, ippTagString(group), value_tag, ippTagString(value_tag), name, + num_values, language, values)); + + /* + * Range check input... + */ + + temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_MASK); + +#if 0 + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + (temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG && + temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE || + num_values < 1) + return (NULL); + + if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG) + != (language != NULL)) + return (NULL); +#else + if (!ipp || !name || group < IPP_TAG_ZERO || + group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || + num_values < 1) + return (NULL); +#endif /* 0 */ + + /* + * See if we need to map charset, language, or locale values... + */ + + if (language && ((int)value_tag & IPP_TAG_COPY) && + strcmp(language, ipp_lang_code(language, code, sizeof(code)))) + value_tag = temp_tag; /* Don't do a fast copy */ + else if (values && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_COPY)) + { + for (i = 0; i < num_values; i ++) + if (strcmp(values[i], ipp_get_code(values[i], code, sizeof(code)))) + { + value_tag = temp_tag; /* Don't do a fast copy */ + break; + } + } + else if (values && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_COPY)) + { + for (i = 0; i < num_values; i ++) + if (strcmp(values[i], ipp_lang_code(values[i], code, sizeof(code)))) + { + value_tag = temp_tag; /* Don't do a fast copy */ + break; + } + } + + /* + * Create the attribute... + */ + + if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL) + return (NULL); + + /* + * Initialize the attribute data... + */ + + for (i = num_values, value = attr->values; + i > 0; + i --, value ++) + { + if (language) + { + if (value == attr->values) + { + if ((int)value_tag & IPP_TAG_COPY) + value->string.language = (char *)language; + else + value->string.language = _cupsStrAlloc(ipp_lang_code(language, code, + sizeof(code))); + } + else + value->string.language = attr->values[0].string.language; + } + + if (values) + { + if ((int)value_tag & IPP_TAG_COPY) + value->string.text = (char *)*values++; + else if (value_tag == IPP_TAG_CHARSET) + value->string.text = _cupsStrAlloc(ipp_get_code(*values++, code, sizeof(code))); + else if (value_tag == IPP_TAG_LANGUAGE) + value->string.text = _cupsStrAlloc(ipp_lang_code(*values++, code, sizeof(code))); + else + value->string.text = _cupsStrAlloc(*values++); + } + } + + return (attr); +} + + +/* + * 'ippCopyAttribute()' - Copy an attribute. + * + * The specified attribute, @code attr@, is copied to the destination IPP message. + * When @code quickcopy@ is non-zero, a "shallow" reference copy of the attribute is + * created - this should only be done as long as the original source IPP message will + * not be freed for the life of the destination. + * + * @since CUPS 1.6/OS X 10.8@ + */ + + +ipp_attribute_t * /* O - New attribute */ +ippCopyAttribute( + ipp_t *dst, /* I - Destination IPP message */ + ipp_attribute_t *srcattr, /* I - Attribute to copy */ + int quickcopy) /* I - 1 for a referenced copy, 0 for normal */ +{ + int i; /* Looping var */ + ipp_attribute_t *dstattr; /* Destination attribute */ + _ipp_value_t *srcval, /* Source value */ + *dstval; /* Destination value */ + + + DEBUG_printf(("ippCopyAttribute(dst=%p, srcattr=%p, quickcopy=%d)", dst, srcattr, + quickcopy)); + + /* + * Range check input... + */ + + if (!dst || !srcattr) + return (NULL); + + /* + * Copy it... + */ + + quickcopy = quickcopy ? IPP_TAG_COPY : 0; + + switch (srcattr->value_tag & ~IPP_TAG_COPY) + { + case IPP_TAG_ZERO : + dstattr = ippAddSeparator(dst); + break; + + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + dstattr = ippAddIntegers(dst, srcattr->group_tag, srcattr->value_tag, + srcattr->name, srcattr->num_values, NULL); + if (!dstattr) + break; + + for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + dstval->integer = srcval->integer; + break; + + case IPP_TAG_BOOLEAN : + dstattr = ippAddBooleans(dst, srcattr->group_tag, srcattr->name, + srcattr->num_values, NULL); + if (!dstattr) + break; + + for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + dstval->boolean = srcval->boolean; + break; + + 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 : + dstattr = ippAddStrings(dst, srcattr->group_tag, + (ipp_tag_t)(srcattr->value_tag | quickcopy), + srcattr->name, srcattr->num_values, NULL, NULL); + if (!dstattr) + break; + + if (quickcopy) + { + for (i = srcattr->num_values, srcval = srcattr->values, + dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + dstval->string.text = srcval->string.text; + } + else if (srcattr->value_tag & IPP_TAG_COPY) + { + for (i = srcattr->num_values, srcval = srcattr->values, + dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + dstval->string.text = _cupsStrAlloc(srcval->string.text); + } + else + { + for (i = srcattr->num_values, srcval = srcattr->values, + dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + dstval->string.text = _cupsStrRetain(srcval->string.text); + } + break; + + case IPP_TAG_DATE : + if (srcattr->num_values != 1) + return (NULL); + + dstattr = ippAddDate(dst, srcattr->group_tag, srcattr->name, + srcattr->values[0].date); + break; + + case IPP_TAG_RESOLUTION : + dstattr = ippAddResolutions(dst, srcattr->group_tag, srcattr->name, + srcattr->num_values, IPP_RES_PER_INCH, + NULL, NULL); + if (!dstattr) + break; + + for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + { + dstval->resolution.xres = srcval->resolution.xres; + dstval->resolution.yres = srcval->resolution.yres; + dstval->resolution.units = srcval->resolution.units; + } + break; + + case IPP_TAG_RANGE : + dstattr = ippAddRanges(dst, srcattr->group_tag, srcattr->name, + srcattr->num_values, NULL, NULL); + if (!dstattr) + break; + + for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + { + dstval->range.lower = srcval->range.lower; + dstval->range.upper = srcval->range.upper; + } + break; + + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + dstattr = ippAddStrings(dst, srcattr->group_tag, + (ipp_tag_t)(srcattr->value_tag | quickcopy), + srcattr->name, srcattr->num_values, NULL, NULL); + if (!dstattr) + break; + + if (quickcopy) + { + for (i = srcattr->num_values, srcval = srcattr->values, + dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + { + dstval->string.language = srcval->string.language; + dstval->string.text = srcval->string.text; + } + } + else if (srcattr->value_tag & IPP_TAG_COPY) + { + for (i = srcattr->num_values, srcval = srcattr->values, + dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + { + if (srcval == srcattr->values) + dstval->string.language = _cupsStrAlloc(srcval->string.language); + else + dstval->string.language = dstattr->values[0].string.language; + + dstval->string.text = _cupsStrAlloc(srcval->string.text); + } + } + else + { + for (i = srcattr->num_values, srcval = srcattr->values, + dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + { + if (srcval == srcattr->values) + dstval->string.language = _cupsStrRetain(srcval->string.language); + else + dstval->string.language = dstattr->values[0].string.language; + + dstval->string.text = _cupsStrRetain(srcval->string.text); + } + } + break; + + case IPP_TAG_BEGIN_COLLECTION : + dstattr = ippAddCollections(dst, srcattr->group_tag, srcattr->name, + srcattr->num_values, NULL); + if (!dstattr) + break; + + for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + { + dstval->collection = srcval->collection; + srcval->collection->use ++; + } + break; + + case IPP_TAG_STRING : + default : + /* TODO: Implement quick copy for unknown/octetString values */ + dstattr = ippAddIntegers(dst, srcattr->group_tag, srcattr->value_tag, + srcattr->name, srcattr->num_values, NULL); + if (!dstattr) + break; + + for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; + i > 0; + i --, srcval ++, dstval ++) + { + dstval->unknown.length = srcval->unknown.length; + + if (dstval->unknown.length > 0) + { + if ((dstval->unknown.data = malloc(dstval->unknown.length)) == NULL) + dstval->unknown.length = 0; + else + memcpy(dstval->unknown.data, srcval->unknown.data, dstval->unknown.length); + } + } + break; /* anti-compiler-warning-code */ + } + + return (dstattr); +} + + +/* + * 'ippCopyAttributes()' - Copy attributes from one IPP message to another. + * + * Zero or more attributes are copied from the source IPP message, @code@ src, to the + * destination IPP message, @code dst@. When @code quickcopy@ is non-zero, a "shallow" + * reference copy of the attribute is created - this should only be done as long as the + * original source IPP message will not be freed for the life of the destination. + * + * The @code cb@ and @code context@ parameters provide a generic way to "filter" the + * attributes that are copied - the function must return 1 to copy the attribute or + * 0 to skip it. The function may also choose to do a partial copy of the source attribute + * itself. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on error */ +ippCopyAttributes( + ipp_t *dst, /* I - Destination IPP message */ + ipp_t *src, /* I - Source IPP message */ + int quickcopy, /* I - 1 for a referenced copy, 0 for normal */ + ipp_copycb_t cb, /* I - Copy callback or @code NULL@ for none */ + void *context) /* I - Context pointer */ +{ + ipp_attribute_t *srcattr; /* Source attribute */ + + + DEBUG_printf(("ippCopyAttributes(dst=%p, src=%p, quickcopy=%d, cb=%p, context=%p)", + dst, src, quickcopy, cb, context)); + + /* + * Range check input... + */ + + if (!dst || !src) + return (0); + + /* + * Loop through source attributes and copy as needed... + */ + + for (srcattr = src->attrs; srcattr; srcattr = srcattr->next) + if (!cb || (*cb)(context, dst, srcattr)) + if (!ippCopyAttribute(dst, srcattr, quickcopy)) + return (0); + + return (1); +} + + +/* + * 'ippDateToTime()' - Convert from RFC 1903 Date/Time format to UNIX time + * in seconds. + */ + +time_t /* O - UNIX time value */ +ippDateToTime(const ipp_uchar_t *date) /* I - RFC 1903 date info */ +{ + struct tm unixdate; /* UNIX date/time info */ + time_t t; /* Computed time */ + + + if (!date) + return (0); + + memset(&unixdate, 0, sizeof(unixdate)); + + /* + * RFC-1903 date/time format is: + * + * Byte(s) Description + * ------- ----------- + * 0-1 Year (0 to 65535) + * 2 Month (1 to 12) + * 3 Day (1 to 31) + * 4 Hours (0 to 23) + * 5 Minutes (0 to 59) + * 6 Seconds (0 to 60, 60 = "leap second") + * 7 Deciseconds (0 to 9) + * 8 +/- UTC + * 9 UTC hours (0 to 11) + * 10 UTC minutes (0 to 59) + */ + + unixdate.tm_year = ((date[0] << 8) | date[1]) - 1900; + unixdate.tm_mon = date[2] - 1; + unixdate.tm_mday = date[3]; + unixdate.tm_hour = date[4]; + unixdate.tm_min = date[5]; + unixdate.tm_sec = date[6]; + + t = mktime(&unixdate); + + if (date[8] == '-') + t += date[9] * 3600 + date[10] * 60; + else + t -= date[9] * 3600 + date[10] * 60; + + return (t); +} + + +/* + * 'ippDelete()' - Delete an IPP message. + */ + +void +ippDelete(ipp_t *ipp) /* I - IPP message */ +{ + ipp_attribute_t *attr, /* Current attribute */ + *next; /* Next attribute */ + + + DEBUG_printf(("ippDelete(ipp=%p)", ipp)); + + if (!ipp) + return; + + ipp->use --; + if (ipp->use > 0) + return; + + for (attr = ipp->attrs; attr != NULL; attr = next) + { + next = attr->next; + + ipp_free_values(attr, 0, attr->num_values); + + if (attr->name) + _cupsStrFree(attr->name); + + free(attr); + } + + free(ipp); +} + + +/* + * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +void +ippDeleteAttribute( + ipp_t *ipp, /* I - IPP message */ + ipp_attribute_t *attr) /* I - Attribute to delete */ +{ + ipp_attribute_t *current, /* Current attribute */ + *prev; /* Previous attribute */ + + + DEBUG_printf(("ippDeleteAttribute(ipp=%p, attr=%p(%s))", ipp, attr, + attr ? attr->name : "(null)")); + + /* + * Range check input... + */ + + if (!attr) + return; + + /* + * Find the attribute in the list... + */ + + if (ipp) + { + for (current = ipp->attrs, prev = NULL; + current; + prev = current, current = current->next) + if (current == attr) + { + /* + * Found it, remove the attribute from the list... + */ + + if (prev) + prev->next = current->next; + else + ipp->attrs = current->next; + + if (current == ipp->last) + ipp->last = prev; + + break; + } + + if (!current) + return; + } + + /* + * Free memory used by the attribute... + */ + + ipp_free_values(attr, 0, attr->num_values); + + if (attr->name) + _cupsStrFree(attr->name); + + free(attr); +} + + +/* + * 'ippDeleteValues()' - Delete values in an attribute. + * + * The @code element@ parameter specifies the first value to delete, starting at + * 0. It must be less than the number of values returned by @link ippGetCount@. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * Deleting all values in an attribute deletes the attribute. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippDeleteValues( + ipp_t *ipp, /* I - IPP message */ + ipp_attribute_t **attr, /* IO - Attribute */ + int element, /* I - Index of first value to delete (0-based) */ + int count) /* I - Number of values to delete */ +{ + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || + element < 0 || element >= (*attr)->num_values || count <= 0 || + (element + count) >= (*attr)->num_values) + return (0); + + /* + * If we are deleting all values, just delete the attribute entirely. + */ + + if (count == (*attr)->num_values) + { + ippDeleteAttribute(ipp, *attr); + *attr = NULL; + return (1); + } + + /* + * Otherwise free the values in question and return. + */ + + ipp_free_values(*attr, element, count); + + return (1); +} + + +/* + * 'ippFindAttribute()' - Find a named attribute in a request. + */ + +ipp_attribute_t * /* O - Matching attribute */ +ippFindAttribute(ipp_t *ipp, /* I - IPP message */ + const char *name, /* I - Name of attribute */ + ipp_tag_t type) /* I - Type of attribute */ +{ + DEBUG_printf(("2ippFindAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", ipp, + name, type, ippTagString(type))); + + if (!ipp || !name) + return (NULL); + + /* + * Reset the current pointer... + */ + + ipp->current = NULL; + + /* + * Search for the attribute... + */ + + return (ippFindNextAttribute(ipp, name, type)); +} + + +/* + * 'ippFindNextAttribute()' - Find the next named attribute in a request. + */ + +ipp_attribute_t * /* O - Matching attribute */ +ippFindNextAttribute(ipp_t *ipp, /* I - IPP message */ + const char *name, /* I - Name of attribute */ + ipp_tag_t type) /* I - Type of attribute */ +{ + ipp_attribute_t *attr; /* Current atttribute */ + ipp_tag_t value_tag; /* Value tag */ + + + DEBUG_printf(("2ippFindNextAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", + ipp, name, type, ippTagString(type))); + + if (!ipp || !name) + return (NULL); + + if (ipp->current) + { + ipp->prev = ipp->current; + attr = ipp->current->next; + } + else + { + ipp->prev = NULL; + attr = ipp->attrs; + } + + for (; attr != NULL; ipp->prev = attr, attr = attr->next) + { + DEBUG_printf(("4ippFindAttribute: attr=%p, name=\"%s\"", attr, + attr->name)); + + value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_MASK); + + if (attr->name != NULL && _cups_strcasecmp(attr->name, name) == 0 && + (value_tag == type || type == IPP_TAG_ZERO || + (value_tag == IPP_TAG_TEXTLANG && type == IPP_TAG_TEXT) || + (value_tag == IPP_TAG_NAMELANG && type == IPP_TAG_NAME))) + { + ipp->current = attr; + + return (attr); + } + } + + ipp->current = NULL; + ipp->prev = NULL; + + return (NULL); +} + + +/* + * 'ippFirstAttribute()' - Return the first attribute in the message. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_attribute_t * /* O - First attribute or @code NULL@ if none */ +ippFirstAttribute(ipp_t *ipp) /* I - IPP message */ +{ + /* + * Range check input... + */ + + if (!ipp) + return (NULL); + + /* + * Return the first attribute... + */ + + return (ipp->current = ipp->attrs); +} + + +/* + * 'ippGetBoolean()' - Get a boolean value for an attribute. + * + * The @code element@ parameter specifies which value to get from 0 to + * @link ippGetCount(attr)@ - 1. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - Boolean value or -1 on error */ +ippGetBoolean(ipp_attribute_t *attr, /* I - IPP attribute */ + int element) /* I - Value number (0-based) */ +{ + /* + * Range check input... + */ + + if (!attr || attr->value_tag != IPP_TAG_BOOLEAN || + element < 0 || element >= attr->num_values) + return (-1); + + /* + * Return the value... + */ + + return (attr->values[element].boolean); +} + + +/* + * 'ippGetCollection()' - Get a collection value for an attribute. + * + * The @code element@ parameter specifies which value to get from 0 to + * @link ippGetCount(attr)@ - 1. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_t * /* O - Collection value or @code NULL@ on error */ +ippGetCollection( + ipp_attribute_t *attr, /* I - IPP attribute */ + int element) /* I - Value number (0-based) */ +{ + /* + * Range check input... + */ + + if (!attr || attr->value_tag != IPP_TAG_BEGIN_COLLECTION || + element < 0 || element >= attr->num_values) + return (NULL); + + /* + * Return the value... + */ + + return (attr->values[element].collection); +} + + +/* + * 'ippGetCount()' - Get the number of values in an attribute. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - Number of values or -1 on error */ +ippGetCount(ipp_attribute_t *attr) /* I - IPP attribute */ +{ + /* + * Range check input... + */ + + if (!attr) + return (-1); + + /* + * Return the number of values... + */ + + return (attr->num_values); +} + + +/* + * 'ippGetDate()' - Get a date value for an attribute. + * + * The @code element@ parameter specifies which value to get from 0 to + * @link ippGetCount(attr)@ - 1. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +const ipp_uchar_t * /* O - Date value or @code NULL@ */ +ippGetDate(ipp_attribute_t *attr, /* I - IPP attribute */ + int element) /* I - Value number (0-based) */ +{ + /* + * Range check input... + */ + + if (!attr || attr->value_tag != IPP_TAG_DATE || + element < 0 || element >= attr->num_values) + return (NULL); + + /* + * Return the value... + */ + + return (attr->values[element].date); +} + + +/* + * 'ippGetGroupTag()' - Get the group associated with an attribute. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_tag_t /* O - Group tag or @code IPP_TAG_ZERO@ on error */ +ippGetGroupTag(ipp_attribute_t *attr) /* I - IPP attribute */ +{ + /* + * Range check input... + */ + + if (!attr) + return (IPP_TAG_ZERO); + + /* + * Return the group... + */ + + return (attr->group_tag); +} + + +/* + * 'ippGetInteger()' - Get the integer/enum value for an attribute. + * + * The @code element@ parameter specifies which value to get from 0 to + * @link ippGetCount(attr)@ - 1. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - Value or -1 on error */ +ippGetInteger(ipp_attribute_t *attr, /* I - IPP attribute */ + int element) /* I - Value number (0-based) */ +{ + /* + * Range check input... + */ + + if (!attr || (attr->value_tag != IPP_TAG_INTEGER && attr->value_tag != IPP_TAG_ENUM) || + element < 0 || element >= attr->num_values) + return (-1); + + /* + * Return the value... + */ + + return (attr->values[element].integer); +} + + +/* + * 'ippGetName()' - Get the attribute name. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +const char * /* O - Attribute name or @code NULL@ for separators */ +ippGetName(ipp_attribute_t *attr) /* I - IPP attribute */ +{ + /* + * Range check input... + */ + + if (!attr) + return (NULL); + + /* + * Return the name... + */ + + return (attr->name); +} + + +/* + * 'ippGetOperation()' - Get the operation ID in an IPP message. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_op_t /* O - Operation ID or -1 on error */ +ippGetOperation(ipp_t *ipp) /* I - IPP request message */ +{ + /* + * Range check input... + */ + + if (!ipp) + return ((ipp_op_t)-1); + + /* + * Return the value... + */ + + return (ipp->request.op.operation_id); +} + + +/* + * 'ippGetRange()' - Get a rangeOfInteger value from an attribute. + * + * The @code element@ parameter specifies which value to get from 0 to + * @link ippGetCount(attr)@ - 1. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - Lower value of range or -1 */ +ippGetRange(ipp_attribute_t *attr, /* I - IPP attribute */ + int element, /* I - Value number (0-based) */ + int *uppervalue)/* O - Upper value of range */ +{ + /* + * Range check input... + */ + + if (!attr || attr->value_tag != IPP_TAG_RANGE || + element < 0 || element >= attr->num_values) + { + if (uppervalue) + *uppervalue = -1; + + return (-1); + } + + /* + * Return the values... + */ + + if (uppervalue) + *uppervalue = attr->values[element].range.upper; + + return (attr->values[element].range.lower); +} + + +/* + * 'ippGetRequestId()' - Get the request ID from an IPP message. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - Request ID or -1 on error */ +ippGetRequestId(ipp_t *ipp) /* I - IPP message */ +{ + /* + * Range check input... + */ + + if (!ipp) + return (-1); + + /* + * Return the request ID... + */ + + return (ipp->request.any.request_id); +} + + +/* + * 'ippGetResolution()' - Get a resolution value for an attribute. + * + * The @code element@ parameter specifies which value to get from 0 to + * @link ippGetCount(attr)@ - 1. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - Horizontal/cross feed resolution or -1 */ +ippGetResolution( + ipp_attribute_t *attr, /* I - IPP attribute */ + int element, /* I - Value number (0-based) */ + int *yres, /* O - Vertical/feed resolution */ + ipp_res_t *units) /* O - Units for resolution */ +{ + /* + * Range check input... + */ + + if (!attr || attr->value_tag != IPP_TAG_RESOLUTION || + element < 0 || element >= attr->num_values) + return (-1); + + /* + * Return the value... + */ + + if (yres) + *yres = attr->values[element].resolution.yres; + + if (units) + *units = attr->values[element].resolution.units; + + return (attr->values[element].resolution.xres); +} + + +/* + * 'ippGetState()' - Get the IPP message state. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_state_t /* O - IPP message state value */ +ippGetState(ipp_t *ipp) /* I - IPP message */ +{ + /* + * Range check input... + */ + + if (!ipp) + return (IPP_IDLE); + + /* + * Return the value... + */ + + return (ipp->state); +} + + +/* + * 'ippGetStatusCode()' - Get the status code from an IPP response or event message. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_status_t /* O - Status code in IPP message */ +ippGetStatusCode(ipp_t *ipp) /* I - IPP response or event message */ +{ + /* + * Range check input... + */ + + if (!ipp) + return (IPP_INTERNAL_ERROR); + + /* + * Return the value... + */ + + return (ipp->request.status.status_code); +} + + +/* + * 'ippGetString()' - Get the string and optionally the language code for an attribute. + * + * The @code element@ parameter specifies which value to get from 0 to + * @link ippGetCount(attr)@ - 1. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +const char * +ippGetString(ipp_attribute_t *attr, /* I - IPP attribute */ + int element, /* I - Value number (0-based) */ + const char **language)/* O - Language code (@code NULL@ for don't care) */ +{ + /* + * Range check input... + */ + + if (!attr || element < 0 || element >= attr->num_values || + (attr->value_tag != IPP_TAG_TEXTLANG && attr->value_tag != IPP_TAG_NAMELANG && + (attr->value_tag < IPP_TAG_TEXT || attr->value_tag > IPP_TAG_MIMETYPE))) + return (NULL); + + /* + * Return the value... + */ + + if (language) + *language = attr->values[element].string.language; + + return (attr->values[element].string.text); +} + + +/* + * 'ippGetValueTag()' - Get the value tag for an attribute. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_tag_t /* O - Value tag or @code IPP_TAG_ZERO@ on error */ +ippGetValueTag(ipp_attribute_t *attr) /* I - IPP attribute */ +{ + /* + * Range check input... + */ + + if (!attr) + return (IPP_TAG_ZERO); + + /* + * Return the value... + */ + + return (attr->value_tag); +} + + +/* + * 'ippGetVersion()' - Get the major and minor version number from an IPP message. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - Major version number or -1 on error */ +ippGetVersion(ipp_t *ipp, /* I - IPP message */ + int *minor) /* O - Minor version number or @code NULL@ */ +{ + /* + * Range check input... + */ + + if (!ipp) + { + if (minor) + *minor = -1; + + return (-1); + } + + /* + * Return the value... + */ + + if (minor) + *minor = ipp->request.any.version[1]; + + return (ipp->request.any.version[0]); +} + + +/* + * 'ippLength()' - Compute the length of an IPP message. + */ + +size_t /* O - Size of IPP message */ +ippLength(ipp_t *ipp) /* I - IPP message */ +{ + return (ipp_length(ipp, 0)); +} + + +/* + * 'ippNextAttribute()' - Return the next attribute in the message. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +ipp_attribute_t * /* O - Next attribute or @code NULL@ if none */ +ippNextAttribute(ipp_t *ipp) /* I - IPP message */ +{ + /* + * Range check input... + */ + + if (!ipp || !ipp->current) + return (NULL); + + /* + * Return the next attribute... + */ + + return (ipp->current = ipp->current->next); +} + + +/* + * 'ippNew()' - Allocate a new IPP message. + */ + +ipp_t * /* O - New IPP message */ +ippNew(void) +{ + ipp_t *temp; /* New IPP message */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + DEBUG_puts("ippNew()"); + + if ((temp = (ipp_t *)calloc(1, sizeof(ipp_t))) != NULL) + { + /* + * Set default version - usually 2.0... + */ + + if (cg->server_version == 0) + _cupsSetDefaults(); + + temp->request.any.version[0] = cg->server_version / 10; + temp->request.any.version[1] = cg->server_version % 10; + temp->use = 1; + } + + DEBUG_printf(("1ippNew: Returning %p", temp)); + + return (temp); +} + + +/* + * 'ippNewRequest()' - Allocate a new IPP request message. + * + * The new request message is initialized with the attributes-charset and + * attributes-natural-language attributes added. The + * attributes-natural-language value is derived from the current locale. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ipp_t * /* O - IPP request message */ +ippNewRequest(ipp_op_t op) /* I - Operation code */ +{ + ipp_t *request; /* IPP request message */ + cups_lang_t *language; /* Current language localization */ + static int request_id = 0; /* Current request ID */ + static _cups_mutex_t request_mutex = _CUPS_MUTEX_INITIALIZER; + /* Mutex for request ID */ + + + DEBUG_printf(("ippNewRequest(op=%02x(%s))", op, ippOpString(op))); + + /* + * Create a new IPP message... + */ + + if ((request = ippNew()) == NULL) + return (NULL); + + /* + * Set the operation and request ID... + */ + + _cupsMutexLock(&request_mutex); + + request->request.op.operation_id = op; + request->request.op.request_id = ++request_id; + + _cupsMutexUnlock(&request_mutex); + + /* + * Use UTF-8 as the character set... + */ + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, + "attributes-charset", NULL, "utf-8"); + + /* + * Get the language from the current locale... + */ + + language = cupsLangDefault(); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, + "attributes-natural-language", NULL, language->language); + + /* + * Return the new request... + */ + + return (request); +} + + +/* + * 'ippRead()' - Read data for an IPP message from a HTTP connection. + */ + +ipp_state_t /* O - Current state */ +ippRead(http_t *http, /* I - HTTP connection */ + ipp_t *ipp) /* I - IPP data */ +{ + DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT, + http, ipp, CUPS_LLCAST (http ? http->data_remaining : -1))); + + if (!http) + return (IPP_ERROR); + + DEBUG_printf(("2ippRead: http->state=%d, http->used=%d", http->state, + http->used)); + + return (ippReadIO(http, (ipp_iocb_t)ipp_read_http, http->blocking, NULL, + ipp)); +} + + +/* + * 'ippReadFile()' - Read data for an IPP message from a file. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +ipp_state_t /* O - Current state */ +ippReadFile(int fd, /* I - HTTP data */ + ipp_t *ipp) /* I - IPP data */ +{ + DEBUG_printf(("ippReadFile(fd=%d, ipp=%p)", fd, ipp)); + + return (ippReadIO(&fd, (ipp_iocb_t)ipp_read_file, 1, NULL, ipp)); +} + + +/* + * 'ippReadIO()' - Read data for an IPP message. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ipp_state_t /* O - Current state */ +ippReadIO(void *src, /* I - Data source */ + ipp_iocb_t cb, /* I - Read callback function */ + int blocking, /* I - Use blocking IO? */ + ipp_t *parent, /* I - Parent request, if any */ + ipp_t *ipp) /* I - IPP data */ +{ + int n; /* Length of data */ + unsigned char *buffer, /* Data buffer */ + string[IPP_MAX_NAME], + /* Small string buffer */ + *bufptr; /* Pointer into buffer */ + ipp_attribute_t *attr; /* Current attribute */ + ipp_tag_t tag; /* Current tag */ + ipp_tag_t value_tag; /* Current value tag */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("ippReadIO(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", + src, cb, blocking, parent, ipp)); + DEBUG_printf(("2ippReadIO: ipp->state=%d", ipp ? ipp->state : IPP_ERROR)); + + if (!src || !ipp) + return (IPP_ERROR); + + if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL) + { + DEBUG_puts("1ippReadIO: Unable to get read buffer."); + return (IPP_ERROR); + } + + switch (ipp->state) + { + case IPP_IDLE : + ipp->state ++; /* Avoid common problem... */ + + case IPP_HEADER : + if (parent == NULL) + { + /* + * Get the request header... + */ + + if ((*cb)(src, buffer, 8) < 8) + { + DEBUG_puts("1ippReadIO: Unable to read header."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + /* + * Then copy the request header over... + */ + + ipp->request.any.version[0] = buffer[0]; + ipp->request.any.version[1] = buffer[1]; + ipp->request.any.op_status = (buffer[2] << 8) | buffer[3]; + ipp->request.any.request_id = (((((buffer[4] << 8) | buffer[5]) << 8) | + buffer[6]) << 8) | buffer[7]; + + DEBUG_printf(("2ippReadIO: version=%d.%d", buffer[0], buffer[1])); + DEBUG_printf(("2ippReadIO: op_status=%04x", + ipp->request.any.op_status)); + DEBUG_printf(("2ippReadIO: request_id=%d", + ipp->request.any.request_id)); + } + + ipp->state = IPP_ATTRIBUTE; + ipp->current = NULL; + ipp->curtag = IPP_TAG_ZERO; + ipp->prev = ipp->last; + + /* + * If blocking is disabled, stop here... + */ + + if (!blocking) + break; + + case IPP_ATTRIBUTE : + for (;;) + { + if ((*cb)(src, buffer, 1) < 1) + { + DEBUG_puts("1ippReadIO: Callback returned EOF/error"); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + DEBUG_printf(("2ippReadIO: ipp->current=%p, ipp->prev=%p", + ipp->current, ipp->prev)); + + /* + * Read this attribute... + */ + + tag = (ipp_tag_t)buffer[0]; + if (tag == IPP_TAG_EXTENSION) + { + /* + * Read 32-bit "extension" tag... + */ + + if ((*cb)(src, buffer, 4) < 1) + { + DEBUG_puts("1ippReadIO: Callback returned EOF/error"); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + tag = (ipp_tag_t)((((((buffer[0] << 8) | buffer[1]) << 8) | + buffer[2]) << 8) | buffer[3]); + + if (tag & IPP_TAG_COPY) + { + /* + * Fail if the high bit is set in the tag... + */ + + _cupsSetError(IPP_INTERNAL_ERROR, _("IPP extension tag larger than 0x7FFFFFFF."), 1); + DEBUG_printf(("1ippReadIO: bad tag 0x%x.", tag)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + } + + if (tag == IPP_TAG_END) + { + /* + * No more attributes left... + */ + + DEBUG_puts("2ippReadIO: IPP_TAG_END."); + + ipp->state = IPP_DATA; + break; + } + else if (tag < IPP_TAG_UNSUPPORTED_VALUE) + { + /* + * Group tag... Set the current group and continue... + */ + + if (ipp->curtag == tag) + ipp->prev = ippAddSeparator(ipp); + else if (ipp->current) + ipp->prev = ipp->current; + + ipp->curtag = tag; + ipp->current = NULL; + DEBUG_printf(("2ippReadIO: group tag=%x(%s), ipp->prev=%p", tag, + ippTagString(tag), ipp->prev)); + continue; + } + + DEBUG_printf(("2ippReadIO: value tag=%x(%s)", tag, + ippTagString(tag))); + + /* + * Get the name... + */ + + if ((*cb)(src, buffer, 2) < 2) + { + DEBUG_puts("1ippReadIO: unable to read name length."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + n = (buffer[0] << 8) | buffer[1]; + + if (n >= IPP_BUF_SIZE) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("IPP name larger than 32767 bytes."), 1); + DEBUG_printf(("1ippReadIO: bad name length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + DEBUG_printf(("2ippReadIO: name length=%d", n)); + + if (n == 0 && tag != IPP_TAG_MEMBERNAME && + tag != IPP_TAG_END_COLLECTION) + { + /* + * More values for current attribute... + */ + + if (ipp->current == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("IPP attribute has no name."), 1); + DEBUG_puts("1ippReadIO: Attribute without name and no current."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + attr = ipp->current; + value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_MASK); + + /* + * Make sure we aren't adding a new value of a different + * type... + */ + + if (value_tag == IPP_TAG_ZERO) + { + /* + * Setting the value of a collection member... + */ + + attr->value_tag = tag; + } + else if (value_tag == IPP_TAG_TEXTLANG || + value_tag == IPP_TAG_NAMELANG || + (value_tag >= IPP_TAG_TEXT && + value_tag <= IPP_TAG_MIMETYPE)) + { + /* + * String values can sometimes come across in different + * forms; accept sets of differing values... + */ + + if (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG && + (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE) && + tag != IPP_TAG_NOVALUE) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP 1setOf attribute with incompatible value " + "tags."), 1); + DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)", + value_tag, ippTagString(value_tag), tag, + ippTagString(tag))); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if (value_tag != tag) + { + DEBUG_printf(("1ippReadIO: Converting %s attribute from %s to %s.", + attr->name, ippTagString(value_tag), ippTagString(tag))); + ippSetValueTag(ipp, &attr, tag); + } + } + else if (value_tag == IPP_TAG_INTEGER || + value_tag == IPP_TAG_RANGE) + { + /* + * Integer and rangeOfInteger values can sometimes be mixed; accept + * sets of differing values... + */ + + if (tag != IPP_TAG_INTEGER && tag != IPP_TAG_RANGE) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP 1setOf attribute with incompatible value " + "tags."), 1); + DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)", + value_tag, ippTagString(value_tag), tag, + ippTagString(tag))); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if (value_tag == IPP_TAG_INTEGER && tag == IPP_TAG_RANGE) + { + /* + * Convert integer values to rangeOfInteger values... + */ + + DEBUG_printf(("1ippReadIO: Converting %s attribute to " + "rangeOfInteger.", attr->name)); + ippSetValueTag(ipp, &attr, IPP_TAG_RANGE); + } + } + else if (value_tag != tag) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP 1setOf attribute with incompatible value " + "tags."), 1); + DEBUG_printf(("1ippReadIO: value tag %x(%s) != %x(%s)", + value_tag, ippTagString(value_tag), tag, + ippTagString(tag))); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + /* + * Finally, reallocate the attribute array as needed... + */ + + if ((value = ipp_set_value(ipp, &attr, attr->num_values)) == NULL) + { + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + } + else if (tag == IPP_TAG_MEMBERNAME) + { + /* + * Name must be length 0! + */ + + if (n) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("IPP member name is not empty."), 1); + DEBUG_puts("1ippReadIO: member name not empty."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if (ipp->current) + ipp->prev = ipp->current; + + attr = ipp->current = ipp_add_attr(ipp, NULL, ipp->curtag, IPP_TAG_ZERO, 1); + if (!attr) + { + _cupsSetHTTPError(HTTP_ERROR); + DEBUG_puts("1ippReadIO: unable to allocate attribute."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + DEBUG_printf(("2ippReadIO: membername, ipp->current=%p, ipp->prev=%p", + ipp->current, ipp->prev)); + + value = attr->values; + } + else if (tag != IPP_TAG_END_COLLECTION) + { + /* + * New attribute; read the name and add it... + */ + + if ((*cb)(src, buffer, n) < n) + { + DEBUG_puts("1ippReadIO: unable to read name."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + buffer[n] = '\0'; + + if (ipp->current) + ipp->prev = ipp->current; + + if ((attr = ipp->current = ipp_add_attr(ipp, (char *)buffer, ipp->curtag, tag, + 1)) == NULL) + { + _cupsSetHTTPError(HTTP_ERROR); + DEBUG_puts("1ippReadIO: unable to allocate attribute."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + DEBUG_printf(("2ippReadIO: name=\"%s\", ipp->current=%p, " + "ipp->prev=%p", buffer, ipp->current, ipp->prev)); + + value = attr->values; + } + else + { + attr = NULL; + value = NULL; + } + + if ((*cb)(src, buffer, 2) < 2) + { + DEBUG_puts("1ippReadIO: unable to read value length."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + n = (buffer[0] << 8) | buffer[1]; + DEBUG_printf(("2ippReadIO: value length=%d", n)); + + if (n >= IPP_BUF_SIZE) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP value larger than 32767 bytes."), 1); + DEBUG_printf(("1ippReadIO: bad value length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + switch (tag) + { + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + if (n != 4) + { + if (tag == IPP_TAG_INTEGER) + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP integer value not 4 bytes."), 1); + else + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP enum value not 4 bytes."), 1); + DEBUG_printf(("1ippReadIO: bad integer value length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((*cb)(src, buffer, 4) < 4) + { + DEBUG_puts("1ippReadIO: Unable to read integer value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + n = (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) | + buffer[3]; + + if (attr->value_tag == IPP_TAG_RANGE) + value->range.lower = value->range.upper = n; + else + value->integer = n; + break; + + case IPP_TAG_BOOLEAN : + if (n != 1) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("IPP boolean value not 1 byte."), + 1); + DEBUG_printf(("1ippReadIO: bad boolean value length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((*cb)(src, buffer, 1) < 1) + { + DEBUG_puts("1ippReadIO: Unable to read boolean value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + value->boolean = buffer[0]; + break; + + case IPP_TAG_NOVALUE : + case IPP_TAG_NOTSETTABLE : + case IPP_TAG_DELETEATTR : + case IPP_TAG_ADMINDEFINE : + /* + * These value types are not supposed to have values, however + * some vendors (Brother) do not implement IPP correctly and so + * we need to map non-empty values to text... + */ + + if (attr->value_tag == tag) + { + if (n == 0) + break; + + attr->value_tag = IPP_TAG_TEXT; + } + + 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 : + if (n > 0) + { + if ((*cb)(src, buffer, n) < n) + { + DEBUG_puts("1ippReadIO: unable to read string value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + } + + buffer[n] = '\0'; + value->string.text = _cupsStrAlloc((char *)buffer); + DEBUG_printf(("2ippReadIO: value=\"%s\"", value->string.text)); + break; + + case IPP_TAG_DATE : + if (n != 11) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("IPP date value not 11 bytes."), 1); + DEBUG_printf(("1ippReadIO: bad date value length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((*cb)(src, value->date, 11) < 11) + { + DEBUG_puts("1ippReadIO: Unable to read date value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + break; + + case IPP_TAG_RESOLUTION : + if (n != 9) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP resolution value not 9 bytes."), 1); + DEBUG_printf(("1ippReadIO: bad resolution value length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((*cb)(src, buffer, 9) < 9) + { + DEBUG_puts("1ippReadIO: Unable to read resolution value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + value->resolution.xres = + (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) | + buffer[3]; + value->resolution.yres = + (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) | + buffer[7]; + value->resolution.units = + (ipp_res_t)buffer[8]; + break; + + case IPP_TAG_RANGE : + if (n != 8) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP rangeOfInteger value not 8 bytes."), 1); + DEBUG_printf(("1ippReadIO: bad rangeOfInteger value length " + "%d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((*cb)(src, buffer, 8) < 8) + { + DEBUG_puts("1ippReadIO: Unable to read range value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + value->range.lower = + (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) | + buffer[3]; + value->range.upper = + (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) | + buffer[7]; + break; + + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + if (n < 4) + { + if (tag == IPP_TAG_TEXTLANG) + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP textWithLanguage value less than " + "minimum 4 bytes."), 1); + else + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP nameWithLanguage value less than " + "minimum 4 bytes."), 1); + DEBUG_printf(("1ippReadIO: bad stringWithLanguage value " + "length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((*cb)(src, buffer, n) < n) + { + DEBUG_puts("1ippReadIO: Unable to read string w/language " + "value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + + /* + * text-with-language and name-with-language are composite + * values: + * + * language-length + * language + * text-length + * text + */ + + n = (bufptr[0] << 8) | bufptr[1]; + + if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE) || + n >= sizeof(string)) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP language length overflows value."), 1); + DEBUG_printf(("1ippReadIO: bad language value length %d.", + n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + memcpy(string, bufptr + 2, n); + string[n] = '\0'; + + value->string.language = _cupsStrAlloc((char *)string); + + bufptr += 2 + n; + n = (bufptr[0] << 8) | bufptr[1]; + + if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE)) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP string length overflows value."), 1); + DEBUG_printf(("1ippReadIO: bad string value length %d.", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr[2 + n] = '\0'; + value->string.text = _cupsStrAlloc((char *)bufptr + 2); + break; + + case IPP_TAG_BEGIN_COLLECTION : + /* + * Oh, boy, here comes a collection value, so read it... + */ + + value->collection = ippNew(); + + if (n > 0) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP begCollection value not 0 bytes."), 1); + DEBUG_puts("1ippReadIO: begCollection tag with value length " + "> 0."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if (ippReadIO(src, cb, 1, ipp, value->collection) == IPP_ERROR) + { + DEBUG_puts("1ippReadIO: Unable to read collection value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + break; + + case IPP_TAG_END_COLLECTION : + _cupsBufferRelease((char *)buffer); + + if (n > 0) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP endCollection value not 0 bytes."), 1); + DEBUG_puts("1ippReadIO: endCollection tag with value length " + "> 0."); + return (IPP_ERROR); + } + + DEBUG_puts("1ippReadIO: endCollection tag..."); + return (ipp->state = IPP_DATA); + + case IPP_TAG_MEMBERNAME : + /* + * The value the name of the member in the collection, which + * we need to carry over... + */ + + if (!attr) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP memberName with no attribute."), 1); + DEBUG_puts("1ippReadIO: Member name without attribute."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + else if (n == 0) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP memberName value is empty."), 1); + DEBUG_puts("1ippReadIO: Empty member name value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + else if ((*cb)(src, buffer, n) < n) + { + DEBUG_puts("1ippReadIO: Unable to read member name value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + buffer[n] = '\0'; + attr->name = _cupsStrAlloc((char *)buffer); + + /* + * Since collection members are encoded differently than + * regular attributes, make sure we don't start with an + * empty value... + */ + + attr->num_values --; + + DEBUG_printf(("2ippReadIO: member name=\"%s\"", attr->name)); + break; + + default : /* Other unsupported values */ + value->unknown.length = n; + if (n > 0) + { + if ((value->unknown.data = malloc(n)) == NULL) + { + _cupsSetHTTPError(HTTP_ERROR); + DEBUG_puts("1ippReadIO: Unable to allocate value"); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((*cb)(src, value->unknown.data, n) < n) + { + DEBUG_puts("1ippReadIO: Unable to read unsupported value."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + } + else + value->unknown.data = NULL; + break; + } + + /* + * If blocking is disabled, stop here... + */ + + if (!blocking) + break; + } + break; + + case IPP_DATA : + break; + + default : + break; /* anti-compiler-warning-code */ + } + + DEBUG_printf(("1ippReadIO: returning ipp->state=%d.", ipp->state)); + _cupsBufferRelease((char *)buffer); + + return (ipp->state); +} + + +/* + * 'ippSetBoolean()' - Set a boolean value in an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code element@ parameter specifies which value to set from 0 to + * @link ippGetCount(attr)@. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetBoolean(ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element, /* I - Value number (0-based) */ + int boolvalue)/* I - Boolean value */ +{ + _ipp_value_t *value; /* Current value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BOOLEAN || + element < 0 || element > (*attr)->num_values) + return (0); + + /* + * Set the value and return... + */ + + if ((value = ipp_set_value(ipp, attr, element)) != NULL) + value->boolean = boolvalue; + + return (value != NULL); +} + + +/* + * 'ippSetCollection()' - Set a collection value in an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code element@ parameter specifies which value to set from 0 to + * @link ippGetCount(attr)@. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetCollection( + ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element, /* I - Value number (0-based) */ + ipp_t *colvalue) /* I - Collection value */ +{ + _ipp_value_t *value; /* Current value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BEGIN_COLLECTION || + element < 0 || element > (*attr)->num_values || !colvalue) + return (0); + + /* + * Set the value and return... + */ + + if ((value = ipp_set_value(ipp, attr, element)) != NULL) + { + if (value->collection) + ippDelete(value->collection); + + value->collection = colvalue; + colvalue->use ++; + } + + return (value != NULL); +} + + +/* + * 'ippSetDate()' - Set a date value in an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code element@ parameter specifies which value to set from 0 to + * @link ippGetCount(attr)@. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetDate(ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element, /* I - Value number (0-based) */ + const ipp_uchar_t *datevalue)/* I - Date value */ +{ + _ipp_value_t *value; /* Current value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_DATE || + element < 0 || element > (*attr)->num_values || !datevalue) + return (0); + + /* + * Set the value and return... + */ + + if ((value = ipp_set_value(ipp, attr, element)) != NULL) + memcpy(value->date, datevalue, sizeof(value->date)); + + return (value != NULL); +} + + +/* + * 'ippSetGroupTag()' - Set the group tag of an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code group@ parameter specifies the IPP attribute group tag: none + * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@), + * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation + * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription + * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@). + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetGroupTag( + ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - Attribute */ + ipp_tag_t group_tag) /* I - Group tag */ +{ + /* + * Range check input - group tag must be 0x01 to 0x0F, per RFC 2911... + */ + + if (!ipp || !attr || group_tag < IPP_TAG_ZERO || group_tag == IPP_TAG_END || + group_tag >= IPP_TAG_UNSUPPORTED_VALUE) + return (0); + + /* + * Set the group tag and return... + */ + + (*attr)->group_tag = group_tag; + + return (1); +} + + +/* + * 'ippSetInteger()' - Set an integer or enum value in an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code element@ parameter specifies which value to set from 0 to + * @link ippGetCount(attr)@. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetInteger(ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element, /* I - Value number (0-based) */ + int intvalue) /* I - Integer/enum value */ +{ + _ipp_value_t *value; /* Current value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || + ((*attr)->value_tag != IPP_TAG_INTEGER && (*attr)->value_tag != IPP_TAG_ENUM) || + element < 0 || element > (*attr)->num_values) + return (0); + + /* + * Set the value and return... + */ + + if ((value = ipp_set_value(ipp, attr, element)) != NULL) + value->integer = intvalue; + + return (value != NULL); +} + + +/* + * 'ippSetName()' - Set the name of an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetName(ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + const char *name) /* I - Attribute name */ +{ + char *temp; /* Temporary name value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr) + return (0); + + /* + * Set the value and return... + */ + + if ((temp = _cupsStrAlloc(name)) != NULL) + { + if ((*attr)->name) + _cupsStrFree((*attr)->name); + + (*attr)->name = temp; + } + + return (temp != NULL); +} + + +/* + * 'ippSetOperation()' - Set the operation ID in an IPP request message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetOperation(ipp_t *ipp, /* I - IPP request message */ + ipp_op_t op) /* I - Operation ID */ +{ + /* + * Range check input... + */ + + if (!ipp) + return (0); + + /* + * Set the operation and return... + */ + + ipp->request.op.operation_id = op; + + return (1); +} + + +/* + * 'ippSetRange()' - Set a rangeOfInteger value in an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code element@ parameter specifies which value to set from 0 to + * @link ippGetCount(attr)@. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetRange(ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element, /* I - Value number (0-based) */ + int lowervalue, /* I - Lower bound for range */ + int uppervalue) /* I - Upper bound for range */ +{ + _ipp_value_t *value; /* Current value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RANGE || + element < 0 || element > (*attr)->num_values || lowervalue > uppervalue) + return (0); + + /* + * Set the value and return... + */ + + if ((value = ipp_set_value(ipp, attr, element)) != NULL) + { + value->range.lower = lowervalue; + value->range.upper = uppervalue; + } + + return (value != NULL); +} + + +/* + * 'ippSetRequestId()' - Set the request ID in an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code request_id@ parameter must be greater than 0. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetRequestId(ipp_t *ipp, /* I - IPP message */ + int request_id) /* I - Request ID */ +{ + /* + * Range check input; not checking request_id values since ipptool wants to send + * invalid values for conformance testing and a bad request_id does not affect the + * encoding of a message... + */ + + if (!ipp) + return (0); + + /* + * Set the request ID and return... + */ + + ipp->request.any.request_id = request_id; + + return (1); +} + + +/* + * 'ippSetResolution()' - Set a resolution value in an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code element@ parameter specifies which value to set from 0 to + * @link ippGetCount(attr)@. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetResolution( + ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element, /* I - Value number (0-based) */ + ipp_res_t unitsvalue, /* I - Resolution units */ + int xresvalue, /* I - Horizontal/cross feed resolution */ + int yresvalue) /* I - Vertical/feed resolution */ +{ + _ipp_value_t *value; /* Current value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RESOLUTION || + element < 0 || element > (*attr)->num_values || xresvalue <= 0 || yresvalue <= 0 || + unitsvalue < IPP_RES_PER_INCH || unitsvalue > IPP_RES_PER_CM) + return (0); + + /* + * Set the value and return... + */ + + if ((value = ipp_set_value(ipp, attr, element)) != NULL) + { + value->resolution.units = unitsvalue; + value->resolution.xres = xresvalue; + value->resolution.yres = yresvalue; + } + + return (value != NULL); +} + + +/* + * 'ippSetState()' - Set the current state of the IPP message. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetState(ipp_t *ipp, /* I - IPP message */ + ipp_state_t state) /* I - IPP state value */ +{ + /* + * Range check input... + */ + + if (!ipp) + return (0); + + /* + * Set the state and return... + */ + + ipp->state = state; + ipp->current = NULL; + + return (1); +} + + +/* + * 'ippSetStatusCode()' - Set the status code in an IPP response or event message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetStatusCode(ipp_t *ipp, /* I - IPP response or event message */ + ipp_status_t status) /* I - Status code */ +{ + /* + * Range check input... + */ + + if (!ipp) + return (0); + + /* + * Set the status code and return... + */ + + ipp->request.status.status_code = status; + + return (1); +} + + +/* + * 'ippSetString()' - Set a string value in an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * The @code element@ parameter specifies which value to set from 0 to + * @link ippGetCount(attr)@. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetString(ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element, /* I - Value number (0-based) */ + const char *strvalue) /* I - String value */ +{ + char *temp; /* Temporary string */ + _ipp_value_t *value; /* Current value */ + + + /* + * Range check input... + */ + + if (!ipp || !attr || !*attr || + ((*attr)->value_tag != IPP_TAG_TEXTLANG && + (*attr)->value_tag != IPP_TAG_NAMELANG && + ((*attr)->value_tag < IPP_TAG_TEXT || + (*attr)->value_tag > IPP_TAG_MIMETYPE)) || + element < 0 || element > (*attr)->num_values || !strvalue) + return (0); + + /* + * Set the value and return... + */ + + if ((value = ipp_set_value(ipp, attr, element)) != NULL) + { + if (element > 0) + value->string.language = (*attr)->values[0].string.language; + + if ((int)((*attr)->value_tag) & IPP_TAG_COPY) + value->string.text = (char *)strvalue; + else if ((temp = _cupsStrAlloc(strvalue)) != NULL) + { + if (value->string.text) + _cupsStrFree(value->string.text); + + value->string.text = temp; + } + else + return (0); + } + + return (value != NULL); +} + + +/* + * 'ippSetValueTag()' - Set the value tag of an attribute. + * + * The @code ipp@ parameter refers to the IPP message containing the attribute that was + * previously created using the @link ippNew@ or @link ippNewRequest@ functions. + * + * The @code attr@ parameter may be modified as a result of setting the value. + * + * Integer (@code IPP_TAG_INTEGER@) values can be promoted to rangeOfInteger + * (@code IPP_TAG_RANGE@) values, the various string tags can be promoted to name + * (@code IPP_TAG_NAME@) or nameWithLanguage (@code IPP_TAG_NAMELANG@) values, text + * (@code IPP_TAG_TEXT@) values can be promoted to textWithLanguage + * (@code IPP_TAG_TEXTLANG@) values, and all values can be demoted to the various + * out-of-band value tags such as no-value (@code IPP_TAG_NOVALUE@). All other changes + * will be rejected. + * + * Promoting a string attribute to nameWithLanguage or textWithLanguage adds the language + * code in the "attributes-natural-language" attribute or, if not present, the language + * code for the current locale. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetValueTag( + ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + ipp_tag_t value_tag) /* I - Value tag */ +{ + int i; /* Looping var */ + _ipp_value_t *value; /* Current value */ + int integer; /* Current integer value */ + cups_lang_t *language; /* Current language */ + char code[32]; /* Language code */ + ipp_tag_t temp_tag; /* Temporary value tag */ + + + /* + * Range check input... + */ + + if (!ipp || !attr) + return (0); + + /* + * If there is no change, return immediately... + */ + + if (value_tag == (*attr)->value_tag) + return (1); + + /* + * Otherwise implement changes as needed... + */ + + temp_tag = (ipp_tag_t)((int)((*attr)->value_tag) & IPP_TAG_MASK); + + switch (value_tag) + { + case IPP_TAG_UNSUPPORTED_VALUE : + case IPP_TAG_DEFAULT : + case IPP_TAG_UNKNOWN : + case IPP_TAG_NOVALUE : + case IPP_TAG_NOTSETTABLE : + case IPP_TAG_DELETEATTR : + case IPP_TAG_ADMINDEFINE : + /* + * Free any existing values... + */ + + if ((*attr)->num_values > 0) + ipp_free_values(*attr, 0, (*attr)->num_values); + + /* + * Set out-of-band value... + */ + + (*attr)->value_tag = value_tag; + break; + + case IPP_TAG_RANGE : + if (temp_tag != IPP_TAG_INTEGER) + return (0); + + for (i = (*attr)->num_values, value = (*attr)->values; + i > 0; + i --, value ++) + { + integer = value->integer; + value->range.lower = value->range.upper = integer; + } + + (*attr)->value_tag = IPP_TAG_RANGE; + break; + + case IPP_TAG_NAME : + if (temp_tag != IPP_TAG_KEYWORD && temp_tag != IPP_TAG_URI && + temp_tag != IPP_TAG_URISCHEME && temp_tag != IPP_TAG_LANGUAGE && + temp_tag != IPP_TAG_MIMETYPE) + return (0); + + (*attr)->value_tag = (ipp_tag_t)(IPP_TAG_NAME | ((*attr)->value_tag & IPP_TAG_COPY)); + break; + + case IPP_TAG_NAMELANG : + case IPP_TAG_TEXTLANG : + if (value_tag == IPP_TAG_NAMELANG && + (temp_tag != IPP_TAG_NAME && temp_tag != IPP_TAG_KEYWORD && + temp_tag != IPP_TAG_URI && temp_tag != IPP_TAG_URISCHEME && + temp_tag != IPP_TAG_LANGUAGE && temp_tag != IPP_TAG_MIMETYPE)) + return (0); + + if (value_tag == IPP_TAG_TEXTLANG && temp_tag != IPP_TAG_TEXT) + return (0); + + if (ipp->attrs && ipp->attrs->next && ipp->attrs->next->name && + !strcmp(ipp->attrs->next->name, "attributes-natural-language")) + { + /* + * Use the language code from the IPP message... + */ + + (*attr)->values[0].string.language = + _cupsStrAlloc(ipp->attrs->next->values[0].string.text); + } + else + { + /* + * Otherwise, use the language code corresponding to the locale... + */ + + language = cupsLangDefault(); + (*attr)->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language->language, + code, + sizeof(code))); + } + + for (i = (*attr)->num_values - 1, value = (*attr)->values + 1; + i > 0; + i --, value ++) + value->string.language = (*attr)->values[0].string.language; + + if ((int)(*attr)->value_tag & IPP_TAG_COPY) + { + /* + * Make copies of all values... + */ + + for (i = (*attr)->num_values, value = (*attr)->values; + i > 0; + i --, value ++) + value->string.text = _cupsStrAlloc(value->string.text); + } + + (*attr)->value_tag = IPP_TAG_NAMELANG; + break; + + case IPP_TAG_KEYWORD : + if (temp_tag == IPP_TAG_NAME || temp_tag == IPP_TAG_NAMELANG) + break; /* Silently "allow" name -> keyword */ + + default : + return (0); + } + + return (1); +} + + +/* + * 'ippSetVersion()' - Set the version number in an IPP message. + * + * The @code ipp@ parameter refers to an IPP message previously created using the + * @link ippNew@ or @link ippNewRequest@ functions. + * + * The valid version numbers are currently 1.0, 1.1, 2.0, 2.1, and 2.2. + * + * @since CUPS 1.6/OS X 10.8@ + */ + +int /* O - 1 on success, 0 on failure */ +ippSetVersion(ipp_t *ipp, /* I - IPP message */ + int major, /* I - Major version number (major.minor) */ + int minor) /* I - Minor version number (major.minor) */ +{ + /* + * Range check input... + */ + + if (!ipp || major < 0 || minor < 0) + return (0); + + /* + * Set the version number... + */ + + ipp->request.any.version[0] = major; + ipp->request.any.version[1] = minor; + + return (1); +} + + +/* + * 'ippTimeToDate()' - Convert from UNIX time to RFC 1903 format. + */ + +const ipp_uchar_t * /* O - RFC-1903 date/time data */ +ippTimeToDate(time_t t) /* I - UNIX time value */ +{ + struct tm *unixdate; /* UNIX unixdate/time info */ + ipp_uchar_t *date = _cupsGlobals()->ipp_date; + /* RFC-1903 date/time data */ + + + /* + * RFC-1903 date/time format is: + * + * Byte(s) Description + * ------- ----------- + * 0-1 Year (0 to 65535) + * 2 Month (1 to 12) + * 3 Day (1 to 31) + * 4 Hours (0 to 23) + * 5 Minutes (0 to 59) + * 6 Seconds (0 to 60, 60 = "leap second") + * 7 Deciseconds (0 to 9) + * 8 +/- UTC + * 9 UTC hours (0 to 11) + * 10 UTC minutes (0 to 59) + */ + + unixdate = gmtime(&t); + unixdate->tm_year += 1900; + + date[0] = unixdate->tm_year >> 8; + date[1] = unixdate->tm_year; + date[2] = unixdate->tm_mon + 1; + date[3] = unixdate->tm_mday; + date[4] = unixdate->tm_hour; + date[5] = unixdate->tm_min; + date[6] = unixdate->tm_sec; + date[7] = 0; + date[8] = '+'; + date[9] = 0; + date[10] = 0; + + return (date); +} + + +/* + * 'ippWrite()' - Write data for an IPP message to a HTTP connection. + */ + +ipp_state_t /* O - Current state */ +ippWrite(http_t *http, /* I - HTTP connection */ + ipp_t *ipp) /* I - IPP data */ +{ + DEBUG_printf(("ippWrite(http=%p, ipp=%p)", http, ipp)); + + if (!http) + return (IPP_ERROR); + + return (ippWriteIO(http, (ipp_iocb_t)httpWrite2, http->blocking, NULL, ipp)); +} + + +/* + * 'ippWriteFile()' - Write data for an IPP message to a file. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +ipp_state_t /* O - Current state */ +ippWriteFile(int fd, /* I - HTTP data */ + ipp_t *ipp) /* I - IPP data */ +{ + DEBUG_printf(("ippWriteFile(fd=%d, ipp=%p)", fd, ipp)); + + ipp->state = IPP_IDLE; + + return (ippWriteIO(&fd, (ipp_iocb_t)ipp_write_file, 1, NULL, ipp)); +} + + +/* + * 'ippWriteIO()' - Write data for an IPP message. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ipp_state_t /* O - Current state */ +ippWriteIO(void *dst, /* I - Destination */ + ipp_iocb_t cb, /* I - Write callback function */ + int blocking, /* I - Use blocking IO? */ + ipp_t *parent, /* I - Parent IPP message */ + ipp_t *ipp) /* I - IPP data */ +{ + int i; /* Looping var */ + int n; /* Length of data */ + unsigned char *buffer, /* Data buffer */ + *bufptr; /* Pointer into buffer */ + ipp_attribute_t *attr; /* Current attribute */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("ippWriteIO(dst=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", + dst, cb, blocking, parent, ipp)); + + if (!dst || !ipp) + return (IPP_ERROR); + + if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL) + { + DEBUG_puts("1ippWriteIO: Unable to get write buffer"); + return (IPP_ERROR); + } + + switch (ipp->state) + { + case IPP_IDLE : + ipp->state ++; /* Avoid common problem... */ + + case IPP_HEADER : + if (parent == NULL) + { + /* + * Send the request header: + * + * Version = 2 bytes + * Operation/Status Code = 2 bytes + * Request ID = 4 bytes + * Total = 8 bytes + */ + + bufptr = buffer; + + *bufptr++ = ipp->request.any.version[0]; + *bufptr++ = ipp->request.any.version[1]; + *bufptr++ = ipp->request.any.op_status >> 8; + *bufptr++ = ipp->request.any.op_status; + *bufptr++ = ipp->request.any.request_id >> 24; + *bufptr++ = ipp->request.any.request_id >> 16; + *bufptr++ = ipp->request.any.request_id >> 8; + *bufptr++ = ipp->request.any.request_id; + + DEBUG_printf(("2ippWriteIO: version=%d.%d", buffer[0], buffer[1])); + DEBUG_printf(("2ippWriteIO: op_status=%04x", + ipp->request.any.op_status)); + DEBUG_printf(("2ippWriteIO: request_id=%d", + ipp->request.any.request_id)); + + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP header..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + } + + /* + * Reset the state engine to point to the first attribute + * in the request/response, with no current group. + */ + + ipp->state = IPP_ATTRIBUTE; + ipp->current = ipp->attrs; + ipp->curtag = IPP_TAG_ZERO; + + DEBUG_printf(("1ippWriteIO: ipp->current=%p", ipp->current)); + + /* + * If blocking is disabled, stop here... + */ + + if (!blocking) + break; + + case IPP_ATTRIBUTE : + while (ipp->current != NULL) + { + /* + * Write this attribute... + */ + + bufptr = buffer; + attr = ipp->current; + + ipp->current = ipp->current->next; + + if (!parent) + { + if (ipp->curtag != attr->group_tag) + { + /* + * Send a group tag byte... + */ + + ipp->curtag = attr->group_tag; + + if (attr->group_tag == IPP_TAG_ZERO) + continue; + + DEBUG_printf(("2ippWriteIO: wrote group tag=%x(%s)", + attr->group_tag, ippTagString(attr->group_tag))); + *bufptr++ = attr->group_tag; + } + else if (attr->group_tag == IPP_TAG_ZERO) + continue; + } + + DEBUG_printf(("1ippWriteIO: %s (%s%s)", attr->name, + attr->num_values > 1 ? "1setOf " : "", + ippTagString(attr->value_tag))); + + /* + * Write the attribute tag and name. + * + * The attribute name length does not include the trailing nul + * character in the source string. + * + * Collection values (parent != NULL) are written differently... + */ + + if (parent == NULL) + { + /* + * Get the length of the attribute name, and make sure it won't + * overflow the buffer... + */ + + if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 8)) + { + DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + /* + * Write the value tag, name length, and name string... + */ + + DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)", + attr->value_tag, ippTagString(attr->value_tag))); + DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n, + attr->name)); + + if (attr->value_tag > 0xff) + { + *bufptr++ = IPP_TAG_EXTENSION; + *bufptr++ = attr->value_tag >> 24; + *bufptr++ = attr->value_tag >> 16; + *bufptr++ = attr->value_tag >> 8; + *bufptr++ = attr->value_tag; + } + else + *bufptr++ = attr->value_tag; + + *bufptr++ = n >> 8; + *bufptr++ = n; + memcpy(bufptr, attr->name, n); + bufptr += n; + } + else + { + /* + * Get the length of the attribute name, and make sure it won't + * overflow the buffer... + */ + + if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 12)) + { + DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + /* + * Write the member name tag, name length, name string, value tag, + * and empty name for the collection member attribute... + */ + + DEBUG_printf(("2ippWriteIO: writing value tag=%x(memberName)", + IPP_TAG_MEMBERNAME)); + DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n, + attr->name)); + DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)", + attr->value_tag, ippTagString(attr->value_tag))); + DEBUG_puts("2ippWriteIO: writing name=0,\"\""); + + *bufptr++ = IPP_TAG_MEMBERNAME; + *bufptr++ = 0; + *bufptr++ = 0; + *bufptr++ = n >> 8; + *bufptr++ = n; + memcpy(bufptr, attr->name, n); + bufptr += n; + + if (attr->value_tag > 0xff) + { + *bufptr++ = IPP_TAG_EXTENSION; + *bufptr++ = attr->value_tag >> 24; + *bufptr++ = attr->value_tag >> 16; + *bufptr++ = attr->value_tag >> 8; + *bufptr++ = attr->value_tag; + } + else + *bufptr++ = attr->value_tag; + + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * Now write the attribute value(s)... + */ + + switch (attr->value_tag & ~IPP_TAG_COPY) + { + case IPP_TAG_UNSUPPORTED_VALUE : + case IPP_TAG_DEFAULT : + case IPP_TAG_UNKNOWN : + case IPP_TAG_NOVALUE : + case IPP_TAG_NOTSETTABLE : + case IPP_TAG_DELETEATTR : + case IPP_TAG_ADMINDEFINE : + *bufptr++ = 0; + *bufptr++ = 0; + break; + + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 9) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * Integers and enumerations are both 4-byte signed + * (twos-complement) values. + * + * Put the 2-byte length and 4-byte value into the buffer... + */ + + *bufptr++ = 0; + *bufptr++ = 4; + *bufptr++ = value->integer >> 24; + *bufptr++ = value->integer >> 16; + *bufptr++ = value->integer >> 8; + *bufptr++ = value->integer; + } + break; + + case IPP_TAG_BOOLEAN : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 6) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * Boolean values are 1-byte; 0 = false, 1 = true. + * + * Put the 2-byte length and 1-byte value into the buffer... + */ + + *bufptr++ = 0; + *bufptr++ = 1; + *bufptr++ = value->boolean; + } + break; + + 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, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)", + attr->value_tag, + ippTagString(attr->value_tag))); + DEBUG_printf(("2ippWriteIO: writing name=0,\"\"")); + + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + if (value->string.text != NULL) + n = (int)strlen(value->string.text); + else + n = 0; + + if (n > (IPP_BUF_SIZE - 2)) + { + DEBUG_printf(("1ippWriteIO: String too long (%d)", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + DEBUG_printf(("2ippWriteIO: writing string=%d,\"%s\"", n, + value->string.text)); + + if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2)) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + /* + * All simple strings consist of the 2-byte length and + * character data without the trailing nul normally found + * in C strings. Also, strings cannot be longer than IPP_MAX_LENGTH + * bytes since the 2-byte length is a signed (twos-complement) + * value. + * + * Put the 2-byte length and string characters in the buffer. + */ + + *bufptr++ = n >> 8; + *bufptr++ = n; + + if (n > 0) + { + memcpy(bufptr, value->string.text, n); + bufptr += n; + } + } + break; + + case IPP_TAG_DATE : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 16) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * Date values consist of a 2-byte length and an + * 11-byte date/time structure defined by RFC 1903. + * + * Put the 2-byte length and 11-byte date/time + * structure in the buffer. + */ + + *bufptr++ = 0; + *bufptr++ = 11; + memcpy(bufptr, value->date, 11); + bufptr += 11; + } + break; + + case IPP_TAG_RESOLUTION : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 14) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * Resolution values consist of a 2-byte length, + * 4-byte horizontal resolution value, 4-byte vertical + * resolution value, and a 1-byte units value. + * + * Put the 2-byte length and resolution value data + * into the buffer. + */ + + *bufptr++ = 0; + *bufptr++ = 9; + *bufptr++ = value->resolution.xres >> 24; + *bufptr++ = value->resolution.xres >> 16; + *bufptr++ = value->resolution.xres >> 8; + *bufptr++ = value->resolution.xres; + *bufptr++ = value->resolution.yres >> 24; + *bufptr++ = value->resolution.yres >> 16; + *bufptr++ = value->resolution.yres >> 8; + *bufptr++ = value->resolution.yres; + *bufptr++ = value->resolution.units; + } + break; + + case IPP_TAG_RANGE : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 13) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * Range values consist of a 2-byte length, + * 4-byte lower value, and 4-byte upper value. + * + * Put the 2-byte length and range value data + * into the buffer. + */ + + *bufptr++ = 0; + *bufptr++ = 8; + *bufptr++ = value->range.lower >> 24; + *bufptr++ = value->range.lower >> 16; + *bufptr++ = value->range.lower >> 8; + *bufptr++ = value->range.lower; + *bufptr++ = value->range.upper >> 24; + *bufptr++ = value->range.upper >> 16; + *bufptr++ = value->range.upper >> 8; + *bufptr++ = value->range.upper; + } + break; + + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * textWithLanguage and nameWithLanguage values consist + * of a 2-byte length for both strings and their + * individual lengths, a 2-byte length for the + * character string, the character string without the + * trailing nul, a 2-byte length for the character + * set string, and the character set string without + * the trailing nul. + */ + + n = 4; + + if (value->string.language != NULL) + n += (int)strlen(value->string.language); + + if (value->string.text != NULL) + n += (int)strlen(value->string.text); + + if (n > (IPP_BUF_SIZE - 2)) + { + DEBUG_printf(("1ippWriteIO: text/nameWithLanguage value " + "too long (%d)", n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2)) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + /* Length of entire value */ + *bufptr++ = n >> 8; + *bufptr++ = n; + + /* Length of language */ + if (value->string.language != NULL) + n = (int)strlen(value->string.language); + else + n = 0; + + *bufptr++ = n >> 8; + *bufptr++ = n; + + /* Language */ + if (n > 0) + { + memcpy(bufptr, value->string.language, n); + bufptr += n; + } + + /* Length of text */ + if (value->string.text != NULL) + n = (int)strlen(value->string.text); + else + n = 0; + + *bufptr++ = n >> 8; + *bufptr++ = n; + + /* Text */ + if (n > 0) + { + memcpy(bufptr, value->string.text, n); + bufptr += n; + } + } + break; + + case IPP_TAG_BEGIN_COLLECTION : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + /* + * Collections are written with the begin-collection + * tag first with a value of 0 length, followed by the + * attributes in the collection, then the end-collection + * value... + */ + + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 5) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * Write a data length of 0 and flush the buffer... + */ + + *bufptr++ = 0; + *bufptr++ = 0; + + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + + /* + * Then write the collection attribute... + */ + + value->collection->state = IPP_IDLE; + + if (ippWriteIO(dst, cb, 1, ipp, + value->collection) == IPP_ERROR) + { + DEBUG_puts("1ippWriteIO: Unable to write collection value"); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + } + break; + + default : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if (i) + { + /* + * Arrays and sets are done by sending additional + * values with a zero-length name... + */ + + if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + *bufptr++ = attr->value_tag; + *bufptr++ = 0; + *bufptr++ = 0; + } + + /* + * An unknown value might some new value that a + * vendor has come up with. It consists of a + * 2-byte length and the bytes in the unknown + * value buffer. + */ + + n = value->unknown.length; + + if (n > (IPP_BUF_SIZE - 2)) + { + DEBUG_printf(("1ippWriteIO: Data length too long (%d)", + n)); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2)) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP " + "attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + bufptr = buffer; + } + + /* Length of unknown value */ + *bufptr++ = n >> 8; + *bufptr++ = n; + + /* Value */ + if (n > 0) + { + memcpy(bufptr, value->unknown.data, n); + bufptr += n; + } + } + break; + } + + /* + * Write the data out... + */ + + if (bufptr > buffer) + { + if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP attribute..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + DEBUG_printf(("2ippWriteIO: wrote %d bytes", + (int)(bufptr - buffer))); + } + + /* + * If blocking is disabled, stop here... + */ + + if (!blocking) + break; + } + + if (ipp->current == NULL) + { + /* + * Done with all of the attributes; add the end-of-attributes + * tag or end-collection attribute... + */ + + if (parent == NULL) + { + buffer[0] = IPP_TAG_END; + n = 1; + } + else + { + buffer[0] = IPP_TAG_END_COLLECTION; + buffer[1] = 0; /* empty name */ + buffer[2] = 0; + buffer[3] = 0; /* empty value */ + buffer[4] = 0; + n = 5; + } + + if ((*cb)(dst, buffer, n) < 0) + { + DEBUG_puts("1ippWriteIO: Could not write IPP end-tag..."); + _cupsBufferRelease((char *)buffer); + return (IPP_ERROR); + } + + ipp->state = IPP_DATA; + } + break; + + case IPP_DATA : + break; + + default : + break; /* anti-compiler-warning-code */ + } + + _cupsBufferRelease((char *)buffer); + + return (ipp->state); +} + + +/* + * 'ipp_add_attr()' - Add a new attribute to the message. + */ + +static ipp_attribute_t * /* O - New attribute */ +ipp_add_attr(ipp_t *ipp, /* I - IPP message */ + const char *name, /* I - Attribute name or NULL */ + ipp_tag_t group_tag, /* I - Group tag or IPP_TAG_ZERO */ + ipp_tag_t value_tag, /* I - Value tag or IPP_TAG_ZERO */ + int num_values) /* I - Number of values */ +{ + int alloc_values; /* Number of values to allocate */ + ipp_attribute_t *attr; /* New attribute */ + + + DEBUG_printf(("4ipp_add_attr(ipp=%p, name=\"%s\", group_tag=0x%x, value_tag=0x%x, " + "num_values=%d)", ipp, name, group_tag, value_tag, num_values)); + + /* + * Range check input... + */ + + if (!ipp || num_values < 0) + return (NULL); + + /* + * Allocate memory, rounding the allocation up as needed... + */ + + if (num_values <= 1) + alloc_values = 1; + else + alloc_values = (num_values + IPP_MAX_VALUES - 1) & ~(IPP_MAX_VALUES - 1); + + attr = calloc(sizeof(ipp_attribute_t) + + (alloc_values - 1) * sizeof(_ipp_value_t), 1); + + if (attr) + { + /* + * Initialize attribute... + */ + + if (name) + attr->name = _cupsStrAlloc(name); + + attr->group_tag = group_tag; + attr->value_tag = value_tag; + attr->num_values = num_values; + + /* + * Add it to the end of the linked list... + */ + + if (ipp->last) + ipp->last->next = attr; + else + ipp->attrs = attr; + + ipp->prev = ipp->last; + ipp->last = ipp->current = attr; + } + + DEBUG_printf(("5ipp_add_attr: Returning %p", attr)); + + return (attr); +} + + +/* + * 'ipp_free_values()' - Free attribute values. + */ + +static void +ipp_free_values(ipp_attribute_t *attr, /* I - Attribute to free values from */ + int element,/* I - First value to free */ + int count) /* I - Number of values to free */ +{ + int i; /* Looping var */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("4ipp_free_values(attr=%p, element=%d, count=%d)", attr, + element, count)); + + if (!(attr->value_tag & IPP_TAG_COPY)) + { + /* + * Free values as needed... + */ + + switch (attr->value_tag) + { + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + if (element == 0 && count == attr->num_values && + attr->values[0].string.language) + { + _cupsStrFree(attr->values[0].string.language); + attr->values[0].string.language = NULL; + } + /* Fall through to other string values */ + + case IPP_TAG_TEXT : + case IPP_TAG_NAME : + case IPP_TAG_RESERVED_STRING : + 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 = count, value = attr->values + element; + i > 0; + i --, value ++) + { + _cupsStrFree(value->string.text); + value->string.text = NULL; + } + break; + + case IPP_TAG_DEFAULT : + case IPP_TAG_UNKNOWN : + case IPP_TAG_NOVALUE : + case IPP_TAG_NOTSETTABLE : + case IPP_TAG_DELETEATTR : + case IPP_TAG_ADMINDEFINE : + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + case IPP_TAG_BOOLEAN : + case IPP_TAG_DATE : + case IPP_TAG_RESOLUTION : + case IPP_TAG_RANGE : + break; + + case IPP_TAG_BEGIN_COLLECTION : + for (i = count, value = attr->values + element; + i > 0; + i --, value ++) + { + ippDelete(value->collection); + value->collection = NULL; + } + break; + + case IPP_TAG_STRING : + default : + for (i = count, value = attr->values + element; + i > 0; + i --, value ++) + { + if (value->unknown.data) + { + free(value->unknown.data); + value->unknown.data = NULL; + } + } + break; + } + } + + /* + * If we are not freeing values from the end, move the remaining values up... + */ + + if ((element + count) < attr->num_values) + memmove(attr->values + element, attr->values + element + count, + (attr->num_values - count - element) * sizeof(_ipp_value_t)); + + attr->num_values -= count; +} + + +/* + * 'ipp_get_code()' - Convert a C locale/charset name into an IPP language/charset code. + * + * This typically converts strings of the form "ll_CC", "ll-REGION", and "CHARSET_NUMBER" + * to "ll-cc", "ll-region", and "charset-number", respectively. + */ + +static char * /* O - Language code string */ +ipp_get_code(const char *value, /* I - Locale/charset string */ + char *buffer, /* I - String buffer */ + size_t bufsize) /* I - Size of string buffer */ +{ + char *bufptr, /* Pointer into buffer */ + *bufend; /* End of buffer */ + + + /* + * Convert values to lowercase and change _ to - as needed... + */ + + for (bufptr = buffer, bufend = buffer + bufsize - 1; + *value && bufptr < bufend; + value ++) + if (*value == '_') + *bufptr++ = '-'; + else + *bufptr++ = _cups_tolower(*value); + + *bufptr = '\0'; + + /* + * Return the converted string... + */ + + return (buffer); +} + + +/* + * 'ipp_lang_code()' - Convert a C locale name into an IPP language code. + * + * This typically converts strings of the form "ll_CC" and "ll-REGION" to "ll-cc" and + * "ll-region", respectively. It also converts the "C" (POSIX) locale to "en". + */ + +static char * /* O - Language code string */ +ipp_lang_code(const char *locale, /* I - Locale string */ + char *buffer, /* I - String buffer */ + size_t bufsize) /* I - Size of string buffer */ +{ + /* + * Map POSIX ("C") locale to generic English, otherwise convert the locale string as-is. + */ + + if (!_cups_strcasecmp(locale, "c")) + { + strlcpy(buffer, "en", bufsize); + return (buffer); + } + else + return (ipp_get_code(locale, buffer, bufsize)); +} + + +/* + * 'ipp_length()' - Compute the length of an IPP message or collection value. + */ + +static size_t /* O - Size of IPP message */ +ipp_length(ipp_t *ipp, /* I - IPP message or collection */ + int collection) /* I - 1 if a collection, 0 otherwise */ +{ + int i; /* Looping var */ + size_t bytes; /* Number of bytes */ + ipp_attribute_t *attr; /* Current attribute */ + ipp_tag_t group; /* Current group */ + _ipp_value_t *value; /* Current value */ + + + DEBUG_printf(("3ipp_length(ipp=%p, collection=%d)", ipp, collection)); + + if (!ipp) + { + DEBUG_puts("4ipp_length: Returning 0 bytes"); + return (0); + } + + /* + * Start with 8 bytes for the IPP message header... + */ + + bytes = collection ? 0 : 8; + + /* + * Then add the lengths of each attribute... + */ + + group = IPP_TAG_ZERO; + + for (attr = ipp->attrs; attr != NULL; attr = attr->next) + { + if (attr->group_tag != group && !collection) + { + group = attr->group_tag; + if (group == IPP_TAG_ZERO) + continue; + + bytes ++; /* Group tag */ + } + + if (!attr->name) + continue; + + DEBUG_printf(("5ipp_length: attr->name=\"%s\", attr->num_values=%d, " + "bytes=" CUPS_LLFMT, attr->name, attr->num_values, CUPS_LLCAST bytes)); + + if (attr->value_tag < IPP_TAG_EXTENSION) + bytes += attr->num_values; /* Value tag for each value */ + else + bytes += 5 * attr->num_values; /* Value tag for each value */ + bytes += 2 * attr->num_values; /* Name lengths */ + bytes += (int)strlen(attr->name); /* Name */ + bytes += 2 * attr->num_values; /* Value lengths */ + + if (collection) + bytes += 5; /* Add membername overhead */ + + switch (attr->value_tag & ~IPP_TAG_COPY) + { + case IPP_TAG_UNSUPPORTED_VALUE : + case IPP_TAG_DEFAULT : + case IPP_TAG_UNKNOWN : + case IPP_TAG_NOVALUE : + case IPP_TAG_NOTSETTABLE : + case IPP_TAG_DELETEATTR : + case IPP_TAG_ADMINDEFINE : + break; + + case IPP_TAG_INTEGER : + case IPP_TAG_ENUM : + bytes += 4 * attr->num_values; + break; + + case IPP_TAG_BOOLEAN : + bytes += attr->num_values; + break; + + 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, value = attr->values; + i < attr->num_values; + i ++, value ++) + if (value->string.text) + bytes += strlen(value->string.text); + break; + + case IPP_TAG_DATE : + bytes += 11 * attr->num_values; + break; + + case IPP_TAG_RESOLUTION : + bytes += 9 * attr->num_values; + break; + + case IPP_TAG_RANGE : + bytes += 8 * attr->num_values; + break; + + case IPP_TAG_TEXTLANG : + case IPP_TAG_NAMELANG : + bytes += 4 * attr->num_values;/* Charset + text length */ + + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + { + if (value->string.language) + bytes += strlen(value->string.language); + + if (value->string.text) + bytes += strlen(value->string.text); + } + break; + + case IPP_TAG_BEGIN_COLLECTION : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + bytes += ipp_length(value->collection, 1); + break; + + default : + for (i = 0, value = attr->values; + i < attr->num_values; + i ++, value ++) + bytes += value->unknown.length; + break; + } + } + + /* + * Finally, add 1 byte for the "end of attributes" tag or 5 bytes + * for the "end of collection" tag and return... + */ + + if (collection) + bytes += 5; + else + bytes ++; + + DEBUG_printf(("4ipp_length: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST bytes)); + + return (bytes); +} + + +/* + * 'ipp_read_http()' - Semi-blocking read on a HTTP connection... + */ + +static ssize_t /* O - Number of bytes read */ +ipp_read_http(http_t *http, /* I - Client connection */ + ipp_uchar_t *buffer, /* O - Buffer for data */ + size_t length) /* I - Total length */ +{ + int tbytes, /* Total bytes read */ + bytes; /* Bytes read this pass */ + + + DEBUG_printf(("7ipp_read_http(http=%p, buffer=%p, length=%d)", + http, buffer, (int)length)); + + /* + * Loop until all bytes are read... + */ + + for (tbytes = 0, bytes = 0; + tbytes < (int)length; + tbytes += bytes, buffer += bytes) + { + DEBUG_printf(("9ipp_read_http: tbytes=%d, http->state=%d", tbytes, + http->state)); + + if (http->state == HTTP_WAITING) + break; + + if (http->used == 0 && !http->blocking) + { + /* + * Wait up to 10 seconds for more data on non-blocking sockets... + */ + + if (!httpWait(http, 10000)) + { + /* + * Signal no data... + */ + + bytes = -1; + break; + } + } + + if ((bytes = httpRead2(http, (char *)buffer, length - tbytes)) < 0) + { +#ifdef WIN32 + break; +#else + if (errno != EAGAIN && errno != EINTR) + break; + + bytes = 0; +#endif /* WIN32 */ + } + else if (bytes == 0) + break; + } + + /* + * Return the number of bytes read... + */ + + if (tbytes == 0 && bytes < 0) + tbytes = -1; + + DEBUG_printf(("8ipp_read_http: Returning %d bytes", tbytes)); + + return (tbytes); +} + + +/* + * 'ipp_read_file()' - Read IPP data from a file. + */ + +static ssize_t /* O - Number of bytes read */ +ipp_read_file(int *fd, /* I - File descriptor */ + ipp_uchar_t *buffer, /* O - Read buffer */ + size_t length) /* I - Number of bytes to read */ +{ +#ifdef WIN32 + return ((ssize_t)read(*fd, buffer, (unsigned)length)); +#else + return (read(*fd, buffer, length)); +#endif /* WIN32 */ +} + + +/* + * 'ipp_set_value()' - Get the value element from an attribute, expanding it as + * needed. + */ + +static _ipp_value_t * /* O - IPP value element or NULL on error */ +ipp_set_value(ipp_t *ipp, /* IO - IPP message */ + ipp_attribute_t **attr, /* IO - IPP attribute */ + int element) /* I - Value number (0-based) */ +{ + ipp_attribute_t *temp, /* New attribute pointer */ + *current, /* Current attribute in list */ + *prev; /* Previous attribute in list */ + int alloc_values; /* Allocated values */ + + + /* + * If we are setting an existing value element, return it... + */ + + temp = *attr; + + if (temp->num_values <= 1) + alloc_values = 1; + else + alloc_values = (temp->num_values + IPP_MAX_VALUES - 1) & + ~(IPP_MAX_VALUES - 1); + + if (element < alloc_values) + { + if (element >= temp->num_values) + temp->num_values = element + 1; + + return (temp->values + element); + } + + /* + * Otherwise re-allocate the attribute - we allocate in groups of IPP_MAX_VALUE + * values when num_values > 1. + */ + + if (alloc_values < IPP_MAX_VALUES) + alloc_values = IPP_MAX_VALUES; + else + alloc_values += IPP_MAX_VALUES; + + DEBUG_printf(("4ipp_set_value: Reallocating for up to %d values.", + alloc_values)); + + /* + * Reallocate memory... + */ + + if ((temp = realloc(temp, sizeof(ipp_attribute_t) + + (alloc_values - 1) * sizeof(_ipp_value_t))) == NULL) + { + _cupsSetHTTPError(HTTP_ERROR); + DEBUG_puts("4ipp_set_value: Unable to resize attribute."); + return (NULL); + } + + /* + * Zero the new memory... + */ + + memset(temp->values + temp->num_values, 0, + (alloc_values - temp->num_values) * sizeof(_ipp_value_t)); + + if (temp != *attr) + { + /* + * Reset pointers in the list... + */ + + if (ipp->current == *attr && ipp->prev) + { + /* + * Use current "previous" pointer... + */ + + prev = ipp->prev; + } + else + { + /* + * Find this attribute in the linked list... + */ + + for (prev = NULL, current = ipp->attrs; + current && current != *attr; + prev = current, current = current->next); + + if (!current) + { + /* + * This is a serious error! + */ + + *attr = temp; + _cupsSetError(IPP_INTERNAL_ERROR, + _("IPP attribute is not a member of the message."), 1); + DEBUG_puts("4ipp_set_value: Unable to find attribute in message."); + return (NULL); + } + } + + if (prev) + prev->next = temp; + else + ipp->attrs = temp; + + ipp->current = temp; + ipp->prev = prev; + + if (ipp->last == *attr) + ipp->last = temp; + + *attr = temp; + } + + /* + * Return the value element... + */ + + if (element >= temp->num_values) + temp->num_values = element + 1; + + return (temp->values + element); +} + + +/* + * 'ipp_write_file()' - Write IPP data to a file. + */ + +static ssize_t /* O - Number of bytes written */ +ipp_write_file(int *fd, /* I - File descriptor */ + ipp_uchar_t *buffer, /* I - Data to write */ + size_t length) /* I - Number of bytes to write */ +{ +#ifdef WIN32 + return ((ssize_t)write(*fd, buffer, (unsigned)length)); +#else + return (write(*fd, buffer, length)); +#endif /* WIN32 */ +} + + +/* + * End of "$Id: ipp.c 10102 2011-11-02 23:52:39Z mike $". + */ diff --git a/cups/ipp.h b/cups/ipp.h new file mode 100644 index 0000000..2fb1995 --- /dev/null +++ b/cups/ipp.h @@ -0,0 +1,671 @@ +/* + * "$Id: ipp.h 7847 2008-08-19 04:22:14Z mike $" + * + * Internet Printing Protocol definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 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. + */ + +#ifndef _CUPS_IPP_H_ +# define _CUPS_IPP_H_ + +/* + * Include necessary headers... + */ + +# include "http.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * IPP version string... + */ + +# define IPP_VERSION "\002\001" + +/* + * IPP registered port number... + * + * Note: Applications should never use IPP_PORT, but instead use the + * ippPort() function to allow overrides via the IPP_PORT environment + * variable and services file if needed! + */ + +# define IPP_PORT 631 + +/* + * Common limits... + */ + +# define IPP_MAX_LENGTH 32767 /* Maximum size of any single value */ +# define IPP_MAX_NAME 256 /* Maximum length of common name values */ +# define IPP_MAX_VALUES 8 /* Power-of-2 allocation increment */ + + +/* + * Types and structures... + */ + +typedef enum ipp_dstate_e /**** Document states ****/ +{ + IPP_DOCUMENT_PENDING = 3, + IPP_DOCUMENT_PROCESSING = 5, + IPP_DOCUMENT_CANCELED = 7, + IPP_DOCUMENT_ABORTED, + IPP_DOCUMENT_COMPLETED +} ipp_dstate_t; + +typedef enum ipp_finish_e /**** Finishings ****/ +{ + IPP_FINISHINGS_NONE = 3, /* No finishing */ + IPP_FINISHINGS_STAPLE, /* Staple (any location) */ + IPP_FINISHINGS_PUNCH, /* Punch (any location/count) */ + IPP_FINISHINGS_COVER, /* Add cover */ + IPP_FINISHINGS_BIND, /* Bind */ + IPP_FINISHINGS_SADDLE_STITCH, /* Staple interior */ + IPP_FINISHINGS_EDGE_STITCH, /* Stitch along any side */ + IPP_FINISHINGS_FOLD, /* Fold (any type) */ + IPP_FINISHINGS_TRIM, /* Trim (any type) */ + IPP_FINISHINGS_BALE, /* Bale (any type) */ + IPP_FINISHINGS_BOOKLET_MAKER, /* Fold to make booklet */ + IPP_FINISHINGS_JOB_OFFSET, /* Offset for binding (any type) */ + IPP_FINISHINGS_STAPLE_TOP_LEFT = 20, /* Staple top left corner */ + IPP_FINISHINGS_STAPLE_BOTTOM_LEFT, /* Staple bottom left corner */ + IPP_FINISHINGS_STAPLE_TOP_RIGHT, /* Staple top right corner */ + IPP_FINISHINGS_STAPLE_BOTTOM_RIGHT, /* Staple bottom right corner */ + IPP_FINISHINGS_EDGE_STITCH_LEFT, /* Stitch along left side */ + IPP_FINISHINGS_EDGE_STITCH_TOP, /* Stitch along top edge */ + IPP_FINISHINGS_EDGE_STITCH_RIGHT, /* Stitch along right side */ + IPP_FINISHINGS_EDGE_STITCH_BOTTOM, /* Stitch along bottom edge */ + IPP_FINISHINGS_STAPLE_DUAL_LEFT, /* Two staples on left */ + IPP_FINISHINGS_STAPLE_DUAL_TOP, /* Two staples on top */ + IPP_FINISHINGS_STAPLE_DUAL_RIGHT, /* Two staples on right */ + IPP_FINISHINGS_STAPLE_DUAL_BOTTOM, /* Two staples on bottom */ + IPP_FINISHINGS_BIND_LEFT = 50, /* Bind on left */ + IPP_FINISHINGS_BIND_TOP, /* Bind on top */ + IPP_FINISHINGS_BIND_RIGHT, /* Bind on right */ + IPP_FINISHINGS_BIND_BOTTOM, /* Bind on bottom */ + IPP_FINISHINGS_TRIM_AFTER_PAGES = 60, /* Trim output after each page */ + IPP_FINISHINGS_TRIM_AFTER_DOCUMENTS, /* Trim output after each document */ + IPP_FINISHINGS_TRIM_AFTER_COPIES, /* Trim output after each copy */ + IPP_FINISHINGS_TRIM_AFTER_JOB /* Trim output after job */ +} ipp_finish_t; + +typedef enum ipp_jcollate_e /**** Job collation types ****/ +{ + IPP_JOB_UNCOLLATED_SHEETS = 3, + IPP_JOB_COLLATED_DOCUMENTS, + IPP_JOB_UNCOLLATED_DOCUMENTS +} ipp_jcollate_t; + +typedef enum ipp_jstate_e /**** Job states ****/ +{ + IPP_JOB_PENDING = 3, /* Job is waiting to be printed */ + IPP_JOB_HELD, /* Job is held for printing */ + IPP_JOB_PROCESSING, /* Job is currently printing */ + IPP_JOB_STOPPED, /* Job has been stopped */ + IPP_JOB_CANCELED, /* Job has been canceled */ + IPP_JOB_ABORTED, /* Job has aborted due to error */ + IPP_JOB_COMPLETED /* Job has completed successfully */ + /* Legacy name for canceled state */ +#define IPP_JOB_CANCELLED IPP_JOB_CANCELED +} ipp_jstate_t; + +typedef enum ipp_op_e /**** IPP operations ****/ +{ + IPP_OP_CUPS_INVALID = -1, /* Invalid operation name for @link ippOpValue@ */ + IPP_PRINT_JOB = 0x0002, /* Print a single file */ + IPP_PRINT_URI, /* Print a single URL @private@ */ + IPP_VALIDATE_JOB, /* Validate job options */ + IPP_CREATE_JOB, /* Create an empty print job */ + IPP_SEND_DOCUMENT, /* Add a file to a job */ + IPP_SEND_URI, /* Add a URL to a job @private@ */ + IPP_CANCEL_JOB, /* Cancel a job */ + IPP_GET_JOB_ATTRIBUTES, /* Get job attributes */ + IPP_GET_JOBS, /* Get a list of jobs */ + IPP_GET_PRINTER_ATTRIBUTES, /* Get printer attributes */ + IPP_HOLD_JOB, /* Hold a job for printing */ + IPP_RELEASE_JOB, /* Release a job for printing */ + IPP_RESTART_JOB, /* Reprint a job */ + IPP_PAUSE_PRINTER = 0x0010, /* Stop a printer */ + IPP_RESUME_PRINTER, /* Start a printer */ + IPP_PURGE_JOBS, /* Cancel all jobs */ + IPP_SET_PRINTER_ATTRIBUTES, /* Set printer attributes @private@ */ + IPP_SET_JOB_ATTRIBUTES, /* Set job attributes */ + IPP_GET_PRINTER_SUPPORTED_VALUES, /* Get supported attribute values */ + IPP_CREATE_PRINTER_SUBSCRIPTION, /* Create a printer subscription @since CUPS 1.2/OS X 10.5@ */ + IPP_CREATE_JOB_SUBSCRIPTION, /* Create a job subscription @since CUPS 1.2/OS X 10.5@ */ + IPP_GET_SUBSCRIPTION_ATTRIBUTES, /* Get subscription attributes @since CUPS 1.2/OS X 10.5@ */ + IPP_GET_SUBSCRIPTIONS, /* Get list of subscriptions @since CUPS 1.2/OS X 10.5@ */ + IPP_RENEW_SUBSCRIPTION, /* Renew a printer subscription @since CUPS 1.2/OS X 10.5@ */ + IPP_CANCEL_SUBSCRIPTION, /* Cancel a subscription @since CUPS 1.2/OS X 10.5@ */ + IPP_GET_NOTIFICATIONS, /* Get notification events @since CUPS 1.2/OS X 10.5@ */ + IPP_SEND_NOTIFICATIONS, /* Send notification events @private@ */ + IPP_GET_RESOURCE_ATTRIBUTES, /* Get resource attributes @private@ */ + IPP_GET_RESOURCE_DATA, /* Get resource data @private@ */ + IPP_GET_RESOURCES, /* Get list of resources @private@ */ + IPP_GET_PRINT_SUPPORT_FILES, /* Get printer support files @private@ */ + IPP_ENABLE_PRINTER, /* Start a printer */ + IPP_DISABLE_PRINTER, /* Stop a printer */ + IPP_PAUSE_PRINTER_AFTER_CURRENT_JOB, /* Stop printer after the current job @private@ */ + IPP_HOLD_NEW_JOBS, /* Hold new jobs @private@ */ + IPP_RELEASE_HELD_NEW_JOBS, /* Release new jobs @private@ */ + IPP_DEACTIVATE_PRINTER, /* Stop a printer @private@ */ + IPP_ACTIVATE_PRINTER, /* Start a printer @private@ */ + IPP_RESTART_PRINTER, /* Restart a printer @private@ */ + IPP_SHUTDOWN_PRINTER, /* Turn a printer off @private@ */ + IPP_STARTUP_PRINTER, /* Turn a printer on @private@ */ + IPP_REPROCESS_JOB, /* Reprint a job @private@ */ + IPP_CANCEL_CURRENT_JOB, /* Cancel the current job @private@ */ + IPP_SUSPEND_CURRENT_JOB, /* Suspend the current job @private@ */ + IPP_RESUME_JOB, /* Resume the current job @private@ */ + IPP_PROMOTE_JOB, /* Promote a job to print sooner @private@ */ + IPP_SCHEDULE_JOB_AFTER, /* Schedule a job to print after another @private@ */ + IPP_CANCEL_DOCUMENT = 0x0033, /* Cancel-Document @private@ */ + IPP_GET_DOCUMENT_ATTRIBUTES, /* Get-Document-Attributes @private@ */ + IPP_GET_DOCUMENTS, /* Get-Documents @private@ */ + IPP_DELETE_DOCUMENT, /* Delete-Document @private@ */ + IPP_SET_DOCUMENT_ATTRIBUTES, /* Set-Document-Attributes @private@ */ + IPP_CANCEL_JOBS, /* Cancel-Jobs */ + IPP_CANCEL_MY_JOBS, /* Cancel-My-Jobs */ + IPP_RESUBMIT_JOB, /* Resubmit-Job */ + IPP_CLOSE_JOB, /* Close-Job */ + IPP_IDENTIFY_PRINTER, /* Identify-Printer (proposed IPP JPS3) */ + IPP_VALIDATE_DOCUMENT, /* Validate-Document (proposed IPP JPS3) */ + IPP_PRIVATE = 0x4000, /* Reserved @private@ */ + CUPS_GET_DEFAULT, /* Get the default printer */ + CUPS_GET_PRINTERS, /* Get a list of printers and/or classes */ + CUPS_ADD_MODIFY_PRINTER, /* Add or modify a printer */ + CUPS_DELETE_PRINTER, /* Delete a printer */ + CUPS_GET_CLASSES, /* Get a list of classes @deprecated@ */ + CUPS_ADD_MODIFY_CLASS, /* Add or modify a class */ + CUPS_DELETE_CLASS, /* Delete a class */ + CUPS_ACCEPT_JOBS, /* Accept new jobs on a printer */ + CUPS_REJECT_JOBS, /* Reject new jobs on a printer */ + CUPS_SET_DEFAULT, /* Set the default printer */ + CUPS_GET_DEVICES, /* Get a list of supported devices */ + CUPS_GET_PPDS, /* Get a list of supported drivers */ + CUPS_MOVE_JOB, /* Move a job to a different printer */ + CUPS_AUTHENTICATE_JOB, /* Authenticate a job @since CUPS 1.2/OS X 10.5@ */ + CUPS_GET_PPD, /* Get a PPD file @since CUPS 1.3/OS X 10.5@ */ + CUPS_GET_DOCUMENT = 0x4027 /* Get a document file @since CUPS 1.4/OS X 10.6@ */ + + /* Legacy names for the add operations */ +#define CUPS_ADD_PRINTER CUPS_ADD_MODIFY_PRINTER +#define CUPS_ADD_CLASS CUPS_ADD_MODIFY_CLASS +} ipp_op_t; + +typedef enum ipp_orient_e /**** Orientation values ****/ +{ + IPP_PORTRAIT = 3, /* No rotation */ + IPP_LANDSCAPE, /* 90 degrees counter-clockwise */ + IPP_REVERSE_LANDSCAPE, /* 90 degrees clockwise */ + IPP_REVERSE_PORTRAIT /* 180 degrees */ +} ipp_orient_t; + +typedef enum ipp_pstate_e /**** Printer states ****/ +{ + IPP_PRINTER_IDLE = 3, /* Printer is idle */ + IPP_PRINTER_PROCESSING, /* Printer is working */ + IPP_PRINTER_STOPPED /* Printer is stopped */ +} ipp_pstate_t; + +typedef enum ipp_quality_e /**** Qualities ****/ +{ + IPP_QUALITY_DRAFT = 3, /* Draft quality */ + IPP_QUALITY_NORMAL, /* Normal quality */ + IPP_QUALITY_HIGH /* High quality */ +} ipp_quality_t; + +typedef enum ipp_res_e /**** Resolution units ****/ +{ + IPP_RES_PER_INCH = 3, /* Pixels per inch */ + IPP_RES_PER_CM /* Pixels per centimeter */ +} ipp_res_t; + +typedef enum ipp_state_e /**** IPP states ****/ +{ + IPP_ERROR = -1, /* An error occurred */ + IPP_IDLE, /* Nothing is happening/request completed */ + IPP_HEADER, /* The request header needs to be sent/received */ + IPP_ATTRIBUTE, /* One or more attributes need to be sent/received */ + IPP_DATA /* IPP request data needs to be sent/received */ +} ipp_state_t; + +typedef enum ipp_status_e /**** IPP status codes ****/ +{ + IPP_STATUS_CUPS_INVALID = -1, /* Invalid status name for @link ippErrorValue@ */ + IPP_OK = 0x0000, /* successful-ok */ + IPP_OK_SUBST, /* successful-ok-ignored-or-substituted-attributes */ + IPP_OK_CONFLICT, /* successful-ok-conflicting-attributes */ + IPP_OK_IGNORED_SUBSCRIPTIONS, /* successful-ok-ignored-subscriptions */ + IPP_OK_IGNORED_NOTIFICATIONS, /* successful-ok-ignored-notifications @private@ */ + IPP_OK_TOO_MANY_EVENTS, /* successful-ok-too-many-events */ + IPP_OK_BUT_CANCEL_SUBSCRIPTION, /* successful-ok-but-cancel-subscription @private@ */ + IPP_OK_EVENTS_COMPLETE, /* successful-ok-events-complete */ + IPP_REDIRECTION_OTHER_SITE = 0x200, /* redirection-other-site @private@ */ + CUPS_SEE_OTHER = 0x280, /* cups-see-other */ + IPP_BAD_REQUEST = 0x0400, /* client-error-bad-request */ + IPP_FORBIDDEN, /* client-error-forbidden */ + IPP_NOT_AUTHENTICATED, /* client-error-not-authenticated */ + IPP_NOT_AUTHORIZED, /* client-error-not-authorized */ + IPP_NOT_POSSIBLE, /* client-error-not-possible */ + IPP_TIMEOUT, /* client-error-timeout */ + IPP_NOT_FOUND, /* client-error-not-found */ + IPP_GONE, /* client-error-gone */ + IPP_REQUEST_ENTITY, /* client-error-request-entity-too-large */ + IPP_REQUEST_VALUE, /* client-error-request-value-too-long */ + IPP_DOCUMENT_FORMAT, /* client-error-document-format-not-supported */ + IPP_ATTRIBUTES, /* client-error-attributes-or-values-not-supported */ + IPP_URI_SCHEME, /* client-error-uri-scheme-not-supported */ + IPP_CHARSET, /* client-error-charset-not-supported */ + IPP_CONFLICT, /* client-error-conflicting-attributes */ + IPP_COMPRESSION_NOT_SUPPORTED, /* client-error-compression-not-supported */ + IPP_COMPRESSION_ERROR, /* client-error-compression-error */ + IPP_DOCUMENT_FORMAT_ERROR, /* client-error-document-format-error */ + IPP_DOCUMENT_ACCESS_ERROR, /* client-error-document-access-error */ + IPP_ATTRIBUTES_NOT_SETTABLE, /* client-error-attributes-not-settable */ + IPP_IGNORED_ALL_SUBSCRIPTIONS, /* client-error-ignored-all-subscriptions */ + IPP_TOO_MANY_SUBSCRIPTIONS, /* client-error-too-many-subscriptions */ + IPP_IGNORED_ALL_NOTIFICATIONS, /* client-error-ignored-all-notifications @private@ */ + IPP_PRINT_SUPPORT_FILE_NOT_FOUND, /* client-error-print-support-file-not-found @private@ */ + IPP_DOCUMENT_PASSWORD_ERROR, /* client-error-document-password-error */ + IPP_DOCUMENT_PERMISSION_ERROR, /* client-error-document-permission-error */ + IPP_DOCUMENT_SECURITY_ERROR, /* client-error-document-security-error */ + IPP_DOCUMENT_UNPRINTABLE_ERROR, /* client-error-document-unprintable-error */ + + IPP_INTERNAL_ERROR = 0x0500, /* server-error-internal-error */ + IPP_OPERATION_NOT_SUPPORTED, /* server-error-operation-not-supported */ + IPP_SERVICE_UNAVAILABLE, /* server-error-service-unavailable */ + IPP_VERSION_NOT_SUPPORTED, /* server-error-version-not-supported */ + IPP_DEVICE_ERROR, /* server-error-device-error */ + IPP_TEMPORARY_ERROR, /* server-error-temporary-error */ + IPP_NOT_ACCEPTING, /* server-error-not-accepting-jobs */ + IPP_PRINTER_BUSY, /* server-error-busy */ + IPP_ERROR_JOB_CANCELED, /* server-error-job-canceled */ + IPP_MULTIPLE_JOBS_NOT_SUPPORTED, /* server-error-multiple-document-jobs-not-supported */ + IPP_PRINTER_IS_DEACTIVATED, /* server-error-printer-is-deactivated */ + IPP_TOO_MANY_JOBS, /* server-error-too-many-jobs */ + IPP_TOO_MANY_DOCUMENTS, /* server-error-too-many-documents */ + + IPP_AUTHENTICATION_CANCELED = 0x1000, /* Authentication canceled by user @since CUPS 1.5/OS X 10.7@ */ + IPP_PKI_ERROR, /* Error negotiating a secure connection @since CUPS 1.5/OS X 10.7@ */ + IPP_UPGRADE_REQUIRED /* TLS upgrade required */ + + /* Legacy name for canceled status */ +#define IPP_ERROR_JOB_CANCELLED IPP_ERROR_JOB_CANCELED + +} ipp_status_t; + +typedef enum ipp_tag_e /**** Format tags for attributes ****/ +{ + IPP_TAG_CUPS_INVALID = -1, /* Invalid tag name for @link ippTagValue@ */ + IPP_TAG_ZERO = 0x00, /* Zero tag - used for separators */ + IPP_TAG_OPERATION, /* Operation group */ + IPP_TAG_JOB, /* Job group */ + IPP_TAG_END, /* End-of-attributes */ + IPP_TAG_PRINTER, /* Printer group */ + IPP_TAG_UNSUPPORTED_GROUP, /* Unsupported attributes group */ + IPP_TAG_SUBSCRIPTION, /* Subscription group */ + IPP_TAG_EVENT_NOTIFICATION, /* Event group */ + IPP_TAG_RESOURCE, /* Resource group @private@ */ + IPP_TAG_DOCUMENT, /* Document group */ + IPP_TAG_UNSUPPORTED_VALUE = 0x10, /* Unsupported value */ + IPP_TAG_DEFAULT, /* Default value */ + IPP_TAG_UNKNOWN, /* Unknown value */ + IPP_TAG_NOVALUE, /* No-value value */ + IPP_TAG_NOTSETTABLE = 0x15, /* Not-settable value */ + IPP_TAG_DELETEATTR, /* Delete-attribute value */ + IPP_TAG_ADMINDEFINE, /* Admin-defined value */ + IPP_TAG_INTEGER = 0x21, /* Integer value */ + IPP_TAG_BOOLEAN, /* Boolean value */ + IPP_TAG_ENUM, /* Enumeration value */ + IPP_TAG_STRING = 0x30, /* Octet string value */ + IPP_TAG_DATE, /* Date/time value */ + IPP_TAG_RESOLUTION, /* Resolution value */ + IPP_TAG_RANGE, /* Range value */ + IPP_TAG_BEGIN_COLLECTION, /* Beginning of collection value */ + IPP_TAG_TEXTLANG, /* Text-with-language value */ + IPP_TAG_NAMELANG, /* Name-with-language value */ + IPP_TAG_END_COLLECTION, /* End of collection value */ + IPP_TAG_TEXT = 0x41, /* Text value */ + IPP_TAG_NAME, /* Name value */ + IPP_TAG_RESERVED_STRING, /* Reserved for future string value @private@ */ + IPP_TAG_KEYWORD, /* Keyword value */ + IPP_TAG_URI, /* URI value */ + IPP_TAG_URISCHEME, /* URI scheme value */ + IPP_TAG_CHARSET, /* Character set value */ + IPP_TAG_LANGUAGE, /* Language value */ + IPP_TAG_MIMETYPE, /* MIME media type value */ + IPP_TAG_MEMBERNAME, /* Collection member name value */ + IPP_TAG_EXTENSION = 0x7f, /* Extension point for 32-bit tags */ + IPP_TAG_MASK = 0x7fffffff, /* Mask for copied attribute values @private@ */ + /* The following expression is used to avoid compiler warnings with +/-0x80000000 */ + IPP_TAG_COPY = -0x7fffffff-1 /* Bitflag for copied attribute values @private@ */ +} ipp_tag_t; + +typedef unsigned char ipp_uchar_t; /**** Unsigned 8-bit integer/character ****/ +typedef struct _ipp_s ipp_t; /**** IPP request/response data ****/ +typedef struct _ipp_attribute_s ipp_attribute_t; + /**** IPP attribute ****/ + +/**** New in CUPS 1.2/OS X 10.5 ****/ +typedef ssize_t (*ipp_iocb_t)(void *context, ipp_uchar_t *buffer, size_t bytes); + /**** IPP IO Callback Function @since CUPS 1.2/OS X 10.5@ ****/ + +/**** New in CUPS 1.6/OS X 10.8 ****/ +typedef int (*ipp_copycb_t)(void *context, ipp_t *dst, ipp_attribute_t *attr); + + +/* + * The following structures are PRIVATE starting with CUPS 1.6/OS X 10.8. + * Please use the new accessor functions available in CUPS 1.6 and later, as + * these definitions will be moved to a private header file in a future release. + * + * Define _IPP_PRIVATE_STRUCTURES to cause the private IPP structures to be + * exposed in CUPS 1.6. This happens automatically on OS X when compiling for + * a deployment target of 10.7 or earlier. + */ + +# if defined(_CUPS_SOURCE) || defined(_CUPS_IPP_PRIVATE_H_) + /* Building CUPS */ +# define _IPP_PRIVATE_STRUCTURES 1 +# elif defined(__APPLE__) +# if defined(MAC_OS_X_VERSION_10_8) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8 + /* Building for 10.7 and earlier */ +# define _IPP_PRIVATE_STRUCTURES 1 +# elif !defined(MAC_OS_X_VERSION_10_8) + /* Building for 10.7 and earlier */ +# define _IPP_PRIVATE_STRUCTURES 1 +# endif /* MAC_OS_X_VERSION_10_8 && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8 */ +# endif /* _CUPS_SOURCE || _CUPS_IPP_PRIVATE_H_ */ + +# ifdef _IPP_PRIVATE_STRUCTURES +typedef union _ipp_request_u /**** Request Header ****/ +{ + struct /* Any Header */ + { + ipp_uchar_t version[2]; /* Protocol version number */ + int op_status; /* Operation ID or status code*/ + int request_id; /* Request ID */ + } any; + + struct /* Operation Header */ + { + ipp_uchar_t version[2]; /* Protocol version number */ + ipp_op_t operation_id; /* Operation ID */ + int request_id; /* Request ID */ + } op; + + struct /* Status Header */ + { + ipp_uchar_t version[2]; /* Protocol version number */ + ipp_status_t status_code; /* Status code */ + int request_id; /* Request ID */ + } status; + + /**** New in CUPS 1.1.19 ****/ + struct /* Event Header @since CUPS 1.1.19/OS X 10.3@ */ + { + ipp_uchar_t version[2]; /* Protocol version number */ + ipp_status_t status_code; /* Status code */ + int request_id; /* Request ID */ + } event; +} _ipp_request_t; + +/**** New in CUPS 1.1.19 ****/ + +typedef union _ipp_value_u /**** Attribute Value ****/ +{ + int integer; /* Integer/enumerated value */ + + char boolean; /* Boolean value */ + + ipp_uchar_t date[11]; /* Date/time value */ + + struct + { + int xres, /* Horizontal resolution */ + yres; /* Vertical resolution */ + ipp_res_t units; /* Resolution units */ + } resolution; /* Resolution value */ + + struct + { + int lower, /* Lower value */ + upper; /* Upper value */ + } range; /* Range of integers value */ + + struct + { + char *language; /* Language code */ + char *text; /* String */ + } string; /* String with language value */ + + struct + { + int length; /* Length of attribute */ + void *data; /* Data in attribute */ + } unknown; /* Unknown attribute type */ + +/**** New in CUPS 1.1.19 ****/ + ipp_t *collection; /* Collection value @since CUPS 1.1.19/OS X 10.3@ */ +} _ipp_value_t; +typedef _ipp_value_t ipp_value_t; /**** Convenience typedef that will be removed @private@ ****/ + +struct _ipp_attribute_s /**** Attribute ****/ +{ + ipp_attribute_t *next; /* Next attribute in list */ + ipp_tag_t group_tag, /* Job/Printer/Operation group tag */ + value_tag; /* What type of value is it? */ + char *name; /* Name of attribute */ + int num_values; /* Number of values */ + _ipp_value_t values[1]; /* Values */ +}; + +struct _ipp_s /**** IPP Request/Response/Notification ****/ +{ + ipp_state_t state; /* State of request */ + _ipp_request_t request; /* Request header */ + ipp_attribute_t *attrs; /* Attributes */ + ipp_attribute_t *last; /* Last attribute in list */ + ipp_attribute_t *current; /* Current attribute (for read/write) */ + ipp_tag_t curtag; /* Current attribute group tag */ + +/**** New in CUPS 1.2 ****/ + ipp_attribute_t *prev; /* Previous attribute (for read) @since CUPS 1.2/OS X 10.5@ */ + +/**** New in CUPS 1.4.4 ****/ + int use; /* Use count @since CUPS 1.4.4/OS X 10.6.?@ */ +}; +# endif /* _IPP_PRIVATE_STRUCTURES */ + + +/* + * Prototypes... + */ + +extern ipp_attribute_t *ippAddBoolean(ipp_t *ipp, ipp_tag_t group, + const char *name, char value); +extern ipp_attribute_t *ippAddBooleans(ipp_t *ipp, ipp_tag_t group, + const char *name, int num_values, + const char *values); +extern ipp_attribute_t *ippAddDate(ipp_t *ipp, ipp_tag_t group, + const char *name, const ipp_uchar_t *value); +extern ipp_attribute_t *ippAddInteger(ipp_t *ipp, ipp_tag_t group, + ipp_tag_t value_tag, const char *name, + int value); +extern ipp_attribute_t *ippAddIntegers(ipp_t *ipp, ipp_tag_t group, + ipp_tag_t value_tag, const char *name, + int num_values, const int *values); +extern ipp_attribute_t *ippAddRange(ipp_t *ipp, ipp_tag_t group, + const char *name, int lower, int upper); +extern ipp_attribute_t *ippAddRanges(ipp_t *ipp, ipp_tag_t group, + const char *name, int num_values, + const int *lower, const int *upper); +extern ipp_attribute_t *ippAddResolution(ipp_t *ipp, ipp_tag_t group, + const char *name, ipp_res_t units, + int xres, int yres); +extern ipp_attribute_t *ippAddResolutions(ipp_t *ipp, ipp_tag_t group, + const char *name, int num_values, + ipp_res_t units, const int *xres, + const int *yres); +extern ipp_attribute_t *ippAddSeparator(ipp_t *ipp); +extern ipp_attribute_t *ippAddString(ipp_t *ipp, ipp_tag_t group, + ipp_tag_t value_tag, const char *name, + const char *language, const char *value); +extern ipp_attribute_t *ippAddStrings(ipp_t *ipp, ipp_tag_t group, + ipp_tag_t value_tag, const char *name, + int num_values, const char *language, + const char * const *values); +extern time_t ippDateToTime(const ipp_uchar_t *date); +extern void ippDelete(ipp_t *ipp); +extern const char *ippErrorString(ipp_status_t error); +extern ipp_attribute_t *ippFindAttribute(ipp_t *ipp, const char *name, + ipp_tag_t value_tag); +extern ipp_attribute_t *ippFindNextAttribute(ipp_t *ipp, const char *name, + ipp_tag_t value_tag); +extern size_t ippLength(ipp_t *ipp); +extern ipp_t *ippNew(void); +extern ipp_state_t ippRead(http_t *http, ipp_t *ipp); +extern const ipp_uchar_t *ippTimeToDate(time_t t); +extern ipp_state_t ippWrite(http_t *http, ipp_t *ipp); +extern int ippPort(void); +extern void ippSetPort(int p); + +/**** New in CUPS 1.1.19 ****/ +extern ipp_attribute_t *ippAddCollection(ipp_t *ipp, ipp_tag_t group, + const char *name, ipp_t *value) _CUPS_API_1_1_19; +extern ipp_attribute_t *ippAddCollections(ipp_t *ipp, ipp_tag_t group, + const char *name, int num_values, + const ipp_t **values) _CUPS_API_1_1_19; +extern void ippDeleteAttribute(ipp_t *ipp, ipp_attribute_t *attr) _CUPS_API_1_1_19; +extern ipp_state_t ippReadFile(int fd, ipp_t *ipp) _CUPS_API_1_1_19; +extern ipp_state_t ippWriteFile(int fd, ipp_t *ipp) _CUPS_API_1_1_19; + +/**** New in CUPS 1.2/OS X 10.5 ****/ +extern ipp_attribute_t *ippAddOctetString(ipp_t *ipp, ipp_tag_t group, + const char *name, + const void *data, int datalen) _CUPS_API_1_2; +extern ipp_status_t ippErrorValue(const char *name) _CUPS_API_1_2; +extern ipp_t *ippNewRequest(ipp_op_t op) _CUPS_API_1_2; +extern const char *ippOpString(ipp_op_t op) _CUPS_API_1_2; +extern ipp_op_t ippOpValue(const char *name) _CUPS_API_1_2; +extern ipp_state_t ippReadIO(void *src, ipp_iocb_t cb, int blocking, + ipp_t *parent, ipp_t *ipp) _CUPS_API_1_2; +extern ipp_state_t ippWriteIO(void *dst, ipp_iocb_t cb, int blocking, + ipp_t *parent, ipp_t *ipp) _CUPS_API_1_2; + +/**** New in CUPS 1.4/OS X 10.6 ****/ +extern const char *ippTagString(ipp_tag_t tag) _CUPS_API_1_4; +extern ipp_tag_t ippTagValue(const char *name) _CUPS_API_1_4; + +/**** New in CUPS 1.6/OS X 10.8 ****/ +extern ipp_attribute_t *ippAddOutOfBand(ipp_t *ipp, ipp_tag_t group, + ipp_tag_t value_tag, const char *name) + _CUPS_API_1_6; +extern size_t ippAttributeString(ipp_attribute_t *attr, char *buffer, + size_t bufsize) _CUPS_API_1_6; +extern ipp_attribute_t *ippCopyAttribute(ipp_t *dst, ipp_attribute_t *attr, + int quickcopy) _CUPS_API_1_6; +extern int ippCopyAttributes(ipp_t *dst, ipp_t *src, + int quickcopy, ipp_copycb_t cb, + void *context) _CUPS_API_1_6; +extern int ippDeleteValues(ipp_t *ipp, ipp_attribute_t **attr, + int element, int count) _CUPS_API_1_6; +extern const char *ippEnumString(const char *attrname, int enumvalue) + _CUPS_API_1_6; +extern int ippEnumValue(const char *attrname, + const char *enumstring) _CUPS_API_1_6; +extern ipp_attribute_t *ippFirstAttribute(ipp_t *ipp) _CUPS_API_1_6; +extern int ippGetBoolean(ipp_attribute_t *attr, int element) + _CUPS_API_1_6; +extern ipp_t *ippGetCollection(ipp_attribute_t *attr, + int element) _CUPS_API_1_6; +extern int ippGetCount(ipp_attribute_t *attr) _CUPS_API_1_6; +extern const ipp_uchar_t *ippGetDate(ipp_attribute_t *attr, int element) + _CUPS_API_1_6; +extern ipp_tag_t ippGetGroupTag(ipp_attribute_t *attr) _CUPS_API_1_6; +extern int ippGetInteger(ipp_attribute_t *attr, int element) + _CUPS_API_1_6; +extern const char *ippGetName(ipp_attribute_t *attr) _CUPS_API_1_6; +extern ipp_op_t ippGetOperation(ipp_t *ipp) _CUPS_API_1_6; +extern int ippGetRange(ipp_attribute_t *attr, int element, + int *upper) _CUPS_API_1_6; +extern int ippGetRequestId(ipp_t *ipp) _CUPS_API_1_6; +extern int ippGetResolution(ipp_attribute_t *attr, int element, + int *yres, ipp_res_t *units) + _CUPS_API_1_6; +extern ipp_state_t ippGetState(ipp_t *ipp) _CUPS_API_1_6; +extern ipp_status_t ippGetStatusCode(ipp_t *ipp) _CUPS_API_1_6; +extern const char *ippGetString(ipp_attribute_t *attr, int element, + const char **language) _CUPS_API_1_6; +extern ipp_tag_t ippGetValueTag(ipp_attribute_t *attr) _CUPS_API_1_6; +extern int ippGetVersion(ipp_t *ipp, int *minor) _CUPS_API_1_6; +extern ipp_attribute_t *ippNextAttribute(ipp_t *ipp) _CUPS_API_1_6; +extern int ippSetBoolean(ipp_t *ipp, ipp_attribute_t **attr, + int element, int boolvalue) _CUPS_API_1_6; +extern int ippSetCollection(ipp_t *ipp, ipp_attribute_t **attr, + int element, ipp_t *colvalue) + _CUPS_API_1_6; +extern int ippSetDate(ipp_t *ipp, ipp_attribute_t **attr, + int element, const ipp_uchar_t *datevalue) + _CUPS_API_1_6; +extern int ippSetGroupTag(ipp_t *ipp, ipp_attribute_t **attr, + ipp_tag_t group_tag) _CUPS_API_1_6; +extern int ippSetInteger(ipp_t *ipp, ipp_attribute_t **attr, + int element, int intvalue) _CUPS_API_1_6; +extern int ippSetName(ipp_t *ipp, ipp_attribute_t **attr, + const char *name) _CUPS_API_1_6; +extern int ippSetOperation(ipp_t *ipp, ipp_op_t op) _CUPS_API_1_6; +extern int ippSetRange(ipp_t *ipp, ipp_attribute_t **attr, + int element, int lowervalue, int uppervalue) + _CUPS_API_1_6; +extern int ippSetRequestId(ipp_t *ipp, int request_id) + _CUPS_API_1_6; +extern int ippSetResolution(ipp_t *ipp, ipp_attribute_t **attr, + int element, ipp_res_t unitsvalue, + int xresvalue, int yresvalue) + _CUPS_API_1_6; +extern int ippSetState(ipp_t *ipp, ipp_state_t state) + _CUPS_API_1_6; +extern int ippSetStatusCode(ipp_t *ipp, ipp_status_t status) + _CUPS_API_1_6; +extern int ippSetString(ipp_t *ipp, ipp_attribute_t **attr, + int element, const char *strvalue) + _CUPS_API_1_6; +extern int ippSetValueTag(ipp_t *ipp, ipp_attribute_t **attr, + ipp_tag_t value_tag) _CUPS_API_1_6; +extern int ippSetVersion(ipp_t *ipp, int major, int minor) + _CUPS_API_1_6; + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_IPP_H_ */ + +/* + * End of "$Id: ipp.h 7847 2008-08-19 04:22:14Z mike $". + */ diff --git a/cups/langprintf.c b/cups/langprintf.c new file mode 100644 index 0000000..1a9f8b7 --- /dev/null +++ b/cups/langprintf.c @@ -0,0 +1,352 @@ +/* + * "$Id: langprintf.c 7802 2008-07-28 18:50:45Z mike $" + * + * Localized printf/puts functions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 2002-2007 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. + * + * Contents: + * + * _cupsLangPrintError() - Print a message followed by a standard error. + * _cupsLangPrintFilter() - Print a formatted filter message string to a file. + * _cupsLangPrintf() - Print a formatted message string to a file. + * _cupsLangPuts() - Print a static message string to a file. + * _cupsSetLocale() - Set the current locale and transcode the + * command-line. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * '_cupsLangPrintError()' - Print a message followed by a standard error. + */ + +void +_cupsLangPrintError(const char *prefix, /* I - Non-localized message prefix */ + const char *message)/* I - Message */ +{ + int bytes; /* Number of bytes formatted */ + int last_errno; /* Last error */ + char buffer[2048], /* Message buffer */ + *bufptr, /* Pointer into buffer */ + output[8192]; /* Output buffer */ + _cups_globals_t *cg; /* Global data */ + + + /* + * Range check... + */ + + if (!message) + return; + + /* + * Save the errno value... + */ + + last_errno = errno; + + /* + * Get the message catalog... + */ + + cg = _cupsGlobals(); + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + /* + * Format the message... + */ + + if (prefix) + { + snprintf(buffer, sizeof(buffer), "%s:", prefix); + bufptr = buffer + strlen(buffer); + } + else + bufptr = buffer; + + snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), + /* TRANSLATORS: Message is "subject: error" */ + _cupsLangString(cg->lang_default, _("%s: %s")), + _cupsLangString(cg->lang_default, message), strerror(last_errno)); + strlcat(buffer, "\n", sizeof(buffer)); + + /* + * Convert and write to stderr... + */ + + bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output), + cg->lang_default->encoding); + + if (bytes > 0) + fwrite(output, 1, bytes, stderr); +} + + +/* + * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file. + */ + +int /* O - Number of bytes written */ +_cupsLangPrintFilter( + FILE *fp, /* I - File to write to */ + const char *prefix, /* I - Non-localized message prefix */ + const char *message, /* I - Message string to use */ + ...) /* I - Additional arguments as needed */ +{ + int bytes; /* Number of bytes formatted */ + char temp[2048], /* Temporary format buffer */ + buffer[2048], /* Message buffer */ + output[8192]; /* Output buffer */ + va_list ap; /* Pointer to additional arguments */ + _cups_globals_t *cg; /* Global data */ + + + /* + * Range check... + */ + + if (!fp || !message) + return (-1); + + cg = _cupsGlobals(); + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + /* + * Format the string... + */ + + va_start(ap, message); + snprintf(temp, sizeof(temp), "%s: %s\n", prefix, + _cupsLangString(cg->lang_default, message)); + vsnprintf(buffer, sizeof(buffer), temp, ap); + va_end(ap); + + /* + * Transcode to the destination charset... + */ + + bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output), + cg->lang_default->encoding); + + /* + * Write the string and return the number of bytes written... + */ + + if (bytes > 0) + return ((int)fwrite(output, 1, bytes, fp)); + else + return (bytes); +} + + +/* + * '_cupsLangPrintf()' - Print a formatted message string to a file. + */ + +int /* O - Number of bytes written */ +_cupsLangPrintf(FILE *fp, /* I - File to write to */ + const char *message, /* I - Message string to use */ + ...) /* I - Additional arguments as needed */ +{ + int bytes; /* Number of bytes formatted */ + char buffer[2048], /* Message buffer */ + output[8192]; /* Output buffer */ + va_list ap; /* Pointer to additional arguments */ + _cups_globals_t *cg; /* Global data */ + + + /* + * Range check... + */ + + if (!fp || !message) + return (-1); + + cg = _cupsGlobals(); + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + /* + * Format the string... + */ + + va_start(ap, message); + vsnprintf(buffer, sizeof(buffer) - 1, + _cupsLangString(cg->lang_default, message), ap); + va_end(ap); + + strlcat(buffer, "\n", sizeof(buffer)); + + /* + * Transcode to the destination charset... + */ + + bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output), + cg->lang_default->encoding); + + /* + * Write the string and return the number of bytes written... + */ + + if (bytes > 0) + return ((int)fwrite(output, 1, bytes, fp)); + else + return (bytes); +} + + +/* + * '_cupsLangPuts()' - Print a static message string to a file. + */ + +int /* O - Number of bytes written */ +_cupsLangPuts(FILE *fp, /* I - File to write to */ + const char *message) /* I - Message string to use */ +{ + int bytes; /* Number of bytes formatted */ + char output[8192]; /* Message buffer */ + _cups_globals_t *cg; /* Global data */ + + + /* + * Range check... + */ + + if (!fp || !message) + return (-1); + + cg = _cupsGlobals(); + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + /* + * Transcode to the destination charset... + */ + + bytes = cupsUTF8ToCharset(output, + (cups_utf8_t *)_cupsLangString(cg->lang_default, + message), + sizeof(output) - 4, cg->lang_default->encoding); + bytes += cupsUTF8ToCharset(output + bytes, (cups_utf8_t *)"\n", + sizeof(output) - bytes, + cg->lang_default->encoding); + + /* + * Write the string and return the number of bytes written... + */ + + if (bytes > 0) + return ((int)fwrite(output, 1, bytes, fp)); + else + return (bytes); +} + + +/* + * '_cupsSetLocale()' - Set the current locale and transcode the command-line. + */ + +void +_cupsSetLocale(char *argv[]) /* IO - Command-line arguments */ +{ + int i; /* Looping var */ + char buffer[8192]; /* Command-line argument buffer */ + _cups_globals_t *cg; /* Global data */ +#ifdef LC_TIME + const char *lc_time; /* Current LC_TIME value */ + char new_lc_time[255], /* New LC_TIME value */ + *charset; /* Pointer to character set */ +#endif /* LC_TIME */ + + + /* + * Set the locale so that times, etc. are displayed properly. + * + * Unfortunately, while we need the localized time value, we *don't* + * want to use the localized charset for the time value, so we need + * to set LC_TIME to the locale name with .UTF-8 on the end (if + * the locale includes a character set specifier...) + */ + + setlocale(LC_ALL, ""); + +#ifdef LC_TIME + if ((lc_time = setlocale(LC_TIME, NULL)) == NULL) + lc_time = setlocale(LC_ALL, NULL); + + if (lc_time) + { + strlcpy(new_lc_time, lc_time, sizeof(new_lc_time)); + if ((charset = strchr(new_lc_time, '.')) == NULL) + charset = new_lc_time + strlen(new_lc_time); + + strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time)); + } + else + strcpy(new_lc_time, "C"); + + setlocale(LC_TIME, new_lc_time); +#endif /* LC_TIME */ + + /* + * Initialize the default language info... + */ + + cg = _cupsGlobals(); + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + /* + * Transcode the command-line arguments from the locale charset to + * UTF-8... + */ + + if (cg->lang_default->encoding != CUPS_US_ASCII && + cg->lang_default->encoding != CUPS_UTF8) + { + for (i = 1; argv[i]; i ++) + { + /* + * Try converting from the locale charset to UTF-8... + */ + + if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer), + cg->lang_default->encoding) < 0) + continue; + + /* + * Save the new string if it differs from the original... + */ + + if (strcmp(buffer, argv[i])) + argv[i] = strdup(buffer); + } + } +} + + +/* + * End of "$Id: langprintf.c 7802 2008-07-28 18:50:45Z mike $". + */ diff --git a/cups/language-private.h b/cups/language-private.h new file mode 100644 index 0000000..cf5b482 --- /dev/null +++ b/cups/language-private.h @@ -0,0 +1,86 @@ +/* + * "$Id$" + * + * Private localization support for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2006 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. + */ + +#ifndef _CUPS_LANGUAGE_PRIVATE_H_ +# define _CUPS_LANGUAGE_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include +# include + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Macro for localized text... + */ + +# define _(x) x + + +/* + * Types... + */ + +typedef struct _cups_message_s /**** Message catalog entry ****/ +{ + char *id, /* Original string */ + *str; /* Localized string */ +} _cups_message_t; + + +/* + * Prototypes... + */ + +# ifdef __APPLE__ +extern const char *_cupsAppleLanguage(const char *locale, char *language, + size_t langsize); +# endif /* __APPLE__ */ +extern void _cupsCharmapFlush(void); +extern const char *_cupsEncodingName(cups_encoding_t encoding); +extern void _cupsLangPrintError(const char *prefix, + const char *message); +extern int _cupsLangPrintFilter(FILE *fp, const char *prefix, + const char *message, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); +extern int _cupsLangPrintf(FILE *fp, const char *message, ...) + __attribute__ ((__format__ (__printf__, 2, 3))); +extern int _cupsLangPuts(FILE *fp, const char *message); +extern const char *_cupsLangString(cups_lang_t *lang, + const char *message); +extern void _cupsMessageFree(cups_array_t *a); +extern cups_array_t *_cupsMessageLoad(const char *filename, int unquote); +extern const char *_cupsMessageLookup(cups_array_t *a, const char *m); +extern cups_array_t *_cupsMessageNew(void *context); +extern void _cupsSetLocale(char *argv[]); + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_LANGUAGE_PRIVATE_H_ */ + +/* + * End of "$Id$". + */ diff --git a/cups/language.c b/cups/language.c new file mode 100644 index 0000000..49f0382 --- /dev/null +++ b/cups/language.c @@ -0,0 +1,1567 @@ +/* + * "$Id: language.c 7558 2008-05-12 23:46:44Z mike $" + * + * I18N/language support for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * _cupsAppleLanguage() - Get the Apple language identifier associated with + * a locale ID. + * _cupsEncodingName() - Return the character encoding name string for the + * given encoding enumeration. + * cupsLangDefault() - Return the default language. + * cupsLangEncoding() - Return the character encoding (us-ascii, etc.) + * for the given language. + * cupsLangFlush() - Flush all language data out of the cache. + * cupsLangFree() - Free language data. + * cupsLangGet() - Get a language. + * _cupsLangString() - Get a message string. + * _cupsMessageFree() - Free a messages array. + * _cupsMessageLoad() - Load a .po file into a messages array. + * _cupsMessageLookup() - Lookup a message string. + * _cupsMessageNew() - Make a new message catalog array. + * appleLangDefault() - Get the default locale string. + * appleMessageLoad() - Load a message catalog from a localizable bundle. + * cups_cache_lookup() - Lookup a language in the cache... + * cups_message_compare() - Compare two messages. + * cups_message_free() - Free a message. + * cups_message_load() - Load the message catalog for a language. + * cups_unquote() - Unquote characters in strings... + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#ifdef HAVE_LANGINFO_H +# include +#endif /* HAVE_LANGINFO_H */ +#ifdef WIN32 +# include +#else +# include +#endif /* WIN32 */ +#ifdef HAVE_COREFOUNDATION_H +# include +#endif /* HAVE_COREFOUNDATION_H */ + + +/* + * Local globals... + */ + +static _cups_mutex_t lang_mutex = _CUPS_MUTEX_INITIALIZER; + /* Mutex to control access to cache */ +static cups_lang_t *lang_cache = NULL; + /* Language string cache */ +static const char * const lang_encodings[] = + { /* Encoding strings */ + "us-ascii", "iso-8859-1", + "iso-8859-2", "iso-8859-3", + "iso-8859-4", "iso-8859-5", + "iso-8859-6", "iso-8859-7", + "iso-8859-8", "iso-8859-9", + "iso-8859-10", "utf-8", + "iso-8859-13", "iso-8859-14", + "iso-8859-15", "cp874", + "cp1250", "cp1251", + "cp1252", "cp1253", + "cp1254", "cp1255", + "cp1256", "cp1257", + "cp1258", "koi8-r", + "koi8-u", "iso-8859-11", + "iso-8859-16", "mac", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "cp932", "cp936", + "cp949", "cp950", + "cp1361", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "euc-cn", "euc-jp", + "euc-kr", "euc-tw", + "jis-x0213" + }; + +#ifdef __APPLE__ +typedef struct +{ + const char * const language; /* Language ID */ + const char * const locale; /* Locale ID */ +} _apple_language_locale_t; + +static const _apple_language_locale_t apple_language_locale[] = +{ /* Locale to language ID LUT */ + { "en", "en_US" }, + { "nb", "no" }, + { "zh-Hans", "zh_CN" }, + { "zh-Hant", "zh_TW" } +}; +#endif /* __APPLE__ */ + + +/* + * Local functions... + */ + + +#ifdef __APPLE__ +static const char *appleLangDefault(void); +# ifdef CUPS_BUNDLEDIR +# ifndef CF_RETURNS_RETAINED +# if __has_feature(attribute_cf_returns_retained) +# define CF_RETURNS_RETAINED __attribute__((cf_returns_retained)) +# else +# define CF_RETURNS_RETAINED +# endif /* __has_feature(attribute_cf_returns_retained) */ +# endif /* !CF_RETURNED_RETAINED */ +static cups_array_t *appleMessageLoad(const char *locale) + CF_RETURNS_RETAINED; +# endif /* CUPS_BUNDLEDIR */ +#endif /* __APPLE__ */ +static cups_lang_t *cups_cache_lookup(const char *name, + cups_encoding_t encoding); +static int cups_message_compare(_cups_message_t *m1, + _cups_message_t *m2); +static void cups_message_free(_cups_message_t *m); +static void cups_message_load(cups_lang_t *lang); +static void cups_unquote(char *d, const char *s); + + +#ifdef __APPLE__ +/* + * '_cupsAppleLanguage()' - Get the Apple language identifier associated with a + * locale ID. + */ + +const char * /* O - Language ID */ +_cupsAppleLanguage(const char *locale, /* I - Locale ID */ + char *language,/* I - Language ID buffer */ + size_t langsize) /* I - Size of language ID buffer */ +{ + int i; /* Looping var */ + CFStringRef localeid, /* CF locale identifier */ + langid; /* CF language identifier */ + + + /* + * Copy the locale name and convert, as needed, to the Apple-specific + * locale identifier... + */ + + switch (strlen(locale)) + { + default : + /* + * Invalid locale... + */ + + strlcpy(language, "en", langsize); + break; + + case 2 : + strlcpy(language, locale, langsize); + break; + + case 5 : + strlcpy(language, locale, langsize); + + if (language[2] == '-') + { + /* + * Convert ll-cc to ll_CC... + */ + + language[2] = '_'; + language[3] = toupper(language[3] & 255); + language[4] = toupper(language[4] & 255); + } + break; + } + + for (i = 0; + i < (int)(sizeof(apple_language_locale) / + sizeof(apple_language_locale[0])); + i ++) + if (!strcmp(locale, apple_language_locale[i].locale)) + { + strlcpy(language, apple_language_locale[i].language, sizeof(language)); + break; + } + + /* + * Attempt to map the locale ID to a language ID... + */ + + if ((localeid = CFStringCreateWithCString(kCFAllocatorDefault, language, + kCFStringEncodingASCII)) != NULL) + { + if ((langid = CFLocaleCreateCanonicalLanguageIdentifierFromString( + kCFAllocatorDefault, localeid)) != NULL) + { + CFStringGetCString(langid, language, langsize, kCFStringEncodingASCII); + CFRelease(langid); + } + + CFRelease(localeid); + } + + /* + * Return what we got... + */ + + return (language); +} +#endif /* __APPLE__ */ + + +/* + * '_cupsEncodingName()' - Return the character encoding name string + * for the given encoding enumeration. + */ + +const char * /* O - Character encoding */ +_cupsEncodingName( + cups_encoding_t encoding) /* I - Encoding value */ +{ + if (encoding < 0 || + encoding >= (sizeof(lang_encodings) / sizeof(const char *))) + { + DEBUG_printf(("1_cupsEncodingName(encoding=%d) = out of range (\"%s\")", + encoding, lang_encodings[0])); + return (lang_encodings[0]); + } + else + { + DEBUG_printf(("1_cupsEncodingName(encoding=%d) = \"%s\"", + encoding, lang_encodings[encoding])); + return (lang_encodings[encoding]); + } +} + + +/* + * 'cupsLangDefault()' - Return the default language. + */ + +cups_lang_t * /* O - Language data */ +cupsLangDefault(void) +{ + return (cupsLangGet(NULL)); +} + + +/* + * 'cupsLangEncoding()' - Return the character encoding (us-ascii, etc.) + * for the given language. + */ + +const char * /* O - Character encoding */ +cupsLangEncoding(cups_lang_t *lang) /* I - Language data */ +{ + if (lang == NULL) + return ((char*)lang_encodings[0]); + else + return ((char*)lang_encodings[lang->encoding]); +} + + +/* + * 'cupsLangFlush()' - Flush all language data out of the cache. + */ + +void +cupsLangFlush(void) +{ + cups_lang_t *lang, /* Current language */ + *next; /* Next language */ + + + /* + * Free all languages in the cache... + */ + + _cupsMutexLock(&lang_mutex); + + for (lang = lang_cache; lang != NULL; lang = next) + { + /* + * Free all messages... + */ + + _cupsMessageFree(lang->strings); + + /* + * Then free the language structure itself... + */ + + next = lang->next; + free(lang); + } + + lang_cache = NULL; + + _cupsMutexUnlock(&lang_mutex); +} + + +/* + * 'cupsLangFree()' - Free language data. + * + * This does not actually free anything; use @link cupsLangFlush@ for that. + */ + +void +cupsLangFree(cups_lang_t *lang) /* I - Language to free */ +{ + _cupsMutexLock(&lang_mutex); + + if (lang != NULL && lang->used > 0) + lang->used --; + + _cupsMutexUnlock(&lang_mutex); +} + + +/* + * 'cupsLangGet()' - Get a language. + */ + +cups_lang_t * /* O - Language data */ +cupsLangGet(const char *language) /* I - Language or locale */ +{ + int i; /* Looping var */ +#ifndef __APPLE__ + char locale[255]; /* Copy of locale name */ +#endif /* !__APPLE__ */ + char langname[16], /* Requested language name */ + country[16], /* Country code */ + charset[16], /* Character set */ + *csptr, /* Pointer to CODESET string */ + *ptr, /* Pointer into language/charset */ + real[48]; /* Real language name */ + cups_encoding_t encoding; /* Encoding to use */ + cups_lang_t *lang; /* Current language... */ + static const char * const locale_encodings[] = + { /* Locale charset names */ + "ASCII", "ISO88591", "ISO88592", "ISO88593", + "ISO88594", "ISO88595", "ISO88596", "ISO88597", + "ISO88598", "ISO88599", "ISO885910", "UTF8", + "ISO885913", "ISO885914", "ISO885915", "CP874", + "CP1250", "CP1251", "CP1252", "CP1253", + "CP1254", "CP1255", "CP1256", "CP1257", + "CP1258", "KOI8R", "KOI8U", "ISO885911", + "ISO885916", "MACROMAN", "", "", + + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + + "CP932", "CP936", "CP949", "CP950", + "CP1361", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + + "EUCCN", "EUCJP", "EUCKR", "EUCTW", + "SHIFT_JISX0213" + }; + + + DEBUG_printf(("2cupsLangGet(language=\"%s\")", language)); + +#ifdef __APPLE__ + /* + * Set the character set to UTF-8... + */ + + strcpy(charset, "UTF8"); + + /* + * Apple's setlocale doesn't give us the user's localization + * preference so we have to look it up this way... + */ + + if (!language) + { + if (!getenv("SOFTWARE") || (language = getenv("LANG")) == NULL) + language = appleLangDefault(); + + DEBUG_printf(("4cupsLangGet: language=\"%s\"", language)); + } + +#else + /* + * Set the charset to "unknown"... + */ + + charset[0] = '\0'; + + /* + * Use setlocale() to determine the currently set locale, and then + * fallback to environment variables to avoid setting the locale, + * since setlocale() is not thread-safe! + */ + + if (!language) + { + /* + * First see if the locale has been set; if it is still "C" or + * "POSIX", use the environment to get the default... + */ + +# ifdef LC_MESSAGES + ptr = setlocale(LC_MESSAGES, NULL); +# else + ptr = setlocale(LC_ALL, NULL); +# endif /* LC_MESSAGES */ + + DEBUG_printf(("4cupsLangGet: current locale is \"%s\"", ptr)); + + if (!ptr || !strcmp(ptr, "C") || !strcmp(ptr, "POSIX")) + { + /* + * Get the character set from the LC_CTYPE locale setting... + */ + + if ((ptr = getenv("LC_CTYPE")) == NULL) + if ((ptr = getenv("LC_ALL")) == NULL) + if ((ptr = getenv("LANG")) == NULL) + ptr = "en_US"; + + if ((csptr = strchr(ptr, '.')) != NULL) + { + /* + * Extract the character set from the environment... + */ + + for (ptr = charset, csptr ++; *csptr; csptr ++) + if (ptr < (charset + sizeof(charset) - 1) && _cups_isalnum(*csptr)) + *ptr++ = *csptr; + + *ptr = '\0'; + } + + /* + * Get the locale for messages from the LC_MESSAGES locale setting... + */ + + if ((ptr = getenv("LC_MESSAGES")) == NULL) + if ((ptr = getenv("LC_ALL")) == NULL) + if ((ptr = getenv("LANG")) == NULL) + ptr = "en_US"; + } + + if (ptr) + { + strlcpy(locale, ptr, sizeof(locale)); + language = locale; + + /* + * CUPS STR #2575: Map "nb" to "no" for back-compatibility... + */ + + if (!strncmp(locale, "nb", 2)) + locale[1] = 'o'; + + DEBUG_printf(("4cupsLangGet: new language value is \"%s\"", language)); + } + } +#endif /* __APPLE__ */ + + /* + * If "language" is NULL at this point, then chances are we are using + * a language that is not installed for the base OS. + */ + + if (!language) + { + /* + * Switch to the POSIX ("C") locale... + */ + + language = "C"; + } + +#ifdef CODESET + /* + * On systems that support the nl_langinfo(CODESET) call, use + * this value as the character set... + */ + + if (!charset[0] && (csptr = nl_langinfo(CODESET)) != NULL) + { + /* + * Copy all of the letters and numbers in the CODESET string... + */ + + for (ptr = charset; *csptr; csptr ++) + if (_cups_isalnum(*csptr) && ptr < (charset + sizeof(charset) - 1)) + *ptr++ = *csptr; + + *ptr = '\0'; + + DEBUG_printf(("4cupsLangGet: charset set to \"%s\" via " + "nl_langinfo(CODESET)...", charset)); + } +#endif /* CODESET */ + + /* + * If we don't have a character set by now, default to UTF-8... + */ + + if (!charset[0]) + strcpy(charset, "UTF8"); + + /* + * Parse the language string passed in to a locale string. "C" is the + * standard POSIX locale and is copied unchanged. Otherwise the + * language string is converted from ll-cc[.charset] (language-country) + * to ll_CC[.CHARSET] to match the file naming convention used by all + * POSIX-compliant operating systems. Invalid language names are mapped + * to the POSIX locale. + */ + + country[0] = '\0'; + + if (language == NULL || !language[0] || + !strcmp(language, "POSIX")) + strcpy(langname, "C"); + else + { + /* + * Copy the parts of the locale string over safely... + */ + + for (ptr = langname; *language; language ++) + if (*language == '_' || *language == '-' || *language == '.') + break; + else if (ptr < (langname + sizeof(langname) - 1)) + *ptr++ = tolower(*language & 255); + + *ptr = '\0'; + + if (*language == '_' || *language == '-') + { + /* + * Copy the country code... + */ + + for (language ++, ptr = country; *language; language ++) + if (*language == '.') + break; + else if (ptr < (country + sizeof(country) - 1)) + *ptr++ = toupper(*language & 255); + + *ptr = '\0'; + } + + if (*language == '.' && !charset[0]) + { + /* + * Copy the encoding... + */ + + for (language ++, ptr = charset; *language; language ++) + if (_cups_isalnum(*language) && ptr < (charset + sizeof(charset) - 1)) + *ptr++ = toupper(*language & 255); + + *ptr = '\0'; + } + + /* + * Force a POSIX locale for an invalid language name... + */ + + if (strlen(langname) != 2) + { + strcpy(langname, "C"); + country[0] = '\0'; + charset[0] = '\0'; + } + } + + DEBUG_printf(("4cupsLangGet: langname=\"%s\", country=\"%s\", charset=\"%s\"", + langname, country, charset)); + + /* + * Figure out the desired encoding... + */ + + encoding = CUPS_AUTO_ENCODING; + + if (charset[0]) + { + for (i = 0; + i < (int)(sizeof(locale_encodings) / sizeof(locale_encodings[0])); + i ++) + if (!_cups_strcasecmp(charset, locale_encodings[i])) + { + encoding = (cups_encoding_t)i; + break; + } + + if (encoding == CUPS_AUTO_ENCODING) + { + /* + * Map alternate names for various character sets... + */ + + if (!_cups_strcasecmp(charset, "iso-2022-jp") || + !_cups_strcasecmp(charset, "sjis")) + encoding = CUPS_WINDOWS_932; + else if (!_cups_strcasecmp(charset, "iso-2022-cn")) + encoding = CUPS_WINDOWS_936; + else if (!_cups_strcasecmp(charset, "iso-2022-kr")) + encoding = CUPS_WINDOWS_949; + else if (!_cups_strcasecmp(charset, "big5")) + encoding = CUPS_WINDOWS_950; + } + } + + DEBUG_printf(("4cupsLangGet: encoding=%d(%s)", encoding, + encoding == CUPS_AUTO_ENCODING ? "auto" : + lang_encodings[encoding])); + + /* + * See if we already have this language/country loaded... + */ + + if (country[0]) + snprintf(real, sizeof(real), "%s_%s", langname, country); + else + strcpy(real, langname); + + _cupsMutexLock(&lang_mutex); + + if ((lang = cups_cache_lookup(real, encoding)) != NULL) + { + _cupsMutexUnlock(&lang_mutex); + + DEBUG_printf(("3cupsLangGet: Using cached copy of \"%s\"...", real)); + + return (lang); + } + + /* + * See if there is a free language available; if so, use that + * record... + */ + + for (lang = lang_cache; lang != NULL; lang = lang->next) + if (lang->used == 0) + break; + + if (lang == NULL) + { + /* + * Allocate memory for the language and add it to the cache. + */ + + if ((lang = calloc(sizeof(cups_lang_t), 1)) == NULL) + { + _cupsMutexUnlock(&lang_mutex); + + return (NULL); + } + + lang->next = lang_cache; + lang_cache = lang; + } + else + { + /* + * Free all old strings as needed... + */ + + _cupsMessageFree(lang->strings); + lang->strings = NULL; + } + + /* + * Then assign the language and encoding fields... + */ + + lang->used ++; + strlcpy(lang->language, real, sizeof(lang->language)); + + if (encoding != CUPS_AUTO_ENCODING) + lang->encoding = encoding; + else + lang->encoding = CUPS_UTF8; + + /* + * Return... + */ + + _cupsMutexUnlock(&lang_mutex); + + return (lang); +} + + +/* + * '_cupsLangString()' - Get a message string. + * + * The returned string is UTF-8 encoded; use cupsUTF8ToCharset() to + * convert the string to the language encoding. + */ + +const char * /* O - Localized message */ +_cupsLangString(cups_lang_t *lang, /* I - Language */ + const char *message) /* I - Message */ +{ + const char *s; /* Localized message */ + + /* + * Range check input... + */ + + if (!lang || !message || !*message) + return (message); + + _cupsMutexLock(&lang_mutex); + + /* + * Load the message catalog if needed... + */ + + if (!lang->strings) + cups_message_load(lang); + + s = _cupsMessageLookup(lang->strings, message); + + _cupsMutexUnlock(&lang_mutex); + + return (s); +} + + +/* + * '_cupsMessageFree()' - Free a messages array. + */ + +void +_cupsMessageFree(cups_array_t *a) /* I - Message array */ +{ +#if defined(__APPLE__) && defined(CUPS_BUNDLEDIR) + /* + * Release the cups.strings dictionary as needed... + */ + + if (cupsArrayUserData(a)) + CFRelease((CFDictionaryRef)cupsArrayUserData(a)); +#endif /* __APPLE__ && CUPS_BUNDLEDIR */ + + /* + * Free the array... + */ + + cupsArrayDelete(a); +} + + +/* + * '_cupsMessageLoad()' - Load a .po file into a messages array. + */ + +cups_array_t * /* O - New message array */ +_cupsMessageLoad(const char *filename, /* I - Message catalog to load */ + int unquote) /* I - Unescape \foo in strings? */ +{ + cups_file_t *fp; /* Message file */ + cups_array_t *a; /* Message array */ + _cups_message_t *m; /* Current message */ + char s[4096], /* String buffer */ + *ptr, /* Pointer into buffer */ + *temp; /* New string */ + int length; /* Length of combined strings */ + + + DEBUG_printf(("4_cupsMessageLoad(filename=\"%s\")", filename)); + + /* + * Create an array to hold the messages... + */ + + if ((a = _cupsMessageNew(NULL)) == NULL) + { + DEBUG_puts("5_cupsMessageLoad: Unable to allocate array!"); + return (NULL); + } + + /* + * Open the message catalog file... + */ + + if ((fp = cupsFileOpen(filename, "r")) == NULL) + { + DEBUG_printf(("5_cupsMessageLoad: Unable to open file: %s", + strerror(errno))); + return (a); + } + + /* + * Read messages from the catalog file until EOF... + * + * The format is the GNU gettext .po format, which is fairly simple: + * + * msgid "some text" + * msgstr "localized text" + * + * The ID and localized text can span multiple lines using the form: + * + * msgid "" + * "some long text" + * msgstr "" + * "localized text spanning " + * "multiple lines" + */ + + m = NULL; + + while (cupsFileGets(fp, s, sizeof(s)) != NULL) + { + /* + * Skip blank and comment lines... + */ + + if (s[0] == '#' || !s[0]) + continue; + + /* + * Strip the trailing quote... + */ + + if ((ptr = strrchr(s, '\"')) == NULL) + continue; + + *ptr = '\0'; + + /* + * Find start of value... + */ + + if ((ptr = strchr(s, '\"')) == NULL) + continue; + + ptr ++; + + /* + * Unquote the text... + */ + + if (unquote) + cups_unquote(ptr, ptr); + + /* + * Create or add to a message... + */ + + if (!strncmp(s, "msgid", 5)) + { + /* + * Add previous message as needed... + */ + + if (m) + { + if (m->str && m->str[0]) + { + cupsArrayAdd(a, m); + } + else + { + /* + * Translation is empty, don't add it... (STR #4033) + */ + + free(m->id); + if (m->str) + free(m->str); + free(m); + } + } + + /* + * Create a new message with the given msgid string... + */ + + if ((m = (_cups_message_t *)calloc(1, sizeof(_cups_message_t))) == NULL) + { + cupsFileClose(fp); + return (a); + } + + if ((m->id = strdup(ptr)) == NULL) + { + free(m); + cupsFileClose(fp); + return (a); + } + } + else if (s[0] == '\"' && m) + { + /* + * Append to current string... + */ + + length = (int)strlen(m->str ? m->str : m->id); + + if ((temp = realloc(m->str ? m->str : m->id, + length + strlen(ptr) + 1)) == NULL) + { + if (m->str) + free(m->str); + free(m->id); + free(m); + + cupsFileClose(fp); + return (a); + } + + if (m->str) + { + /* + * Copy the new portion to the end of the msgstr string - safe + * to use strcpy because the buffer is allocated to the correct + * size... + */ + + m->str = temp; + + strcpy(m->str + length, ptr); + } + else + { + /* + * Copy the new portion to the end of the msgid string - safe + * to use strcpy because the buffer is allocated to the correct + * size... + */ + + m->id = temp; + + strcpy(m->id + length, ptr); + } + } + else if (!strncmp(s, "msgstr", 6) && m) + { + /* + * Set the string... + */ + + if ((m->str = strdup(ptr)) == NULL) + { + free(m->id); + free(m); + + cupsFileClose(fp); + return (a); + } + } + } + + /* + * Add the last message string to the array as needed... + */ + + if (m) + { + if (m->str && m->str[0]) + { + cupsArrayAdd(a, m); + } + else + { + /* + * Translation is empty, don't add it... (STR #4033) + */ + + free(m->id); + if (m->str) + free(m->str); + free(m); + } + } + + /* + * Close the message catalog file and return the new array... + */ + + cupsFileClose(fp); + + DEBUG_printf(("5_cupsMessageLoad: Returning %d messages...", + cupsArrayCount(a))); + + return (a); +} + + +/* + * '_cupsMessageLookup()' - Lookup a message string. + */ + +const char * /* O - Localized message */ +_cupsMessageLookup(cups_array_t *a, /* I - Message array */ + const char *m) /* I - Message */ +{ + _cups_message_t key, /* Search key */ + *match; /* Matching message */ + + + /* + * Lookup the message string; if it doesn't exist in the catalog, + * then return the message that was passed to us... + */ + + key.id = (char *)m; + match = (_cups_message_t *)cupsArrayFind(a, &key); + +#if defined(__APPLE__) && defined(CUPS_BUNDLEDIR) + if (!match && cupsArrayUserData(a)) + { + /* + * Try looking the string up in the cups.strings dictionary... + */ + + CFDictionaryRef dict; /* cups.strings dictionary */ + CFStringRef cfm, /* Message as a CF string */ + cfstr; /* Localized text as a CF string */ + + dict = (CFDictionaryRef)cupsArrayUserData(a); + cfm = CFStringCreateWithCString(kCFAllocatorDefault, m, + kCFStringEncodingUTF8); + match = calloc(1, sizeof(_cups_message_t)); + match->id = strdup(m); + cfstr = cfm ? CFDictionaryGetValue(dict, cfm) : NULL; + + if (cfstr) + { + char buffer[1024]; /* Message buffer */ + + CFStringGetCString(cfstr, buffer, sizeof(buffer), kCFStringEncodingUTF8); + match->str = strdup(buffer); + + DEBUG_printf(("1_cupsMessageLookup: Found \"%s\" as \"%s\"...", + m, buffer)); + } + else + { + match->str = strdup(m); + + DEBUG_printf(("1_cupsMessageLookup: Did not find \"%s\"...", m)); + } + + cupsArrayAdd(a, match); + + if (cfm) + CFRelease(cfm); + } +#endif /* __APPLE__ && CUPS_BUNDLEDIR */ + + if (match && match->str) + return (match->str); + else + return (m); +} + + +/* + * '_cupsMessageNew()' - Make a new message catalog array. + */ + +cups_array_t * /* O - Array */ +_cupsMessageNew(void *context) /* I - User data */ +{ + return (cupsArrayNew3((cups_array_func_t)cups_message_compare, context, + (cups_ahash_func_t)NULL, 0, + (cups_acopy_func_t)NULL, + (cups_afree_func_t)cups_message_free)); +} + + +#ifdef __APPLE__ +/* + * 'appleLangDefault()' - Get the default locale string. + */ + +static const char * /* O - Locale string */ +appleLangDefault(void) +{ + int i; /* Looping var */ + CFBundleRef bundle; /* Main bundle (if any) */ + CFArrayRef bundleList; /* List of localizations in bundle */ + CFPropertyListRef localizationList; + /* List of localization data */ + CFStringRef languageName; /* Current name */ + CFStringRef localeName; /* Canonical from of name */ + char *lang; /* LANG environment variable */ + _cups_globals_t *cg = _cupsGlobals(); + /* Pointer to library globals */ + + + DEBUG_puts("2appleLangDefault()"); + + /* + * Only do the lookup and translation the first time. + */ + + if (!cg->language[0]) + { + if (getenv("SOFTWARE") != NULL && (lang = getenv("LANG")) != NULL) + { + strlcpy(cg->language, lang, sizeof(cg->language)); + return (cg->language); + } + else if ((bundle = CFBundleGetMainBundle()) != NULL && + (bundleList = CFBundleCopyBundleLocalizations(bundle)) != NULL) + { + localizationList = + CFBundleCopyPreferredLocalizationsFromArray(bundleList); + + CFRelease(bundleList); + } + else + localizationList = + CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), + kCFPreferencesCurrentApplication); + + if (localizationList) + { + if (CFGetTypeID(localizationList) == CFArrayGetTypeID() && + CFArrayGetCount(localizationList) > 0) + { + languageName = CFArrayGetValueAtIndex(localizationList, 0); + + if (languageName && + CFGetTypeID(languageName) == CFStringGetTypeID()) + { + localeName = CFLocaleCreateCanonicalLocaleIdentifierFromString( + kCFAllocatorDefault, languageName); + + if (localeName) + { + CFStringGetCString(localeName, cg->language, sizeof(cg->language), + kCFStringEncodingASCII); + CFRelease(localeName); + + DEBUG_printf(("9appleLangDefault: cg->language=\"%s\"", + cg->language)); + + /* + * Map new language identifiers to locales... + */ + + for (i = 0; + i < (int)(sizeof(apple_language_locale) / + sizeof(apple_language_locale[0])); + i ++) + { + if (!strcmp(cg->language, apple_language_locale[i].language)) + { + DEBUG_printf(("9appleLangDefault: mapping \"%s\" to \"%s\"...", + cg->language, apple_language_locale[i].locale)); + strlcpy(cg->language, apple_language_locale[i].locale, + sizeof(cg->language)); + break; + } + } + + /* + * Convert language subtag into region subtag... + */ + + if (cg->language[2] == '-') + cg->language[2] = '_'; + + if (!strchr(cg->language, '.')) + strlcat(cg->language, ".UTF-8", sizeof(cg->language)); + } + } + } + + CFRelease(localizationList); + } + + /* + * If we didn't find the language, default to en_US... + */ + + if (!cg->language[0]) + strlcpy(cg->language, "en_US.UTF-8", sizeof(cg->language)); + } + + /* + * Return the cached locale... + */ + + return (cg->language); +} + + +# ifdef CUPS_BUNDLEDIR +/* + * 'appleMessageLoad()' - Load a message catalog from a localizable bundle. + */ + +static cups_array_t * /* O - Message catalog */ +appleMessageLoad(const char *locale) /* I - Locale ID */ +{ + char filename[1024], /* Path to cups.strings file */ + applelang[256]; /* Apple language ID */ + CFURLRef url; /* URL to cups.strings file */ + CFReadStreamRef stream = NULL; /* File stream */ + CFPropertyListRef plist = NULL; /* Localization file */ +#ifdef DEBUG + CFErrorRef error = NULL; /* Error when opening file */ +#endif /* DEBUG */ + + + DEBUG_printf(("appleMessageLoad(locale=\"%s\")", locale)); + + /* + * Load the cups.strings file... + */ + + snprintf(filename, sizeof(filename), + CUPS_BUNDLEDIR "/Resources/%s.lproj/cups.strings", + _cupsAppleLanguage(locale, applelang, sizeof(applelang))); + DEBUG_printf(("1appleMessageLoad: filename=\"%s\"", filename)); + + if (access(filename, 0)) + { + /* + * Try alternate lproj directory names... + */ + + if (!strncmp(locale, "en", 2)) + locale = "English"; + else if (!strncmp(locale, "nb", 2) || !strncmp(locale, "nl", 2)) + locale = "Dutch"; + else if (!strncmp(locale, "fr", 2)) + locale = "French"; + else if (!strncmp(locale, "de", 2)) + locale = "German"; + else if (!strncmp(locale, "it", 2)) + locale = "Italian"; + else if (!strncmp(locale, "ja", 2)) + locale = "Japanese"; + else if (!strncmp(locale, "es", 2)) + locale = "Spanish"; + + snprintf(filename, sizeof(filename), + CUPS_BUNDLEDIR "/Resources/%s.lproj/cups.strings", locale); + DEBUG_printf(("1appleMessageLoad: alternate filename=\"%s\"", filename)); + } + + url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, + (UInt8 *)filename, + strlen(filename), false); + if (url) + { + stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url); + if (stream) + { + /* + * Read the property list containing the localization data. + * + * NOTE: This code currently generates a clang "potential leak" + * warning, but the object is released in _cupsMessageFree(). + */ + + CFReadStreamOpen(stream); + +#ifdef DEBUG + plist = CFPropertyListCreateWithStream(kCFAllocatorDefault, stream, 0, + kCFPropertyListImmutable, NULL, + &error); + if (error) + { + CFStringRef msg = CFErrorCopyDescription(error); + /* Error message */ + + CFStringGetCString(msg, filename, sizeof(filename), + kCFStringEncodingUTF8); + DEBUG_printf(("1appleMessageLoad: %s", filename)); + + CFRelease(msg); + CFRelease(error); + } + +#else + plist = CFPropertyListCreateWithStream(kCFAllocatorDefault, stream, 0, + kCFPropertyListImmutable, NULL, + NULL); +#endif /* DEBUG */ + + if (plist && CFGetTypeID(plist) != CFDictionaryGetTypeID()) + { + CFRelease(plist); + plist = NULL; + } + + CFRelease(stream); + } + + CFRelease(url); + } + + DEBUG_printf(("1appleMessageLoad: url=%p, stream=%p, plist=%p", url, stream, + plist)); + + /* + * Create and return an empty array to act as a cache for messages, passing the + * plist as the user data. + */ + + return (_cupsMessageNew((void *)plist)); +} +# endif /* CUPS_BUNDLEDIR */ +#endif /* __APPLE__ */ + + +/* + * 'cups_cache_lookup()' - Lookup a language in the cache... + */ + +static cups_lang_t * /* O - Language data or NULL */ +cups_cache_lookup( + const char *name, /* I - Name of locale */ + cups_encoding_t encoding) /* I - Encoding of locale */ +{ + cups_lang_t *lang; /* Current language */ + + + DEBUG_printf(("7cups_cache_lookup(name=\"%s\", encoding=%d(%s))", name, + encoding, encoding == CUPS_AUTO_ENCODING ? "auto" : + lang_encodings[encoding])); + + /* + * Loop through the cache and return a match if found... + */ + + for (lang = lang_cache; lang != NULL; lang = lang->next) + { + DEBUG_printf(("9cups_cache_lookup: lang=%p, language=\"%s\", " + "encoding=%d(%s)", lang, lang->language, lang->encoding, + lang_encodings[lang->encoding])); + + if (!strcmp(lang->language, name) && + (encoding == CUPS_AUTO_ENCODING || encoding == lang->encoding)) + { + lang->used ++; + + DEBUG_puts("8cups_cache_lookup: returning match!"); + + return (lang); + } + } + + DEBUG_puts("8cups_cache_lookup: returning NULL!"); + + return (NULL); +} + + +/* + * 'cups_message_compare()' - Compare two messages. + */ + +static int /* O - Result of comparison */ +cups_message_compare( + _cups_message_t *m1, /* I - First message */ + _cups_message_t *m2) /* I - Second message */ +{ + return (strcmp(m1->id, m2->id)); +} + + +/* + * 'cups_message_free()' - Free a message. + */ + +static void +cups_message_free(_cups_message_t *m) /* I - Message */ +{ + if (m->id) + free(m->id); + + if (m->str) + free(m->str); + + free(m); +} + + +/* + * 'cups_message_load()' - Load the message catalog for a language. + */ + +static void +cups_message_load(cups_lang_t *lang) /* I - Language */ +{ +#if defined(__APPLE__) && defined(CUPS_BUNDLEDIR) + lang->strings = appleMessageLoad(lang->language); + +#else + char filename[1024]; /* Filename for language locale file */ + _cups_globals_t *cg = _cupsGlobals(); + /* Pointer to library globals */ + + + snprintf(filename, sizeof(filename), "%s/%s/cups_%s.po", cg->localedir, + lang->language, lang->language); + + if (strchr(lang->language, '_') && access(filename, 0)) + { + /* + * Country localization not available, look for generic localization... + */ + + snprintf(filename, sizeof(filename), "%s/%.2s/cups_%.2s.po", cg->localedir, + lang->language, lang->language); + + if (access(filename, 0)) + { + /* + * No generic localization, so use POSIX... + */ + + DEBUG_printf(("4cups_message_load: access(\"%s\", 0): %s", filename, + strerror(errno))); + + snprintf(filename, sizeof(filename), "%s/C/cups_C.po", cg->localedir); + } + } + + /* + * Read the strings from the file... + */ + + lang->strings = _cupsMessageLoad(filename, 1); +#endif /* __APPLE__ && CUPS_BUNDLEDIR */ +} + + +/* + * 'cups_unquote()' - Unquote characters in strings... + */ + +static void +cups_unquote(char *d, /* O - Unquoted string */ + const char *s) /* I - Original string */ +{ + while (*s) + { + if (*s == '\\') + { + s ++; + if (isdigit(*s)) + { + *d = 0; + + while (isdigit(*s)) + { + *d = *d * 8 + *s - '0'; + s ++; + } + + d ++; + } + else + { + if (*s == 'n') + *d ++ = '\n'; + else if (*s == 'r') + *d ++ = '\r'; + else if (*s == 't') + *d ++ = '\t'; + else + *d++ = *s; + + s ++; + } + } + else + *d++ = *s++; + } + + *d = '\0'; +} + + +/* + * End of "$Id: language.c 7558 2008-05-12 23:46:44Z mike $". + */ diff --git a/cups/language.h b/cups/language.h new file mode 100644 index 0000000..91d2012 --- /dev/null +++ b/cups/language.h @@ -0,0 +1,115 @@ +/* + * "$Id: language.h 6649 2007-07-11 21:46:42Z mike $" + * + * Multi-language support for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + */ + +#ifndef _CUPS_LANGUAGE_H_ +# define _CUPS_LANGUAGE_H_ + +/* + * Include necessary headers... + */ + +# include +# include "array.h" + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Types... + */ + +typedef enum cups_encoding_e /**** Language Encodings ****/ +{ + CUPS_AUTO_ENCODING = -1, /* Auto-detect the encoding @private@ */ + CUPS_US_ASCII, /* US ASCII */ + CUPS_ISO8859_1, /* ISO-8859-1 */ + CUPS_ISO8859_2, /* ISO-8859-2 */ + CUPS_ISO8859_3, /* ISO-8859-3 */ + CUPS_ISO8859_4, /* ISO-8859-4 */ + CUPS_ISO8859_5, /* ISO-8859-5 */ + CUPS_ISO8859_6, /* ISO-8859-6 */ + CUPS_ISO8859_7, /* ISO-8859-7 */ + CUPS_ISO8859_8, /* ISO-8859-8 */ + CUPS_ISO8859_9, /* ISO-8859-9 */ + CUPS_ISO8859_10, /* ISO-8859-10 */ + CUPS_UTF8, /* UTF-8 */ + CUPS_ISO8859_13, /* ISO-8859-13 */ + CUPS_ISO8859_14, /* ISO-8859-14 */ + CUPS_ISO8859_15, /* ISO-8859-15 */ + CUPS_WINDOWS_874, /* CP-874 */ + CUPS_WINDOWS_1250, /* CP-1250 */ + CUPS_WINDOWS_1251, /* CP-1251 */ + CUPS_WINDOWS_1252, /* CP-1252 */ + CUPS_WINDOWS_1253, /* CP-1253 */ + CUPS_WINDOWS_1254, /* CP-1254 */ + CUPS_WINDOWS_1255, /* CP-1255 */ + CUPS_WINDOWS_1256, /* CP-1256 */ + CUPS_WINDOWS_1257, /* CP-1257 */ + CUPS_WINDOWS_1258, /* CP-1258 */ + CUPS_KOI8_R, /* KOI-8-R */ + CUPS_KOI8_U, /* KOI-8-U */ + CUPS_ISO8859_11, /* ISO-8859-11 */ + CUPS_ISO8859_16, /* ISO-8859-16 */ + CUPS_MAC_ROMAN, /* MacRoman */ + CUPS_ENCODING_SBCS_END = 63, /* End of single-byte encodings @private@ */ + + CUPS_WINDOWS_932, /* Japanese JIS X0208-1990 */ + CUPS_WINDOWS_936, /* Simplified Chinese GB 2312-80 */ + CUPS_WINDOWS_949, /* Korean KS C5601-1992 */ + CUPS_WINDOWS_950, /* Traditional Chinese Big Five */ + CUPS_WINDOWS_1361, /* Korean Johab */ + CUPS_ENCODING_DBCS_END = 127, /* End of double-byte encodings @private@ */ + + CUPS_EUC_CN, /* EUC Simplified Chinese */ + CUPS_EUC_JP, /* EUC Japanese */ + CUPS_EUC_KR, /* EUC Korean */ + CUPS_EUC_TW, /* EUC Traditional Chinese */ + CUPS_JIS_X0213, /* JIS X0213 aka Shift JIS */ + CUPS_ENCODING_VBCS_END = 191 /* End of variable-length encodings @private@ */ +} cups_encoding_t; + +typedef struct cups_lang_s /**** Language Cache Structure ****/ +{ + struct cups_lang_s *next; /* Next language in cache */ + int used; /* Number of times this entry has been used. */ + cups_encoding_t encoding; /* Text encoding */ + char language[16]; /* Language/locale name */ + cups_array_t *strings; /* Message strings @private@ */ +} cups_lang_t; + + +/* + * Prototypes... + */ + +extern cups_lang_t *cupsLangDefault(void); +extern const char *cupsLangEncoding(cups_lang_t *lang); +extern void cupsLangFlush(void); +extern void cupsLangFree(cups_lang_t *lang); +extern cups_lang_t *cupsLangGet(const char *language); + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_LANGUAGE_H_ */ + +/* + * End of "$Id: language.h 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/cups/libcups2.def b/cups/libcups2.def new file mode 100644 index 0000000..ef22daa --- /dev/null +++ b/cups/libcups2.def @@ -0,0 +1,382 @@ +LIBRARY libcups2 +VERSION 2.9 +EXPORTS +_cupsBufferGet +_cupsBufferRelease +_cupsGet1284Values +_cupsGetDests +_cupsGetPassword +_cupsGlobals +_cupsLangPrintError +_cupsLangPrintf +_cupsLangPuts +_cupsLangString +_cupsMD5Append +_cupsMD5Finish +_cupsMD5Init +_cupsMessageFree +_cupsMessageLoad +_cupsMessageLookup +_cupsMutexLock +_cupsMutexUnlock +_cupsNextDelay +_cupsSetError +_cupsSetLocale +_cupsStrAlloc +_cupsStrFlush +_cupsStrFormatd +_cupsStrFree +_cupsStrRetain +_cupsStrScand +_cupsStrStatistics +_cups_strcasecmp +_cups_strncasecmp +_cups_strcpy +_cups_strlcat +_cups_strlcpy +_httpAddrPort +_httpAddrSetPort +_httpAssembleUUID +_httpCreate +_httpEncodeURI +_httpPeek +_httpResolveURI +_httpWait +_ippFindOption +_ppdCacheCreateWithFile +_ppdCacheCreateWithPPD +_ppdCacheDestroy +_ppdCacheGetBin +_ppdCacheGetInputSlot +_ppdCacheGetMediaType +_ppdCacheGetOutputBin +_ppdCacheGetPageSize +_ppdCacheGetSize +_ppdCacheGetSource +_ppdCacheGetType +_ppdCacheWriteFile +_ppdFreeLanguages +_ppdGetEncoding +_ppdGetLanguages +_ppdHashName +_ppdLocalizedAttr +_ppdNormalizeMakeAndModel +_ppdOpen +_ppdOpenFile +_ppdParseOptions +_pwgGenerateSize +_pwgInitSize +_pwgMediaForLegacy +_pwgMediaForPPD +_pwgMediaForPWG +_pwgMediaForSize +_pwgMediaTypeForType +_pwgPageSizeForMedia +cupsAddDest +cupsAddOption +cupsAdminCreateWindowsPPD +cupsAdminExportSamba +cupsArrayAdd +cupsArrayClear +cupsArrayCount +cupsArrayCurrent +cupsArrayDelete +cupsArrayDup +cupsArrayFind +cupsArrayFirst +cupsArrayGetIndex +cupsArrayGetInsert +cupsArrayIndex +cupsArrayInsert +cupsArrayLast +cupsArrayNew +cupsArrayNew2 +cupsArrayNew3 +cupsArrayNext +cupsArrayPrev +cupsArrayRemove +cupsArrayRestore +cupsArraySave +cupsArrayUserData +cupsCancelJob +cupsCharsetToUTF8 +cupsDirClose +cupsDirOpen +cupsDirRead +cupsDirRewind +cupsDoAuthentication +cupsDoFileRequest +cupsDoIORequest +cupsDoRequest +cupsEncodeOptions +cupsEncodeOptions2 +cupsEncryption +cupsFileClose +cupsFileCompression +cupsFileEOF +cupsFileFind +cupsFileFlush +cupsFileGetChar +cupsFileGetConf +cupsFileGetLine +cupsFileGets +cupsFileLock +cupsFileNumber +cupsFileOpen +cupsFileOpenFd +cupsFilePeekChar +cupsFilePrintf +cupsFilePutChar +cupsFilePuts +cupsFileRead +cupsFileRewind +cupsFileSeek +cupsFileStderr +cupsFileStdin +cupsFileStdout +cupsFileTell +cupsFileUnlock +cupsFileWrite +cupsFreeDests +cupsFreeJobs +cupsFreeOptions +cupsGetClasses +cupsGetDefault +cupsGetDefault2 +cupsGetDest +cupsGetDests +cupsGetDests2 +cupsGetFd +cupsGetFile +cupsGetJobs +cupsGetJobs2 +cupsGetOption +cupsGetPPD +cupsGetPPD2 +cupsGetPassword +cupsGetPrinters +cupsGetResponse +cupsLangDefault +cupsLangEncoding +cupsLangFlush +cupsLangFree +cupsLangGet +cupsLastError +cupsLastErrorString +cupsMarkOptions +cupsNotifySubject +cupsNotifyText +cupsParseOptions +cupsPrintFile +cupsPrintFile2 +cupsPrintFiles +cupsPrintFiles2 +cupsPutFd +cupsPutFile +cupsRemoveOption +cupsResolveConflicts +cupsSendRequest +cupsServer +cupsSetClientCertCB +cupsSetCredentials +cupsSetDests +cupsSetDests2 +cupsSetEncryption +cupsSetPasswordCB +cupsSetServer +cupsSetServerCertCB +cupsSetUser +cupsTempFd +cupsTempFile +cupsTempFile2 +cupsUTF32ToUTF8 +cupsUTF8ToCharset +cupsUTF8ToUTF32 +cupsUser +cupsWriteRequestData +httpAddCredential +httpAddrAny +httpAddrConnect +httpAddrEqual +httpAddrFreeList +httpAddrGetList +httpAddrLength +httpAddrLocalhost +httpAddrLookup +httpAddrString +httpAssembleURI +httpAssembleURIf +httpBlocking +httpCheck +httpClearCookie +httpClearFields +httpClose +httpConnect +httpConnectEncrypt +httpCopyCredentials +httpDecode64 +httpDecode64_2 +httpDelete +httpEncode64 +httpEncode64_2 +httpEncryption +httpError +httpFlush +httpFlushWrite +httpFreeCredentials +httpGet +httpGetBlocking +httpGetCookie +httpGetDateString +httpGetDateString2 +httpGetDateTime +httpGetFd +httpGetField +httpGetHostByName +httpGetHostname +httpGetLength +httpGetLength2 +httpGetStatus +httpGetSubField +httpGetSubField2 +httpGets +httpHead +httpInitialize +httpMD5 +httpMD5Final +httpMD5String +httpOptions +httpPost +httpPrintf +httpPut +httpRead +httpRead2 +httpReconnect +httpSeparate +httpSeparate2 +httpSeparateURI +httpSetCookie +httpSetCredentials +httpSetExpect +httpSetField +httpSetLength +httpSetTimeout +httpStatus +httpTrace +httpUpdate +httpWait +httpWrite +httpWrite2 +ippAddBoolean +ippAddBooleans +ippAddCollection +ippAddCollections +ippAddDate +ippAddInteger +ippAddIntegers +ippAddOctetString +ippAddOutOfBand +ippAddRange +ippAddRanges +ippAddResolution +ippAddResolutions +ippAddSeparator +ippAddString +ippAddStrings +ippAttributeString +ippCopyAttribute +ippCopyAttributes +ippDateToTime +ippDelete +ippDeleteAttribute +ippDeleteValues +ippEnumString +ippEnumValue +ippErrorString +ippErrorValue +ippFindAttribute +ippFindNextAttribute +ippFirstAttribute +ippGetBoolean +ippGetCollection +ippGetCount +ippGetDate +ippGetGroupTag +ippGetInteger +ippGetName +ippGetOperation +ippGetRange +ippGetRequestId +ippGetResolution +ippGetState +ippGetStatusCode +ippGetString +ippGetValueTag +ippGetVersion +ippLength +ippNew +ippNewRequest +ippNextAttribute +ippOpString +ippOpValue +ippPort +ippRead +ippReadFile +ippReadIO +ippSetPort +ippSetBoolean +ippSetCollection +ippSetDate +ippSetGroupTag +ippSetInteger +ippSetName +ippSetOperation +ippSetRange +ippSetRequestId +ippSetResolution +ippSetState +ippSetStatusCode +ippSetString +ippSetValueTag +ippSetVersion +ippTagString +ippTagValue +ippTimeToDate +ippWrite +ippWriteFile +ippWriteIO +ppdClose +ppdCollect +ppdCollect2 +ppdConflicts +ppdEmit +ppdEmitAfterOrder +ppdEmitFd +ppdEmitJCL +ppdEmitJCLEnd +ppdEmitString +ppdErrorString +ppdFindAttr +ppdFindChoice +ppdFindCustomOption +ppdFindCustomParam +ppdFindMarkedChoice +ppdFindNextAttr +ppdFindOption +ppdFirstCustomParam +ppdFirstOption +ppdIsMarked +ppdLastError +ppdLocalize +ppdMarkDefaults +ppdMarkOption +ppdNextCustomParam +ppdNextOption +ppdOpen +ppdOpen2 +ppdOpenFd +ppdOpenFile +ppdPageLength +ppdPageSize +ppdPageWidth +ppdSetConformance diff --git a/cups/libcups2.rc b/cups/libcups2.rc new file mode 100644 index 0000000..bac3b17 --- /dev/null +++ b/cups/libcups2.rc @@ -0,0 +1,75 @@ +// Microsoft Visual C++ generated resource script. +// + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" +#include "WinVersRes.h" +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION MASTER_PROD_VERS + PRODUCTVERSION MASTER_PROD_VERS + FILEFLAGSMASK 0x17L +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "CompanyName", MASTER_COMPANY_NAME + VALUE "FileDescription", "CUPS Library" + VALUE "FileVersion", MASTER_PROD_VERS_STR + VALUE "InternalName", "libcups2.dll" + VALUE "LegalCopyright", MASTER_LEGAL_COPYRIGHT + VALUE "OriginalFilename", "libcups2.dll" + VALUE "ProductName", MASTER_PROD_NAME + VALUE "ProductVersion", MASTER_PROD_VERS_STR + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/cups/libcups_s.exp b/cups/libcups_s.exp new file mode 100644 index 0000000..b8b2207 --- /dev/null +++ b/cups/libcups_s.exp @@ -0,0 +1,85 @@ +_cups_debug_fd +_cupsBufferGet +_cupsBufferRelease +_cupsGet1284Values +_cupsGetDests +_cupsGetPassword +_cupsGlobals +_cupsLangPrintError +_cupsLangPrintf +_cupsLangPuts +_cupsLangString +_cupsMD5Append +_cupsMD5Finish +_cupsMD5Init +_cupsMessageFree +_cupsMessageLoad +_cupsMessageLookup +_cupsNextDelay +_cupsSetError +_cupsSetLocale +_cupsSNMPClose +_cupsSNMPCopyOID +_cupsSNMPDefaultCommunity +_cupsSNMPIsOID +_cupsSNMPIsOIDPrefixed +_cupsSNMPOIDToString +_cupsSNMPOpen +_cupsSNMPRead +_cupsSNMPSetDebug +_cupsSNMPStringToOID +_cupsSNMPWalk +_cupsSNMPWrite +_cupsStrAlloc +_cupsStrFlush +_cupsStrFormatd +_cupsStrFree +_cupsStrRetain +_cupsStrScand +_cupsStrStatistics +_cups_getifaddrs +_cups_freeifaddrs +_cups_strcpy +_cups_strlcat +_cups_strlcpy +_httpAddrPort +_httpAddrSetPort +_httpAssembleUUID +_httpBIOMethods +_httpCreate +_httpEncodeURI +_httpPeek +_httpResolveURI +_httpSetTimeout +_httpWait +_ippFindOption +_ppdFreeLanguages +_ppdGetEncoding +_ppdGetLanguages +_ppdHashName +_ppdLocalizedAttr +_ppdNormalizeMakeAndModel +_ppdOpen +_ppdOpenFile +_ppdParseOptions +_pwgCreateWithFile +_pwgDestroy +_pwgWriteFile +_pwgGenerateSize +_pwgInitSize +_pwgMediaForLegacy +_pwgMediaForPPD +_pwgMediaForPWG +_pwgMediaForSize +_pwgCreateWithPPD +_pwgGetBin +_pwgGetInputSlot +_pwgGetMediaType +_pwgGetOutputBin +_pwgGetPageSize +_pwgGetSize +_pwgGetSource +_pwgGetType +_pwgInputSlotForSource +_pwgMediaTypeForType +_pwgPageSizeForMedia diff --git a/cups/localize.c b/cups/localize.c new file mode 100644 index 0000000..7bb0fed --- /dev/null +++ b/cups/localize.c @@ -0,0 +1,779 @@ +/* + * "$Id: localize.c 7679 2008-06-19 23:37:45Z mike $" + * + * PPD localization routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This code and any derivative of it may be used and distributed + * freely under the terms of the GNU General Public License when + * used with GNU Ghostscript or its derivatives. Use of the code + * (or any derivative of it) with software other than GNU + * GhostScript (or its derivatives) is governed by the CUPS license + * agreement. + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * ppdLocalize() - Localize the PPD file to the current locale. + * ppdLocalizeAttr() - Localize an attribute. + * ppdLocalizeIPPReason() - Get the localized version of a cupsIPPReason + * attribute. + * ppdLocalizeMarkerName() - Get the localized version of a marker-names + * attribute value. + * _ppdFreeLanguages() - Free an array of languages from _ppdGetLanguages. + * _ppdGetLanguages() - Get an array of languages from a PPD file. + * _ppdHashName() - Generate a hash value for a device or profile + * name. + * _ppdLocalizedAttr() - Find a localized attribute. + * ppd_ll_CC() - Get the current locale names. + */ + +/* + * Include necessary headers. + */ + +#include "cups-private.h" +#include "ppd-private.h" + + +/* + * Local functions... + */ + +static cups_lang_t *ppd_ll_CC(char *ll_CC, int ll_CC_size); + + +/* + * 'ppdLocalize()' - Localize the PPD file to the current locale. + * + * All groups, options, and choices are localized, as are ICC profile + * descriptions, printer presets, and custom option parameters. Each + * localized string uses the UTF-8 character encoding. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +ppdLocalize(ppd_file_t *ppd) /* I - PPD file */ +{ + int i, j, k; /* Looping vars */ + ppd_group_t *group; /* Current group */ + ppd_option_t *option; /* Current option */ + ppd_choice_t *choice; /* Current choice */ + ppd_coption_t *coption; /* Current custom option */ + ppd_cparam_t *cparam; /* Current custom parameter */ + ppd_attr_t *attr, /* Current attribute */ + *locattr; /* Localized attribute */ + char ckeyword[PPD_MAX_NAME], /* Custom keyword */ + ll_CC[6]; /* Language + country locale */ + + + /* + * Range check input... + */ + + DEBUG_printf(("ppdLocalize(ppd=%p)", ppd)); + + if (!ppd) + return (-1); + + /* + * Get the default language... + */ + + ppd_ll_CC(ll_CC, sizeof(ll_CC)); + + /* + * Now lookup all of the groups, options, choices, etc. + */ + + for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++) + { + if ((locattr = _ppdLocalizedAttr(ppd, "Translation", group->name, + ll_CC)) != NULL) + strlcpy(group->text, locattr->text, sizeof(group->text)); + + for (j = group->num_options, option = group->options; j > 0; j --, option ++) + { + if ((locattr = _ppdLocalizedAttr(ppd, "Translation", option->keyword, + ll_CC)) != NULL) + strlcpy(option->text, locattr->text, sizeof(option->text)); + + for (k = option->num_choices, choice = option->choices; + k > 0; + k --, choice ++) + { + if (strcmp(choice->choice, "Custom") || + !ppdFindCustomOption(ppd, option->keyword)) + locattr = _ppdLocalizedAttr(ppd, option->keyword, choice->choice, + ll_CC); + else + { + snprintf(ckeyword, sizeof(ckeyword), "Custom%s", option->keyword); + + locattr = _ppdLocalizedAttr(ppd, ckeyword, "True", ll_CC); + } + + if (locattr) + strlcpy(choice->text, locattr->text, sizeof(choice->text)); + } + } + } + + /* + * Translate any custom parameters... + */ + + for (coption = (ppd_coption_t *)cupsArrayFirst(ppd->coptions); + coption; + coption = (ppd_coption_t *)cupsArrayNext(ppd->coptions)) + { + for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) + { + snprintf(ckeyword, sizeof(ckeyword), "ParamCustom%s", coption->keyword); + + if ((locattr = _ppdLocalizedAttr(ppd, ckeyword, cparam->name, + ll_CC)) != NULL) + strlcpy(cparam->text, locattr->text, sizeof(cparam->text)); + } + } + + /* + * Translate ICC profile names... + */ + + if ((attr = ppdFindAttr(ppd, "APCustomColorMatchingName", NULL)) != NULL) + { + if ((locattr = _ppdLocalizedAttr(ppd, "APCustomColorMatchingName", + attr->spec, ll_CC)) != NULL) + strlcpy(attr->text, locattr->text, sizeof(attr->text)); + } + + for (attr = ppdFindAttr(ppd, "cupsICCProfile", NULL); + attr; + attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL)) + { + cupsArraySave(ppd->sorted_attrs); + + if ((locattr = _ppdLocalizedAttr(ppd, "cupsICCProfile", attr->spec, + ll_CC)) != NULL) + strlcpy(attr->text, locattr->text, sizeof(attr->text)); + + cupsArrayRestore(ppd->sorted_attrs); + } + + /* + * Translate printer presets... + */ + + for (attr = ppdFindAttr(ppd, "APPrinterPreset", NULL); + attr; + attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL)) + { + cupsArraySave(ppd->sorted_attrs); + + if ((locattr = _ppdLocalizedAttr(ppd, "APPrinterPreset", attr->spec, + ll_CC)) != NULL) + strlcpy(attr->text, locattr->text, sizeof(attr->text)); + + cupsArrayRestore(ppd->sorted_attrs); + } + + return (0); +} + + +/* + * 'ppdLocalizeAttr()' - Localize an attribute. + * + * This function uses the current locale to find the localized attribute for + * the given main and option keywords. If no localized version of the + * attribute exists for the current locale, the unlocalized version is returned. + */ + +ppd_attr_t * /* O - Localized attribute or @code NULL@ if none exists */ +ppdLocalizeAttr(ppd_file_t *ppd, /* I - PPD file */ + const char *keyword, /* I - Main keyword */ + const char *spec) /* I - Option keyword or @code NULL@ for none */ +{ + ppd_attr_t *locattr; /* Localized attribute */ + char ll_CC[6]; /* Language + country locale */ + + + /* + * Get the default language... + */ + + ppd_ll_CC(ll_CC, sizeof(ll_CC)); + + /* + * Find the localized attribute... + */ + + if (spec) + locattr = _ppdLocalizedAttr(ppd, keyword, spec, ll_CC); + else + locattr = _ppdLocalizedAttr(ppd, "Translation", keyword, ll_CC); + + if (!locattr) + locattr = ppdFindAttr(ppd, keyword, spec); + + return (locattr); +} + + +/* + * 'ppdLocalizeIPPReason()' - Get the localized version of a cupsIPPReason + * attribute. + * + * This function uses the current locale to find the corresponding reason + * text or URI from the attribute value. If "scheme" is NULL or "text", + * the returned value contains human-readable (UTF-8) text from the translation + * string or attribute value. Otherwise the corresponding URI is returned. + * + * If no value of the requested scheme can be found, NULL is returned. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +const char * /* O - Value or NULL if not found */ +ppdLocalizeIPPReason( + ppd_file_t *ppd, /* I - PPD file */ + const char *reason, /* I - IPP reason keyword to look up */ + const char *scheme, /* I - URI scheme or NULL for text */ + char *buffer, /* I - Value buffer */ + size_t bufsize) /* I - Size of value buffer */ +{ + cups_lang_t *lang; /* Current language */ + ppd_attr_t *locattr; /* Localized attribute */ + char ll_CC[6], /* Language + country locale */ + *bufptr, /* Pointer into buffer */ + *bufend, /* Pointer to end of buffer */ + *valptr; /* Pointer into value */ + int ch, /* Hex-encoded character */ + schemelen; /* Length of scheme name */ + + + /* + * Range check input... + */ + + if (buffer) + *buffer = '\0'; + + if (!ppd || !reason || (scheme && !*scheme) || + !buffer || bufsize < PPD_MAX_TEXT) + return (NULL); + + /* + * Get the default language... + */ + + lang = ppd_ll_CC(ll_CC, sizeof(ll_CC)); + + /* + * Find the localized attribute... + */ + + if ((locattr = _ppdLocalizedAttr(ppd, "cupsIPPReason", reason, + ll_CC)) == NULL) + locattr = ppdFindAttr(ppd, "cupsIPPReason", reason); + + if (!locattr) + { + if (lang && (!scheme || !strcmp(scheme, "text"))) + { + /* + * Try to localize a standard printer-state-reason keyword... + */ + + const char *message = NULL; /* Localized message */ + + if (!strncmp(reason, "media-needed", 12)) + message = _("The paper tray needs to be filled."); + else if (!strncmp(reason, "media-jam", 9)) + message = _("There is a paper jam."); + else if (!strncmp(reason, "offline", 7) || + !strncmp(reason, "shutdown", 8)) + message = _("The printer is not connected."); + else if (!strncmp(reason, "toner-low", 9)) + message = _("The printer is running low on toner."); + else if (!strncmp(reason, "toner-empty", 11)) + message = _("The printer may be out of toner."); + else if (!strncmp(reason, "cover-open", 10)) + message = _("The printer's cover is open."); + else if (!strncmp(reason, "interlock-open", 14)) + message = _("The printer's interlock is open."); + else if (!strncmp(reason, "door-open", 9)) + message = _("The printer's door is open."); + else if (!strncmp(reason, "input-tray-missing", 18)) + message = _("The paper tray is missing."); + else if (!strncmp(reason, "media-low", 9)) + message = _("The paper tray is almost empty."); + else if (!strncmp(reason, "media-empty", 11)) + message = _("The paper tray is empty."); + else if (!strncmp(reason, "output-tray-missing", 19)) + message = _("The output bin is missing."); + else if (!strncmp(reason, "output-area-almost-full", 23)) + message = _("The output bin is almost full."); + else if (!strncmp(reason, "output-area-full", 16)) + message = _("The output bin is full."); + else if (!strncmp(reason, "marker-supply-low", 17)) + message = _("The printer is running low on ink."); + else if (!strncmp(reason, "marker-supply-empty", 19)) + message = _("The printer may be out of ink."); + else if (!strncmp(reason, "marker-waste-almost-full", 24)) + message = _("The printer's waste bin is almost full."); + else if (!strncmp(reason, "marker-waste-full", 17)) + message = _("The printer's waste bin is full."); + else if (!strncmp(reason, "fuser-over-temp", 15)) + message = _("The fuser's temperature is high."); + else if (!strncmp(reason, "fuser-under-temp", 16)) + message = _("The fuser's temperature is low."); + else if (!strncmp(reason, "opc-near-eol", 12)) + message = _("The optical photoconductor will need to be replaced soon."); + else if (!strncmp(reason, "opc-life-over", 13)) + message = _("The optical photoconductor needs to be replaced."); + else if (!strncmp(reason, "developer-low", 13)) + message = _("The developer unit will need to be replaced soon."); + else if (!strncmp(reason, "developer-empty", 15)) + message = _("The developer unit needs to be replaced."); + + if (message) + { + strlcpy(buffer, _cupsLangString(lang, message), bufsize); + return (buffer); + } + } + + return (NULL); + } + + /* + * Now find the value we need... + */ + + bufend = buffer + bufsize - 1; + + if (!scheme || !strcmp(scheme, "text")) + { + /* + * Copy a text value (either the translation text or text:... URIs from + * the value... + */ + + strlcpy(buffer, locattr->text, bufsize); + + for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;) + { + if (!strncmp(valptr, "text:", 5)) + { + /* + * Decode text: URI and add to the buffer... + */ + + valptr += 5; + + while (*valptr && !_cups_isspace(*valptr) && bufptr < bufend) + { + if (*valptr == '%' && isxdigit(valptr[1] & 255) && + isxdigit(valptr[2] & 255)) + { + /* + * Pull a hex-encoded character from the URI... + */ + + valptr ++; + + if (isdigit(*valptr & 255)) + ch = (*valptr - '0') << 4; + else + ch = (tolower(*valptr) - 'a' + 10) << 4; + valptr ++; + + if (isdigit(*valptr & 255)) + *bufptr++ = ch | (*valptr - '0'); + else + *bufptr++ = ch | (tolower(*valptr) - 'a' + 10); + valptr ++; + } + else if (*valptr == '+') + { + *bufptr++ = ' '; + valptr ++; + } + else + *bufptr++ = *valptr++; + } + } + else + { + /* + * Skip this URI... + */ + + while (*valptr && !_cups_isspace(*valptr)) + valptr++; + } + + /* + * Skip whitespace... + */ + + while (_cups_isspace(*valptr)) + valptr ++; + } + + if (bufptr > buffer) + *bufptr = '\0'; + + return (buffer); + } + else + { + /* + * Copy a URI... + */ + + schemelen = strlen(scheme); + if (scheme[schemelen - 1] == ':') /* Force scheme to be just the name */ + schemelen --; + + for (valptr = locattr->value, bufptr = buffer; *valptr && bufptr < bufend;) + { + if ((!strncmp(valptr, scheme, schemelen) && valptr[schemelen] == ':') || + (*valptr == '/' && !strcmp(scheme, "file"))) + { + /* + * Copy URI... + */ + + while (*valptr && !_cups_isspace(*valptr) && bufptr < bufend) + *bufptr++ = *valptr++; + + *bufptr = '\0'; + + return (buffer); + } + else + { + /* + * Skip this URI... + */ + + while (*valptr && !_cups_isspace(*valptr)) + valptr++; + } + + /* + * Skip whitespace... + */ + + while (_cups_isspace(*valptr)) + valptr ++; + } + + return (NULL); + } +} + + +/* + * 'ppdLocalizeMarkerName()' - Get the localized version of a marker-names + * attribute value. + * + * This function uses the current locale to find the corresponding name + * text from the attribute value. If no localized text for the requested + * name can be found, @code NULL@ is returned. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +const char * /* O - Value or @code NULL@ if not found */ +ppdLocalizeMarkerName( + ppd_file_t *ppd, /* I - PPD file */ + const char *name) /* I - Marker name to look up */ +{ + ppd_attr_t *locattr; /* Localized attribute */ + char ll_CC[6]; /* Language + country locale */ + + + /* + * Range check input... + */ + + if (!ppd || !name) + return (NULL); + + /* + * Get the default language... + */ + + ppd_ll_CC(ll_CC, sizeof(ll_CC)); + + /* + * Find the localized attribute... + */ + + if ((locattr = _ppdLocalizedAttr(ppd, "cupsMarkerName", name, + ll_CC)) == NULL) + locattr = ppdFindAttr(ppd, "cupsMarkerName", name); + + return (locattr ? locattr->text : NULL); +} + + +/* + * '_ppdFreeLanguages()' - Free an array of languages from _ppdGetLanguages. + */ + +void +_ppdFreeLanguages( + cups_array_t *languages) /* I - Languages array */ +{ + char *language; /* Current language */ + + + for (language = (char *)cupsArrayFirst(languages); + language; + language = (char *)cupsArrayNext(languages)) + free(language); + + cupsArrayDelete(languages); +} + + +/* + * '_ppdGetLanguages()' - Get an array of languages from a PPD file. + */ + +cups_array_t * /* O - Languages array */ +_ppdGetLanguages(ppd_file_t *ppd) /* I - PPD file */ +{ + cups_array_t *languages; /* Languages array */ + ppd_attr_t *attr; /* cupsLanguages attribute */ + char *value, /* Copy of attribute value */ + *start, /* Start of current language */ + *ptr; /* Pointer into languages */ + + + /* + * See if we have a cupsLanguages attribute... + */ + + if ((attr = ppdFindAttr(ppd, "cupsLanguages", NULL)) == NULL || !attr->value) + return (NULL); + + /* + * Yes, load the list... + */ + + if ((languages = cupsArrayNew((cups_array_func_t)strcmp, NULL)) == NULL) + return (NULL); + + if ((value = strdup(attr->value)) == NULL) + { + cupsArrayDelete(languages); + return (NULL); + } + + for (ptr = value; *ptr;) + { + /* + * Skip leading whitespace... + */ + + while (_cups_isspace(*ptr)) + ptr ++; + + if (!*ptr) + break; + + /* + * Find the end of this language name... + */ + + for (start = ptr; *ptr && !_cups_isspace(*ptr); ptr ++); + + if (*ptr) + *ptr++ = '\0'; + + if (!strcmp(start, "en")) + continue; + + cupsArrayAdd(languages, strdup(start)); + } + + /* + * Free the temporary string and return either an array with one or more + * values or a NULL pointer... + */ + + free(value); + + if (cupsArrayCount(languages) == 0) + { + cupsArrayDelete(languages); + return (NULL); + } + else + return (languages); +} + + +/* + * '_ppdHashName()' - Generate a hash value for a device or profile name. + * + * This function is primarily used on OS X, but is generally accessible + * since cupstestppd needs to check for profile name collisions in PPD files... + */ + +unsigned /* O - Hash value */ +_ppdHashName(const char *name) /* I - Name to hash */ +{ + int mult; /* Multiplier */ + unsigned hash = 0; /* Hash value */ + + + for (mult = 1; *name && mult <= 128; mult ++, name ++) + hash += (*name & 255) * mult; + + return (hash); +} + + +/* + * '_ppdLocalizedAttr()' - Find a localized attribute. + */ + +ppd_attr_t * /* O - Localized attribute or NULL */ +_ppdLocalizedAttr(ppd_file_t *ppd, /* I - PPD file */ + const char *keyword, /* I - Main keyword */ + const char *spec, /* I - Option keyword */ + const char *ll_CC) /* I - Language + country locale */ +{ + char lkeyword[PPD_MAX_NAME]; /* Localization keyword */ + ppd_attr_t *attr; /* Current attribute */ + + + DEBUG_printf(("4_ppdLocalizedAttr(ppd=%p, keyword=\"%s\", spec=\"%s\", " + "ll_CC=\"%s\")", ppd, keyword, spec, ll_CC)); + + /* + * Look for Keyword.ll_CC, then Keyword.ll... + */ + + snprintf(lkeyword, sizeof(lkeyword), "%s.%s", ll_CC, keyword); + if ((attr = ppdFindAttr(ppd, lkeyword, spec)) == NULL) + { + snprintf(lkeyword, sizeof(lkeyword), "%2.2s.%s", ll_CC, keyword); + attr = ppdFindAttr(ppd, lkeyword, spec); + + if (!attr) + { + if (!strncmp(ll_CC, "ja", 2)) + { + /* + * Due to a bug in the CUPS DDK 1.1.0 ppdmerge program, Japanese + * PPD files were incorrectly assigned "jp" as the locale name + * instead of "ja". Support both the old (incorrect) and new + * locale names for Japanese... + */ + + snprintf(lkeyword, sizeof(lkeyword), "jp.%s", keyword); + attr = ppdFindAttr(ppd, lkeyword, spec); + } + else if (!strncmp(ll_CC, "no", 2)) + { + /* + * Norway has two languages, "Bokmal" (the primary one) + * and "Nynorsk" (new Norwegian); we map "no" to "nb" here as + * recommended by the locale folks... + */ + + snprintf(lkeyword, sizeof(lkeyword), "nb.%s", keyword); + attr = ppdFindAttr(ppd, lkeyword, spec); + } + } + } + +#ifdef DEBUG + if (attr) + DEBUG_printf(("5_ppdLocalizedAttr: *%s %s/%s: \"%s\"\n", attr->name, + attr->spec, attr->text, attr->value ? attr->value : "")); + else + DEBUG_puts("5_ppdLocalizedAttr: NOT FOUND"); +#endif /* DEBUG */ + + return (attr); +} + + +/* + * 'ppd_ll_CC()' - Get the current locale names. + */ + +static cups_lang_t * /* O - Current language */ +ppd_ll_CC(char *ll_CC, /* O - Country-specific locale name */ + int ll_CC_size) /* I - Size of country-specific name */ +{ + cups_lang_t *lang; /* Current language */ + + + /* + * Get the current locale... + */ + + if ((lang = cupsLangDefault()) == NULL) + { + strlcpy(ll_CC, "en_US", ll_CC_size); + return (NULL); + } + + /* + * Copy the locale name... + */ + + strlcpy(ll_CC, lang->language, ll_CC_size); + + if (strlen(ll_CC) == 2) + { + /* + * Map "ll" to primary/origin country locales to have the best + * chance of finding a match... + */ + + if (!strcmp(ll_CC, "cs")) + strlcpy(ll_CC, "cs_CZ", ll_CC_size); + else if (!strcmp(ll_CC, "en")) + strlcpy(ll_CC, "en_US", ll_CC_size); + else if (!strcmp(ll_CC, "ja")) + strlcpy(ll_CC, "ja_JP", ll_CC_size); + else if (!strcmp(ll_CC, "sv")) + strlcpy(ll_CC, "sv_SE", ll_CC_size); + else if (!strcmp(ll_CC, "zh")) /* Simplified Chinese */ + strlcpy(ll_CC, "zh_CN", ll_CC_size); + } + + DEBUG_printf(("8ppd_ll_CC: lang->language=\"%s\", ll_CC=\"%s\"...", + lang->language, ll_CC)); + return (lang); +} + + +/* + * End of "$Id: localize.c 7679 2008-06-19 23:37:45Z mike $". + */ diff --git a/cups/mark.c b/cups/mark.c new file mode 100644 index 0000000..dc2257f --- /dev/null +++ b/cups/mark.c @@ -0,0 +1,1101 @@ +/* + * "$Id: mark.c 9042 2010-03-24 00:45:34Z mike $" + * + * Option marking routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * cupsMarkOptions() - Mark command-line options in a PPD file. + * ppdFindChoice() - Return a pointer to an option choice. + * ppdFindMarkedChoice() - Return the marked choice for the specified option. + * ppdFindOption() - Return a pointer to the specified option. + * ppdIsMarked() - Check to see if an option is marked. + * ppdMarkDefaults() - Mark all default options in the PPD file. + * ppdMarkOption() - Mark an option in a PPD file and return the number + * of conflicts. + * ppdFirstOption() - Return the first option in the PPD file. + * ppdNextOption() - Return the next option in the PPD file. + * _ppdParseOptions() - Parse options from a PPD file. + * ppd_debug_marked() - Output the marked array to stdout... + * ppd_defaults() - Set the defaults for this group and all sub-groups. + * ppd_mark_choices() - Mark one or more option choices from a string. + * ppd_mark_option() - Quickly mark an option without checking for + * conflicts. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * Local functions... + */ + +#ifdef DEBUG +static void ppd_debug_marked(ppd_file_t *ppd, const char *title); +#else +# define ppd_debug_marked(ppd,title) +#endif /* DEBUG */ +static void ppd_defaults(ppd_file_t *ppd, ppd_group_t *g); +static void ppd_mark_choices(ppd_file_t *ppd, const char *s); +static void ppd_mark_option(ppd_file_t *ppd, const char *option, + const char *choice); + + +/* + * 'cupsMarkOptions()' - Mark command-line options in a PPD file. + * + * This function maps the IPP "finishings", "media", "mirror", + * "multiple-document-handling", "output-bin", "print-color-mode", + * "print-quality", "printer-resolution", and "sides" attributes to their + * corresponding PPD options and choices. + */ + +int /* O - 1 if conflicts exist, 0 otherwise */ +cupsMarkOptions( + ppd_file_t *ppd, /* I - PPD file */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + int i, j; /* Looping vars */ + char *ptr, /* Pointer into string */ + s[255]; /* Temporary string */ + const char *val, /* Pointer into value */ + *media, /* media option */ + *output_bin, /* output-bin option */ + *page_size, /* PageSize option */ + *ppd_keyword, /* PPD keyword */ + *print_color_mode, /* print-color-mode option */ + *print_quality, /* print-quality option */ + *sides; /* sides option */ + cups_option_t *optptr; /* Current option */ + ppd_attr_t *attr; /* PPD attribute */ + _ppd_cache_t *cache; /* PPD cache and mapping data */ + + + /* + * Check arguments... + */ + + if (!ppd || num_options <= 0 || !options) + return (0); + + ppd_debug_marked(ppd, "Before..."); + + /* + * Do special handling for finishings, media, output-bin, output-mode, + * print-color-mode, print-quality, and PageSize... + */ + + media = cupsGetOption("media", num_options, options); + output_bin = cupsGetOption("output-bin", num_options, options); + page_size = cupsGetOption("PageSize", num_options, options); + print_quality = cupsGetOption("print-quality", num_options, options); + sides = cupsGetOption("sides", num_options, options); + + if ((print_color_mode = cupsGetOption("print-color-mode", num_options, + options)) == NULL) + print_color_mode = cupsGetOption("output-mode", num_options, options); + + if ((media || output_bin || print_color_mode || print_quality || sides) && + !ppd->cache) + { + /* + * Load PPD cache and mapping data as needed... + */ + + ppd->cache = _ppdCacheCreateWithPPD(ppd); + } + + cache = ppd->cache; + + if (media) + { + /* + * Loop through the option string, separating it at commas and marking each + * individual option as long as the corresponding PPD option (PageSize, + * InputSlot, etc.) is not also set. + * + * For PageSize, we also check for an empty option value since some versions + * of MacOS X use it to specify auto-selection of the media based solely on + * the size. + */ + + for (val = media; *val;) + { + /* + * Extract the sub-option from the string... + */ + + for (ptr = s; *val && *val != ',' && (ptr - s) < (sizeof(s) - 1);) + *ptr++ = *val++; + *ptr++ = '\0'; + + if (*val == ',') + val ++; + + /* + * Mark it... + */ + + if (!page_size || !page_size[0]) + { + if (!_cups_strncasecmp(s, "Custom.", 7) || ppdPageSize(ppd, s)) + ppd_mark_option(ppd, "PageSize", s); + else if ((ppd_keyword = _ppdCacheGetPageSize(cache, NULL, s, NULL)) != NULL) + ppd_mark_option(ppd, "PageSize", ppd_keyword); + } + + if (cache && cache->source_option && + !cupsGetOption(cache->source_option, num_options, options) && + (ppd_keyword = _ppdCacheGetInputSlot(cache, NULL, s)) != NULL) + ppd_mark_option(ppd, cache->source_option, ppd_keyword); + + if (!cupsGetOption("MediaType", num_options, options) && + (ppd_keyword = _ppdCacheGetMediaType(cache, NULL, s)) != NULL) + ppd_mark_option(ppd, "MediaType", ppd_keyword); + } + } + + if (cache) + { + if (!cupsGetOption("com.apple.print.DocumentTicket.PMSpoolFormat", + num_options, options) && + !cupsGetOption("APPrinterPreset", num_options, options) && + (print_color_mode || print_quality)) + { + /* + * Map output-mode and print-quality to a preset... + */ + + _pwg_print_color_mode_t pwg_pcm;/* print-color-mode index */ + _pwg_print_quality_t pwg_pq; /* print-quality index */ + cups_option_t *preset;/* Current preset option */ + + if (print_color_mode && !strcmp(print_color_mode, "monochrome")) + pwg_pcm = _PWG_PRINT_COLOR_MODE_MONOCHROME; + else + pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR; + + if (print_quality) + { + pwg_pq = atoi(print_quality) - IPP_QUALITY_DRAFT; + if (pwg_pq < _PWG_PRINT_QUALITY_DRAFT) + pwg_pq = _PWG_PRINT_QUALITY_DRAFT; + else if (pwg_pq > _PWG_PRINT_QUALITY_HIGH) + pwg_pq = _PWG_PRINT_QUALITY_HIGH; + } + else + pwg_pq = _PWG_PRINT_QUALITY_NORMAL; + + if (cache->num_presets[pwg_pcm][pwg_pq] == 0) + { + /* + * Try to find a preset that works so that we maximize the chances of us + * getting a good print using IPP attributes. + */ + + if (cache->num_presets[pwg_pcm][_PWG_PRINT_QUALITY_NORMAL] > 0) + pwg_pq = _PWG_PRINT_QUALITY_NORMAL; + else if (cache->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_pq] > 0) + pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR; + else + { + pwg_pq = _PWG_PRINT_QUALITY_NORMAL; + pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR; + } + } + + if (cache->num_presets[pwg_pcm][pwg_pq] > 0) + { + /* + * Copy the preset options as long as the corresponding names are not + * already defined in the IPP request... + */ + + for (i = cache->num_presets[pwg_pcm][pwg_pq], + preset = cache->presets[pwg_pcm][pwg_pq]; + i > 0; + i --, preset ++) + { + if (!cupsGetOption(preset->name, num_options, options)) + ppd_mark_option(ppd, preset->name, preset->value); + } + } + } + + if (output_bin && !cupsGetOption("OutputBin", num_options, options) && + (ppd_keyword = _ppdCacheGetOutputBin(cache, output_bin)) != NULL) + { + /* + * Map output-bin to OutputBin... + */ + + ppd_mark_option(ppd, "OutputBin", ppd_keyword); + } + + if (sides && cache->sides_option && + !cupsGetOption(cache->sides_option, num_options, options)) + { + /* + * Map sides to duplex option... + */ + + if (!strcmp(sides, "one-sided") && cache->sides_1sided) + ppd_mark_option(ppd, cache->sides_option, cache->sides_1sided); + else if (!strcmp(sides, "two-sided-long-edge") && + cache->sides_2sided_long) + ppd_mark_option(ppd, cache->sides_option, cache->sides_2sided_long); + else if (!strcmp(sides, "two-sided-short-edge") && + cache->sides_2sided_short) + ppd_mark_option(ppd, cache->sides_option, cache->sides_2sided_short); + } + } + + /* + * Mark other options... + */ + + for (i = num_options, optptr = options; i > 0; i --, optptr ++) + if (!_cups_strcasecmp(optptr->name, "media") || + !_cups_strcasecmp(optptr->name, "output-bin") || + !_cups_strcasecmp(optptr->name, "output-mode") || + !_cups_strcasecmp(optptr->name, "print-quality") || + !_cups_strcasecmp(optptr->name, "sides")) + continue; + else if (!_cups_strcasecmp(optptr->name, "resolution") || + !_cups_strcasecmp(optptr->name, "printer-resolution")) + { + ppd_mark_option(ppd, "Resolution", optptr->value); + ppd_mark_option(ppd, "SetResolution", optptr->value); + /* Calcomp, Linotype, QMS, Summagraphics, Tektronix, Varityper */ + ppd_mark_option(ppd, "JCLResolution", optptr->value); + /* HP */ + ppd_mark_option(ppd, "CNRes_PGP", optptr->value); + /* Canon */ + } + else if (!_cups_strcasecmp(optptr->name, "multiple-document-handling")) + { + if (!cupsGetOption("Collate", num_options, options) && + ppdFindOption(ppd, "Collate")) + { + if (_cups_strcasecmp(optptr->value, "separate-documents-uncollated-copies")) + ppd_mark_option(ppd, "Collate", "True"); + else + ppd_mark_option(ppd, "Collate", "False"); + } + } + else if (!_cups_strcasecmp(optptr->name, "finishings")) + { + /* + * Lookup cupsIPPFinishings attributes for each value... + */ + + for (ptr = optptr->value; *ptr;) + { + /* + * Get the next finishings number... + */ + + if (!isdigit(*ptr & 255)) + break; + + if ((j = strtol(ptr, &ptr, 10)) < 3) + break; + + /* + * Skip separator as needed... + */ + + if (*ptr == ',') + ptr ++; + + /* + * Look it up in the PPD file... + */ + + sprintf(s, "%d", j); + + if ((attr = ppdFindAttr(ppd, "cupsIPPFinishings", s)) == NULL) + continue; + + /* + * Apply "*Option Choice" settings from the attribute value... + */ + + ppd_mark_choices(ppd, attr->value); + } + } + else if (!_cups_strcasecmp(optptr->name, "APPrinterPreset")) + { + /* + * Lookup APPrinterPreset value... + */ + + if ((attr = ppdFindAttr(ppd, "APPrinterPreset", optptr->value)) != NULL) + { + /* + * Apply "*Option Choice" settings from the attribute value... + */ + + ppd_mark_choices(ppd, attr->value); + } + } + else if (!_cups_strcasecmp(optptr->name, "mirror")) + ppd_mark_option(ppd, "MirrorPrint", optptr->value); + else + ppd_mark_option(ppd, optptr->name, optptr->value); + + ppd_debug_marked(ppd, "After..."); + + return (ppdConflicts(ppd) > 0); +} + + +/* + * 'ppdFindChoice()' - Return a pointer to an option choice. + */ + +ppd_choice_t * /* O - Choice pointer or @code NULL@ */ +ppdFindChoice(ppd_option_t *o, /* I - Pointer to option */ + const char *choice) /* I - Name of choice */ +{ + int i; /* Looping var */ + ppd_choice_t *c; /* Current choice */ + + + if (!o || !choice) + return (NULL); + + if (choice[0] == '{' || !_cups_strncasecmp(choice, "Custom.", 7)) + choice = "Custom"; + + for (i = o->num_choices, c = o->choices; i > 0; i --, c ++) + if (!_cups_strcasecmp(c->choice, choice)) + return (c); + + return (NULL); +} + + +/* + * 'ppdFindMarkedChoice()' - Return the marked choice for the specified option. + */ + +ppd_choice_t * /* O - Pointer to choice or @code NULL@ */ +ppdFindMarkedChoice(ppd_file_t *ppd, /* I - PPD file */ + const char *option) /* I - Keyword/option name */ +{ + ppd_choice_t key, /* Search key for choice */ + *marked; /* Marked choice */ + + + DEBUG_printf(("2ppdFindMarkedChoice(ppd=%p, option=\"%s\")", ppd, option)); + + if ((key.option = ppdFindOption(ppd, option)) == NULL) + { + DEBUG_puts("3ppdFindMarkedChoice: Option not found, returning NULL"); + return (NULL); + } + + marked = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key); + + DEBUG_printf(("3ppdFindMarkedChoice: Returning %p(%s)...", marked, + marked ? marked->choice : "NULL")); + + return (marked); +} + + +/* + * 'ppdFindOption()' - Return a pointer to the specified option. + */ + +ppd_option_t * /* O - Pointer to option or @code NULL@ */ +ppdFindOption(ppd_file_t *ppd, /* I - PPD file data */ + const char *option) /* I - Option/Keyword name */ +{ + /* + * Range check input... + */ + + if (!ppd || !option) + return (NULL); + + if (ppd->options) + { + /* + * Search in the array... + */ + + ppd_option_t key; /* Option search key */ + + + strlcpy(key.keyword, option, sizeof(key.keyword)); + + return ((ppd_option_t *)cupsArrayFind(ppd->options, &key)); + } + else + { + /* + * Search in each group... + */ + + int i, j; /* Looping vars */ + ppd_group_t *group; /* Current group */ + ppd_option_t *optptr; /* Current option */ + + + for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++) + for (j = group->num_options, optptr = group->options; + j > 0; + j --, optptr ++) + if (!_cups_strcasecmp(optptr->keyword, option)) + return (optptr); + + return (NULL); + } +} + + +/* + * 'ppdIsMarked()' - Check to see if an option is marked. + */ + +int /* O - Non-zero if option is marked */ +ppdIsMarked(ppd_file_t *ppd, /* I - PPD file data */ + const char *option, /* I - Option/Keyword name */ + const char *choice) /* I - Choice name */ +{ + ppd_choice_t key, /* Search key */ + *c; /* Choice pointer */ + + + if (!ppd) + return (0); + + if ((key.option = ppdFindOption(ppd, option)) == NULL) + return (0); + + if ((c = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) == NULL) + return (0); + + return (!strcmp(c->choice, choice)); +} + + +/* + * 'ppdMarkDefaults()' - Mark all default options in the PPD file. + */ + +void +ppdMarkDefaults(ppd_file_t *ppd) /* I - PPD file record */ +{ + int i; /* Looping variables */ + ppd_group_t *g; /* Current group */ + ppd_choice_t *c; /* Current choice */ + + + if (!ppd) + return; + + /* + * Clean out the marked array... + */ + + for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked); + c; + c = (ppd_choice_t *)cupsArrayNext(ppd->marked)) + { + cupsArrayRemove(ppd->marked, c); + c->marked = 0; + } + + /* + * Then repopulate it with the defaults... + */ + + for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++) + ppd_defaults(ppd, g); +} + + +/* + * 'ppdMarkOption()' - Mark an option in a PPD file and return the number of + * conflicts. + */ + +int /* O - Number of conflicts */ +ppdMarkOption(ppd_file_t *ppd, /* I - PPD file record */ + const char *option, /* I - Keyword */ + const char *choice) /* I - Option name */ +{ + DEBUG_printf(("ppdMarkOption(ppd=%p, option=\"%s\", choice=\"%s\")", + ppd, option, choice)); + + /* + * Range check input... + */ + + if (!ppd || !option || !choice) + return (0); + + /* + * Mark the option... + */ + + ppd_mark_option(ppd, option, choice); + + /* + * Return the number of conflicts... + */ + + return (ppdConflicts(ppd)); +} + + +/* + * 'ppdFirstOption()' - Return the first option in the PPD file. + * + * Options are returned from all groups in ascending alphanumeric order. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_option_t * /* O - First option or @code NULL@ */ +ppdFirstOption(ppd_file_t *ppd) /* I - PPD file */ +{ + if (!ppd) + return (NULL); + else + return ((ppd_option_t *)cupsArrayFirst(ppd->options)); +} + + +/* + * 'ppdNextOption()' - Return the next option in the PPD file. + * + * Options are returned from all groups in ascending alphanumeric order. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_option_t * /* O - Next option or @code NULL@ */ +ppdNextOption(ppd_file_t *ppd) /* I - PPD file */ +{ + if (!ppd) + return (NULL); + else + return ((ppd_option_t *)cupsArrayNext(ppd->options)); +} + + +/* + * '_ppdParseOptions()' - Parse options from a PPD file. + * + * This function looks for strings of the form: + * + * *option choice ... *optionN choiceN + * property value ... propertyN valueN + * + * It stops when it finds a string that doesn't match this format. + */ + +int /* O - Number of options */ +_ppdParseOptions( + const char *s, /* I - String to parse */ + int num_options, /* I - Number of options */ + cups_option_t **options, /* IO - Options */ + _ppd_parse_t which) /* I - What to parse */ +{ + char option[PPD_MAX_NAME * 2 + 1], /* Current option/property */ + choice[PPD_MAX_NAME], /* Current choice/value */ + *ptr; /* Pointer into option or choice */ + + + if (!s) + return (num_options); + + /* + * Read all of the "*Option Choice" and "property value" pairs from the + * string, add them to an options array as we go... + */ + + while (*s) + { + /* + * Skip leading whitespace... + */ + + while (_cups_isspace(*s)) + s ++; + + /* + * Get the option/property name... + */ + + ptr = option; + while (*s && !_cups_isspace(*s) && ptr < (option + sizeof(option) - 1)) + *ptr++ = *s++; + + if (ptr == s || !_cups_isspace(*s)) + break; + + *ptr = '\0'; + + /* + * Get the choice... + */ + + while (_cups_isspace(*s)) + s ++; + + if (!*s) + break; + + ptr = choice; + while (*s && !_cups_isspace(*s) && ptr < (choice + sizeof(choice) - 1)) + *ptr++ = *s++; + + if (*s && !_cups_isspace(*s)) + break; + + *ptr = '\0'; + + /* + * Add it to the options array... + */ + + if (option[0] == '*' && which != _PPD_PARSE_PROPERTIES) + num_options = cupsAddOption(option + 1, choice, num_options, options); + else if (option[0] != '*' && which != _PPD_PARSE_OPTIONS) + num_options = cupsAddOption(option, choice, num_options, options); + } + + return (num_options); +} + + +#ifdef DEBUG +/* + * 'ppd_debug_marked()' - Output the marked array to stdout... + */ + +static void +ppd_debug_marked(ppd_file_t *ppd, /* I - PPD file data */ + const char *title) /* I - Title for list */ +{ + ppd_choice_t *c; /* Current choice */ + + + DEBUG_printf(("2cupsMarkOptions: %s", title)); + + for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked); + c; + c = (ppd_choice_t *)cupsArrayNext(ppd->marked)) + DEBUG_printf(("2cupsMarkOptions: %s=%s", c->option->keyword, c->choice)); +} +#endif /* DEBUG */ + + +/* + * 'ppd_defaults()' - Set the defaults for this group and all sub-groups. + */ + +static void +ppd_defaults(ppd_file_t *ppd, /* I - PPD file */ + ppd_group_t *g) /* I - Group to default */ +{ + int i; /* Looping var */ + ppd_option_t *o; /* Current option */ + ppd_group_t *sg; /* Current sub-group */ + + + for (i = g->num_options, o = g->options; i > 0; i --, o ++) + if (_cups_strcasecmp(o->keyword, "PageRegion") != 0) + ppdMarkOption(ppd, o->keyword, o->defchoice); + + for (i = g->num_subgroups, sg = g->subgroups; i > 0; i --, sg ++) + ppd_defaults(ppd, sg); +} + + +/* + * 'ppd_mark_choices()' - Mark one or more option choices from a string. + */ + +static void +ppd_mark_choices(ppd_file_t *ppd, /* I - PPD file */ + const char *s) /* I - "*Option Choice ..." string */ +{ + int i, /* Looping var */ + num_options; /* Number of options */ + cups_option_t *options, /* Options */ + *option; /* Current option */ + + + if (!s) + return; + + options = NULL; + num_options = _ppdParseOptions(s, 0, &options, 0); + + for (i = num_options, option = options; i > 0; i --, option ++) + ppd_mark_option(ppd, option->name, option->value); + + cupsFreeOptions(num_options, options); +} + + +/* + * 'ppd_mark_option()' - Quick mark an option without checking for conflicts. + */ + +static void +ppd_mark_option(ppd_file_t *ppd, /* I - PPD file */ + const char *option, /* I - Option name */ + const char *choice) /* I - Choice name */ +{ + int i, j; /* Looping vars */ + ppd_option_t *o; /* Option pointer */ + ppd_choice_t *c, /* Choice pointer */ + *oldc, /* Old choice pointer */ + key; /* Search key for choice */ + struct lconv *loc; /* Locale data */ + + + DEBUG_printf(("7ppd_mark_option(ppd=%p, option=\"%s\", choice=\"%s\")", + ppd, option, choice)); + + /* + * AP_D_InputSlot is the "default input slot" on MacOS X, and setting + * it clears the regular InputSlot choices... + */ + + if (!_cups_strcasecmp(option, "AP_D_InputSlot")) + { + cupsArraySave(ppd->options); + + if ((o = ppdFindOption(ppd, "InputSlot")) != NULL) + { + key.option = o; + if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL) + { + oldc->marked = 0; + cupsArrayRemove(ppd->marked, oldc); + } + } + + cupsArrayRestore(ppd->options); + } + + /* + * Check for custom options... + */ + + cupsArraySave(ppd->options); + + o = ppdFindOption(ppd, option); + + cupsArrayRestore(ppd->options); + + if (!o) + return; + + loc = localeconv(); + + if (!_cups_strncasecmp(choice, "Custom.", 7)) + { + /* + * Handle a custom option... + */ + + if ((c = ppdFindChoice(o, "Custom")) == NULL) + return; + + if (!_cups_strcasecmp(option, "PageSize")) + { + /* + * Handle custom page sizes... + */ + + ppdPageSize(ppd, choice); + } + else + { + /* + * Handle other custom options... + */ + + ppd_coption_t *coption; /* Custom option */ + ppd_cparam_t *cparam; /* Custom parameter */ + char *units; /* Custom points units */ + + + if ((coption = ppdFindCustomOption(ppd, option)) != NULL) + { + if ((cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params)) == NULL) + return; + + switch (cparam->type) + { + case PPD_CUSTOM_CURVE : + case PPD_CUSTOM_INVCURVE : + case PPD_CUSTOM_REAL : + cparam->current.custom_real = (float)_cupsStrScand(choice + 7, + NULL, loc); + break; + + case PPD_CUSTOM_POINTS : + cparam->current.custom_points = (float)_cupsStrScand(choice + 7, + &units, + loc); + + if (units) + { + if (!_cups_strcasecmp(units, "cm")) + cparam->current.custom_points *= 72.0f / 2.54f; + else if (!_cups_strcasecmp(units, "mm")) + cparam->current.custom_points *= 72.0f / 25.4f; + else if (!_cups_strcasecmp(units, "m")) + cparam->current.custom_points *= 72.0f / 0.0254f; + else if (!_cups_strcasecmp(units, "in")) + cparam->current.custom_points *= 72.0f; + else if (!_cups_strcasecmp(units, "ft")) + cparam->current.custom_points *= 12.0f * 72.0f; + } + break; + + case PPD_CUSTOM_INT : + cparam->current.custom_int = atoi(choice + 7); + break; + + case PPD_CUSTOM_PASSCODE : + case PPD_CUSTOM_PASSWORD : + case PPD_CUSTOM_STRING : + if (cparam->current.custom_string) + _cupsStrFree(cparam->current.custom_string); + + cparam->current.custom_string = _cupsStrAlloc(choice + 7); + break; + } + } + } + + /* + * Make sure that we keep the option marked below... + */ + + choice = "Custom"; + } + else if (choice[0] == '{') + { + /* + * Handle multi-value custom options... + */ + + ppd_coption_t *coption; /* Custom option */ + ppd_cparam_t *cparam; /* Custom parameter */ + char *units; /* Custom points units */ + int num_vals; /* Number of values */ + cups_option_t *vals, /* Values */ + *val; /* Value */ + + + if ((c = ppdFindChoice(o, "Custom")) == NULL) + return; + + if ((coption = ppdFindCustomOption(ppd, option)) != NULL) + { + num_vals = cupsParseOptions(choice, 0, &vals); + + for (i = 0, val = vals; i < num_vals; i ++, val ++) + { + if ((cparam = ppdFindCustomParam(coption, val->name)) == NULL) + continue; + + switch (cparam->type) + { + case PPD_CUSTOM_CURVE : + case PPD_CUSTOM_INVCURVE : + case PPD_CUSTOM_REAL : + cparam->current.custom_real = (float)_cupsStrScand(val->value, + NULL, loc); + break; + + case PPD_CUSTOM_POINTS : + cparam->current.custom_points = (float)_cupsStrScand(val->value, + &units, + loc); + + if (units) + { + if (!_cups_strcasecmp(units, "cm")) + cparam->current.custom_points *= 72.0f / 2.54f; + else if (!_cups_strcasecmp(units, "mm")) + cparam->current.custom_points *= 72.0f / 25.4f; + else if (!_cups_strcasecmp(units, "m")) + cparam->current.custom_points *= 72.0f / 0.0254f; + else if (!_cups_strcasecmp(units, "in")) + cparam->current.custom_points *= 72.0f; + else if (!_cups_strcasecmp(units, "ft")) + cparam->current.custom_points *= 12.0f * 72.0f; + } + break; + + case PPD_CUSTOM_INT : + cparam->current.custom_int = atoi(val->value); + break; + + case PPD_CUSTOM_PASSCODE : + case PPD_CUSTOM_PASSWORD : + case PPD_CUSTOM_STRING : + if (cparam->current.custom_string) + _cupsStrFree(cparam->current.custom_string); + + cparam->current.custom_string = _cupsStrRetain(val->value); + break; + } + } + + cupsFreeOptions(num_vals, vals); + } + } + else + { + for (i = o->num_choices, c = o->choices; i > 0; i --, c ++) + if (!_cups_strcasecmp(c->choice, choice)) + break; + + if (!i) + return; + } + + /* + * Option found; mark it and then handle unmarking any other options. + */ + + if (o->ui != PPD_UI_PICKMANY) + { + /* + * Unmark all other choices... + */ + + if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, c)) != NULL) + { + oldc->marked = 0; + cupsArrayRemove(ppd->marked, oldc); + } + + if (!_cups_strcasecmp(option, "PageSize") || !_cups_strcasecmp(option, "PageRegion")) + { + /* + * Mark current page size... + */ + + for (j = 0; j < ppd->num_sizes; j ++) + ppd->sizes[j].marked = !_cups_strcasecmp(ppd->sizes[j].name, + choice); + + /* + * Unmark the current PageSize or PageRegion setting, as + * appropriate... + */ + + cupsArraySave(ppd->options); + + if (!_cups_strcasecmp(option, "PageSize")) + { + if ((o = ppdFindOption(ppd, "PageRegion")) != NULL) + { + key.option = o; + if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL) + { + oldc->marked = 0; + cupsArrayRemove(ppd->marked, oldc); + } + } + } + else + { + if ((o = ppdFindOption(ppd, "PageSize")) != NULL) + { + key.option = o; + if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL) + { + oldc->marked = 0; + cupsArrayRemove(ppd->marked, oldc); + } + } + } + + cupsArrayRestore(ppd->options); + } + else if (!_cups_strcasecmp(option, "InputSlot")) + { + /* + * Unmark ManualFeed option... + */ + + cupsArraySave(ppd->options); + + if ((o = ppdFindOption(ppd, "ManualFeed")) != NULL) + { + key.option = o; + if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL) + { + oldc->marked = 0; + cupsArrayRemove(ppd->marked, oldc); + } + } + + cupsArrayRestore(ppd->options); + } + else if (!_cups_strcasecmp(option, "ManualFeed") && + !_cups_strcasecmp(choice, "True")) + { + /* + * Unmark InputSlot option... + */ + + cupsArraySave(ppd->options); + + if ((o = ppdFindOption(ppd, "InputSlot")) != NULL) + { + key.option = o; + if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL) + { + oldc->marked = 0; + cupsArrayRemove(ppd->marked, oldc); + } + } + + cupsArrayRestore(ppd->options); + } + } + + c->marked = 1; + + cupsArrayAdd(ppd->marked, c); +} + + +/* + * End of "$Id: mark.c 9042 2010-03-24 00:45:34Z mike $". + */ diff --git a/cups/md5-private.h b/cups/md5-private.h new file mode 100644 index 0000000..3667cf0 --- /dev/null +++ b/cups/md5-private.h @@ -0,0 +1,79 @@ +/* + * "$Id$" + * + * Private MD5 definitions for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 2005 by Easy Software Products + * + * Copyright (C) 1999 Aladdin Enterprises. All rights reserved. + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * L. Peter Deutsch + * ghost@aladdin.com + */ + +/* + Independent implementation of MD5 (RFC 1321). + + This code implements the MD5 Algorithm defined in RFC 1321. + It is derived directly from the text of the RFC and not from the + reference implementation. + + The original and principal author of md5.h is L. Peter Deutsch + . Other authors are noted in the change history + that follows (in reverse chronological order): + + 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. + 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5); + added conditionalization for C++ compilation from Martin + Purschke . + 1999-05-03 lpd Original version. + */ + +#ifndef _CUPS_MD5_PRIVATE_H_ +# define _CUPS_MD5_PRIVATE_H_ + +/* Define the state of the MD5 Algorithm. */ +typedef struct _cups_md5_state_s { + unsigned int count[2]; /* message length in bits, lsw first */ + unsigned int abcd[4]; /* digest buffer */ + unsigned char buf[64]; /* accumulate block */ +} _cups_md5_state_t; + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + +/* Initialize the algorithm. */ +void _cupsMD5Init(_cups_md5_state_t *pms); + +/* Append a string to the message. */ +void _cupsMD5Append(_cups_md5_state_t *pms, const unsigned char *data, int nbytes); + +/* Finish the message and return the digest. */ +void _cupsMD5Finish(_cups_md5_state_t *pms, unsigned char digest[16]); + +# ifdef __cplusplus +} /* end extern "C" */ +# endif /* __cplusplus */ +#endif /* !_CUPS_MD5_PRIVATE_H_ */ + +/* + * End of "$Id$". + */ diff --git a/cups/md5.c b/cups/md5.c new file mode 100644 index 0000000..f7c42ad --- /dev/null +++ b/cups/md5.c @@ -0,0 +1,346 @@ +/* + * "$Id: md5.c 9042 2010-03-24 00:45:34Z mike $" + * + * Private MD5 implementation for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 2005 by Easy Software Products + * + * Copyright (C) 1999 Aladdin Enterprises. All rights reserved. + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * L. Peter Deutsch + * ghost@aladdin.com + */ +/* + Independent implementation of MD5 (RFC 1321). + + This code implements the MD5 Algorithm defined in RFC 1321. + It is derived directly from the text of the RFC and not from the + reference implementation. + + The original and principal author of md5.c is L. Peter Deutsch + . Other authors are noted in the change history + that follows (in reverse chronological order): + + 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. + 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5). + 1999-05-03 lpd Original version. + */ + +#include "md5-private.h" +#include "string-private.h" + +#define T1 0xd76aa478 +#define T2 0xe8c7b756 +#define T3 0x242070db +#define T4 0xc1bdceee +#define T5 0xf57c0faf +#define T6 0x4787c62a +#define T7 0xa8304613 +#define T8 0xfd469501 +#define T9 0x698098d8 +#define T10 0x8b44f7af +#define T11 0xffff5bb1 +#define T12 0x895cd7be +#define T13 0x6b901122 +#define T14 0xfd987193 +#define T15 0xa679438e +#define T16 0x49b40821 +#define T17 0xf61e2562 +#define T18 0xc040b340 +#define T19 0x265e5a51 +#define T20 0xe9b6c7aa +#define T21 0xd62f105d +#define T22 0x02441453 +#define T23 0xd8a1e681 +#define T24 0xe7d3fbc8 +#define T25 0x21e1cde6 +#define T26 0xc33707d6 +#define T27 0xf4d50d87 +#define T28 0x455a14ed +#define T29 0xa9e3e905 +#define T30 0xfcefa3f8 +#define T31 0x676f02d9 +#define T32 0x8d2a4c8a +#define T33 0xfffa3942 +#define T34 0x8771f681 +#define T35 0x6d9d6122 +#define T36 0xfde5380c +#define T37 0xa4beea44 +#define T38 0x4bdecfa9 +#define T39 0xf6bb4b60 +#define T40 0xbebfbc70 +#define T41 0x289b7ec6 +#define T42 0xeaa127fa +#define T43 0xd4ef3085 +#define T44 0x04881d05 +#define T45 0xd9d4d039 +#define T46 0xe6db99e5 +#define T47 0x1fa27cf8 +#define T48 0xc4ac5665 +#define T49 0xf4292244 +#define T50 0x432aff97 +#define T51 0xab9423a7 +#define T52 0xfc93a039 +#define T53 0x655b59c3 +#define T54 0x8f0ccc92 +#define T55 0xffeff47d +#define T56 0x85845dd1 +#define T57 0x6fa87e4f +#define T58 0xfe2ce6e0 +#define T59 0xa3014314 +#define T60 0x4e0811a1 +#define T61 0xf7537e82 +#define T62 0xbd3af235 +#define T63 0x2ad7d2bb +#define T64 0xeb86d391 + +static void +_cups_md5_process(_cups_md5_state_t *pms, const unsigned char *data /*[64]*/) +{ + unsigned int + a = pms->abcd[0], b = pms->abcd[1], + c = pms->abcd[2], d = pms->abcd[3]; + unsigned int t; + +#ifndef ARCH_IS_BIG_ENDIAN +# define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */ +#endif +#if ARCH_IS_BIG_ENDIAN + + /* + * On big-endian machines, we must arrange the bytes in the right + * order. (This also works on machines of unknown byte order.) + */ + unsigned int X[16]; + const unsigned char *xp = data; + int i; + + for (i = 0; i < 16; ++i, xp += 4) + X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); + +#else /* !ARCH_IS_BIG_ENDIAN */ + + /* + * On little-endian machines, we can process properly aligned data + * without copying it. + */ + unsigned int xbuf[16]; + const unsigned int *X; + + if (!((data - (const unsigned char *)0) & 3)) { + /* data are properly aligned */ + X = (const unsigned int *)data; + } else { + /* not aligned */ + memcpy(xbuf, data, 64); + X = xbuf; + } +#endif + +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) + + /* Round 1. */ + /* Let [abcd k s i] denote the operation + a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */ +#define F(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define SET(a, b, c, d, k, s, Ti)\ + t = a + F(b,c,d) + X[k] + Ti;\ + a = ROTATE_LEFT(t, s) + b + /* Do the following 16 operations. */ + SET(a, b, c, d, 0, 7, T1); + SET(d, a, b, c, 1, 12, T2); + SET(c, d, a, b, 2, 17, T3); + SET(b, c, d, a, 3, 22, T4); + SET(a, b, c, d, 4, 7, T5); + SET(d, a, b, c, 5, 12, T6); + SET(c, d, a, b, 6, 17, T7); + SET(b, c, d, a, 7, 22, T8); + SET(a, b, c, d, 8, 7, T9); + SET(d, a, b, c, 9, 12, T10); + SET(c, d, a, b, 10, 17, T11); + SET(b, c, d, a, 11, 22, T12); + SET(a, b, c, d, 12, 7, T13); + SET(d, a, b, c, 13, 12, T14); + SET(c, d, a, b, 14, 17, T15); + SET(b, c, d, a, 15, 22, T16); +#undef SET + + /* Round 2. */ + /* Let [abcd k s i] denote the operation + a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */ +#define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) +#define SET(a, b, c, d, k, s, Ti)\ + t = a + G(b,c,d) + X[k] + Ti;\ + a = ROTATE_LEFT(t, s) + b + /* Do the following 16 operations. */ + SET(a, b, c, d, 1, 5, T17); + SET(d, a, b, c, 6, 9, T18); + SET(c, d, a, b, 11, 14, T19); + SET(b, c, d, a, 0, 20, T20); + SET(a, b, c, d, 5, 5, T21); + SET(d, a, b, c, 10, 9, T22); + SET(c, d, a, b, 15, 14, T23); + SET(b, c, d, a, 4, 20, T24); + SET(a, b, c, d, 9, 5, T25); + SET(d, a, b, c, 14, 9, T26); + SET(c, d, a, b, 3, 14, T27); + SET(b, c, d, a, 8, 20, T28); + SET(a, b, c, d, 13, 5, T29); + SET(d, a, b, c, 2, 9, T30); + SET(c, d, a, b, 7, 14, T31); + SET(b, c, d, a, 12, 20, T32); +#undef SET + + /* Round 3. */ + /* Let [abcd k s t] denote the operation + a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */ +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define SET(a, b, c, d, k, s, Ti)\ + t = a + H(b,c,d) + X[k] + Ti;\ + a = ROTATE_LEFT(t, s) + b + /* Do the following 16 operations. */ + SET(a, b, c, d, 5, 4, T33); + SET(d, a, b, c, 8, 11, T34); + SET(c, d, a, b, 11, 16, T35); + SET(b, c, d, a, 14, 23, T36); + SET(a, b, c, d, 1, 4, T37); + SET(d, a, b, c, 4, 11, T38); + SET(c, d, a, b, 7, 16, T39); + SET(b, c, d, a, 10, 23, T40); + SET(a, b, c, d, 13, 4, T41); + SET(d, a, b, c, 0, 11, T42); + SET(c, d, a, b, 3, 16, T43); + SET(b, c, d, a, 6, 23, T44); + SET(a, b, c, d, 9, 4, T45); + SET(d, a, b, c, 12, 11, T46); + SET(c, d, a, b, 15, 16, T47); + SET(b, c, d, a, 2, 23, T48); +#undef SET + + /* Round 4. */ + /* Let [abcd k s t] denote the operation + a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */ +#define I(x, y, z) ((y) ^ ((x) | ~(z))) +#define SET(a, b, c, d, k, s, Ti)\ + t = a + I(b,c,d) + X[k] + Ti;\ + a = ROTATE_LEFT(t, s) + b + /* Do the following 16 operations. */ + SET(a, b, c, d, 0, 6, T49); + SET(d, a, b, c, 7, 10, T50); + SET(c, d, a, b, 14, 15, T51); + SET(b, c, d, a, 5, 21, T52); + SET(a, b, c, d, 12, 6, T53); + SET(d, a, b, c, 3, 10, T54); + SET(c, d, a, b, 10, 15, T55); + SET(b, c, d, a, 1, 21, T56); + SET(a, b, c, d, 8, 6, T57); + SET(d, a, b, c, 15, 10, T58); + SET(c, d, a, b, 6, 15, T59); + SET(b, c, d, a, 13, 21, T60); + SET(a, b, c, d, 4, 6, T61); + SET(d, a, b, c, 11, 10, T62); + SET(c, d, a, b, 2, 15, T63); + SET(b, c, d, a, 9, 21, T64); +#undef SET + + /* Then perform the following additions. (That is increment each + of the four registers by the value it had before this block + was started.) */ + pms->abcd[0] += a; + pms->abcd[1] += b; + pms->abcd[2] += c; + pms->abcd[3] += d; +} + +void +_cupsMD5Init(_cups_md5_state_t *pms) +{ + pms->count[0] = pms->count[1] = 0; + pms->abcd[0] = 0x67452301; + pms->abcd[1] = 0xefcdab89; + pms->abcd[2] = 0x98badcfe; + pms->abcd[3] = 0x10325476; +} + +void +_cupsMD5Append(_cups_md5_state_t *pms, const unsigned char *data, int nbytes) +{ + const unsigned char *p = data; + int left = nbytes; + int offset = (pms->count[0] >> 3) & 63; + unsigned int nbits = (unsigned int)(nbytes << 3); + + if (nbytes <= 0) + return; + + /* Update the message length. */ + pms->count[1] += nbytes >> 29; + pms->count[0] += nbits; + if (pms->count[0] < nbits) + pms->count[1]++; + + /* Process an initial partial block. */ + if (offset) { + int copy = (offset + nbytes > 64 ? 64 - offset : nbytes); + + memcpy(pms->buf + offset, p, copy); + if (offset + copy < 64) + return; + p += copy; + left -= copy; + _cups_md5_process(pms, pms->buf); + } + + /* Process full blocks. */ + for (; left >= 64; p += 64, left -= 64) + _cups_md5_process(pms, p); + + /* Process a final partial block. */ + if (left) + memcpy(pms->buf, p, left); +} + +void +_cupsMD5Finish(_cups_md5_state_t *pms, unsigned char digest[16]) +{ + static const unsigned char pad[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + unsigned char data[8]; + int i; + + /* Save the length before padding. */ + for (i = 0; i < 8; ++i) + data[i] = (unsigned char)(pms->count[i >> 2] >> ((i & 3) << 3)); + /* Pad to 56 bytes mod 64. */ + _cupsMD5Append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1); + /* Append the length. */ + _cupsMD5Append(pms, data, 8); + for (i = 0; i < 16; ++i) + digest[i] = (unsigned char)(pms->abcd[i >> 2] >> ((i & 3) << 3)); +} + + +/* + * End of "$Id: md5.c 9042 2010-03-24 00:45:34Z mike $". + */ diff --git a/cups/md5passwd.c b/cups/md5passwd.c new file mode 100644 index 0000000..777073a --- /dev/null +++ b/cups/md5passwd.c @@ -0,0 +1,142 @@ +/* + * "$Id: md5passwd.c 6649 2007-07-11 21:46:42Z mike $" + * + * MD5 password support for CUPS. + * + * Copyright 2007-2010 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. + * + * Contents: + * + * httpMD5() - Compute the MD5 sum of the username:group:password. + * httpMD5Nonce() - Combine the MD5 sum of the username, group, and password + * with the server-supplied nonce value. + * httpMD5String() - Convert an MD5 sum to a character string. + */ + +/* + * Include necessary headers... + */ + +#include "http-private.h" +#include "string-private.h" + + +/* + * 'httpMD5()' - Compute the MD5 sum of the username:group:password. + */ + +char * /* O - MD5 sum */ +httpMD5(const char *username, /* I - User name */ + const char *realm, /* I - Realm name */ + const char *passwd, /* I - Password string */ + char md5[33]) /* O - MD5 string */ +{ + _cups_md5_state_t state; /* MD5 state info */ + unsigned char sum[16]; /* Sum data */ + char line[256]; /* Line to sum */ + + + /* + * Compute the MD5 sum of the user name, group name, and password. + */ + + snprintf(line, sizeof(line), "%s:%s:%s", username, realm, passwd); + _cupsMD5Init(&state); + _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line)); + _cupsMD5Finish(&state, sum); + + /* + * Return the sum... + */ + + return (httpMD5String(sum, md5)); +} + + +/* + * 'httpMD5Final()' - Combine the MD5 sum of the username, group, and password + * with the server-supplied nonce value, method, and + * request-uri. + */ + +char * /* O - New sum */ +httpMD5Final(const char *nonce, /* I - Server nonce value */ + const char *method, /* I - METHOD (GET, POST, etc.) */ + const char *resource, /* I - Resource path */ + char md5[33]) /* IO - MD5 sum */ +{ + _cups_md5_state_t state; /* MD5 state info */ + unsigned char sum[16]; /* Sum data */ + char line[1024]; /* Line of data */ + char a2[33]; /* Hash of method and resource */ + + + /* + * First compute the MD5 sum of the method and resource... + */ + + snprintf(line, sizeof(line), "%s:%s", method, resource); + _cupsMD5Init(&state); + _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line)); + _cupsMD5Finish(&state, sum); + httpMD5String(sum, a2); + + /* + * Then combine A1 (MD5 of username, realm, and password) with the nonce + * and A2 (method + resource) values to get the final MD5 sum for the + * request... + */ + + snprintf(line, sizeof(line), "%s:%s:%s", md5, nonce, a2); + + _cupsMD5Init(&state); + _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line)); + _cupsMD5Finish(&state, sum); + + return (httpMD5String(sum, md5)); +} + + +/* + * 'httpMD5String()' - Convert an MD5 sum to a character string. + */ + +char * /* O - MD5 sum in hex */ +httpMD5String(const unsigned char *sum, /* I - MD5 sum data */ + char md5[33]) + /* O - MD5 sum in hex */ +{ + int i; /* Looping var */ + char *md5ptr; /* Pointer into MD5 string */ + static const char hex[] = "0123456789abcdef"; + /* Hex digits */ + + + /* + * Convert the MD5 sum to hexadecimal... + */ + + for (i = 16, md5ptr = md5; i > 0; i --, sum ++) + { + *md5ptr++ = hex[*sum >> 4]; + *md5ptr++ = hex[*sum & 15]; + } + + *md5ptr = '\0'; + + return (md5); +} + + +/* + * End of "$Id: md5passwd.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/cups/notify.c b/cups/notify.c new file mode 100644 index 0000000..9d520f1 --- /dev/null +++ b/cups/notify.c @@ -0,0 +1,202 @@ +/* + * "$Id: notify.c 7337 2008-02-22 04:44:04Z mike $" + * + * Notification routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2005-2006 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. + * + * Contents: + * + * cupsNotifySubject() - Return the subject for the given notification + * message. + * cupsNotifyText() - Return the text for the given notification message. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * 'cupsNotifySubject()' - Return the subject for the given notification message. + * + * The returned string must be freed by the caller using @code free@. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - Subject string or @code NULL@ */ +cupsNotifySubject(cups_lang_t *lang, /* I - Language data */ + ipp_t *event) /* I - Event data */ +{ + char buffer[1024]; /* Subject buffer */ + const char *prefix, /* Prefix on subject */ + *state; /* Printer/job state string */ + ipp_attribute_t *job_id, /* notify-job-id */ + *job_name, /* job-name */ + *job_state, /* job-state */ + *printer_name, /* printer-name */ + *printer_state, /* printer-state */ + *printer_uri, /* notify-printer-uri */ + *subscribed; /* notify-subscribed-event */ + + + /* + * Range check input... + */ + + if (!event || !lang) + return (NULL); + + /* + * Get the required attributes... + */ + + job_id = ippFindAttribute(event, "notify-job-id", IPP_TAG_INTEGER); + job_name = ippFindAttribute(event, "job-name", IPP_TAG_NAME); + job_state = ippFindAttribute(event, "job-state", IPP_TAG_ENUM); + printer_name = ippFindAttribute(event, "printer-name", IPP_TAG_NAME); + printer_state = ippFindAttribute(event, "printer-state", IPP_TAG_ENUM); + printer_uri = ippFindAttribute(event, "notify-printer-uri", IPP_TAG_URI); + subscribed = ippFindAttribute(event, "notify-subscribed-event", + IPP_TAG_KEYWORD); + + + if (job_id && printer_name && printer_uri && job_state) + { + /* + * Job event... + */ + + prefix = _cupsLangString(lang, _("Print Job:")); + + switch (job_state->values[0].integer) + { + case IPP_JOB_PENDING : + state = _cupsLangString(lang, _("pending")); + break; + case IPP_JOB_HELD : + state = _cupsLangString(lang, _("held")); + break; + case IPP_JOB_PROCESSING : + state = _cupsLangString(lang, _("processing")); + break; + case IPP_JOB_STOPPED : + state = _cupsLangString(lang, _("stopped")); + break; + case IPP_JOB_CANCELED : + state = _cupsLangString(lang, _("canceled")); + break; + case IPP_JOB_ABORTED : + state = _cupsLangString(lang, _("aborted")); + break; + case IPP_JOB_COMPLETED : + state = _cupsLangString(lang, _("completed")); + break; + default : + state = _cupsLangString(lang, _("unknown")); + break; + } + + snprintf(buffer, sizeof(buffer), "%s %s-%d (%s) %s", + prefix, + printer_name->values[0].string.text, + job_id->values[0].integer, + job_name ? job_name->values[0].string.text : + _cupsLangString(lang, _("untitled")), + state); + } + else if (printer_uri && printer_name && printer_state) + { + /* + * Printer event... + */ + + prefix = _cupsLangString(lang, _("Printer:")); + + switch (printer_state->values[0].integer) + { + case IPP_PRINTER_IDLE : + state = _cupsLangString(lang, _("idle")); + break; + case IPP_PRINTER_PROCESSING : + state = _cupsLangString(lang, _("processing")); + break; + case IPP_PRINTER_STOPPED : + state = _cupsLangString(lang, _("stopped")); + break; + default : + state = _cupsLangString(lang, _("unknown")); + break; + } + + snprintf(buffer, sizeof(buffer), "%s %s %s", + prefix, + printer_name->values[0].string.text, + state); + } + else if (subscribed) + strlcpy(buffer, subscribed->values[0].string.text, sizeof(buffer)); + else + return (NULL); + + /* + * Duplicate and return the subject string... + */ + + return (strdup(buffer)); +} + + +/* + * 'cupsNotifyText()' - Return the text for the given notification message. + * + * The returned string must be freed by the caller using @code free@. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +char * /* O - Message text or @code NULL@ */ +cupsNotifyText(cups_lang_t *lang, /* I - Language data */ + ipp_t *event) /* I - Event data */ +{ + ipp_attribute_t *notify_text; /* notify-text */ + + + /* + * Range check input... + */ + + if (!event || !lang) + return (NULL); + + /* + * Get the notify-text attribute from the server... + */ + + if ((notify_text = ippFindAttribute(event, "notify-text", + IPP_TAG_TEXT)) == NULL) + return (NULL); + + /* + * Return a copy... + */ + + return (strdup(notify_text->values[0].string.text)); +} + + +/* + * End of "$Id: notify.c 7337 2008-02-22 04:44:04Z mike $". + */ diff --git a/cups/options.c b/cups/options.c new file mode 100644 index 0000000..8efe0b4 --- /dev/null +++ b/cups/options.c @@ -0,0 +1,711 @@ +/* + * "$Id: options.c 8181 2008-12-10 17:29:57Z mike $" + * + * Option routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * cupsAddOption() - Add an option to an option array. + * cupsFreeOptions() - Free all memory used by options. + * cupsGetOption() - Get an option value. + * cupsParseOptions() - Parse options from a command-line argument. + * cupsRemoveOption() - Remove an option from an option array. + * _cupsGet1284Values() - Get 1284 device ID keys and values. + * cups_compare_options() - Compare two options. + * cups_find_option() - Find an option using a binary search. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * Local functions... + */ + +static int cups_compare_options(cups_option_t *a, cups_option_t *b); +static int cups_find_option(const char *name, int num_options, + cups_option_t *option, int prev, int *rdiff); + + +/* + * 'cupsAddOption()' - Add an option to an option array. + * + * New option arrays can be initialized simply by passing 0 for the + * "num_options" parameter. + */ + +int /* O - Number of options */ +cupsAddOption(const char *name, /* I - Name of option */ + const char *value, /* I - Value of option */ + int num_options,/* I - Number of options */ + cups_option_t **options) /* IO - Pointer to options */ +{ + cups_option_t *temp; /* Pointer to new option */ + int insert, /* Insertion point */ + diff; /* Result of search */ + + + DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, " + "options=%p)", name, value, num_options, options)); + + if (!name || !name[0] || !value || !options || num_options < 0) + { + DEBUG_printf(("3cupsAddOption: Returning %d", num_options)); + return (num_options); + } + + /* + * Look for an existing option with the same name... + */ + + if (num_options == 0) + { + insert = 0; + diff = 1; + } + else + { + insert = cups_find_option(name, num_options, *options, num_options - 1, + &diff); + + if (diff > 0) + insert ++; + } + + if (diff) + { + /* + * No matching option name... + */ + + DEBUG_printf(("4cupsAddOption: New option inserted at index %d...", + insert)); + + if (num_options == 0) + temp = (cups_option_t *)malloc(sizeof(cups_option_t)); + else + temp = (cups_option_t *)realloc(*options, sizeof(cups_option_t) * + (num_options + 1)); + + if (temp == NULL) + { + DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0"); + return (0); + } + + *options = temp; + + if (insert < num_options) + { + DEBUG_printf(("4cupsAddOption: Shifting %d options...", + (int)(num_options - insert))); + memmove(temp + insert + 1, temp + insert, + (num_options - insert) * sizeof(cups_option_t)); + } + + temp += insert; + temp->name = _cupsStrAlloc(name); + num_options ++; + } + else + { + /* + * Match found; free the old value... + */ + + DEBUG_printf(("4cupsAddOption: Option already exists at index %d...", + insert)); + + temp = *options + insert; + _cupsStrFree(temp->value); + } + + temp->value = _cupsStrAlloc(value); + + DEBUG_printf(("3cupsAddOption: Returning %d", num_options)); + + return (num_options); +} + + +/* + * 'cupsFreeOptions()' - Free all memory used by options. + */ + +void +cupsFreeOptions( + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Pointer to options */ +{ + int i; /* Looping var */ + + + DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options, + options)); + + if (num_options <= 0 || !options) + return; + + for (i = 0; i < num_options; i ++) + { + _cupsStrFree(options[i].name); + _cupsStrFree(options[i].value); + } + + free(options); +} + + +/* + * 'cupsGetOption()' - Get an option value. + */ + +const char * /* O - Option value or @code NULL@ */ +cupsGetOption(const char *name, /* I - Name of option */ + int num_options,/* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + int diff, /* Result of comparison */ + match; /* Matching index */ + + + DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", + name, num_options, options)); + + if (!name || num_options <= 0 || !options) + { + DEBUG_puts("3cupsGetOption: Returning NULL"); + return (NULL); + } + + match = cups_find_option(name, num_options, options, -1, &diff); + + if (!diff) + { + DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options[match].value)); + return (options[match].value); + } + + DEBUG_puts("3cupsGetOption: Returning NULL"); + return (NULL); +} + + +/* + * 'cupsParseOptions()' - Parse options from a command-line argument. + * + * This function converts space-delimited name/value pairs according + * to the PAPI text option ABNF specification. Collection values + * ("name={a=... b=... c=...}") are stored with the curley brackets + * intact - use @code cupsParseOptions@ on the value to extract the + * collection attributes. + */ + +int /* O - Number of options found */ +cupsParseOptions( + const char *arg, /* I - Argument to parse */ + int num_options, /* I - Number of options */ + cups_option_t **options) /* O - Options found */ +{ + char *copyarg, /* Copy of input string */ + *ptr, /* Pointer into string */ + *name, /* Pointer to name */ + *value, /* Pointer to value */ + sep, /* Separator character */ + quote; /* Quote character */ + + + DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", + arg, num_options, options)); + + /* + * Range check input... + */ + + if (!arg) + { + DEBUG_printf(("1cupsParseOptions: Returning %d", num_options)); + return (num_options); + } + + if (!options || num_options < 0) + { + DEBUG_puts("1cupsParseOptions: Returning 0"); + return (0); + } + + /* + * Make a copy of the argument string and then divide it up... + */ + + if ((copyarg = strdup(arg)) == NULL) + { + DEBUG_puts("1cupsParseOptions: Unable to copy arg string"); + DEBUG_printf(("1cupsParseOptions: Returning %d", num_options)); + return (num_options); + } + + if (*copyarg == '{') + { + /* + * Remove surrounding {} so we can parse "{name=value ... name=value}"... + */ + + if ((ptr = copyarg + strlen(copyarg) - 1) > copyarg && *ptr == '}') + { + *ptr = '\0'; + ptr = copyarg + 1; + } + else + ptr = copyarg; + } + else + ptr = copyarg; + + /* + * Skip leading spaces... + */ + + while (_cups_isspace(*ptr)) + ptr ++; + + /* + * Loop through the string... + */ + + while (*ptr != '\0') + { + /* + * Get the name up to a SPACE, =, or end-of-string... + */ + + name = ptr; + while (!strchr("\f\n\r\t\v =", *ptr) && *ptr) + ptr ++; + + /* + * Avoid an empty name... + */ + + if (ptr == name) + break; + + /* + * Skip trailing spaces... + */ + + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; + + if ((sep = *ptr) == '=') + *ptr++ = '\0'; + + DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name)); + + if (sep != '=') + { + /* + * Boolean option... + */ + + if (!_cups_strncasecmp(name, "no", 2)) + num_options = cupsAddOption(name + 2, "false", num_options, + options); + else + num_options = cupsAddOption(name, "true", num_options, options); + + continue; + } + + /* + * Remove = and parse the value... + */ + + value = ptr; + + while (*ptr && !_cups_isspace(*ptr)) + { + if (*ptr == ',') + ptr ++; + else if (*ptr == '\'' || *ptr == '\"') + { + /* + * Quoted string constant... + */ + + quote = *ptr; + _cups_strcpy(ptr, ptr + 1); + + while (*ptr != quote && *ptr) + { + if (*ptr == '\\' && ptr[1]) + _cups_strcpy(ptr, ptr + 1); + + ptr ++; + } + + if (*ptr) + _cups_strcpy(ptr, ptr + 1); + } + else if (*ptr == '{') + { + /* + * Collection value... + */ + + int depth; + + for (depth = 0; *ptr; ptr ++) + { + if (*ptr == '{') + depth ++; + else if (*ptr == '}') + { + depth --; + if (!depth) + { + ptr ++; + break; + } + } + else if (*ptr == '\\' && ptr[1]) + _cups_strcpy(ptr, ptr + 1); + } + } + else + { + /* + * Normal space-delimited string... + */ + + while (*ptr && !_cups_isspace(*ptr)) + { + if (*ptr == '\\' && ptr[1]) + _cups_strcpy(ptr, ptr + 1); + + ptr ++; + } + } + } + + if (*ptr != '\0') + *ptr++ = '\0'; + + DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value)); + + /* + * Skip trailing whitespace... + */ + + while (_cups_isspace(*ptr)) + ptr ++; + + /* + * Add the string value... + */ + + num_options = cupsAddOption(name, value, num_options, options); + } + + /* + * Free the copy of the argument we made and return the number of options + * found. + */ + + free(copyarg); + + DEBUG_printf(("1cupsParseOptions: Returning %d", num_options)); + + return (num_options); +} + + +/* + * 'cupsRemoveOption()' - Remove an option from an option array. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - New number of options */ +cupsRemoveOption( + const char *name, /* I - Option name */ + int num_options, /* I - Current number of options */ + cups_option_t **options) /* IO - Options */ +{ + int i; /* Looping var */ + cups_option_t *option; /* Current option */ + + + DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", + name, num_options, options)); + + /* + * Range check input... + */ + + if (!name || num_options < 1 || !options) + { + DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options)); + return (num_options); + } + + /* + * Loop for the option... + */ + + for (i = num_options, option = *options; i > 0; i --, option ++) + if (!_cups_strcasecmp(name, option->name)) + break; + + if (i) + { + /* + * Remove this option from the array... + */ + + DEBUG_puts("4cupsRemoveOption: Found option, removing it..."); + + num_options --; + i --; + + _cupsStrFree(option->name); + _cupsStrFree(option->value); + + if (i > 0) + memmove(option, option + 1, i * sizeof(cups_option_t)); + } + + /* + * Return the new number of options... + */ + + DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options)); + return (num_options); +} + + +/* + * '_cupsGet1284Values()' - Get 1284 device ID keys and values. + * + * The returned dictionary is a CUPS option array that can be queried with + * cupsGetOption and freed with cupsFreeOptions. + */ + +int /* O - Number of key/value pairs */ +_cupsGet1284Values( + const char *device_id, /* I - IEEE-1284 device ID string */ + cups_option_t **values) /* O - Array of key/value pairs */ +{ + int num_values; /* Number of values */ + char key[256], /* Key string */ + value[256], /* Value string */ + *ptr; /* Pointer into key/value */ + + + /* + * Range check input... + */ + + if (values) + *values = NULL; + + if (!device_id || !values) + return (0); + + /* + * Parse the 1284 device ID value into keys and values. The format is + * repeating sequences of: + * + * [whitespace]key:value[whitespace]; + */ + + num_values = 0; + while (*device_id) + { + while (_cups_isspace(*device_id)) + device_id ++; + + if (!*device_id) + break; + + for (ptr = key; *device_id && *device_id != ':'; device_id ++) + if (ptr < (key + sizeof(key) - 1)) + *ptr++ = *device_id; + + if (!*device_id) + break; + + while (ptr > key && _cups_isspace(ptr[-1])) + ptr --; + + *ptr = '\0'; + device_id ++; + + while (_cups_isspace(*device_id)) + device_id ++; + + if (!*device_id) + break; + + for (ptr = value; *device_id && *device_id != ';'; device_id ++) + if (ptr < (value + sizeof(value) - 1)) + *ptr++ = *device_id; + + if (!*device_id) + break; + + while (ptr > value && _cups_isspace(ptr[-1])) + ptr --; + + *ptr = '\0'; + device_id ++; + + num_values = cupsAddOption(key, value, num_values, values); + } + + return (num_values); +} + + +/* + * 'cups_compare_options()' - Compare two options. + */ + +static int /* O - Result of comparison */ +cups_compare_options(cups_option_t *a, /* I - First option */ + cups_option_t *b) /* I - Second option */ +{ + return (_cups_strcasecmp(a->name, b->name)); +} + + +/* + * 'cups_find_option()' - Find an option using a binary search. + */ + +static int /* O - Index of match */ +cups_find_option( + const char *name, /* I - Option name */ + int num_options, /* I - Number of options */ + cups_option_t *options, /* I - Options */ + int prev, /* I - Previous index */ + int *rdiff) /* O - Difference of match */ +{ + int left, /* Low mark for binary search */ + right, /* High mark for binary search */ + current, /* Current index */ + diff; /* Result of comparison */ + cups_option_t key; /* Search key */ + + + DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, " + "prev=%d, rdiff=%p)", name, num_options, options, prev, + rdiff)); + +#ifdef DEBUG + for (left = 0; left < num_options; left ++) + DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"", + left, options[left].name, options[left].value)); +#endif /* DEBUG */ + + key.name = (char *)name; + + if (prev >= 0) + { + /* + * Start search on either side of previous... + */ + + if ((diff = cups_compare_options(&key, options + prev)) == 0 || + (diff < 0 && prev == 0) || + (diff > 0 && prev == (num_options - 1))) + { + *rdiff = diff; + return (prev); + } + else if (diff < 0) + { + /* + * Start with previous on right side... + */ + + left = 0; + right = prev; + } + else + { + /* + * Start wih previous on left side... + */ + + left = prev; + right = num_options - 1; + } + } + else + { + /* + * Start search in the middle... + */ + + left = 0; + right = num_options - 1; + } + + do + { + current = (left + right) / 2; + diff = cups_compare_options(&key, options + current); + + if (diff == 0) + break; + else if (diff < 0) + right = current; + else + left = current; + } + while ((right - left) > 1); + + if (diff != 0) + { + /* + * Check the last 1 or 2 elements... + */ + + if ((diff = cups_compare_options(&key, options + left)) <= 0) + current = left; + else + { + diff = cups_compare_options(&key, options + right); + current = right; + } + } + + /* + * Return the closest destination and the difference... + */ + + *rdiff = diff; + + return (current); +} + + +/* + * End of "$Id: options.c 8181 2008-12-10 17:29:57Z mike $". + */ diff --git a/cups/page.c b/cups/page.c new file mode 100644 index 0000000..b5c5e34 --- /dev/null +++ b/cups/page.c @@ -0,0 +1,396 @@ +/* + * "$Id: page.c 7791 2008-07-24 00:55:30Z mike $" + * + * Page size functions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * ppdPageSize() - Get the page size record for the given size. + * ppdPageSizeLimits() - Return the custom page size limits. + * ppdPageWidth() - Get the page width for the given size. + * ppdPageLength() - Get the page length for the given size. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "debug-private.h" +#include "ppd.h" + + +/* + * 'ppdPageSize()' - Get the page size record for the named size. + */ + +ppd_size_t * /* O - Size record for page or NULL */ +ppdPageSize(ppd_file_t *ppd, /* I - PPD file record */ + const char *name) /* I - Size name */ +{ + int i; /* Looping var */ + ppd_size_t *size; /* Current page size */ + double w, l; /* Width and length of page */ + char *nameptr; /* Pointer into name */ + struct lconv *loc; /* Locale data */ + ppd_coption_t *coption; /* Custom option for page size */ + ppd_cparam_t *cparam; /* Custom option parameter */ + + + DEBUG_printf(("2ppdPageSize(ppd=%p, name=\"%s\")", ppd, name)); + + if (!ppd) + { + DEBUG_puts("3ppdPageSize: Bad PPD pointer, returning NULL..."); + return (NULL); + } + + if (name) + { + if (!strncmp(name, "Custom.", 7) && ppd->variable_sizes) + { + /* + * Find the custom page size... + */ + + for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++) + if (!strcmp("Custom", size->name)) + break; + + if (!i) + { + DEBUG_puts("3ppdPageSize: No custom sizes, returning NULL..."); + return (NULL); + } + + /* + * Variable size; size name can be one of the following: + * + * Custom.WIDTHxLENGTHin - Size in inches + * Custom.WIDTHxLENGTHft - Size in feet + * Custom.WIDTHxLENGTHcm - Size in centimeters + * Custom.WIDTHxLENGTHmm - Size in millimeters + * Custom.WIDTHxLENGTHm - Size in meters + * Custom.WIDTHxLENGTH[pt] - Size in points + */ + + loc = localeconv(); + w = _cupsStrScand(name + 7, &nameptr, loc); + if (!nameptr || *nameptr != 'x') + return (NULL); + + l = _cupsStrScand(nameptr + 1, &nameptr, loc); + if (!nameptr) + return (NULL); + + if (!_cups_strcasecmp(nameptr, "in")) + { + w *= 72.0; + l *= 72.0; + } + else if (!_cups_strcasecmp(nameptr, "ft")) + { + w *= 12.0 * 72.0; + l *= 12.0 * 72.0; + } + else if (!_cups_strcasecmp(nameptr, "mm")) + { + w *= 72.0 / 25.4; + l *= 72.0 / 25.4; + } + else if (!_cups_strcasecmp(nameptr, "cm")) + { + w *= 72.0 / 2.54; + l *= 72.0 / 2.54; + } + else if (!_cups_strcasecmp(nameptr, "m")) + { + w *= 72.0 / 0.0254; + l *= 72.0 / 0.0254; + } + + size->width = (float)w; + size->length = (float)l; + size->left = ppd->custom_margins[0]; + size->bottom = ppd->custom_margins[1]; + size->right = (float)(w - ppd->custom_margins[2]); + size->top = (float)(l - ppd->custom_margins[3]); + + /* + * Update the custom option records for the page size, too... + */ + + if ((coption = ppdFindCustomOption(ppd, "PageSize")) != NULL) + { + if ((cparam = ppdFindCustomParam(coption, "Width")) != NULL) + cparam->current.custom_points = (float)w; + + if ((cparam = ppdFindCustomParam(coption, "Height")) != NULL) + cparam->current.custom_points = (float)l; + } + + /* + * Return the page size... + */ + + DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size, + size->name, size->width, size->length)); + + return (size); + } + else + { + /* + * Lookup by name... + */ + + for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++) + if (!_cups_strcasecmp(name, size->name)) + { + DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size, + size->name, size->width, size->length)); + + return (size); + } + } + } + else + { + /* + * Find default... + */ + + for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++) + if (size->marked) + { + DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size, + size->name, size->width, size->length)); + + return (size); + } + } + + DEBUG_puts("3ppdPageSize: Size not found, returning NULL"); + + return (NULL); +} + + +/* + * 'ppdPageSizeLimits()' - Return the custom page size limits. + * + * This function returns the minimum and maximum custom page sizes and printable + * areas based on the currently-marked (selected) options. + * + * If the specified PPD file does not support custom page sizes, both + * "minimum" and "maximum" are filled with zeroes. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +int /* O - 1 if custom sizes are supported, 0 otherwise */ +ppdPageSizeLimits(ppd_file_t *ppd, /* I - PPD file record */ + ppd_size_t *minimum, /* O - Minimum custom size */ + ppd_size_t *maximum) /* O - Maximum custom size */ +{ + ppd_choice_t *qualifier2, /* Second media qualifier */ + *qualifier3; /* Third media qualifier */ + ppd_attr_t *attr; /* Attribute */ + float width, /* Min/max width */ + length; /* Min/max length */ + char spec[PPD_MAX_NAME]; /* Selector for min/max */ + + + /* + * Range check input... + */ + + if (!ppd || !ppd->variable_sizes || !minimum || !maximum) + { + if (minimum) + memset(minimum, 0, sizeof(ppd_size_t)); + + if (maximum) + memset(maximum, 0, sizeof(ppd_size_t)); + + return (0); + } + + /* + * See if we have the cupsMediaQualifier2 and cupsMediaQualifier3 attributes... + */ + + cupsArraySave(ppd->sorted_attrs); + + if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier2", NULL)) != NULL && + attr->value) + qualifier2 = ppdFindMarkedChoice(ppd, attr->value); + else + qualifier2 = NULL; + + if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier3", NULL)) != NULL && + attr->value) + qualifier3 = ppdFindMarkedChoice(ppd, attr->value); + else + qualifier3 = NULL; + + /* + * Figure out the current minimum width and length... + */ + + width = ppd->custom_min[0]; + length = ppd->custom_min[1]; + + if (qualifier2) + { + /* + * Try getting cupsMinSize... + */ + + if (qualifier3) + { + snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice, + qualifier3->choice); + attr = ppdFindAttr(ppd, "cupsMinSize", spec); + } + else + attr = NULL; + + if (!attr) + { + snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice); + attr = ppdFindAttr(ppd, "cupsMinSize", spec); + } + + if (!attr && qualifier3) + { + snprintf(spec, sizeof(spec), "..%s", qualifier3->choice); + attr = ppdFindAttr(ppd, "cupsMinSize", spec); + } + + if ((attr && attr->value && + sscanf(attr->value, "%f%f", &width, &length) != 2) || !attr) + { + width = ppd->custom_min[0]; + length = ppd->custom_min[1]; + } + } + + minimum->width = width; + minimum->length = length; + minimum->left = ppd->custom_margins[0]; + minimum->bottom = ppd->custom_margins[1]; + minimum->right = width - ppd->custom_margins[2]; + minimum->top = length - ppd->custom_margins[3]; + + /* + * Figure out the current maximum width and length... + */ + + width = ppd->custom_max[0]; + length = ppd->custom_max[1]; + + if (qualifier2) + { + /* + * Try getting cupsMaxSize... + */ + + if (qualifier3) + { + snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice, + qualifier3->choice); + attr = ppdFindAttr(ppd, "cupsMaxSize", spec); + } + else + attr = NULL; + + if (!attr) + { + snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice); + attr = ppdFindAttr(ppd, "cupsMaxSize", spec); + } + + if (!attr && qualifier3) + { + snprintf(spec, sizeof(spec), "..%s", qualifier3->choice); + attr = ppdFindAttr(ppd, "cupsMaxSize", spec); + } + + if (!attr || + (attr->value && sscanf(attr->value, "%f%f", &width, &length) != 2)) + { + width = ppd->custom_max[0]; + length = ppd->custom_max[1]; + } + } + + maximum->width = width; + maximum->length = length; + maximum->left = ppd->custom_margins[0]; + maximum->bottom = ppd->custom_margins[1]; + maximum->right = width - ppd->custom_margins[2]; + maximum->top = length - ppd->custom_margins[3]; + + /* + * Return the min and max... + */ + + cupsArrayRestore(ppd->sorted_attrs); + + return (1); +} + + +/* + * 'ppdPageWidth()' - Get the page width for the given size. + */ + +float /* O - Width of page in points or 0.0 */ +ppdPageWidth(ppd_file_t *ppd, /* I - PPD file record */ + const char *name) /* I - Size name */ +{ + ppd_size_t *size; /* Page size */ + + + if ((size = ppdPageSize(ppd, name)) == NULL) + return (0.0); + else + return (size->width); +} + + +/* + * 'ppdPageLength()' - Get the page length for the given size. + */ + +float /* O - Length of page in points or 0.0 */ +ppdPageLength(ppd_file_t *ppd, /* I - PPD file */ + const char *name) /* I - Size name */ +{ + ppd_size_t *size; /* Page size */ + + + if ((size = ppdPageSize(ppd, name)) == NULL) + return (0.0); + else + return (size->length); +} + + +/* + * End of "$Id: page.c 7791 2008-07-24 00:55:30Z mike $". + */ diff --git a/cups/ppd-cache.c b/cups/ppd-cache.c new file mode 100644 index 0000000..ca1c773 --- /dev/null +++ b/cups/ppd-cache.c @@ -0,0 +1,2635 @@ +/* + * "$Id: ppd-cache.c 4185 2013-02-20 02:19:13Z msweet $" + * + * PPD cache implementation for CUPS. + * + * Copyright 2010-2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * _ppdCacheCreateWithFile() - Create PPD cache and mapping data from a + * written file. + * _ppdCacheCreateWithPPD() - Create PWG mapping data from a PPD file. + * _ppdCacheDestroy() - Free all memory used for PWG mapping data. + * _ppdCacheGetBin() - Get the PWG output-bin keyword associated with + * a PPD OutputBin. + * _ppdCacheGetInputSlot() - Get the PPD InputSlot associated with the job + * attributes or a keyword string. + * _ppdCacheGetMediaType() - Get the PPD MediaType associated with the job + * attributes or a keyword string. + * _ppdCacheGetOutputBin() - Get the PPD OutputBin associated with the + * keyword string. + * _ppdCacheGetPageSize() - Get the PPD PageSize associated with the job + * attributes or a keyword string. + * _ppdCacheGetSize() - Get the PWG size associated with a PPD + * PageSize. + * _ppdCacheGetSource() - Get the PWG media-source associated with a PPD + * InputSlot. + * _ppdCacheGetType() - Get the PWG media-type associated with a PPD + * MediaType. + * _ppdCacheWriteFile() - Write PWG mapping data to a file. + * _pwgInputSlotForSource() - Get the InputSlot name for the given PWG + * media-source. + * _pwgMediaTypeForType() - Get the MediaType name for the given PWG + * media-type. + * _pwgPageSizeForMedia() - Get the PageSize name for the given media. + * pwg_ppdize_name() - Convert an IPP keyword to a PPD keyword. + * pwg_unppdize_name() - Convert a PPD keyword to a lowercase IPP + * keyword. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include + + +/* + * Macro to test for two almost-equal PWG measurements. + */ + +#define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2) + + +/* + * Local functions... + */ + +static int pwg_compare_finishings(_pwg_finishings_t *a, + _pwg_finishings_t *b); +static void pwg_free_finishings(_pwg_finishings_t *f); +static void pwg_ppdize_name(const char *ipp, char *name, size_t namesize); +static void pwg_unppdize_name(const char *ppd, char *name, size_t namesize, + const char *dashchars); + + +/* + * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a + * written file. + * + * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a + * file. + */ + +_ppd_cache_t * /* O - PPD cache and mapping data */ +_ppdCacheCreateWithFile( + const char *filename, /* I - File to read */ + ipp_t **attrs) /* IO - IPP attributes, if any */ +{ + cups_file_t *fp; /* File */ + _ppd_cache_t *pc; /* PWG mapping data */ + _pwg_size_t *size; /* Current size */ + _pwg_map_t *map; /* Current map */ + _pwg_finishings_t *finishings; /* Current finishings option */ + int linenum, /* Current line number */ + num_bins, /* Number of bins in file */ + num_sizes, /* Number of sizes in file */ + num_sources, /* Number of sources in file */ + num_types; /* Number of types in file */ + char line[2048], /* Current line */ + *value, /* Pointer to value in line */ + *valueptr, /* Pointer into value */ + pwg_keyword[128], /* PWG keyword */ + ppd_keyword[PPD_MAX_NAME]; + /* PPD keyword */ + _pwg_print_color_mode_t print_color_mode; + /* Print color mode for preset */ + _pwg_print_quality_t print_quality; /* Print quality for preset */ + + + DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename)); + + /* + * Range check input... + */ + + if (attrs) + *attrs = NULL; + + if (!filename) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (NULL); + } + + /* + * Open the file... + */ + + if ((fp = cupsFileOpen(filename, "r")) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + return (NULL); + } + + /* + * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it... + */ + + if (!cupsFileGets(fp, line, sizeof(line))) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line."); + cupsFileClose(fp); + return (NULL); + } + + if (strncmp(line, "#CUPS-PPD-CACHE-", 16)) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line)); + cupsFileClose(fp); + return (NULL); + } + + if (atoi(line + 16) != _PPD_CACHE_VERSION) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Out of date PPD cache file."), 1); + DEBUG_printf(("_ppdCacheCreateWithFile: Cache file has version %s, " + "expected %d.", line + 16, _PPD_CACHE_VERSION)); + cupsFileClose(fp); + return (NULL); + } + + /* + * Allocate the mapping data structure... + */ + + if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t."); + goto create_error; + } + + pc->max_copies = 9999; + + /* + * Read the file... + */ + + linenum = 0; + num_bins = 0; + num_sizes = 0; + num_sources = 0; + num_types = 0; + + while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) + { + DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", " + "linenum=%d", line, value, linenum)); + + if (!value) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + else if (!_cups_strcasecmp(line, "Filter")) + { + if (!pc->filters) + pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0, + (cups_acopy_func_t)_cupsStrAlloc, + (cups_afree_func_t)_cupsStrFree); + + cupsArrayAdd(pc->filters, value); + } + else if (!_cups_strcasecmp(line, "PreFilter")) + { + if (!pc->prefilters) + pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0, + (cups_acopy_func_t)_cupsStrAlloc, + (cups_afree_func_t)_cupsStrFree); + + cupsArrayAdd(pc->prefilters, value); + } + else if (!_cups_strcasecmp(line, "Product")) + { + pc->product = _cupsStrAlloc(value); + } + else if (!_cups_strcasecmp(line, "SingleFile")) + { + pc->single_file = !_cups_strcasecmp(value, "true"); + } + else if (!_cups_strcasecmp(line, "IPP")) + { + off_t pos = cupsFileTell(fp), /* Position in file */ + length = strtol(value, NULL, 10); + /* Length of IPP attributes */ + + if (attrs && *attrs) + { + DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + else if (length <= 0) + { + DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (attrs) + { + /* + * Read IPP attributes into the provided variable... + */ + + *attrs = ippNew(); + + if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL, + *attrs) != IPP_DATA) + { + DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + } + else + { + /* + * Skip the IPP data entirely... + */ + + cupsFileSeek(fp, pos + length); + } + + if (cupsFileTell(fp) != (pos + length)) + { + DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + } + else if (!_cups_strcasecmp(line, "NumBins")) + { + if (num_bins > 0) + { + DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if ((num_bins = atoi(value)) <= 0 || num_bins > 65536) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumBins value %d on line " + "%d.", num_sizes, linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if ((pc->bins = calloc(num_bins, sizeof(_pwg_map_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.", + num_sizes)); + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + goto create_error; + } + } + else if (!_cups_strcasecmp(line, "Bin")) + { + if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (pc->num_bins >= num_bins) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + map = pc->bins + pc->num_bins; + map->pwg = _cupsStrAlloc(pwg_keyword); + map->ppd = _cupsStrAlloc(ppd_keyword); + + pc->num_bins ++; + } + else if (!_cups_strcasecmp(line, "NumSizes")) + { + if (num_sizes > 0) + { + DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if ((num_sizes = atoi(value)) < 0 || num_sizes > 65536) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSizes value %d on line " + "%d.", num_sizes, linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (num_sizes > 0) + { + if ((pc->sizes = calloc(num_sizes, sizeof(_pwg_size_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.", + num_sizes)); + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + goto create_error; + } + } + } + else if (!_cups_strcasecmp(line, "Size")) + { + if (pc->num_sizes >= num_sizes) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + size = pc->sizes + pc->num_sizes; + + if (sscanf(value, "%127s%40s%d%d%d%d%d%d", pwg_keyword, ppd_keyword, + &(size->width), &(size->length), &(size->left), + &(size->bottom), &(size->right), &(size->top)) != 8) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + size->map.pwg = _cupsStrAlloc(pwg_keyword); + size->map.ppd = _cupsStrAlloc(ppd_keyword); + + pc->num_sizes ++; + } + else if (!_cups_strcasecmp(line, "CustomSize")) + { + if (pc->custom_max_width > 0) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line " + "%d.", linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (sscanf(value, "%d%d%d%d%d%d%d%d", &(pc->custom_max_width), + &(pc->custom_max_length), &(pc->custom_min_width), + &(pc->custom_min_length), &(pc->custom_size.left), + &(pc->custom_size.bottom), &(pc->custom_size.right), + &(pc->custom_size.top)) != 8) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "max", + pc->custom_max_width, pc->custom_max_length); + pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword); + + _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "min", + pc->custom_min_width, pc->custom_min_length); + pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword); + } + else if (!_cups_strcasecmp(line, "SourceOption")) + { + pc->source_option = _cupsStrAlloc(value); + } + else if (!_cups_strcasecmp(line, "NumSources")) + { + if (num_sources > 0) + { + DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple " + "times."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if ((num_sources = atoi(value)) <= 0 || num_sources > 65536) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSources value %d on " + "line %d.", num_sources, linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if ((pc->sources = calloc(num_sources, sizeof(_pwg_map_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.", + num_sources)); + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + goto create_error; + } + } + else if (!_cups_strcasecmp(line, "Source")) + { + if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (pc->num_sources >= num_sources) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + map = pc->sources + pc->num_sources; + map->pwg = _cupsStrAlloc(pwg_keyword); + map->ppd = _cupsStrAlloc(ppd_keyword); + + pc->num_sources ++; + } + else if (!_cups_strcasecmp(line, "NumTypes")) + { + if (num_types > 0) + { + DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times."); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if ((num_types = atoi(value)) <= 0 || num_types > 65536) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumTypes value %d on " + "line %d.", num_types, linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if ((pc->types = calloc(num_types, sizeof(_pwg_map_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.", + num_types)); + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + goto create_error; + } + } + else if (!_cups_strcasecmp(line, "Type")) + { + if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (pc->num_types >= num_types) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + map = pc->types + pc->num_types; + map->pwg = _cupsStrAlloc(pwg_keyword); + map->ppd = _cupsStrAlloc(ppd_keyword); + + pc->num_types ++; + } + else if (!_cups_strcasecmp(line, "Preset")) + { + /* + * Preset output-mode print-quality name=value ... + */ + + print_color_mode = (_pwg_print_color_mode_t)strtol(value, &valueptr, 10); + print_quality = (_pwg_print_quality_t)strtol(valueptr, &valueptr, 10); + + if (print_color_mode < _PWG_PRINT_COLOR_MODE_MONOCHROME || + print_color_mode >= _PWG_PRINT_COLOR_MODE_MAX || + print_quality < _PWG_PRINT_QUALITY_DRAFT || + print_quality >= _PWG_PRINT_QUALITY_MAX || + valueptr == value || !*valueptr) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.", + linenum)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + pc->num_presets[print_color_mode][print_quality] = + cupsParseOptions(valueptr, 0, + pc->presets[print_color_mode] + print_quality); + } + else if (!_cups_strcasecmp(line, "SidesOption")) + pc->sides_option = _cupsStrAlloc(value); + else if (!_cups_strcasecmp(line, "Sides1Sided")) + pc->sides_1sided = _cupsStrAlloc(value); + else if (!_cups_strcasecmp(line, "Sides2SidedLong")) + pc->sides_2sided_long = _cupsStrAlloc(value); + else if (!_cups_strcasecmp(line, "Sides2SidedShort")) + pc->sides_2sided_short = _cupsStrAlloc(value); + else if (!_cups_strcasecmp(line, "Finishings")) + { + if (!pc->finishings) + pc->finishings = + cupsArrayNew3((cups_array_func_t)pwg_compare_finishings, + NULL, NULL, 0, NULL, + (cups_afree_func_t)pwg_free_finishings); + + if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL) + goto create_error; + + finishings->value = strtol(value, &valueptr, 10); + finishings->num_options = cupsParseOptions(valueptr, 0, + &(finishings->options)); + + cupsArrayAdd(pc->finishings, finishings); + } + else if (!_cups_strcasecmp(line, "MaxCopies")) + pc->max_copies = atoi(value); + else + { + DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line, + linenum)); + } + } + + if (pc->num_sizes < num_sizes) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).", + pc->num_sizes, num_sizes)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (pc->num_sources < num_sources) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sources (%d < %d).", + pc->num_sources, num_sources)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + if (pc->num_types < num_types) + { + DEBUG_printf(("_ppdCacheCreateWithFile: Not enough types (%d < %d).", + pc->num_types, num_types)); + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1); + goto create_error; + } + + cupsFileClose(fp); + + return (pc); + + /* + * If we get here the file was bad - free any data and return... + */ + + create_error: + + cupsFileClose(fp); + _ppdCacheDestroy(pc); + + if (attrs) + { + ippDelete(*attrs); + *attrs = NULL; + } + + return (NULL); +} + + +/* + * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file. + */ + +_ppd_cache_t * /* O - PPD cache and mapping data */ +_ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */ +{ + int i, j, k; /* Looping vars */ + _ppd_cache_t *pc; /* PWG mapping data */ + ppd_option_t *input_slot, /* InputSlot option */ + *media_type, /* MediaType option */ + *output_bin, /* OutputBin option */ + *color_model, /* ColorModel option */ + *duplex; /* Duplex option */ + ppd_choice_t *choice; /* Current InputSlot/MediaType */ + _pwg_map_t *map; /* Current source/type map */ + ppd_attr_t *ppd_attr; /* Current PPD preset attribute */ + int num_options; /* Number of preset options and props */ + cups_option_t *options; /* Preset options and properties */ + ppd_size_t *ppd_size; /* Current PPD size */ + _pwg_size_t *pwg_size; /* Current PWG size */ + char pwg_keyword[3 + PPD_MAX_NAME + 1 + 12 + 1 + 12 + 3], + /* PWG keyword string */ + ppd_name[PPD_MAX_NAME]; + /* Normalized PPD name */ + const char *pwg_name; /* Standard PWG media name */ + _pwg_media_t *pwg_media; /* PWG media data */ + _pwg_print_color_mode_t pwg_print_color_mode; + /* print-color-mode index */ + _pwg_print_quality_t pwg_print_quality; + /* print-quality index */ + int similar; /* Are the old and new size similar? */ + _pwg_size_t *old_size; /* Current old size */ + int old_imageable, /* Old imageable length in 2540ths */ + old_borderless, /* Old borderless state */ + old_known_pwg; /* Old PWG name is well-known */ + int new_width, /* New width in 2540ths */ + new_length, /* New length in 2540ths */ + new_left, /* New left margin in 2540ths */ + new_bottom, /* New bottom margin in 2540ths */ + new_right, /* New right margin in 2540ths */ + new_top, /* New top margin in 2540ths */ + new_imageable, /* New imageable length in 2540ths */ + new_borderless, /* New borderless state */ + new_known_pwg; /* New PWG name is well-known */ + _pwg_size_t *new_size; /* New size to add, if any */ + const char *filter; /* Current filter */ + _pwg_finishings_t *finishings; /* Current finishings value */ + + + DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd)); + + /* + * Range check input... + */ + + if (!ppd) + return (NULL); + + /* + * Allocate memory... + */ + + if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL) + { + DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t."); + goto create_error; + } + + /* + * Copy and convert size data... + */ + + if (ppd->num_sizes > 0) + { + if ((pc->sizes = calloc(ppd->num_sizes, sizeof(_pwg_size_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d " + "_pwg_size_t's.", ppd->num_sizes)); + goto create_error; + } + + for (i = ppd->num_sizes, pwg_size = pc->sizes, ppd_size = ppd->sizes; + i > 0; + i --, ppd_size ++) + { + /* + * Don't copy over custom size... + */ + + if (!_cups_strcasecmp(ppd_size->name, "Custom")) + continue; + + /* + * Convert the PPD size name to the corresponding PWG keyword name. + */ + + if ((pwg_media = _pwgMediaForPPD(ppd_size->name)) != NULL) + { + /* + * Standard name, do we have conflicts? + */ + + for (j = 0; j < pc->num_sizes; j ++) + if (!strcmp(pc->sizes[j].map.pwg, pwg_media->pwg)) + { + pwg_media = NULL; + break; + } + } + + if (pwg_media) + { + /* + * Standard name and no conflicts, use it! + */ + + pwg_name = pwg_media->pwg; + new_known_pwg = 1; + } + else + { + /* + * Not a standard name; convert it to a PWG vendor name of the form: + * + * pp_lowerppd_WIDTHxHEIGHTuu + */ + + pwg_name = pwg_keyword; + new_known_pwg = 0; + + pwg_unppdize_name(ppd_size->name, ppd_name, sizeof(ppd_name), "_."); + _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), NULL, ppd_name, + _PWG_FROMPTS(ppd_size->width), + _PWG_FROMPTS(ppd_size->length)); + } + + /* + * If we have a similar paper with non-zero margins then we only want to + * keep it if it has a larger imageable area length. The NULL check is for + * dimensions that are <= 0... + */ + + if ((pwg_media = _pwgMediaForSize(_PWG_FROMPTS(ppd_size->width), + _PWG_FROMPTS(ppd_size->length))) == NULL) + continue; + + new_width = pwg_media->width; + new_length = pwg_media->length; + new_left = _PWG_FROMPTS(ppd_size->left); + new_bottom = _PWG_FROMPTS(ppd_size->bottom); + new_right = _PWG_FROMPTS(ppd_size->width - ppd_size->right); + new_top = _PWG_FROMPTS(ppd_size->length - ppd_size->top); + new_imageable = new_length - new_top - new_bottom; + new_borderless = new_bottom == 0 && new_top == 0 && + new_left == 0 && new_right == 0; + + for (k = pc->num_sizes, similar = 0, old_size = pc->sizes, new_size = NULL; + k > 0 && !similar; + k --, old_size ++) + { + old_imageable = old_size->length - old_size->top - old_size->bottom; + old_borderless = old_size->left == 0 && old_size->bottom == 0 && + old_size->right == 0 && old_size->top == 0; + old_known_pwg = strncmp(old_size->map.pwg, "oe_", 3) && + strncmp(old_size->map.pwg, "om_", 3); + + similar = old_borderless == new_borderless && + _PWG_EQUIVALENT(old_size->width, new_width) && + _PWG_EQUIVALENT(old_size->length, new_length); + + if (similar && + (new_known_pwg || (!old_known_pwg && new_imageable > old_imageable))) + { + /* + * The new paper has a larger imageable area so it could replace + * the older paper. Regardless of the imageable area, we always + * prefer the size with a well-known PWG name. + */ + + new_size = old_size; + _cupsStrFree(old_size->map.ppd); + _cupsStrFree(old_size->map.pwg); + } + } + + if (!similar) + { + /* + * The paper was unique enough to deserve its own entry so add it to the + * end. + */ + + new_size = pwg_size ++; + pc->num_sizes ++; + } + + if (new_size) + { + /* + * Save this size... + */ + + new_size->map.ppd = _cupsStrAlloc(ppd_size->name); + new_size->map.pwg = _cupsStrAlloc(pwg_name); + new_size->width = new_width; + new_size->length = new_length; + new_size->left = new_left; + new_size->bottom = new_bottom; + new_size->right = new_right; + new_size->top = new_top; + } + } + } + + if (ppd->variable_sizes) + { + /* + * Generate custom size data... + */ + + _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "max", + _PWG_FROMPTS(ppd->custom_max[0]), + _PWG_FROMPTS(ppd->custom_max[1])); + pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword); + pc->custom_max_width = _PWG_FROMPTS(ppd->custom_max[0]); + pc->custom_max_length = _PWG_FROMPTS(ppd->custom_max[1]); + + _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "min", + _PWG_FROMPTS(ppd->custom_min[0]), + _PWG_FROMPTS(ppd->custom_min[1])); + pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword); + pc->custom_min_width = _PWG_FROMPTS(ppd->custom_min[0]); + pc->custom_min_length = _PWG_FROMPTS(ppd->custom_min[1]); + + pc->custom_size.left = _PWG_FROMPTS(ppd->custom_margins[0]); + pc->custom_size.bottom = _PWG_FROMPTS(ppd->custom_margins[1]); + pc->custom_size.right = _PWG_FROMPTS(ppd->custom_margins[2]); + pc->custom_size.top = _PWG_FROMPTS(ppd->custom_margins[3]); + } + + /* + * Copy and convert InputSlot data... + */ + + if ((input_slot = ppdFindOption(ppd, "InputSlot")) == NULL) + input_slot = ppdFindOption(ppd, "HPPaperSource"); + + if (input_slot) + { + pc->source_option = _cupsStrAlloc(input_slot->keyword); + + if ((pc->sources = calloc(input_slot->num_choices, + sizeof(_pwg_map_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d " + "_pwg_map_t's for InputSlot.", input_slot->num_choices)); + goto create_error; + } + + pc->num_sources = input_slot->num_choices; + + for (i = input_slot->num_choices, choice = input_slot->choices, + map = pc->sources; + i > 0; + i --, choice ++, map ++) + { + if (!_cups_strncasecmp(choice->choice, "Auto", 4) || + !_cups_strcasecmp(choice->choice, "Default")) + pwg_name = "auto"; + else if (!_cups_strcasecmp(choice->choice, "Cassette")) + pwg_name = "main"; + else if (!_cups_strcasecmp(choice->choice, "PhotoTray")) + pwg_name = "photo"; + else if (!_cups_strcasecmp(choice->choice, "CDTray")) + pwg_name = "disc"; + else if (!_cups_strncasecmp(choice->choice, "Multipurpose", 12) || + !_cups_strcasecmp(choice->choice, "MP") || + !_cups_strcasecmp(choice->choice, "MPTray")) + pwg_name = "by-pass-tray"; + else if (!_cups_strcasecmp(choice->choice, "LargeCapacity")) + pwg_name = "large-capacity"; + else if (!_cups_strncasecmp(choice->choice, "Lower", 5)) + pwg_name = "bottom"; + else if (!_cups_strncasecmp(choice->choice, "Middle", 6)) + pwg_name = "middle"; + else if (!_cups_strncasecmp(choice->choice, "Upper", 5)) + pwg_name = "top"; + else if (!_cups_strncasecmp(choice->choice, "Side", 4)) + pwg_name = "side"; + else if (!_cups_strcasecmp(choice->choice, "Roll")) + pwg_name = "main-roll"; + else + { + /* + * Convert PPD name to lowercase... + */ + + pwg_name = pwg_keyword; + pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword), + "_"); + } + + map->pwg = _cupsStrAlloc(pwg_name); + map->ppd = _cupsStrAlloc(choice->choice); + } + } + + /* + * Copy and convert MediaType data... + */ + + if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL) + { + if ((pc->types = calloc(media_type->num_choices, + sizeof(_pwg_map_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d " + "_pwg_map_t's for MediaType.", media_type->num_choices)); + goto create_error; + } + + pc->num_types = media_type->num_choices; + + for (i = media_type->num_choices, choice = media_type->choices, + map = pc->types; + i > 0; + i --, choice ++, map ++) + { + if (!_cups_strncasecmp(choice->choice, "Auto", 4) || + !_cups_strcasecmp(choice->choice, "Any") || + !_cups_strcasecmp(choice->choice, "Default")) + pwg_name = "auto"; + else if (!_cups_strncasecmp(choice->choice, "Card", 4)) + pwg_name = "cardstock"; + else if (!_cups_strncasecmp(choice->choice, "Env", 3)) + pwg_name = "envelope"; + else if (!_cups_strncasecmp(choice->choice, "Gloss", 5)) + pwg_name = "photographic-glossy"; + else if (!_cups_strcasecmp(choice->choice, "HighGloss")) + pwg_name = "photographic-high-gloss"; + else if (!_cups_strcasecmp(choice->choice, "Matte")) + pwg_name = "photographic-matte"; + else if (!_cups_strncasecmp(choice->choice, "Plain", 5)) + pwg_name = "stationery"; + else if (!_cups_strncasecmp(choice->choice, "Coated", 6)) + pwg_name = "stationery-coated"; + else if (!_cups_strcasecmp(choice->choice, "Inkjet")) + pwg_name = "stationery-inkjet"; + else if (!_cups_strcasecmp(choice->choice, "Letterhead")) + pwg_name = "stationery-letterhead"; + else if (!_cups_strncasecmp(choice->choice, "Preprint", 8)) + pwg_name = "stationery-preprinted"; + else if (!_cups_strcasecmp(choice->choice, "Recycled")) + pwg_name = "stationery-recycled"; + else if (!_cups_strncasecmp(choice->choice, "Transparen", 10)) + pwg_name = "transparency"; + else + { + /* + * Convert PPD name to lowercase... + */ + + pwg_name = pwg_keyword; + pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword), + "_"); + } + + map->pwg = _cupsStrAlloc(pwg_name); + map->ppd = _cupsStrAlloc(choice->choice); + } + } + + /* + * Copy and convert OutputBin data... + */ + + if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL) + { + if ((pc->bins = calloc(output_bin->num_choices, + sizeof(_pwg_map_t))) == NULL) + { + DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d " + "_pwg_map_t's for OutputBin.", output_bin->num_choices)); + goto create_error; + } + + pc->num_bins = output_bin->num_choices; + + for (i = output_bin->num_choices, choice = output_bin->choices, + map = pc->bins; + i > 0; + i --, choice ++, map ++) + { + pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword), "_"); + + map->pwg = _cupsStrAlloc(pwg_keyword); + map->ppd = _cupsStrAlloc(choice->choice); + } + } + + if ((ppd_attr = ppdFindAttr(ppd, "APPrinterPreset", NULL)) != NULL) + { + /* + * Copy and convert APPrinterPreset (output-mode + print-quality) data... + */ + + const char *quality, /* com.apple.print.preset.quality value */ + *output_mode, /* com.apple.print.preset.output-mode value */ + *color_model_val, /* ColorModel choice */ + *graphicsType, /* com.apple.print.preset.graphicsType value */ + *media_front_coating; /* com.apple.print.preset.media-front-coating value */ + + do + { + num_options = _ppdParseOptions(ppd_attr->value, 0, &options, + _PPD_PARSE_ALL); + + if ((quality = cupsGetOption("com.apple.print.preset.quality", + num_options, options)) != NULL) + { + /* + * Get the print-quality for this preset... + */ + + if (!strcmp(quality, "low")) + pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT; + else if (!strcmp(quality, "high")) + pwg_print_quality = _PWG_PRINT_QUALITY_HIGH; + else + pwg_print_quality = _PWG_PRINT_QUALITY_NORMAL; + + /* + * Ignore graphicsType "Photo" presets that are not high quality. + */ + + graphicsType = cupsGetOption("com.apple.print.preset.graphicsType", + num_options, options); + + if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH && graphicsType && + !strcmp(graphicsType, "Photo")) + continue; + + /* + * Ignore presets for normal and draft quality where the coating + * isn't "none" or "autodetect". + */ + + media_front_coating = cupsGetOption( + "com.apple.print.preset.media-front-coating", + num_options, options); + + if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH && + media_front_coating && + strcmp(media_front_coating, "none") && + strcmp(media_front_coating, "autodetect")) + continue; + + /* + * Get the output mode for this preset... + */ + + output_mode = cupsGetOption("com.apple.print.preset.output-mode", + num_options, options); + color_model_val = cupsGetOption("ColorModel", num_options, options); + + if (output_mode) + { + if (!strcmp(output_mode, "monochrome")) + pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME; + else + pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR; + } + else if (color_model_val) + { + if (!_cups_strcasecmp(color_model_val, "Gray")) + pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME; + else + pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR; + } + else + pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR; + + /* + * Save the options for this combination as needed... + */ + + if (!pc->num_presets[pwg_print_color_mode][pwg_print_quality]) + pc->num_presets[pwg_print_color_mode][pwg_print_quality] = + _ppdParseOptions(ppd_attr->value, 0, + pc->presets[pwg_print_color_mode] + + pwg_print_quality, _PPD_PARSE_OPTIONS); + } + + cupsFreeOptions(num_options, options); + } + while ((ppd_attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL)) != NULL); + } + + if (!pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_DRAFT] && + !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_NORMAL] && + !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_HIGH]) + { + /* + * Try adding some common color options to create grayscale presets. These + * are listed in order of popularity... + */ + + const char *color_option = NULL, /* Color control option */ + *gray_choice = NULL; /* Choice to select grayscale */ + + if ((color_model = ppdFindOption(ppd, "ColorModel")) != NULL && + ppdFindChoice(color_model, "Gray")) + { + color_option = "ColorModel"; + gray_choice = "Gray"; + } + else if ((color_model = ppdFindOption(ppd, "HPColorMode")) != NULL && + ppdFindChoice(color_model, "grayscale")) + { + color_option = "HPColorMode"; + gray_choice = "grayscale"; + } + else if ((color_model = ppdFindOption(ppd, "BRMonoColor")) != NULL && + ppdFindChoice(color_model, "Mono")) + { + color_option = "BRMonoColor"; + gray_choice = "Mono"; + } + else if ((color_model = ppdFindOption(ppd, "CNIJSGrayScale")) != NULL && + ppdFindChoice(color_model, "1")) + { + color_option = "CNIJSGrayScale"; + gray_choice = "1"; + } + else if ((color_model = ppdFindOption(ppd, "HPColorAsGray")) != NULL && + ppdFindChoice(color_model, "True")) + { + color_option = "HPColorAsGray"; + gray_choice = "True"; + } + + if (color_option && gray_choice) + { + /* + * Copy and convert ColorModel (output-mode) data... + */ + + cups_option_t *coption, /* Color option */ + *moption; /* Monochrome option */ + + for (pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT; + pwg_print_quality < _PWG_PRINT_QUALITY_MAX; + pwg_print_quality ++) + { + if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_print_quality]) + { + /* + * Copy the color options... + */ + + num_options = pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR] + [pwg_print_quality]; + options = calloc(sizeof(cups_option_t), num_options); + + if (options) + { + for (i = num_options, moption = options, + coption = pc->presets[_PWG_PRINT_COLOR_MODE_COLOR] + [pwg_print_quality]; + i > 0; + i --, moption ++, coption ++) + { + moption->name = _cupsStrRetain(coption->name); + moption->value = _cupsStrRetain(coption->value); + } + + pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] = + num_options; + pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] = + options; + } + } + else if (pwg_print_quality != _PWG_PRINT_QUALITY_NORMAL) + continue; + + /* + * Add the grayscale option to the preset... + */ + + pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] = + cupsAddOption(color_option, gray_choice, + pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME] + [pwg_print_quality], + pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME] + + pwg_print_quality); + } + } + } + + /* + * Copy and convert Duplex (sides) data... + */ + + if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL) + if ((duplex = ppdFindOption(ppd, "JCLDuplex")) == NULL) + if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL) + if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL) + duplex = ppdFindOption(ppd, "KD03Duplex"); + + if (duplex) + { + pc->sides_option = _cupsStrAlloc(duplex->keyword); + + for (i = duplex->num_choices, choice = duplex->choices; + i > 0; + i --, choice ++) + { + if ((!_cups_strcasecmp(choice->choice, "None") || + !_cups_strcasecmp(choice->choice, "False")) && !pc->sides_1sided) + pc->sides_1sided = _cupsStrAlloc(choice->choice); + else if ((!_cups_strcasecmp(choice->choice, "DuplexNoTumble") || + !_cups_strcasecmp(choice->choice, "LongEdge") || + !_cups_strcasecmp(choice->choice, "Top")) && !pc->sides_2sided_long) + pc->sides_2sided_long = _cupsStrAlloc(choice->choice); + else if ((!_cups_strcasecmp(choice->choice, "DuplexTumble") || + !_cups_strcasecmp(choice->choice, "ShortEdge") || + !_cups_strcasecmp(choice->choice, "Bottom")) && + !pc->sides_2sided_short) + pc->sides_2sided_short = _cupsStrAlloc(choice->choice); + } + } + + /* + * Copy filters and pre-filters... + */ + + pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0, + (cups_acopy_func_t)_cupsStrAlloc, + (cups_afree_func_t)_cupsStrFree); + + cupsArrayAdd(pc->filters, + "application/vnd.cups-raw application/octet-stream 0 -"); + + if ((ppd_attr = ppdFindAttr(ppd, "cupsFilter2", NULL)) != NULL) + { + do + { + cupsArrayAdd(pc->filters, ppd_attr->value); + } + while ((ppd_attr = ppdFindNextAttr(ppd, "cupsFilter2", NULL)) != NULL); + } + else if (ppd->num_filters > 0) + { + for (i = 0; i < ppd->num_filters; i ++) + cupsArrayAdd(pc->filters, ppd->filters[i]); + } + else + cupsArrayAdd(pc->filters, "application/vnd.cups-postscript 0 -"); + + /* + * See if we have a command filter... + */ + + for (filter = (const char *)cupsArrayFirst(pc->filters); + filter; + filter = (const char *)cupsArrayNext(pc->filters)) + if (!_cups_strncasecmp(filter, "application/vnd.cups-command", 28) && + _cups_isspace(filter[28])) + break; + + if (!filter && + ((ppd_attr = ppdFindAttr(ppd, "cupsCommands", NULL)) == NULL || + _cups_strcasecmp(ppd_attr->value, "none"))) + { + /* + * No command filter and no cupsCommands keyword telling us not to use one. + * See if this is a PostScript printer, and if so add a PostScript command + * filter... + */ + + for (filter = (const char *)cupsArrayFirst(pc->filters); + filter; + filter = (const char *)cupsArrayNext(pc->filters)) + if (!_cups_strncasecmp(filter, "application/vnd.cups-postscript", 31) && + _cups_isspace(filter[31])) + break; + + if (filter) + cupsArrayAdd(pc->filters, + "application/vnd.cups-command application/postscript 100 " + "commandtops"); + } + + if ((ppd_attr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL) + { + pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0, + (cups_acopy_func_t)_cupsStrAlloc, + (cups_afree_func_t)_cupsStrFree); + + do + { + cupsArrayAdd(pc->prefilters, ppd_attr->value); + } + while ((ppd_attr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL)) != NULL); + } + + if ((ppd_attr = ppdFindAttr(ppd, "cupsSingleFile", NULL)) != NULL) + pc->single_file = !_cups_strcasecmp(ppd_attr->value, "true"); + + /* + * Copy the product string, if any... + */ + + if (ppd->product) + pc->product = _cupsStrAlloc(ppd->product); + + /* + * Copy finishings mapping data... + */ + + if ((ppd_attr = ppdFindAttr(ppd, "cupsIPPFinishings", NULL)) != NULL) + { + pc->finishings = cupsArrayNew3((cups_array_func_t)pwg_compare_finishings, + NULL, NULL, 0, NULL, + (cups_afree_func_t)pwg_free_finishings); + + do + { + if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL) + goto create_error; + + finishings->value = atoi(ppd_attr->spec); + finishings->num_options = _ppdParseOptions(ppd_attr->value, 0, + &(finishings->options), + _PPD_PARSE_OPTIONS); + + cupsArrayAdd(pc->finishings, finishings); + } + while ((ppd_attr = ppdFindNextAttr(ppd, "cupsIPPFinishings", + NULL)) != NULL); + } + + /* + * Max copies... + */ + + if ((ppd_attr = ppdFindAttr(ppd, "cupsMaxCopies", NULL)) != NULL) + pc->max_copies = atoi(ppd_attr->value); + else if (ppd->manual_copies) + pc->max_copies = 1; + else + pc->max_copies = 9999; + + /* + * Return the cache data... + */ + + return (pc); + + /* + * If we get here we need to destroy the PWG mapping data and return NULL... + */ + + create_error: + + _cupsSetError(IPP_INTERNAL_ERROR, _("Out of memory."), 1); + _ppdCacheDestroy(pc); + + return (NULL); +} + + +/* + * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data. + */ + +void +_ppdCacheDestroy(_ppd_cache_t *pc) /* I - PPD cache and mapping data */ +{ + int i; /* Looping var */ + _pwg_map_t *map; /* Current map */ + _pwg_size_t *size; /* Current size */ + + + /* + * Range check input... + */ + + if (!pc) + return; + + /* + * Free memory as needed... + */ + + if (pc->bins) + { + for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++) + { + _cupsStrFree(map->pwg); + _cupsStrFree(map->ppd); + } + + free(pc->bins); + } + + if (pc->sizes) + { + for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++) + { + _cupsStrFree(size->map.pwg); + _cupsStrFree(size->map.ppd); + } + + free(pc->sizes); + } + + if (pc->source_option) + _cupsStrFree(pc->source_option); + + if (pc->sources) + { + for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++) + { + _cupsStrFree(map->pwg); + _cupsStrFree(map->ppd); + } + + free(pc->sources); + } + + if (pc->types) + { + for (i = pc->num_types, map = pc->types; i > 0; i --, map ++) + { + _cupsStrFree(map->pwg); + _cupsStrFree(map->ppd); + } + + free(pc->types); + } + + if (pc->custom_max_keyword) + _cupsStrFree(pc->custom_max_keyword); + + if (pc->custom_min_keyword) + _cupsStrFree(pc->custom_min_keyword); + + _cupsStrFree(pc->product); + cupsArrayDelete(pc->filters); + cupsArrayDelete(pc->prefilters); + cupsArrayDelete(pc->finishings); + + free(pc); +} + + +/* + * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD + * OutputBin. + */ + +const char * /* O - output-bin or NULL */ +_ppdCacheGetBin( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + const char *output_bin) /* I - PPD OutputBin string */ +{ + int i; /* Looping var */ + + + /* + * Range check input... + */ + + if (!pc || !output_bin) + return (NULL); + + /* + * Look up the OutputBin string... + */ + + + for (i = 0; i < pc->num_bins; i ++) + if (!_cups_strcasecmp(output_bin, pc->bins[i].ppd)) + return (pc->bins[i].pwg); + + return (NULL); +} + + +/* + * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given + * IPP finishings value(s). + */ + +int /* O - New number of options */ +_ppdCacheGetFinishingOptions( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + ipp_t *job, /* I - Job attributes or NULL */ + ipp_finish_t value, /* I - IPP finishings value of IPP_FINISHINGS_NONE */ + int num_options, /* I - Number of options */ + cups_option_t **options) /* IO - Options */ +{ + int i; /* Looping var */ + _pwg_finishings_t *f, /* PWG finishings options */ + key; /* Search key */ + ipp_attribute_t *attr; /* Finishings attribute */ + cups_option_t *option; /* Current finishings option */ + + + /* + * Range check input... + */ + + if (!pc || cupsArrayCount(pc->finishings) == 0 || !options || + (!job && value == IPP_FINISHINGS_NONE)) + return (num_options); + + /* + * Apply finishing options... + */ + + if (job && (attr = ippFindAttribute(job, "finishings", IPP_TAG_ENUM)) != NULL) + { + int num_values = ippGetCount(attr); /* Number of values */ + + for (i = 0; i < num_values; i ++) + { + key.value = ippGetInteger(attr, i); + + if ((f = cupsArrayFind(pc->finishings, &key)) != NULL) + { + int j; /* Another looping var */ + + for (j = f->num_options, option = f->options; j > 0; j --, option ++) + num_options = cupsAddOption(option->name, option->value, + num_options, options); + } + } + } + else if (value != IPP_FINISHINGS_NONE) + { + key.value = value; + + if ((f = cupsArrayFind(pc->finishings, &key)) != NULL) + { + int j; /* Another looping var */ + + for (j = f->num_options, option = f->options; j > 0; j --, option ++) + num_options = cupsAddOption(option->name, option->value, + num_options, options); + } + } + + return (num_options); +} + + +/* + * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given + * PPD options. + */ + +int /* O - Number of finishings values */ +_ppdCacheGetFinishingValues( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + int num_options, /* I - Number of options */ + cups_option_t *options, /* I - Options */ + int max_values, /* I - Maximum number of finishings values */ + int *values) /* O - Finishings values */ +{ + int i, /* Looping var */ + num_values = 0; /* Number of values */ + _pwg_finishings_t *f; /* Current finishings option */ + cups_option_t *option; /* Current option */ + const char *val; /* Value for option */ + + + /* + * Range check input... + */ + + if (!pc || !pc->finishings || num_options < 1 || max_values < 1 || !values) + return (0); + + /* + * Go through the finishings options and see what is set... + */ + + for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings); + f; + f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings)) + { + for (i = f->num_options, option = f->options; i > 0; i --, option ++) + if ((val = cupsGetOption(option->name, num_options, options)) == NULL || + _cups_strcasecmp(option->value, val)) + break; + + if (i == 0) + { + values[num_values ++] = f->value; + + if (num_values >= max_values) + break; + } + } + + return (num_values); +} + + +/* + * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job + * attributes or a keyword string. + */ + +const char * /* O - PPD InputSlot or NULL */ +_ppdCacheGetInputSlot( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + ipp_t *job, /* I - Job attributes or NULL */ + const char *keyword) /* I - Keyword string or NULL */ +{ + /* + * Range check input... + */ + + if (!pc || pc->num_sources == 0 || (!job && !keyword)) + return (NULL); + + if (job && !keyword) + { + /* + * Lookup the media-col attribute and any media-source found there... + */ + + ipp_attribute_t *media_col, /* media-col attribute */ + *media_source; /* media-source attribute */ + _pwg_size_t size; /* Dimensional size */ + int margins_set; /* Were the margins set? */ + + media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION); + if (media_col && + (media_source = ippFindAttribute(ippGetCollection(media_col, 0), + "media-source", + IPP_TAG_KEYWORD)) != NULL) + { + /* + * Use the media-source value from media-col... + */ + + keyword = ippGetString(media_source, 0, NULL); + } + else if (_pwgInitSize(&size, job, &margins_set)) + { + /* + * For media <= 5x7, look for a photo tray... + */ + + if (size.width <= (5 * 2540) && size.length <= (7 * 2540)) + keyword = "photo"; + } + } + + if (keyword) + { + int i; /* Looping var */ + + for (i = 0; i < pc->num_sources; i ++) + if (!_cups_strcasecmp(keyword, pc->sources[i].pwg)) + return (pc->sources[i].ppd); + } + + return (NULL); +} + + +/* + * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job + * attributes or a keyword string. + */ + +const char * /* O - PPD MediaType or NULL */ +_ppdCacheGetMediaType( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + ipp_t *job, /* I - Job attributes or NULL */ + const char *keyword) /* I - Keyword string or NULL */ +{ + /* + * Range check input... + */ + + if (!pc || pc->num_types == 0 || (!job && !keyword)) + return (NULL); + + if (job && !keyword) + { + /* + * Lookup the media-col attribute and any media-source found there... + */ + + ipp_attribute_t *media_col, /* media-col attribute */ + *media_type; /* media-type attribute */ + + media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION); + if (media_col) + { + if ((media_type = ippFindAttribute(media_col->values[0].collection, + "media-type", + IPP_TAG_KEYWORD)) == NULL) + media_type = ippFindAttribute(media_col->values[0].collection, + "media-type", IPP_TAG_NAME); + + if (media_type) + keyword = media_type->values[0].string.text; + } + } + + if (keyword) + { + int i; /* Looping var */ + + for (i = 0; i < pc->num_types; i ++) + if (!_cups_strcasecmp(keyword, pc->types[i].pwg)) + return (pc->types[i].ppd); + } + + return (NULL); +} + + +/* + * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword + * string. + */ + +const char * /* O - PPD OutputBin or NULL */ +_ppdCacheGetOutputBin( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + const char *output_bin) /* I - Keyword string */ +{ + int i; /* Looping var */ + + + /* + * Range check input... + */ + + if (!pc || !output_bin) + return (NULL); + + /* + * Look up the OutputBin string... + */ + + + for (i = 0; i < pc->num_bins; i ++) + if (!_cups_strcasecmp(output_bin, pc->bins[i].pwg)) + return (pc->bins[i].ppd); + + return (NULL); +} + + +/* + * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job + * attributes or a keyword string. + */ + +const char * /* O - PPD PageSize or NULL */ +_ppdCacheGetPageSize( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + ipp_t *job, /* I - Job attributes or NULL */ + const char *keyword, /* I - Keyword string or NULL */ + int *exact) /* O - 1 if exact match, 0 otherwise */ +{ + int i; /* Looping var */ + _pwg_size_t *size, /* Current size */ + *closest, /* Closest size */ + jobsize; /* Size data from job */ + int margins_set, /* Were the margins set? */ + dwidth, /* Difference in width */ + dlength, /* Difference in length */ + dleft, /* Difference in left margins */ + dright, /* Difference in right margins */ + dbottom, /* Difference in bottom margins */ + dtop, /* Difference in top margins */ + dmin, /* Minimum difference */ + dclosest; /* Closest difference */ + const char *ppd_name; /* PPD media name */ + + + DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)", + pc, job, keyword, exact)); + + /* + * Range check input... + */ + + if (!pc || (!job && !keyword)) + return (NULL); + + if (exact) + *exact = 0; + + ppd_name = keyword; + + if (job) + { + /* + * Try getting the PPD media name from the job attributes... + */ + + ipp_attribute_t *attr; /* Job attribute */ + + if ((attr = ippFindAttribute(job, "PageSize", IPP_TAG_ZERO)) == NULL) + if ((attr = ippFindAttribute(job, "PageRegion", IPP_TAG_ZERO)) == NULL) + attr = ippFindAttribute(job, "media", IPP_TAG_ZERO); + +#ifdef DEBUG + if (attr) + DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)", + attr->name, ippTagString(attr->value_tag))); + else + DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute."); +#endif /* DEBUG */ + + if (attr && (attr->value_tag == IPP_TAG_NAME || + attr->value_tag == IPP_TAG_KEYWORD)) + ppd_name = attr->values[0].string.text; + } + + DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name)); + + if (ppd_name) + { + /* + * Try looking up the named PPD size first... + */ + + for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++) + { + DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]", + (int)(size - pc->sizes), size->map.pwg, size->map.ppd)); + + if (!_cups_strcasecmp(ppd_name, size->map.ppd) || + !_cups_strcasecmp(ppd_name, size->map.pwg)) + { + if (exact) + *exact = 1; + + DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name)); + + return (size->map.ppd); + } + } + } + + if (job && !keyword) + { + /* + * Get the size using media-col or media, with the preference being + * media-col. + */ + + if (!_pwgInitSize(&jobsize, job, &margins_set)) + return (NULL); + } + else + { + /* + * Get the size using a media keyword... + */ + + _pwg_media_t *media; /* Media definition */ + + + if ((media = _pwgMediaForPWG(keyword)) == NULL) + if ((media = _pwgMediaForLegacy(keyword)) == NULL) + if ((media = _pwgMediaForPPD(keyword)) == NULL) + return (NULL); + + jobsize.width = media->width; + jobsize.length = media->length; + margins_set = 0; + } + + /* + * Now that we have the dimensions and possibly the margins, look at the + * available sizes and find the match... + */ + + closest = NULL; + dclosest = 999999999; + + if (!ppd_name || _cups_strncasecmp(ppd_name, "Custom.", 7) || + _cups_strncasecmp(ppd_name, "custom_", 7)) + { + for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++) + { + /* + * Adobe uses a size matching algorithm with an epsilon of 5 points, which + * is just about 176/2540ths... + */ + + dwidth = size->width - jobsize.width; + dlength = size->length - jobsize.length; + + if (dwidth <= -176 || dwidth >= 176 || dlength <= -176 || dlength >= 176) + continue; + + if (margins_set) + { + /* + * Use a tighter epsilon of 1 point (35/2540ths) for margins... + */ + + dleft = size->left - jobsize.left; + dright = size->right - jobsize.right; + dtop = size->top - jobsize.top; + dbottom = size->bottom - jobsize.bottom; + + if (dleft <= -35 || dleft >= 35 || dright <= -35 || dright >= 35 || + dtop <= -35 || dtop >= 35 || dbottom <= -35 || dbottom >= 35) + { + dleft = dleft < 0 ? -dleft : dleft; + dright = dright < 0 ? -dright : dright; + dbottom = dbottom < 0 ? -dbottom : dbottom; + dtop = dtop < 0 ? -dtop : dtop; + dmin = dleft + dright + dbottom + dtop; + + if (dmin < dclosest) + { + dclosest = dmin; + closest = size; + } + + continue; + } + } + + if (exact) + *exact = 1; + + DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size->map.ppd)); + + return (size->map.ppd); + } + } + + if (closest) + { + DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)", + closest->map.ppd)); + + return (closest->map.ppd); + } + + /* + * If we get here we need to check for custom page size support... + */ + + if (jobsize.width >= pc->custom_min_width && + jobsize.width <= pc->custom_max_width && + jobsize.length >= pc->custom_min_length && + jobsize.length <= pc->custom_max_length) + { + /* + * In range, format as Custom.WWWWxLLLL (points). + */ + + snprintf(pc->custom_ppd_size, sizeof(pc->custom_ppd_size), "Custom.%dx%d", + (int)_PWG_TOPTS(jobsize.width), (int)_PWG_TOPTS(jobsize.length)); + + if (margins_set && exact) + { + dleft = pc->custom_size.left - jobsize.left; + dright = pc->custom_size.right - jobsize.right; + dtop = pc->custom_size.top - jobsize.top; + dbottom = pc->custom_size.bottom - jobsize.bottom; + + if (dleft > -35 && dleft < 35 && dright > -35 && dright < 35 && + dtop > -35 && dtop < 35 && dbottom > -35 && dbottom < 35) + *exact = 1; + } + else if (exact) + *exact = 1; + + DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)", + pc->custom_ppd_size)); + + return (pc->custom_ppd_size); + } + + /* + * No custom page size support or the size is out of range - return NULL. + */ + + DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL"); + + return (NULL); +} + + +/* + * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize. + */ + +_pwg_size_t * /* O - PWG size or NULL */ +_ppdCacheGetSize( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + const char *page_size) /* I - PPD PageSize */ +{ + int i; /* Looping var */ + _pwg_media_t *media; /* Media */ + _pwg_size_t *size; /* Current size */ + + + /* + * Range check input... + */ + + if (!pc || !page_size) + return (NULL); + + if (!_cups_strncasecmp(page_size, "Custom.", 7)) + { + /* + * Custom size; size name can be one of the following: + * + * Custom.WIDTHxLENGTHin - Size in inches + * Custom.WIDTHxLENGTHft - Size in feet + * Custom.WIDTHxLENGTHcm - Size in centimeters + * Custom.WIDTHxLENGTHmm - Size in millimeters + * Custom.WIDTHxLENGTHm - Size in meters + * Custom.WIDTHxLENGTH[pt] - Size in points + */ + + double w, l; /* Width and length of page */ + char *ptr; /* Pointer into PageSize */ + struct lconv *loc; /* Locale data */ + + loc = localeconv(); + w = (float)_cupsStrScand(page_size + 7, &ptr, loc); + if (!ptr || *ptr != 'x') + return (NULL); + + l = (float)_cupsStrScand(ptr + 1, &ptr, loc); + if (!ptr) + return (NULL); + + if (!_cups_strcasecmp(ptr, "in")) + { + w *= 2540.0; + l *= 2540.0; + } + else if (!_cups_strcasecmp(ptr, "ft")) + { + w *= 12.0 * 2540.0; + l *= 12.0 * 2540.0; + } + else if (!_cups_strcasecmp(ptr, "mm")) + { + w *= 100.0; + l *= 100.0; + } + else if (!_cups_strcasecmp(ptr, "cm")) + { + w *= 1000.0; + l *= 1000.0; + } + else if (!_cups_strcasecmp(ptr, "m")) + { + w *= 100000.0; + l *= 100000.0; + } + else + { + w *= 2540.0 / 72.0; + l *= 2540.0 / 72.0; + } + + pc->custom_size.width = (int)w; + pc->custom_size.length = (int)l; + + return (&(pc->custom_size)); + } + + /* + * Not a custom size - look it up... + */ + + for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++) + if (!_cups_strcasecmp(page_size, size->map.ppd) || + !_cups_strcasecmp(page_size, size->map.pwg)) + return (size); + + /* + * Look up standard sizes... + */ + + if ((media = _pwgMediaForPPD(page_size)) == NULL) + if ((media = _pwgMediaForLegacy(page_size)) == NULL) + media = _pwgMediaForPWG(page_size); + + if (media) + { + pc->custom_size.width = media->width; + pc->custom_size.length = media->length; + + return (&(pc->custom_size)); + } + + return (NULL); +} + + +/* + * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD + * InputSlot. + */ + +const char * /* O - PWG media-source keyword */ +_ppdCacheGetSource( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + const char *input_slot) /* I - PPD InputSlot */ +{ + int i; /* Looping var */ + _pwg_map_t *source; /* Current source */ + + + /* + * Range check input... + */ + + if (!pc || !input_slot) + return (NULL); + + for (i = pc->num_sources, source = pc->sources; i > 0; i --, source ++) + if (!_cups_strcasecmp(input_slot, source->ppd)) + return (source->pwg); + + return (NULL); +} + + +/* + * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD + * MediaType. + */ + +const char * /* O - PWG media-type keyword */ +_ppdCacheGetType( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + const char *media_type) /* I - PPD MediaType */ +{ + int i; /* Looping var */ + _pwg_map_t *type; /* Current type */ + + + /* + * Range check input... + */ + + if (!pc || !media_type) + return (NULL); + + for (i = pc->num_types, type = pc->types; i > 0; i --, type ++) + if (!_cups_strcasecmp(media_type, type->ppd)) + return (type->pwg); + + return (NULL); +} + + +/* + * '_ppdCacheWriteFile()' - Write PWG mapping data to a file. + */ + +int /* O - 1 on success, 0 on failure */ +_ppdCacheWriteFile( + _ppd_cache_t *pc, /* I - PPD cache and mapping data */ + const char *filename, /* I - File to write */ + ipp_t *attrs) /* I - Attributes to write, if any */ +{ + int i, j, k; /* Looping vars */ + cups_file_t *fp; /* Output file */ + _pwg_size_t *size; /* Current size */ + _pwg_map_t *map; /* Current map */ + _pwg_finishings_t *f; /* Current finishing option */ + cups_option_t *option; /* Current option */ + const char *value; /* Filter/pre-filter value */ + char newfile[1024]; /* New filename */ + + + /* + * Range check input... + */ + + if (!pc || !filename) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (0); + } + + /* + * Open the file and write with compression... + */ + + snprintf(newfile, sizeof(newfile), "%s.N", filename); + if ((fp = cupsFileOpen(newfile, "w9")) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + return (0); + } + + /* + * Standard header... + */ + + cupsFilePrintf(fp, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION); + + /* + * Output bins... + */ + + if (pc->num_bins > 0) + { + cupsFilePrintf(fp, "NumBins %d\n", pc->num_bins); + for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++) + cupsFilePrintf(fp, "Bin %s %s\n", map->pwg, map->ppd); + } + + /* + * Media sizes... + */ + + cupsFilePrintf(fp, "NumSizes %d\n", pc->num_sizes); + for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++) + cupsFilePrintf(fp, "Size %s %s %d %d %d %d %d %d\n", size->map.pwg, + size->map.ppd, size->width, size->length, size->left, + size->bottom, size->right, size->top); + if (pc->custom_max_width > 0) + cupsFilePrintf(fp, "CustomSize %d %d %d %d %d %d %d %d\n", + pc->custom_max_width, pc->custom_max_length, + pc->custom_min_width, pc->custom_min_length, + pc->custom_size.left, pc->custom_size.bottom, + pc->custom_size.right, pc->custom_size.top); + + /* + * Media sources... + */ + + if (pc->source_option) + cupsFilePrintf(fp, "SourceOption %s\n", pc->source_option); + + if (pc->num_sources > 0) + { + cupsFilePrintf(fp, "NumSources %d\n", pc->num_sources); + for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++) + cupsFilePrintf(fp, "Source %s %s\n", map->pwg, map->ppd); + } + + /* + * Media types... + */ + + if (pc->num_types > 0) + { + cupsFilePrintf(fp, "NumTypes %d\n", pc->num_types); + for (i = pc->num_types, map = pc->types; i > 0; i --, map ++) + cupsFilePrintf(fp, "Type %s %s\n", map->pwg, map->ppd); + } + + /* + * Presets... + */ + + for (i = _PWG_PRINT_COLOR_MODE_MONOCHROME; i < _PWG_PRINT_COLOR_MODE_MAX; i ++) + for (j = _PWG_PRINT_QUALITY_DRAFT; j < _PWG_PRINT_QUALITY_MAX; j ++) + if (pc->num_presets[i][j]) + { + cupsFilePrintf(fp, "Preset %d %d", i, j); + for (k = pc->num_presets[i][j], option = pc->presets[i][j]; + k > 0; + k --, option ++) + cupsFilePrintf(fp, " %s=%s", option->name, option->value); + cupsFilePutChar(fp, '\n'); + } + + /* + * Duplex/sides... + */ + + if (pc->sides_option) + cupsFilePrintf(fp, "SidesOption %s\n", pc->sides_option); + + if (pc->sides_1sided) + cupsFilePrintf(fp, "Sides1Sided %s\n", pc->sides_1sided); + + if (pc->sides_2sided_long) + cupsFilePrintf(fp, "Sides2SidedLong %s\n", pc->sides_2sided_long); + + if (pc->sides_2sided_short) + cupsFilePrintf(fp, "Sides2SidedShort %s\n", pc->sides_2sided_short); + + /* + * Product, cupsFilter, cupsFilter2, and cupsPreFilter... + */ + + if (pc->product) + cupsFilePutConf(fp, "Product", pc->product); + + for (value = (const char *)cupsArrayFirst(pc->filters); + value; + value = (const char *)cupsArrayNext(pc->filters)) + cupsFilePutConf(fp, "Filter", value); + + for (value = (const char *)cupsArrayFirst(pc->prefilters); + value; + value = (const char *)cupsArrayNext(pc->prefilters)) + cupsFilePutConf(fp, "PreFilter", value); + + cupsFilePrintf(fp, "SingleFile %s\n", pc->single_file ? "true" : "false"); + + /* + * Finishing options... + */ + + for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings); + f; + f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings)) + { + cupsFilePrintf(fp, "Finishings %d", f->value); + for (i = f->num_options, option = f->options; i > 0; i --, option ++) + cupsFilePrintf(fp, " %s=%s", option->name, option->value); + cupsFilePutChar(fp, '\n'); + } + + /* + * Max copies... + */ + + cupsFilePrintf(fp, "MaxCopies %d\n", pc->max_copies); + + /* + * IPP attributes, if any... + */ + + if (attrs) + { + cupsFilePrintf(fp, "IPP " CUPS_LLFMT "\n", CUPS_LLCAST ippLength(attrs)); + + attrs->state = IPP_IDLE; + ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL, attrs); + } + + /* + * Close and return... + */ + + if (cupsFileClose(fp)) + { + unlink(newfile); + return (0); + } + + unlink(filename); + return (!rename(newfile, filename)); +} + + +/* + * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG + * media-source. + */ + +const char * /* O - InputSlot name */ +_pwgInputSlotForSource( + const char *media_source, /* I - PWG media-source */ + char *name, /* I - Name buffer */ + size_t namesize) /* I - Size of name buffer */ +{ + /* + * Range check input... + */ + + if (!media_source || !name || namesize < PPD_MAX_NAME) + return (NULL); + + if (_cups_strcasecmp(media_source, "main")) + strlcpy(name, "Cassette", namesize); + else if (_cups_strcasecmp(media_source, "alternate")) + strlcpy(name, "Multipurpose", namesize); + else if (_cups_strcasecmp(media_source, "large-capacity")) + strlcpy(name, "LargeCapacity", namesize); + else if (_cups_strcasecmp(media_source, "bottom")) + strlcpy(name, "Lower", namesize); + else if (_cups_strcasecmp(media_source, "middle")) + strlcpy(name, "Middle", namesize); + else if (_cups_strcasecmp(media_source, "top")) + strlcpy(name, "Upper", namesize); + else if (_cups_strcasecmp(media_source, "rear")) + strlcpy(name, "Rear", namesize); + else if (_cups_strcasecmp(media_source, "side")) + strlcpy(name, "Side", namesize); + else if (_cups_strcasecmp(media_source, "envelope")) + strlcpy(name, "Envelope", namesize); + else if (_cups_strcasecmp(media_source, "main-roll")) + strlcpy(name, "Roll", namesize); + else if (_cups_strcasecmp(media_source, "alternate-roll")) + strlcpy(name, "Roll2", namesize); + else + pwg_ppdize_name(media_source, name, namesize); + + return (name); +} + + +/* + * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG + * media-type. + */ + +const char * /* O - MediaType name */ +_pwgMediaTypeForType( + const char *media_type, /* I - PWG media-type */ + char *name, /* I - Name buffer */ + size_t namesize) /* I - Size of name buffer */ +{ + /* + * Range check input... + */ + + if (!media_type || !name || namesize < PPD_MAX_NAME) + return (NULL); + + if (_cups_strcasecmp(media_type, "auto")) + strlcpy(name, "Auto", namesize); + else if (_cups_strcasecmp(media_type, "cardstock")) + strlcpy(name, "Cardstock", namesize); + else if (_cups_strcasecmp(media_type, "envelope")) + strlcpy(name, "Envelope", namesize); + else if (_cups_strcasecmp(media_type, "photographic-glossy")) + strlcpy(name, "Glossy", namesize); + else if (_cups_strcasecmp(media_type, "photographic-high-gloss")) + strlcpy(name, "HighGloss", namesize); + else if (_cups_strcasecmp(media_type, "photographic-matte")) + strlcpy(name, "Matte", namesize); + else if (_cups_strcasecmp(media_type, "stationery")) + strlcpy(name, "Plain", namesize); + else if (_cups_strcasecmp(media_type, "stationery-coated")) + strlcpy(name, "Coated", namesize); + else if (_cups_strcasecmp(media_type, "stationery-inkjet")) + strlcpy(name, "Inkjet", namesize); + else if (_cups_strcasecmp(media_type, "stationery-letterhead")) + strlcpy(name, "Letterhead", namesize); + else if (_cups_strcasecmp(media_type, "stationery-preprinted")) + strlcpy(name, "Preprinted", namesize); + else if (_cups_strcasecmp(media_type, "transparency")) + strlcpy(name, "Transparency", namesize); + else + pwg_ppdize_name(media_type, name, namesize); + + return (name); +} + + +/* + * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media. + */ + +const char * /* O - PageSize name */ +_pwgPageSizeForMedia( + _pwg_media_t *media, /* I - Media */ + char *name, /* I - PageSize name buffer */ + size_t namesize) /* I - Size of name buffer */ +{ + const char *sizeptr, /* Pointer to size in PWG name */ + *dimptr; /* Pointer to dimensions in PWG name */ + + + /* + * Range check input... + */ + + if (!media || !name || namesize < PPD_MAX_NAME) + return (NULL); + + /* + * Copy or generate a PageSize name... + */ + + if (media->ppd) + { + /* + * Use a standard Adobe name... + */ + + strlcpy(name, media->ppd, namesize); + } + else if (!media->pwg || !strncmp(media->pwg, "custom_", 7) || + (sizeptr = strchr(media->pwg, '_')) == NULL || + (dimptr = strchr(sizeptr + 1, '_')) == NULL || + (size_t)(dimptr - sizeptr) > namesize) + { + /* + * Use a name of the form "wNNNhNNN"... + */ + + snprintf(name, namesize, "w%dh%d", (int)_PWG_TOPTS(media->width), + (int)_PWG_TOPTS(media->length)); + } + else + { + /* + * Copy the size name from class_sizename_dimensions... + */ + + memcpy(name, sizeptr + 1, dimptr - sizeptr - 1); + name[dimptr - sizeptr - 1] = '\0'; + } + + return (name); +} + + +/* + * 'pwg_compare_finishings()' - Compare two finishings values. + */ + +static int /* O- Result of comparison */ +pwg_compare_finishings( + _pwg_finishings_t *a, /* I - First finishings value */ + _pwg_finishings_t *b) /* I - Second finishings value */ +{ + return (b->value - a->value); +} + + +/* + * 'pwg_free_finishings()' - Free a finishings value. + */ + +static void +pwg_free_finishings( + _pwg_finishings_t *f) /* I - Finishings value */ +{ + cupsFreeOptions(f->num_options, f->options); + free(f); +} + + +/* + * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword. + */ + +static void +pwg_ppdize_name(const char *ipp, /* I - IPP keyword */ + char *name, /* I - Name buffer */ + size_t namesize) /* I - Size of name buffer */ +{ + char *ptr, /* Pointer into name buffer */ + *end; /* End of name buffer */ + + + *name = toupper(*ipp++); + + for (ptr = name + 1, end = name + namesize - 1; *ipp && ptr < end;) + { + if (*ipp == '-' && _cups_isalpha(ipp[1])) + { + ipp ++; + *ptr++ = toupper(*ipp++ & 255); + } + else + *ptr++ = *ipp++; + } + + *ptr = '\0'; +} + + +/* + * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword. + */ + +static void +pwg_unppdize_name(const char *ppd, /* I - PPD keyword */ + char *name, /* I - Name buffer */ + size_t namesize, /* I - Size of name buffer */ + const char *dashchars)/* I - Characters to be replaced by dashes */ +{ + char *ptr, /* Pointer into name buffer */ + *end; /* End of name buffer */ + + + for (ptr = name, end = name + namesize - 1; *ppd && ptr < end; ppd ++) + { + if (_cups_isalnum(*ppd) || *ppd == '-') + *ptr++ = tolower(*ppd & 255); + else if (strchr(dashchars, *ppd)) + *ptr++ = '-'; + else + *ptr++ = *ppd; + + if (!_cups_isupper(*ppd) && _cups_isalnum(*ppd) && + _cups_isupper(ppd[1]) && ptr < end) + *ptr++ = '-'; + } + + *ptr = '\0'; +} + + +/* + * End of "$Id: ppd-cache.c 4185 2013-02-20 02:19:13Z msweet $". + */ diff --git a/cups/ppd-private.h b/cups/ppd-private.h new file mode 100644 index 0000000..b9d79c8 --- /dev/null +++ b/cups/ppd-private.h @@ -0,0 +1,215 @@ +/* + * "$Id: ppd-private.h 3683 2012-02-16 22:03:53Z msweet $" + * + * Private PPD definitions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This code and any derivative of it may be used and distributed + * freely under the terms of the GNU General Public License when + * used with GNU Ghostscript or its derivatives. Use of the code + * (or any derivative of it) with software other than GNU + * GhostScript (or its derivatives) is governed by the CUPS license + * agreement. + * + * This file is subject to the Apple OS-Developed Software exception. + */ + +#ifndef _CUPS_PPD_PRIVATE_H_ +# define _CUPS_PPD_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include +# include +# include "pwg-private.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Constants... + */ + +# define _PPD_CACHE_VERSION 3 /* Version number in cache file */ + + +/* + * Types and structures... + */ + +typedef enum _ppd_localization_e /**** Selector for _ppdOpen ****/ +{ + _PPD_LOCALIZATION_DEFAULT, /* Load only the default localization */ + _PPD_LOCALIZATION_ICC_PROFILES, /* Load only the color profile localization */ + _PPD_LOCALIZATION_NONE, /* Load no localizations */ + _PPD_LOCALIZATION_ALL /* Load all localizations */ +} _ppd_localization_t; + +typedef enum _ppd_parse_e /**** Selector for _ppdParseOptions ****/ +{ + _PPD_PARSE_OPTIONS, /* Parse only the options */ + _PPD_PARSE_PROPERTIES, /* Parse only the properties */ + _PPD_PARSE_ALL /* Parse everything */ +} _ppd_parse_t; + +typedef struct _ppd_cups_uiconst_s /**** Constraint from cupsUIConstraints ****/ +{ + ppd_option_t *option; /* Constrained option */ + ppd_choice_t *choice; /* Constrained choice or @code NULL@ */ + int installable; /* Installable option? */ +} _ppd_cups_uiconst_t; + +typedef struct _ppd_cups_uiconsts_s /**** cupsUIConstraints ****/ +{ + char resolver[PPD_MAX_NAME]; /* Resolver name */ + int installable, /* Constrained against any installable options? */ + num_constraints; /* Number of constraints */ + _ppd_cups_uiconst_t *constraints; /* Constraints */ +} _ppd_cups_uiconsts_t; + +typedef enum _pwg_print_color_mode_e /**** PWG print-color-mode indices ****/ +{ + _PWG_PRINT_COLOR_MODE_MONOCHROME = 0, /* print-color-mode=monochrome */ + _PWG_PRINT_COLOR_MODE_COLOR, /* print-color-mode=color */ + /* Other proposed values are not supported by CUPS yet. */ + _PWG_PRINT_COLOR_MODE_MAX +} _pwg_print_color_mode_t; + +typedef enum _pwg_print_quality_e /**** PWG print-quality values ****/ +{ + _PWG_PRINT_QUALITY_DRAFT = 0, /* print-quality=3 */ + _PWG_PRINT_QUALITY_NORMAL, /* print-quality=4 */ + _PWG_PRINT_QUALITY_HIGH, /* print-quality=5 */ + _PWG_PRINT_QUALITY_MAX +} _pwg_print_quality_t; + +typedef struct _pwg_finishings_s /**** PWG finishings mapping data ****/ +{ + ipp_finish_t value; /* finishings value */ + int num_options; /* Number of options to apply */ + cups_option_t *options; /* Options to apply */ +} _pwg_finishings_t; + +struct _ppd_cache_s /**** PPD cache and PWG conversion data ****/ +{ + int num_bins; /* Number of output bins */ + _pwg_map_t *bins; /* Output bins */ + int num_sizes; /* Number of media sizes */ + _pwg_size_t *sizes; /* Media sizes */ + int custom_max_width, /* Maximum custom width in 2540ths */ + custom_max_length, /* Maximum custom length in 2540ths */ + custom_min_width, /* Minimum custom width in 2540ths */ + custom_min_length; /* Minimum custom length in 2540ths */ + char *custom_max_keyword, /* Maximum custom size PWG keyword */ + *custom_min_keyword, /* Minimum custom size PWG keyword */ + custom_ppd_size[41]; /* Custom PPD size name */ + _pwg_size_t custom_size; /* Custom size record */ + char *source_option; /* PPD option for media source */ + int num_sources; /* Number of media sources */ + _pwg_map_t *sources; /* Media sources */ + int num_types; /* Number of media types */ + _pwg_map_t *types; /* Media types */ + int num_presets[_PWG_PRINT_COLOR_MODE_MAX][_PWG_PRINT_QUALITY_MAX]; + /* Number of print-color-mode/print-quality options */ + cups_option_t *presets[_PWG_PRINT_COLOR_MODE_MAX][_PWG_PRINT_QUALITY_MAX]; + /* print-color-mode/print-quality options */ + char *sides_option, /* PPD option for sides */ + *sides_1sided, /* Choice for one-sided */ + *sides_2sided_long, /* Choice for two-sided-long-edge */ + *sides_2sided_short; /* Choice for two-sided-short-edge */ + char *product; /* Product value */ + cups_array_t *filters, /* cupsFilter/cupsFilter2 values */ + *prefilters; /* cupsPreFilter values */ + int single_file; /* cupsSingleFile value */ + cups_array_t *finishings; /* cupsIPPFinishings values */ + int max_copies; /* cupsMaxCopies value */ +}; + + +/* + * Prototypes... + */ + +extern _ppd_cache_t *_ppdCacheCreateWithFile(const char *filename, + ipp_t **attrs); +extern _ppd_cache_t *_ppdCacheCreateWithPPD(ppd_file_t *ppd); +extern void _ppdCacheDestroy(_ppd_cache_t *pc); +extern const char *_ppdCacheGetBin(_ppd_cache_t *pc, + const char *output_bin); +extern int _ppdCacheGetFinishingOptions(_ppd_cache_t *pc, ipp_t *job, + ipp_finish_t value, int num_options, + cups_option_t **options); +extern int _ppdCacheGetFinishingValues(_ppd_cache_t *pc, int num_options, + cups_option_t *options, + int max_values, int *values); +extern const char *_ppdCacheGetInputSlot(_ppd_cache_t *pc, ipp_t *job, + const char *keyword); +extern const char *_ppdCacheGetMediaType(_ppd_cache_t *pc, ipp_t *job, + const char *keyword); +extern const char *_ppdCacheGetOutputBin(_ppd_cache_t *pc, + const char *keyword); +extern const char *_ppdCacheGetPageSize(_ppd_cache_t *pc, ipp_t *job, + const char *keyword, int *exact); +extern _pwg_size_t *_ppdCacheGetSize(_ppd_cache_t *pc, + const char *page_size); +extern const char *_ppdCacheGetSource(_ppd_cache_t *pc, + const char *input_slot); +extern const char *_ppdCacheGetType(_ppd_cache_t *pc, + const char *media_type); +extern int _ppdCacheWriteFile(_ppd_cache_t *pc, + const char *filename, ipp_t *attrs); +extern void _ppdFreeLanguages(cups_array_t *languages); +extern cups_encoding_t _ppdGetEncoding(const char *name); +extern cups_array_t *_ppdGetLanguages(ppd_file_t *ppd); +extern unsigned _ppdHashName(const char *name); +extern ppd_attr_t *_ppdLocalizedAttr(ppd_file_t *ppd, const char *keyword, + const char *spec, const char *ll_CC); +extern char *_ppdNormalizeMakeAndModel(const char *make_and_model, + char *buffer, + size_t bufsize); +extern ppd_file_t *_ppdOpen(cups_file_t *fp, + _ppd_localization_t localization); +extern ppd_file_t *_ppdOpenFile(const char *filename, + _ppd_localization_t localization); +extern int _ppdParseOptions(const char *s, int num_options, + cups_option_t **options, + _ppd_parse_t which); +extern const char *_pwgInputSlotForSource(const char *media_source, + char *name, size_t namesize); +extern const char *_pwgMediaTypeForType(const char *media_type, + char *name, size_t namesize); +extern const char *_pwgPageSizeForMedia(_pwg_media_t *media, + char *name, size_t namesize); + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_PPD_PRIVATE_H_ */ + +/* + * End of "$Id: ppd-private.h 3683 2012-02-16 22:03:53Z msweet $". + */ diff --git a/cups/ppd.c b/cups/ppd.c new file mode 100644 index 0000000..1ff51b2 --- /dev/null +++ b/cups/ppd.c @@ -0,0 +1,3398 @@ +/* + * "$Id: ppd.c 9900 2011-08-17 20:59:46Z mike $" + * + * PPD file routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This code and any derivative of it may be used and distributed + * freely under the terms of the GNU General Public License when + * used with GNU Ghostscript or its derivatives. Use of the code + * (or any derivative of it) with software other than GNU + * GhostScript (or its derivatives) is governed by the CUPS license + * agreement. + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * ppdClose() - Free all memory used by the PPD file. + * ppdErrorString() - Returns the text assocated with a status. + * _ppdGetEncoding() - Get the CUPS encoding value for the given + * LanguageEncoding. + * ppdLastError() - Return the status from the last ppdOpen*(). + * ppdOpen() - Read a PPD file into memory. + * _ppdOpen() - Read a PPD file into memory. + * ppdOpen2() - Read a PPD file into memory. + * ppdOpenFd() - Read a PPD file into memory. + * _ppdOpenFile() - Read a PPD file into memory. + * ppdOpenFile() - Read a PPD file into memory. + * ppdSetConformance() - Set the conformance level for PPD files. + * ppd_add_attr() - Add an attribute to the PPD data. + * ppd_add_choice() - Add a choice to an option. + * ppd_add_size() - Add a page size. + * ppd_compare_attrs() - Compare two attributes. + * ppd_compare_choices() - Compare two choices... + * ppd_compare_coptions() - Compare two custom options. + * ppd_compare_options() - Compare two options. + * ppd_decode() - Decode a string value... + * ppd_free_filters() - Free the filters array. + * ppd_free_group() - Free a single UI group. + * ppd_free_option() - Free a single option. + * ppd_get_coption() - Get a custom option record. + * ppd_get_cparam() - Get a custom parameter record. + * ppd_get_group() - Find or create the named group as needed. + * ppd_get_option() - Find or create the named option as needed. + * ppd_hash_option() - Generate a hash of the option name... + * ppd_read() - Read a line from a PPD file, skipping comment + * lines as necessary. + * ppd_update_filters() - Update the filters array as needed. + */ + +/* + * Include necessary headers. + */ + +#include "cups-private.h" +#include "ppd-private.h" + + +/* + * Definitions... + */ + +#if defined(WIN32) || defined(__EMX__) +# define READ_BINARY "rb" /* Open a binary file for reading */ +# define WRITE_BINARY "wb" /* Open a binary file for writing */ +#else +# define READ_BINARY "r" /* Open a binary file for reading */ +# define WRITE_BINARY "w" /* Open a binary file for writing */ +#endif /* WIN32 || __EMX__ */ + +#define ppd_free(p) if (p) free(p) /* Safe free macro */ + +#define PPD_KEYWORD 1 /* Line contained a keyword */ +#define PPD_OPTION 2 /* Line contained an option name */ +#define PPD_TEXT 4 /* Line contained human-readable text */ +#define PPD_STRING 8 /* Line contained a string or code */ + +#define PPD_HASHSIZE 512 /* Size of hash */ + + +/* + * Line buffer structure... + */ + +typedef struct _ppd_line_s +{ + char *buffer; /* Pointer to buffer */ + size_t bufsize; /* Size of the buffer */ +} _ppd_line_t; + + +/* + * Local functions... + */ + +static ppd_attr_t *ppd_add_attr(ppd_file_t *ppd, const char *name, + const char *spec, const char *text, + const char *value); +static ppd_choice_t *ppd_add_choice(ppd_option_t *option, const char *name); +static ppd_size_t *ppd_add_size(ppd_file_t *ppd, const char *name); +static int ppd_compare_attrs(ppd_attr_t *a, ppd_attr_t *b); +static int ppd_compare_choices(ppd_choice_t *a, ppd_choice_t *b); +static int ppd_compare_coptions(ppd_coption_t *a, + ppd_coption_t *b); +static int ppd_compare_options(ppd_option_t *a, ppd_option_t *b); +static int ppd_decode(char *string); +static void ppd_free_filters(ppd_file_t *ppd); +static void ppd_free_group(ppd_group_t *group); +static void ppd_free_option(ppd_option_t *option); +static ppd_coption_t *ppd_get_coption(ppd_file_t *ppd, const char *name); +static ppd_cparam_t *ppd_get_cparam(ppd_coption_t *opt, + const char *param, + const char *text); +static ppd_group_t *ppd_get_group(ppd_file_t *ppd, const char *name, + const char *text, _cups_globals_t *cg, + cups_encoding_t encoding); +static ppd_option_t *ppd_get_option(ppd_group_t *group, const char *name); +static int ppd_hash_option(ppd_option_t *option); +static int ppd_read(cups_file_t *fp, _ppd_line_t *line, + char *keyword, char *option, char *text, + char **string, int ignoreblank, + _cups_globals_t *cg); +static int ppd_update_filters(ppd_file_t *ppd, + _cups_globals_t *cg); + + +/* + * 'ppdClose()' - Free all memory used by the PPD file. + */ + +void +ppdClose(ppd_file_t *ppd) /* I - PPD file record */ +{ + int i; /* Looping var */ + ppd_emul_t *emul; /* Current emulation */ + ppd_group_t *group; /* Current group */ + char **font; /* Current font */ + ppd_attr_t **attr; /* Current attribute */ + ppd_coption_t *coption; /* Current custom option */ + ppd_cparam_t *cparam; /* Current custom parameter */ + + + /* + * Range check arguments... + */ + + if (!ppd) + return; + + /* + * Free all strings at the top level... + */ + + _cupsStrFree(ppd->lang_encoding); + _cupsStrFree(ppd->nickname); + if (ppd->patches) + free(ppd->patches); + _cupsStrFree(ppd->jcl_begin); + _cupsStrFree(ppd->jcl_end); + _cupsStrFree(ppd->jcl_ps); + + /* + * Free any emulations... + */ + + if (ppd->num_emulations > 0) + { + for (i = ppd->num_emulations, emul = ppd->emulations; i > 0; i --, emul ++) + { + _cupsStrFree(emul->start); + _cupsStrFree(emul->stop); + } + + ppd_free(ppd->emulations); + } + + /* + * Free any UI groups, subgroups, and options... + */ + + if (ppd->num_groups > 0) + { + for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++) + ppd_free_group(group); + + ppd_free(ppd->groups); + } + + cupsArrayDelete(ppd->options); + cupsArrayDelete(ppd->marked); + + /* + * Free any page sizes... + */ + + if (ppd->num_sizes > 0) + ppd_free(ppd->sizes); + + /* + * Free any constraints... + */ + + if (ppd->num_consts > 0) + ppd_free(ppd->consts); + + /* + * Free any filters... + */ + + ppd_free_filters(ppd); + + /* + * Free any fonts... + */ + + if (ppd->num_fonts > 0) + { + for (i = ppd->num_fonts, font = ppd->fonts; i > 0; i --, font ++) + _cupsStrFree(*font); + + ppd_free(ppd->fonts); + } + + /* + * Free any profiles... + */ + + if (ppd->num_profiles > 0) + ppd_free(ppd->profiles); + + /* + * Free any attributes... + */ + + if (ppd->num_attrs > 0) + { + for (i = ppd->num_attrs, attr = ppd->attrs; i > 0; i --, attr ++) + { + _cupsStrFree((*attr)->value); + ppd_free(*attr); + } + + ppd_free(ppd->attrs); + } + + cupsArrayDelete(ppd->sorted_attrs); + + /* + * Free custom options... + */ + + for (coption = (ppd_coption_t *)cupsArrayFirst(ppd->coptions); + coption; + coption = (ppd_coption_t *)cupsArrayNext(ppd->coptions)) + { + for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) + { + switch (cparam->type) + { + case PPD_CUSTOM_PASSCODE : + case PPD_CUSTOM_PASSWORD : + case PPD_CUSTOM_STRING : + _cupsStrFree(cparam->current.custom_string); + break; + + default : + break; + } + + free(cparam); + } + + cupsArrayDelete(coption->params); + + free(coption); + } + + cupsArrayDelete(ppd->coptions); + + /* + * Free constraints... + */ + + if (ppd->cups_uiconstraints) + { + _ppd_cups_uiconsts_t *consts; /* Current constraints */ + + + for (consts = (_ppd_cups_uiconsts_t *)cupsArrayFirst(ppd->cups_uiconstraints); + consts; + consts = (_ppd_cups_uiconsts_t *)cupsArrayNext(ppd->cups_uiconstraints)) + { + free(consts->constraints); + free(consts); + } + + cupsArrayDelete(ppd->cups_uiconstraints); + } + + /* + * Free any PPD cache/mapping data... + */ + + if (ppd->cache) + _ppdCacheDestroy(ppd->cache); + + /* + * Free the whole record... + */ + + ppd_free(ppd); +} + + +/* + * 'ppdErrorString()' - Returns the text assocated with a status. + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +const char * /* O - Status string */ +ppdErrorString(ppd_status_t status) /* I - PPD status */ +{ + static const char * const messages[] =/* Status messages */ + { + _("OK"), + _("Unable to open PPD file"), + _("NULL PPD file pointer"), + _("Memory allocation error"), + _("Missing PPD-Adobe-4.x header"), + _("Missing value string"), + _("Internal error"), + _("Bad OpenGroup"), + _("OpenGroup without a CloseGroup first"), + _("Bad OpenUI/JCLOpenUI"), + _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"), + _("Bad OrderDependency"), + _("Bad UIConstraints"), + _("Missing asterisk in column 1"), + _("Line longer than the maximum allowed (255 characters)"), + _("Illegal control character"), + _("Illegal main keyword string"), + _("Illegal option keyword string"), + _("Illegal translation string"), + _("Illegal whitespace character"), + _("Bad custom parameter"), + _("Missing option keyword"), + _("Bad value string"), + _("Missing CloseGroup") + }; + + + if (status < PPD_OK || status >= PPD_MAX_STATUS) + return (_cupsLangString(cupsLangDefault(), _("Unknown"))); + else + return (_cupsLangString(cupsLangDefault(), messages[status])); +} + + +/* + * '_ppdGetEncoding()' - Get the CUPS encoding value for the given + * LanguageEncoding. + */ + +cups_encoding_t /* O - CUPS encoding value */ +_ppdGetEncoding(const char *name) /* I - LanguageEncoding string */ +{ + if (!_cups_strcasecmp(name, "ISOLatin1")) + return (CUPS_ISO8859_1); + else if (!_cups_strcasecmp(name, "ISOLatin2")) + return (CUPS_ISO8859_2); + else if (!_cups_strcasecmp(name, "ISOLatin5")) + return (CUPS_ISO8859_5); + else if (!_cups_strcasecmp(name, "JIS83-RKSJ")) + return (CUPS_JIS_X0213); + else if (!_cups_strcasecmp(name, "MacStandard")) + return (CUPS_MAC_ROMAN); + else if (!_cups_strcasecmp(name, "WindowsANSI")) + return (CUPS_WINDOWS_1252); + else + return (CUPS_UTF8); +} + + +/* + * 'ppdLastError()' - Return the status from the last ppdOpen*(). + * + * @since CUPS 1.1.19/OS X 10.3@ + */ + +ppd_status_t /* O - Status code */ +ppdLastError(int *line) /* O - Line number */ +{ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + if (line) + *line = cg->ppd_line; + + return (cg->ppd_status); +} + + +/* + * '_ppdOpen()' - Read a PPD file into memory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_file_t * /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */ +_ppdOpen( + cups_file_t *fp, /* I - File to read from */ + _ppd_localization_t localization) /* I - Localization to load */ +{ + int i, j, k; /* Looping vars */ + int count; /* Temporary count */ + _ppd_line_t line; /* Line buffer */ + ppd_file_t *ppd; /* PPD file record */ + ppd_group_t *group, /* Current group */ + *subgroup; /* Current sub-group */ + ppd_option_t *option; /* Current option */ + ppd_choice_t *choice; /* Current choice */ + ppd_const_t *constraint; /* Current constraint */ + ppd_size_t *size; /* Current page size */ + int mask; /* Line data mask */ + char keyword[PPD_MAX_NAME], + /* Keyword from file */ + name[PPD_MAX_NAME], + /* Option from file */ + text[PPD_MAX_LINE], + /* Human-readable text from file */ + *string, /* Code/text from file */ + *sptr, /* Pointer into string */ + *nameptr, /* Pointer into name */ + *temp, /* Temporary string pointer */ + **tempfonts; /* Temporary fonts pointer */ + float order; /* Order dependency number */ + ppd_section_t section; /* Order dependency section */ + ppd_profile_t *profile; /* Pointer to color profile */ + char **filter; /* Pointer to filter */ + struct lconv *loc; /* Locale data */ + int ui_keyword; /* Is this line a UI keyword? */ + cups_lang_t *lang; /* Language data */ + cups_encoding_t encoding; /* Encoding of PPD file */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + char custom_name[PPD_MAX_NAME]; + /* CustomFoo attribute name */ + ppd_attr_t *custom_attr; /* CustomFoo attribute */ + char ll[4], /* Language + '.' */ + ll_CC[7]; /* Language + country + '.' */ + size_t ll_len = 0, /* Language length */ + ll_CC_len = 0; /* Language + country length */ + static const char * const ui_keywords[] = + { +#ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST + /* + * Adobe defines some 41 keywords as "UI", meaning that they are + * user interface elements and that they should be treated as such + * even if the PPD creator doesn't use Open/CloseUI around them. + * + * Since this can cause previously invisible options to appear and + * confuse users, the default is to only treat the PageSize and + * PageRegion keywords this way. + */ + /* Boolean keywords */ + "BlackSubstitution", + "Booklet", + "Collate", + "ManualFeed", + "MirrorPrint", + "NegativePrint", + "Sorter", + "TraySwitch", + + /* PickOne keywords */ + "AdvanceMedia", + "BindColor", + "BindEdge", + "BindType", + "BindWhen", + "BitsPerPixel", + "ColorModel", + "CutMedia", + "Duplex", + "FoldType", + "FoldWhen", + "InputSlot", + "JCLFrameBufferSize", + "JCLResolution", + "Jog", + "MediaColor", + "MediaType", + "MediaWeight", + "OutputBin", + "OutputMode", + "OutputOrder", + "PageRegion", + "PageSize", + "Resolution", + "Separations", + "Signature", + "Slipsheet", + "Smoothing", + "StapleLocation", + "StapleOrientation", + "StapleWhen", + "StapleX", + "StapleY" +#else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */ + "PageRegion", + "PageSize" +#endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */ + }; + static const char * const color_keywords[] = /* Keywords associated with color profiles */ + { + ".cupsICCProfile", + ".ColorModel", + }; + + + DEBUG_printf(("_ppdOpen(fp=%p)", fp)); + + /* + * Default to "OK" status... + */ + + cg->ppd_status = PPD_OK; + cg->ppd_line = 0; + + /* + * Range check input... + */ + + if (fp == NULL) + { + cg->ppd_status = PPD_NULL_FILE; + return (NULL); + } + + /* + * If only loading a single localization set up the strings to match... + */ + + if (localization == _PPD_LOCALIZATION_DEFAULT) + { + if ((lang = cupsLangDefault()) == NULL) + return (NULL); + + snprintf(ll_CC, sizeof(ll_CC), "%s.", lang->language); + snprintf(ll, sizeof(ll), "%2.2s.", lang->language); + + ll_CC_len = strlen(ll_CC); + ll_len = strlen(ll); + + DEBUG_printf(("2_ppdOpen: Loading localizations matching \"%s\" and \"%s\"", + ll_CC, ll)); + } + + /* + * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'... + */ + + line.buffer = NULL; + line.bufsize = 0; + + mask = ppd_read(fp, &line, keyword, name, text, &string, 0, cg); + + DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\"...", mask, keyword)); + + if (mask == 0 || + strcmp(keyword, "PPD-Adobe") || + string == NULL || string[0] != '4') + { + /* + * Either this is not a PPD file, or it is not a 4.x PPD file. + */ + + if (cg->ppd_status == PPD_OK) + cg->ppd_status = PPD_MISSING_PPDADOBE4; + + _cupsStrFree(string); + ppd_free(line.buffer); + + return (NULL); + } + + DEBUG_printf(("2_ppdOpen: keyword=%s, string=%p", keyword, string)); + + _cupsStrFree(string); + + /* + * Allocate memory for the PPD file record... + */ + + if ((ppd = calloc(1, sizeof(ppd_file_t))) == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + _cupsStrFree(string); + ppd_free(line.buffer); + + return (NULL); + } + + ppd->language_level = 2; + ppd->color_device = 0; + ppd->colorspace = PPD_CS_N; + ppd->landscape = -90; + ppd->coptions = cupsArrayNew((cups_array_func_t)ppd_compare_coptions, + NULL); + + /* + * Read lines from the PPD file and add them to the file record... + */ + + group = NULL; + subgroup = NULL; + option = NULL; + choice = NULL; + ui_keyword = 0; + encoding = CUPS_ISO8859_1; + loc = localeconv(); + + while ((mask = ppd_read(fp, &line, keyword, name, text, &string, 1, cg)) != 0) + { + DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\", name=\"%s\", " + "text=\"%s\", string=%d chars...", mask, keyword, name, text, + string ? (int)strlen(string) : 0)); + + if (strncmp(keyword, "Default", 7) && !string && + cg->ppd_conform != PPD_CONFORM_RELAXED) + { + /* + * Need a string value! + */ + + cg->ppd_status = PPD_MISSING_VALUE; + + goto error; + } + else if (!string) + continue; + + /* + * Certain main keywords (as defined by the PPD spec) may be used + * without the usual OpenUI/CloseUI stuff. Presumably this is just + * so that Adobe wouldn't completely break compatibility with PPD + * files prior to v4.0 of the spec, but it is hopelessly + * inconsistent... Catch these main keywords and automatically + * create the corresponding option, as needed... + */ + + if (ui_keyword) + { + /* + * Previous line was a UI keyword... + */ + + option = NULL; + ui_keyword = 0; + } + + /* + * If we are filtering out keyword localizations, see if this line needs to + * be used... + */ + + if (localization != _PPD_LOCALIZATION_ALL && + (temp = strchr(keyword, '.')) != NULL && + ((temp - keyword) == 2 || (temp - keyword) == 5) && + _cups_isalpha(keyword[0]) && + _cups_isalpha(keyword[1]) && + (keyword[2] == '.' || + (keyword[2] == '_' && _cups_isalpha(keyword[3]) && + _cups_isalpha(keyword[4]) && keyword[5] == '.'))) + { + if (localization == _PPD_LOCALIZATION_NONE || + (localization == _PPD_LOCALIZATION_DEFAULT && + strncmp(ll_CC, keyword, ll_CC_len) && + strncmp(ll, keyword, ll_len))) + { + DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword)); + continue; + } + else if (localization == _PPD_LOCALIZATION_ICC_PROFILES) + { + /* + * Only load localizations for the color profile related keywords... + */ + + for (i = 0; + i < (int)(sizeof(color_keywords) / sizeof(color_keywords[0])); + i ++) + { + if (!_cups_strcasecmp(temp, color_keywords[i])) + break; + } + + if (i >= (int)(sizeof(color_keywords) / sizeof(color_keywords[0]))) + { + DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword)); + continue; + } + } + } + + if (option == NULL && + (mask & (PPD_KEYWORD | PPD_OPTION | PPD_STRING)) == + (PPD_KEYWORD | PPD_OPTION | PPD_STRING)) + { + for (i = 0; i < (int)(sizeof(ui_keywords) / sizeof(ui_keywords[0])); i ++) + if (!strcmp(keyword, ui_keywords[i])) + break; + + if (i < (int)(sizeof(ui_keywords) / sizeof(ui_keywords[0]))) + { + /* + * Create the option in the appropriate group... + */ + + ui_keyword = 1; + + DEBUG_printf(("2_ppdOpen: FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!", + keyword)); + + if (!group) + { + if ((group = ppd_get_group(ppd, "General", _("General"), cg, + encoding)) == NULL) + goto error; + + DEBUG_printf(("2_ppdOpen: Adding to group %s...", group->text)); + option = ppd_get_option(group, keyword); + group = NULL; + } + else + option = ppd_get_option(group, keyword); + + if (option == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + /* + * Now fill in the initial information for the option... + */ + + if (!strncmp(keyword, "JCL", 3)) + option->section = PPD_ORDER_JCL; + else + option->section = PPD_ORDER_ANY; + + option->order = 10.0f; + + if (i < 8) + option->ui = PPD_UI_BOOLEAN; + else + option->ui = PPD_UI_PICKONE; + + for (j = 0; j < ppd->num_attrs; j ++) + if (!strncmp(ppd->attrs[j]->name, "Default", 7) && + !strcmp(ppd->attrs[j]->name + 7, keyword) && + ppd->attrs[j]->value) + { + DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...", + option->keyword, ppd->attrs[j]->value)); + strlcpy(option->defchoice, ppd->attrs[j]->value, + sizeof(option->defchoice)); + break; + } + + if (!strcmp(keyword, "PageSize")) + strlcpy(option->text, _("Media Size"), sizeof(option->text)); + else if (!strcmp(keyword, "MediaType")) + strlcpy(option->text, _("Media Type"), sizeof(option->text)); + else if (!strcmp(keyword, "InputSlot")) + strlcpy(option->text, _("Media Source"), sizeof(option->text)); + else if (!strcmp(keyword, "ColorModel")) + strlcpy(option->text, _("Output Mode"), sizeof(option->text)); + else if (!strcmp(keyword, "Resolution")) + strlcpy(option->text, _("Resolution"), sizeof(option->text)); + else + strlcpy(option->text, keyword, sizeof(option->text)); + } + } + + if (!strcmp(keyword, "LanguageLevel")) + ppd->language_level = atoi(string); + else if (!strcmp(keyword, "LanguageEncoding")) + { + /* + * Say all PPD files are UTF-8, since we convert to UTF-8... + */ + + ppd->lang_encoding = _cupsStrAlloc("UTF-8"); + encoding = _ppdGetEncoding(string); + } + else if (!strcmp(keyword, "LanguageVersion")) + ppd->lang_version = string; + else if (!strcmp(keyword, "Manufacturer")) + ppd->manufacturer = string; + else if (!strcmp(keyword, "ModelName")) + ppd->modelname = string; + else if (!strcmp(keyword, "Protocols")) + ppd->protocols = string; + else if (!strcmp(keyword, "PCFileName")) + ppd->pcfilename = string; + else if (!strcmp(keyword, "NickName")) + { + if (encoding != CUPS_UTF8) + { + cups_utf8_t utf8[256]; /* UTF-8 version of NickName */ + + + cupsCharsetToUTF8(utf8, string, sizeof(utf8), encoding); + ppd->nickname = _cupsStrAlloc((char *)utf8); + } + else + ppd->nickname = _cupsStrAlloc(string); + } + else if (!strcmp(keyword, "Product")) + ppd->product = string; + else if (!strcmp(keyword, "ShortNickName")) + ppd->shortnickname = string; + else if (!strcmp(keyword, "TTRasterizer")) + ppd->ttrasterizer = string; + else if (!strcmp(keyword, "JCLBegin")) + { + ppd->jcl_begin = _cupsStrAlloc(string); + ppd_decode(ppd->jcl_begin); /* Decode quoted string */ + } + else if (!strcmp(keyword, "JCLEnd")) + { + ppd->jcl_end = _cupsStrAlloc(string); + ppd_decode(ppd->jcl_end); /* Decode quoted string */ + } + else if (!strcmp(keyword, "JCLToPSInterpreter")) + { + ppd->jcl_ps = _cupsStrAlloc(string); + ppd_decode(ppd->jcl_ps); /* Decode quoted string */ + } + else if (!strcmp(keyword, "AccurateScreensSupport")) + ppd->accurate_screens = !strcmp(string, "True"); + else if (!strcmp(keyword, "ColorDevice")) + ppd->color_device = !strcmp(string, "True"); + else if (!strcmp(keyword, "ContoneOnly")) + ppd->contone_only = !strcmp(string, "True"); + else if (!strcmp(keyword, "cupsFlipDuplex")) + ppd->flip_duplex = !strcmp(string, "True"); + else if (!strcmp(keyword, "cupsManualCopies")) + ppd->manual_copies = !strcmp(string, "True"); + else if (!strcmp(keyword, "cupsModelNumber")) + ppd->model_number = atoi(string); + else if (!strcmp(keyword, "cupsColorProfile")) + { + if (ppd->num_profiles == 0) + profile = malloc(sizeof(ppd_profile_t)); + else + profile = realloc(ppd->profiles, sizeof(ppd_profile_t) * + (ppd->num_profiles + 1)); + + if (!profile) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + ppd->profiles = profile; + profile += ppd->num_profiles; + ppd->num_profiles ++; + + memset(profile, 0, sizeof(ppd_profile_t)); + strlcpy(profile->resolution, name, sizeof(profile->resolution)); + strlcpy(profile->media_type, text, sizeof(profile->media_type)); + + profile->density = (float)_cupsStrScand(string, &sptr, loc); + profile->gamma = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[0][0] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[0][1] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[0][2] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[1][0] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[1][1] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[1][2] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[2][0] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[2][1] = (float)_cupsStrScand(sptr, &sptr, loc); + profile->matrix[2][2] = (float)_cupsStrScand(sptr, &sptr, loc); + } + else if (!strcmp(keyword, "cupsFilter")) + { + if (ppd->num_filters == 0) + filter = malloc(sizeof(char *)); + else + filter = realloc(ppd->filters, sizeof(char *) * (ppd->num_filters + 1)); + + if (filter == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + ppd->filters = filter; + filter += ppd->num_filters; + ppd->num_filters ++; + + /* + * Retain a copy of the filter string... + */ + + *filter = _cupsStrRetain(string); + } + else if (!strcmp(keyword, "Throughput")) + ppd->throughput = atoi(string); + else if (!strcmp(keyword, "Font")) + { + /* + * Add this font to the list of available fonts... + */ + + if (ppd->num_fonts == 0) + tempfonts = (char **)malloc(sizeof(char *)); + else + tempfonts = (char **)realloc(ppd->fonts, + sizeof(char *) * (ppd->num_fonts + 1)); + + if (tempfonts == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + ppd->fonts = tempfonts; + ppd->fonts[ppd->num_fonts] = _cupsStrAlloc(name); + ppd->num_fonts ++; + } + else if (!strncmp(keyword, "ParamCustom", 11)) + { + ppd_coption_t *coption; /* Custom option */ + ppd_cparam_t *cparam; /* Custom parameter */ + int corder; /* Order number */ + char ctype[33], /* Data type */ + cminimum[65], /* Minimum value */ + cmaximum[65]; /* Maximum value */ + + + /* + * Get the custom option and parameter... + */ + + if ((coption = ppd_get_coption(ppd, keyword + 11)) == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + if ((cparam = ppd_get_cparam(coption, name, text)) == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + /* + * Get the parameter data... + */ + + if (!string || + sscanf(string, "%d%32s%64s%64s", &corder, ctype, cminimum, + cmaximum) != 4) + { + cg->ppd_status = PPD_BAD_CUSTOM_PARAM; + + goto error; + } + + cparam->order = corder; + + if (!strcmp(ctype, "curve")) + { + cparam->type = PPD_CUSTOM_CURVE; + cparam->minimum.custom_curve = (float)_cupsStrScand(cminimum, NULL, loc); + cparam->maximum.custom_curve = (float)_cupsStrScand(cmaximum, NULL, loc); + } + else if (!strcmp(ctype, "int")) + { + cparam->type = PPD_CUSTOM_INT; + cparam->minimum.custom_int = atoi(cminimum); + cparam->maximum.custom_int = atoi(cmaximum); + } + else if (!strcmp(ctype, "invcurve")) + { + cparam->type = PPD_CUSTOM_INVCURVE; + cparam->minimum.custom_invcurve = (float)_cupsStrScand(cminimum, NULL, loc); + cparam->maximum.custom_invcurve = (float)_cupsStrScand(cmaximum, NULL, loc); + } + else if (!strcmp(ctype, "passcode")) + { + cparam->type = PPD_CUSTOM_PASSCODE; + cparam->minimum.custom_passcode = atoi(cminimum); + cparam->maximum.custom_passcode = atoi(cmaximum); + } + else if (!strcmp(ctype, "password")) + { + cparam->type = PPD_CUSTOM_PASSWORD; + cparam->minimum.custom_password = atoi(cminimum); + cparam->maximum.custom_password = atoi(cmaximum); + } + else if (!strcmp(ctype, "points")) + { + cparam->type = PPD_CUSTOM_POINTS; + cparam->minimum.custom_points = (float)_cupsStrScand(cminimum, NULL, loc); + cparam->maximum.custom_points = (float)_cupsStrScand(cmaximum, NULL, loc); + } + else if (!strcmp(ctype, "real")) + { + cparam->type = PPD_CUSTOM_REAL; + cparam->minimum.custom_real = (float)_cupsStrScand(cminimum, NULL, loc); + cparam->maximum.custom_real = (float)_cupsStrScand(cmaximum, NULL, loc); + } + else if (!strcmp(ctype, "string")) + { + cparam->type = PPD_CUSTOM_STRING; + cparam->minimum.custom_string = atoi(cminimum); + cparam->maximum.custom_string = atoi(cmaximum); + } + else + { + cg->ppd_status = PPD_BAD_CUSTOM_PARAM; + + goto error; + } + + /* + * Now special-case for CustomPageSize... + */ + + if (!strcmp(coption->keyword, "PageSize")) + { + if (!strcmp(name, "Width")) + { + ppd->custom_min[0] = cparam->minimum.custom_points; + ppd->custom_max[0] = cparam->maximum.custom_points; + } + else if (!strcmp(name, "Height")) + { + ppd->custom_min[1] = cparam->minimum.custom_points; + ppd->custom_max[1] = cparam->maximum.custom_points; + } + } + } + else if (!strcmp(keyword, "HWMargins")) + { + for (i = 0, sptr = string; i < 4; i ++) + ppd->custom_margins[i] = (float)_cupsStrScand(sptr, &sptr, loc); + } + else if (!strncmp(keyword, "Custom", 6) && !strcmp(name, "True") && !option) + { + ppd_option_t *custom_option; /* Custom option */ + + DEBUG_puts("2_ppdOpen: Processing Custom option..."); + + /* + * Get the option and custom option... + */ + + if (!ppd_get_coption(ppd, keyword + 6)) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + if (option && !_cups_strcasecmp(option->keyword, keyword + 6)) + custom_option = option; + else + custom_option = ppdFindOption(ppd, keyword + 6); + + if (custom_option) + { + /* + * Add the "custom" option... + */ + + if ((choice = ppdFindChoice(custom_option, "Custom")) == NULL) + if ((choice = ppd_add_choice(custom_option, "Custom")) == NULL) + { + DEBUG_puts("1_ppdOpen: Unable to add Custom choice!"); + + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + strlcpy(choice->text, text[0] ? text : _("Custom"), + sizeof(choice->text)); + + choice->code = _cupsStrAlloc(string); + + if (custom_option->section == PPD_ORDER_JCL) + ppd_decode(choice->code); + } + + /* + * Now process custom page sizes specially... + */ + + if (!strcmp(keyword, "CustomPageSize")) + { + /* + * Add a "Custom" page size entry... + */ + + ppd->variable_sizes = 1; + + ppd_add_size(ppd, "Custom"); + + if (option && !_cups_strcasecmp(option->keyword, "PageRegion")) + custom_option = option; + else + custom_option = ppdFindOption(ppd, "PageRegion"); + + if (custom_option) + { + if ((choice = ppdFindChoice(custom_option, "Custom")) == NULL) + if ((choice = ppd_add_choice(custom_option, "Custom")) == NULL) + { + DEBUG_puts("1_ppdOpen: Unable to add Custom choice!"); + + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + strlcpy(choice->text, text[0] ? text : _("Custom"), + sizeof(choice->text)); + } + } + } + else if (!strcmp(keyword, "LandscapeOrientation")) + { + if (!strcmp(string, "Minus90")) + ppd->landscape = -90; + else if (!strcmp(string, "Plus90")) + ppd->landscape = 90; + } + else if (!strcmp(keyword, "Emulators") && string) + { + for (count = 1, sptr = string; sptr != NULL;) + if ((sptr = strchr(sptr, ' ')) != NULL) + { + count ++; + while (*sptr == ' ') + sptr ++; + } + + ppd->num_emulations = count; + if ((ppd->emulations = calloc(count, sizeof(ppd_emul_t))) == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + for (i = 0, sptr = string; i < count; i ++) + { + for (nameptr = ppd->emulations[i].name; + *sptr != '\0' && *sptr != ' '; + sptr ++) + if (nameptr < (ppd->emulations[i].name + sizeof(ppd->emulations[i].name) - 1)) + *nameptr++ = *sptr; + + *nameptr = '\0'; + + while (*sptr == ' ') + sptr ++; + } + } + else if (!strncmp(keyword, "StartEmulator_", 14)) + { + ppd_decode(string); + + for (i = 0; i < ppd->num_emulations; i ++) + if (!strcmp(keyword + 14, ppd->emulations[i].name)) + { + ppd->emulations[i].start = string; + string = NULL; + } + } + else if (!strncmp(keyword, "StopEmulator_", 13)) + { + ppd_decode(string); + + for (i = 0; i < ppd->num_emulations; i ++) + if (!strcmp(keyword + 13, ppd->emulations[i].name)) + { + ppd->emulations[i].stop = string; + string = NULL; + } + } + else if (!strcmp(keyword, "JobPatchFile")) + { + /* + * CUPS STR #3421: Check for "*JobPatchFile: int: string" + */ + + if (isdigit(*string & 255)) + { + for (sptr = string + 1; isdigit(*sptr & 255); sptr ++); + + if (*sptr == ':') + { + /* + * Found "*JobPatchFile: int: string"... + */ + + cg->ppd_status = PPD_BAD_VALUE; + + goto error; + } + } + + if (!name[0] && cg->ppd_conform == PPD_CONFORM_STRICT) + { + /* + * Found "*JobPatchFile: string"... + */ + + cg->ppd_status = PPD_MISSING_OPTION_KEYWORD; + + goto error; + } + + if (ppd->patches == NULL) + ppd->patches = strdup(string); + else + { + temp = realloc(ppd->patches, strlen(ppd->patches) + + strlen(string) + 1); + if (temp == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + ppd->patches = temp; + + strcpy(ppd->patches + strlen(ppd->patches), string); + } + } + else if (!strcmp(keyword, "OpenUI")) + { + /* + * Don't allow nesting of options... + */ + + if (option && cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_NESTED_OPEN_UI; + + goto error; + } + + /* + * Add an option record to the current sub-group, group, or file... + */ + + DEBUG_printf(("2_ppdOpen: name=\"%s\" (%d)", name, (int)strlen(name))); + + if (name[0] == '*') + _cups_strcpy(name, name + 1); /* Eliminate leading asterisk */ + + for (i = (int)strlen(name) - 1; i > 0 && _cups_isspace(name[i]); i --) + name[i] = '\0'; /* Eliminate trailing spaces */ + + DEBUG_printf(("2_ppdOpen: OpenUI of %s in group %s...", name, + group ? group->text : "(null)")); + + if (subgroup != NULL) + option = ppd_get_option(subgroup, name); + else if (group == NULL) + { + if ((group = ppd_get_group(ppd, "General", _("General"), cg, + encoding)) == NULL) + goto error; + + DEBUG_printf(("2_ppdOpen: Adding to group %s...", group->text)); + option = ppd_get_option(group, name); + group = NULL; + } + else + option = ppd_get_option(group, name); + + if (option == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + /* + * Now fill in the initial information for the option... + */ + + if (string && !strcmp(string, "PickMany")) + option->ui = PPD_UI_PICKMANY; + else if (string && !strcmp(string, "Boolean")) + option->ui = PPD_UI_BOOLEAN; + else if (string && !strcmp(string, "PickOne")) + option->ui = PPD_UI_PICKONE; + else if (cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_BAD_OPEN_UI; + + goto error; + } + else + option->ui = PPD_UI_PICKONE; + + for (j = 0; j < ppd->num_attrs; j ++) + if (!strncmp(ppd->attrs[j]->name, "Default", 7) && + !strcmp(ppd->attrs[j]->name + 7, name) && + ppd->attrs[j]->value) + { + DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...", + option->keyword, ppd->attrs[j]->value)); + strlcpy(option->defchoice, ppd->attrs[j]->value, + sizeof(option->defchoice)); + break; + } + + if (text[0]) + cupsCharsetToUTF8((cups_utf8_t *)option->text, text, + sizeof(option->text), encoding); + else + { + if (!strcmp(name, "PageSize")) + strlcpy(option->text, _("Media Size"), sizeof(option->text)); + else if (!strcmp(name, "MediaType")) + strlcpy(option->text, _("Media Type"), sizeof(option->text)); + else if (!strcmp(name, "InputSlot")) + strlcpy(option->text, _("Media Source"), sizeof(option->text)); + else if (!strcmp(name, "ColorModel")) + strlcpy(option->text, _("Output Mode"), sizeof(option->text)); + else if (!strcmp(name, "Resolution")) + strlcpy(option->text, _("Resolution"), sizeof(option->text)); + else + strlcpy(option->text, name, sizeof(option->text)); + } + + option->section = PPD_ORDER_ANY; + + _cupsStrFree(string); + string = NULL; + + /* + * Add a custom option choice if we have already seen a CustomFoo + * attribute... + */ + + if (!_cups_strcasecmp(name, "PageRegion")) + strcpy(custom_name, "CustomPageSize"); + else + snprintf(custom_name, sizeof(custom_name), "Custom%s", name); + + if ((custom_attr = ppdFindAttr(ppd, custom_name, "True")) != NULL) + { + if ((choice = ppdFindChoice(option, "Custom")) == NULL) + if ((choice = ppd_add_choice(option, "Custom")) == NULL) + { + DEBUG_puts("1_ppdOpen: Unable to add Custom choice!"); + + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + strlcpy(choice->text, + custom_attr->text[0] ? custom_attr->text : _("Custom"), + sizeof(choice->text)); + choice->code = _cupsStrRetain(custom_attr->value); + } + } + else if (!strcmp(keyword, "JCLOpenUI")) + { + /* + * Don't allow nesting of options... + */ + + if (option && cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_NESTED_OPEN_UI; + + goto error; + } + + /* + * Find the JCL group, and add if needed... + */ + + group = ppd_get_group(ppd, "JCL", _("JCL"), cg, encoding); + + if (group == NULL) + goto error; + + /* + * Add an option record to the current JCLs... + */ + + if (name[0] == '*') + _cups_strcpy(name, name + 1); + + option = ppd_get_option(group, name); + + if (option == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + /* + * Now fill in the initial information for the option... + */ + + if (string && !strcmp(string, "PickMany")) + option->ui = PPD_UI_PICKMANY; + else if (string && !strcmp(string, "Boolean")) + option->ui = PPD_UI_BOOLEAN; + else if (string && !strcmp(string, "PickOne")) + option->ui = PPD_UI_PICKONE; + else + { + cg->ppd_status = PPD_BAD_OPEN_UI; + + goto error; + } + + for (j = 0; j < ppd->num_attrs; j ++) + if (!strncmp(ppd->attrs[j]->name, "Default", 7) && + !strcmp(ppd->attrs[j]->name + 7, name) && + ppd->attrs[j]->value) + { + DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...", + option->keyword, ppd->attrs[j]->value)); + strlcpy(option->defchoice, ppd->attrs[j]->value, + sizeof(option->defchoice)); + break; + } + + if (text[0]) + cupsCharsetToUTF8((cups_utf8_t *)option->text, text, + sizeof(option->text), encoding); + else + strlcpy(option->text, name, sizeof(option->text)); + + option->section = PPD_ORDER_JCL; + group = NULL; + + _cupsStrFree(string); + string = NULL; + + /* + * Add a custom option choice if we have already seen a CustomFoo + * attribute... + */ + + snprintf(custom_name, sizeof(custom_name), "Custom%s", name); + + if ((custom_attr = ppdFindAttr(ppd, custom_name, "True")) != NULL) + { + if ((choice = ppd_add_choice(option, "Custom")) == NULL) + { + DEBUG_puts("1_ppdOpen: Unable to add Custom choice!"); + + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + strlcpy(choice->text, + custom_attr->text[0] ? custom_attr->text : _("Custom"), + sizeof(choice->text)); + choice->code = _cupsStrRetain(custom_attr->value); + } + } + else if (!strcmp(keyword, "CloseUI") || !strcmp(keyword, "JCLCloseUI")) + { + option = NULL; + + _cupsStrFree(string); + string = NULL; + } + else if (!strcmp(keyword, "OpenGroup")) + { + /* + * Open a new group... + */ + + if (group != NULL) + { + cg->ppd_status = PPD_NESTED_OPEN_GROUP; + + goto error; + } + + if (!string) + { + cg->ppd_status = PPD_BAD_OPEN_GROUP; + + goto error; + } + + /* + * Separate the group name from the text (name/text)... + */ + + if ((sptr = strchr(string, '/')) != NULL) + *sptr++ = '\0'; + else + sptr = string; + + /* + * Fix up the text... + */ + + ppd_decode(sptr); + + /* + * Find/add the group... + */ + + group = ppd_get_group(ppd, string, sptr, cg, encoding); + + if (group == NULL) + goto error; + + _cupsStrFree(string); + string = NULL; + } + else if (!strcmp(keyword, "CloseGroup")) + { + group = NULL; + + _cupsStrFree(string); + string = NULL; + } + else if (!strcmp(keyword, "OrderDependency")) + { + order = (float)_cupsStrScand(string, &sptr, loc); + + if (!sptr || sscanf(sptr, "%40s%40s", name, keyword) != 2) + { + cg->ppd_status = PPD_BAD_ORDER_DEPENDENCY; + + goto error; + } + + if (keyword[0] == '*') + _cups_strcpy(keyword, keyword + 1); + + if (!strcmp(name, "ExitServer")) + section = PPD_ORDER_EXIT; + else if (!strcmp(name, "Prolog")) + section = PPD_ORDER_PROLOG; + else if (!strcmp(name, "DocumentSetup")) + section = PPD_ORDER_DOCUMENT; + else if (!strcmp(name, "PageSetup")) + section = PPD_ORDER_PAGE; + else if (!strcmp(name, "JCLSetup")) + section = PPD_ORDER_JCL; + else + section = PPD_ORDER_ANY; + + if (option == NULL) + { + ppd_group_t *gtemp; + + + /* + * Only valid for Non-UI options... + */ + + for (i = ppd->num_groups, gtemp = ppd->groups; i > 0; i --, gtemp ++) + if (gtemp->text[0] == '\0') + break; + + if (i > 0) + for (i = 0; i < gtemp->num_options; i ++) + if (!strcmp(keyword, gtemp->options[i].keyword)) + { + gtemp->options[i].section = section; + gtemp->options[i].order = order; + break; + } + } + else + { + option->section = section; + option->order = order; + } + + _cupsStrFree(string); + string = NULL; + } + else if (!strncmp(keyword, "Default", 7)) + { + if (string == NULL) + continue; + + /* + * Drop UI text, if any, from value... + */ + + if (strchr(string, '/') != NULL) + *strchr(string, '/') = '\0'; + + /* + * Assign the default value as appropriate... + */ + + if (!strcmp(keyword, "DefaultColorSpace")) + { + /* + * Set default colorspace... + */ + + if (!strcmp(string, "CMY")) + ppd->colorspace = PPD_CS_CMY; + else if (!strcmp(string, "CMYK")) + ppd->colorspace = PPD_CS_CMYK; + else if (!strcmp(string, "RGB")) + ppd->colorspace = PPD_CS_RGB; + else if (!strcmp(string, "RGBK")) + ppd->colorspace = PPD_CS_RGBK; + else if (!strcmp(string, "N")) + ppd->colorspace = PPD_CS_N; + else + ppd->colorspace = PPD_CS_GRAY; + } + else if (option && !strcmp(keyword + 7, option->keyword)) + { + /* + * Set the default as part of the current option... + */ + + DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword, string)); + + strlcpy(option->defchoice, string, sizeof(option->defchoice)); + + DEBUG_printf(("2_ppdOpen: %s is now %s...", keyword, option->defchoice)); + } + else + { + /* + * Lookup option and set if it has been defined... + */ + + ppd_option_t *toption; /* Temporary option */ + + + if ((toption = ppdFindOption(ppd, keyword + 7)) != NULL) + { + DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword, string)); + strlcpy(toption->defchoice, string, sizeof(toption->defchoice)); + } + } + } + else if (!strcmp(keyword, "UIConstraints") || + !strcmp(keyword, "NonUIConstraints")) + { + if (!string) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + if (ppd->num_consts == 0) + constraint = calloc(2, sizeof(ppd_const_t)); + else + constraint = realloc(ppd->consts, + (ppd->num_consts + 2) * sizeof(ppd_const_t)); + + if (constraint == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + ppd->consts = constraint; + constraint += ppd->num_consts; + ppd->num_consts ++; + + switch (sscanf(string, "%40s%40s%40s%40s", constraint->option1, + constraint->choice1, constraint->option2, + constraint->choice2)) + { + case 0 : /* Error */ + case 1 : /* Error */ + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + + case 2 : /* Two options... */ + /* + * Check for broken constraints like "* Option"... + */ + + if (cg->ppd_conform == PPD_CONFORM_STRICT && + (!strcmp(constraint->option1, "*") || + !strcmp(constraint->choice1, "*"))) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + /* + * The following strcpy's are safe, as optionN and + * choiceN are all the same size (size defined by PPD spec...) + */ + + if (constraint->option1[0] == '*') + _cups_strcpy(constraint->option1, constraint->option1 + 1); + else if (cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + if (constraint->choice1[0] == '*') + _cups_strcpy(constraint->option2, constraint->choice1 + 1); + else if (cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + constraint->choice1[0] = '\0'; + constraint->choice2[0] = '\0'; + break; + + case 3 : /* Two options, one choice... */ + /* + * Check for broken constraints like "* Option"... + */ + + if (cg->ppd_conform == PPD_CONFORM_STRICT && + (!strcmp(constraint->option1, "*") || + !strcmp(constraint->choice1, "*") || + !strcmp(constraint->option2, "*"))) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + /* + * The following _cups_strcpy's are safe, as optionN and + * choiceN are all the same size (size defined by PPD spec...) + */ + + if (constraint->option1[0] == '*') + _cups_strcpy(constraint->option1, constraint->option1 + 1); + else if (cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + if (constraint->choice1[0] == '*') + { + if (cg->ppd_conform == PPD_CONFORM_STRICT && + constraint->option2[0] == '*') + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + _cups_strcpy(constraint->choice2, constraint->option2); + _cups_strcpy(constraint->option2, constraint->choice1 + 1); + constraint->choice1[0] = '\0'; + } + else + { + if (constraint->option2[0] == '*') + _cups_strcpy(constraint->option2, constraint->option2 + 1); + else if (cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + constraint->choice2[0] = '\0'; + } + break; + + case 4 : /* Two options, two choices... */ + /* + * Check for broken constraints like "* Option"... + */ + + if (cg->ppd_conform == PPD_CONFORM_STRICT && + (!strcmp(constraint->option1, "*") || + !strcmp(constraint->choice1, "*") || + !strcmp(constraint->option2, "*") || + !strcmp(constraint->choice2, "*"))) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + if (constraint->option1[0] == '*') + _cups_strcpy(constraint->option1, constraint->option1 + 1); + else if (cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + if (cg->ppd_conform == PPD_CONFORM_STRICT && + constraint->choice1[0] == '*') + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + if (constraint->option2[0] == '*') + _cups_strcpy(constraint->option2, constraint->option2 + 1); + else if (cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + + if (cg->ppd_conform == PPD_CONFORM_STRICT && + constraint->choice2[0] == '*') + { + cg->ppd_status = PPD_BAD_UI_CONSTRAINTS; + goto error; + } + break; + } + + /* + * Don't add this one as an attribute... + */ + + _cupsStrFree(string); + string = NULL; + } + else if (!strcmp(keyword, "PaperDimension")) + { + if ((size = ppdPageSize(ppd, name)) == NULL) + size = ppd_add_size(ppd, name); + + if (size == NULL) + { + /* + * Unable to add or find size! + */ + + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + size->width = (float)_cupsStrScand(string, &sptr, loc); + size->length = (float)_cupsStrScand(sptr, NULL, loc); + + _cupsStrFree(string); + string = NULL; + } + else if (!strcmp(keyword, "ImageableArea")) + { + if ((size = ppdPageSize(ppd, name)) == NULL) + size = ppd_add_size(ppd, name); + + if (size == NULL) + { + /* + * Unable to add or find size! + */ + + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + size->left = (float)_cupsStrScand(string, &sptr, loc); + size->bottom = (float)_cupsStrScand(sptr, &sptr, loc); + size->right = (float)_cupsStrScand(sptr, &sptr, loc); + size->top = (float)_cupsStrScand(sptr, NULL, loc); + + _cupsStrFree(string); + string = NULL; + } + else if (option != NULL && + (mask & (PPD_KEYWORD | PPD_OPTION | PPD_STRING)) == + (PPD_KEYWORD | PPD_OPTION | PPD_STRING) && + !strcmp(keyword, option->keyword)) + { + DEBUG_printf(("2_ppdOpen: group=%p, subgroup=%p", group, subgroup)); + + if (!strcmp(keyword, "PageSize")) + { + /* + * Add a page size... + */ + + if (ppdPageSize(ppd, name) == NULL) + ppd_add_size(ppd, name); + } + + /* + * Add the option choice... + */ + + if ((choice = ppd_add_choice(option, name)) == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + goto error; + } + + if (text[0]) + cupsCharsetToUTF8((cups_utf8_t *)choice->text, text, + sizeof(choice->text), encoding); + else if (!strcmp(name, "True")) + strcpy(choice->text, _("Yes")); + else if (!strcmp(name, "False")) + strcpy(choice->text, _("No")); + else + strlcpy(choice->text, name, sizeof(choice->text)); + + if (option->section == PPD_ORDER_JCL) + ppd_decode(string); /* Decode quoted string */ + + choice->code = string; + string = NULL; /* Don't add as an attribute below */ + } + + /* + * Add remaining lines with keywords and string values as attributes... + */ + + if (string && + (mask & (PPD_KEYWORD | PPD_STRING)) == (PPD_KEYWORD | PPD_STRING)) + ppd_add_attr(ppd, keyword, name, text, string); + else + _cupsStrFree(string); + } + + /* + * Check for a missing CloseGroup... + */ + + if (group && cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_MISSING_CLOSE_GROUP; + goto error; + } + + ppd_free(line.buffer); + + /* + * Reset language preferences... + */ + +#ifdef DEBUG + if (!cupsFileEOF(fp)) + DEBUG_printf(("1_ppdOpen: Premature EOF at %lu...\n", + (unsigned long)cupsFileTell(fp))); +#endif /* DEBUG */ + + if (cg->ppd_status != PPD_OK) + { + /* + * Had an error reading the PPD file, cannot continue! + */ + + ppdClose(ppd); + + return (NULL); + } + + /* + * Update the filters array as needed... + */ + + if (!ppd_update_filters(ppd, cg)) + { + ppdClose(ppd); + + return (NULL); + } + + /* + * Create the sorted options array and set the option back-pointer for + * each choice and custom option... + */ + + ppd->options = cupsArrayNew2((cups_array_func_t)ppd_compare_options, NULL, + (cups_ahash_func_t)ppd_hash_option, + PPD_HASHSIZE); + + for (i = ppd->num_groups, group = ppd->groups; + i > 0; + i --, group ++) + { + for (j = group->num_options, option = group->options; + j > 0; + j --, option ++) + { + ppd_coption_t *coption; /* Custom option */ + + + cupsArrayAdd(ppd->options, option); + + for (k = 0; k < option->num_choices; k ++) + option->choices[k].option = option; + + if ((coption = ppdFindCustomOption(ppd, option->keyword)) != NULL) + coption->option = option; + } + } + + /* + * Create an array to track the marked choices... + */ + + ppd->marked = cupsArrayNew((cups_array_func_t)ppd_compare_choices, NULL); + + /* + * Return the PPD file structure... + */ + + return (ppd); + + /* + * Common exit point for errors to save code size... + */ + + error: + + _cupsStrFree(string); + ppd_free(line.buffer); + + ppdClose(ppd); + + return (NULL); +} + + +/* + * 'ppdOpen()' - Read a PPD file into memory. + */ + +ppd_file_t * /* O - PPD file record */ +ppdOpen(FILE *fp) /* I - File to read from */ +{ + ppd_file_t *ppd; /* PPD file record */ + cups_file_t *cf; /* CUPS file */ + + + /* + * Reopen the stdio file as a CUPS file... + */ + + if ((cf = cupsFileOpenFd(fileno(fp), "r")) == NULL) + return (NULL); + + /* + * Load the PPD file using the newer API... + */ + + ppd = _ppdOpen(cf, _PPD_LOCALIZATION_DEFAULT); + + /* + * Close the CUPS file and return the PPD... + */ + + cupsFileClose(cf); + + return (ppd); +} + + +/* + * 'ppdOpen2()' - Read a PPD file into memory. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +ppd_file_t * /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */ +ppdOpen2(cups_file_t *fp) /* I - File to read from */ +{ + return _ppdOpen(fp, _PPD_LOCALIZATION_DEFAULT); +} + + +/* + * 'ppdOpenFd()' - Read a PPD file into memory. + */ + +ppd_file_t * /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */ +ppdOpenFd(int fd) /* I - File to read from */ +{ + cups_file_t *fp; /* CUPS file pointer */ + ppd_file_t *ppd; /* PPD file record */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + /* + * Set the line number to 0... + */ + + cg->ppd_line = 0; + + /* + * Range check input... + */ + + if (fd < 0) + { + cg->ppd_status = PPD_NULL_FILE; + + return (NULL); + } + + /* + * Try to open the file and parse it... + */ + + if ((fp = cupsFileOpenFd(fd, "r")) != NULL) + { + ppd = ppdOpen2(fp); + + cupsFileClose(fp); + } + else + { + cg->ppd_status = PPD_FILE_OPEN_ERROR; + ppd = NULL; + } + + return (ppd); +} + + +/* + * '_ppdOpenFile()' - Read a PPD file into memory. + */ + +ppd_file_t * /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */ +_ppdOpenFile(const char *filename, /* I - File to read from */ + _ppd_localization_t localization) /* I - Localization to load */ +{ + cups_file_t *fp; /* File pointer */ + ppd_file_t *ppd; /* PPD file record */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + /* + * Set the line number to 0... + */ + + cg->ppd_line = 0; + + /* + * Range check input... + */ + + if (filename == NULL) + { + cg->ppd_status = PPD_NULL_FILE; + + return (NULL); + } + + /* + * Try to open the file and parse it... + */ + + if ((fp = cupsFileOpen(filename, "r")) != NULL) + { + ppd = _ppdOpen(fp, localization); + + cupsFileClose(fp); + } + else + { + cg->ppd_status = PPD_FILE_OPEN_ERROR; + ppd = NULL; + } + + return (ppd); +} + + +/* + * 'ppdOpenFile()' - Read a PPD file into memory. + */ + +ppd_file_t * /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */ +ppdOpenFile(const char *filename) /* I - File to read from */ +{ + return _ppdOpenFile(filename, _PPD_LOCALIZATION_DEFAULT); +} + + +/* + * 'ppdSetConformance()' - Set the conformance level for PPD files. + * + * @since CUPS 1.1.20/OS X 10.4@ + */ + +void +ppdSetConformance(ppd_conform_t c) /* I - Conformance level */ +{ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ + + + cg->ppd_conform = c; +} + + +/* + * 'ppd_add_attr()' - Add an attribute to the PPD data. + */ + +static ppd_attr_t * /* O - New attribute */ +ppd_add_attr(ppd_file_t *ppd, /* I - PPD file data */ + const char *name, /* I - Attribute name */ + const char *spec, /* I - Specifier string, if any */ + const char *text, /* I - Text string, if any */ + const char *value) /* I - Value of attribute */ +{ + ppd_attr_t **ptr, /* New array */ + *temp; /* New attribute */ + + + /* + * Range check input... + */ + + if (ppd == NULL || name == NULL || spec == NULL) + return (NULL); + + /* + * Create the array as needed... + */ + + if (!ppd->sorted_attrs) + ppd->sorted_attrs = cupsArrayNew((cups_array_func_t)ppd_compare_attrs, + NULL); + + /* + * Allocate memory for the new attribute... + */ + + if (ppd->num_attrs == 0) + ptr = malloc(sizeof(ppd_attr_t *)); + else + ptr = realloc(ppd->attrs, (ppd->num_attrs + 1) * sizeof(ppd_attr_t *)); + + if (ptr == NULL) + return (NULL); + + ppd->attrs = ptr; + ptr += ppd->num_attrs; + + if ((temp = calloc(1, sizeof(ppd_attr_t))) == NULL) + return (NULL); + + *ptr = temp; + + ppd->num_attrs ++; + + /* + * Copy data over... + */ + + strlcpy(temp->name, name, sizeof(temp->name)); + strlcpy(temp->spec, spec, sizeof(temp->spec)); + strlcpy(temp->text, text, sizeof(temp->text)); + temp->value = (char *)value; + + /* + * Add the attribute to the sorted array... + */ + + cupsArrayAdd(ppd->sorted_attrs, temp); + + /* + * Return the attribute... + */ + + return (temp); +} + + +/* + * 'ppd_add_choice()' - Add a choice to an option. + */ + +static ppd_choice_t * /* O - Named choice */ +ppd_add_choice(ppd_option_t *option, /* I - Option */ + const char *name) /* I - Name of choice */ +{ + ppd_choice_t *choice; /* Choice */ + + + if (option->num_choices == 0) + choice = malloc(sizeof(ppd_choice_t)); + else + choice = realloc(option->choices, + sizeof(ppd_choice_t) * (option->num_choices + 1)); + + if (choice == NULL) + return (NULL); + + option->choices = choice; + choice += option->num_choices; + option->num_choices ++; + + memset(choice, 0, sizeof(ppd_choice_t)); + strlcpy(choice->choice, name, sizeof(choice->choice)); + + return (choice); +} + + +/* + * 'ppd_add_size()' - Add a page size. + */ + +static ppd_size_t * /* O - Named size */ +ppd_add_size(ppd_file_t *ppd, /* I - PPD file */ + const char *name) /* I - Name of size */ +{ + ppd_size_t *size; /* Size */ + + + if (ppd->num_sizes == 0) + size = malloc(sizeof(ppd_size_t)); + else + size = realloc(ppd->sizes, sizeof(ppd_size_t) * (ppd->num_sizes + 1)); + + if (size == NULL) + return (NULL); + + ppd->sizes = size; + size += ppd->num_sizes; + ppd->num_sizes ++; + + memset(size, 0, sizeof(ppd_size_t)); + strlcpy(size->name, name, sizeof(size->name)); + + return (size); +} + + +/* + * 'ppd_compare_attrs()' - Compare two attributes. + */ + +static int /* O - Result of comparison */ +ppd_compare_attrs(ppd_attr_t *a, /* I - First attribute */ + ppd_attr_t *b) /* I - Second attribute */ +{ + return (_cups_strcasecmp(a->name, b->name)); +} + + +/* + * 'ppd_compare_choices()' - Compare two choices... + */ + +static int /* O - Result of comparison */ +ppd_compare_choices(ppd_choice_t *a, /* I - First choice */ + ppd_choice_t *b) /* I - Second choice */ +{ + return (strcmp(a->option->keyword, b->option->keyword)); +} + + +/* + * 'ppd_compare_coptions()' - Compare two custom options. + */ + +static int /* O - Result of comparison */ +ppd_compare_coptions(ppd_coption_t *a, /* I - First option */ + ppd_coption_t *b) /* I - Second option */ +{ + return (_cups_strcasecmp(a->keyword, b->keyword)); +} + + +/* + * 'ppd_compare_options()' - Compare two options. + */ + +static int /* O - Result of comparison */ +ppd_compare_options(ppd_option_t *a, /* I - First option */ + ppd_option_t *b) /* I - Second option */ +{ + return (_cups_strcasecmp(a->keyword, b->keyword)); +} + + +/* + * 'ppd_decode()' - Decode a string value... + */ + +static int /* O - Length of decoded string */ +ppd_decode(char *string) /* I - String to decode */ +{ + char *inptr, /* Input pointer */ + *outptr; /* Output pointer */ + + + inptr = string; + outptr = string; + + while (*inptr != '\0') + if (*inptr == '<' && isxdigit(inptr[1] & 255)) + { + /* + * Convert hex to 8-bit values... + */ + + inptr ++; + while (isxdigit(*inptr & 255)) + { + if (_cups_isalpha(*inptr)) + *outptr = (tolower(*inptr) - 'a' + 10) << 4; + else + *outptr = (*inptr - '0') << 4; + + inptr ++; + + if (!isxdigit(*inptr & 255)) + break; + + if (_cups_isalpha(*inptr)) + *outptr |= tolower(*inptr) - 'a' + 10; + else + *outptr |= *inptr - '0'; + + inptr ++; + outptr ++; + } + + while (*inptr != '>' && *inptr != '\0') + inptr ++; + while (*inptr == '>') + inptr ++; + } + else + *outptr++ = *inptr++; + + *outptr = '\0'; + + return ((int)(outptr - string)); +} + + +/* + * 'ppd_free_filters()' - Free the filters array. + */ + +static void +ppd_free_filters(ppd_file_t *ppd) /* I - PPD file */ +{ + int i; /* Looping var */ + char **filter; /* Current filter */ + + + if (ppd->num_filters > 0) + { + for (i = ppd->num_filters, filter = ppd->filters; i > 0; i --, filter ++) + _cupsStrFree(*filter); + + ppd_free(ppd->filters); + + ppd->num_filters = 0; + ppd->filters = NULL; + } +} + + +/* + * 'ppd_free_group()' - Free a single UI group. + */ + +static void +ppd_free_group(ppd_group_t *group) /* I - Group to free */ +{ + int i; /* Looping var */ + ppd_option_t *option; /* Current option */ + ppd_group_t *subgroup; /* Current sub-group */ + + + if (group->num_options > 0) + { + for (i = group->num_options, option = group->options; + i > 0; + i --, option ++) + ppd_free_option(option); + + ppd_free(group->options); + } + + if (group->num_subgroups > 0) + { + for (i = group->num_subgroups, subgroup = group->subgroups; + i > 0; + i --, subgroup ++) + ppd_free_group(subgroup); + + ppd_free(group->subgroups); + } +} + + +/* + * 'ppd_free_option()' - Free a single option. + */ + +static void +ppd_free_option(ppd_option_t *option) /* I - Option to free */ +{ + int i; /* Looping var */ + ppd_choice_t *choice; /* Current choice */ + + + if (option->num_choices > 0) + { + for (i = option->num_choices, choice = option->choices; + i > 0; + i --, choice ++) + { + _cupsStrFree(choice->code); + } + + ppd_free(option->choices); + } +} + + +/* + * 'ppd_get_coption()' - Get a custom option record. + */ + +static ppd_coption_t * /* O - Custom option... */ +ppd_get_coption(ppd_file_t *ppd, /* I - PPD file */ + const char *name) /* I - Name of option */ +{ + ppd_coption_t *copt; /* New custom option */ + + + /* + * See if the option already exists... + */ + + if ((copt = ppdFindCustomOption(ppd, name)) != NULL) + return (copt); + + /* + * Not found, so create the custom option record... + */ + + if ((copt = calloc(1, sizeof(ppd_coption_t))) == NULL) + return (NULL); + + strlcpy(copt->keyword, name, sizeof(copt->keyword)); + + copt->params = cupsArrayNew((cups_array_func_t)NULL, NULL); + + cupsArrayAdd(ppd->coptions, copt); + + /* + * Return the new record... + */ + + return (copt); +} + + +/* + * 'ppd_get_cparam()' - Get a custom parameter record. + */ + +static ppd_cparam_t * /* O - Extended option... */ +ppd_get_cparam(ppd_coption_t *opt, /* I - PPD file */ + const char *param, /* I - Name of parameter */ + const char *text) /* I - Human-readable text */ +{ + ppd_cparam_t *cparam; /* New custom parameter */ + + + /* + * See if the parameter already exists... + */ + + if ((cparam = ppdFindCustomParam(opt, param)) != NULL) + return (cparam); + + /* + * Not found, so create the custom parameter record... + */ + + if ((cparam = calloc(1, sizeof(ppd_cparam_t))) == NULL) + return (NULL); + + strlcpy(cparam->name, param, sizeof(cparam->name)); + strlcpy(cparam->text, text[0] ? text : param, sizeof(cparam->text)); + + /* + * Add this record to the array... + */ + + cupsArrayAdd(opt->params, cparam); + + /* + * Return the new record... + */ + + return (cparam); +} + + +/* + * 'ppd_get_group()' - Find or create the named group as needed. + */ + +static ppd_group_t * /* O - Named group */ +ppd_get_group(ppd_file_t *ppd, /* I - PPD file */ + const char *name, /* I - Name of group */ + const char *text, /* I - Text for group */ + _cups_globals_t *cg, /* I - Global data */ + cups_encoding_t encoding) /* I - Encoding of text */ +{ + int i; /* Looping var */ + ppd_group_t *group; /* Group */ + + + DEBUG_printf(("7ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)", + ppd, name, text, cg)); + + for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++) + if (!strcmp(group->name, name)) + break; + + if (i == 0) + { + DEBUG_printf(("8ppd_get_group: Adding group %s...", name)); + + if (cg->ppd_conform == PPD_CONFORM_STRICT && strlen(text) >= sizeof(group->text)) + { + cg->ppd_status = PPD_ILLEGAL_TRANSLATION; + + return (NULL); + } + + if (ppd->num_groups == 0) + group = malloc(sizeof(ppd_group_t)); + else + group = realloc(ppd->groups, + (ppd->num_groups + 1) * sizeof(ppd_group_t)); + + if (group == NULL) + { + cg->ppd_status = PPD_ALLOC_ERROR; + + return (NULL); + } + + ppd->groups = group; + group += ppd->num_groups; + ppd->num_groups ++; + + memset(group, 0, sizeof(ppd_group_t)); + strlcpy(group->name, name, sizeof(group->name)); + + cupsCharsetToUTF8((cups_utf8_t *)group->text, text, + sizeof(group->text), encoding); + } + + return (group); +} + + +/* + * 'ppd_get_option()' - Find or create the named option as needed. + */ + +static ppd_option_t * /* O - Named option */ +ppd_get_option(ppd_group_t *group, /* I - Group */ + const char *name) /* I - Name of option */ +{ + int i; /* Looping var */ + ppd_option_t *option; /* Option */ + + + DEBUG_printf(("7ppd_get_option(group=%p(\"%s\"), name=\"%s\")", + group, group->name, name)); + + for (i = group->num_options, option = group->options; i > 0; i --, option ++) + if (!strcmp(option->keyword, name)) + break; + + if (i == 0) + { + if (group->num_options == 0) + option = malloc(sizeof(ppd_option_t)); + else + option = realloc(group->options, + (group->num_options + 1) * sizeof(ppd_option_t)); + + if (option == NULL) + return (NULL); + + group->options = option; + option += group->num_options; + group->num_options ++; + + memset(option, 0, sizeof(ppd_option_t)); + strlcpy(option->keyword, name, sizeof(option->keyword)); + } + + return (option); +} + + +/* + * 'ppd_hash_option()' - Generate a hash of the option name... + */ + +static int /* O - Hash index */ +ppd_hash_option(ppd_option_t *option) /* I - Option */ +{ + int hash = 0; /* Hash index */ + const char *k; /* Pointer into keyword */ + + + for (hash = option->keyword[0], k = option->keyword + 1; *k;) + hash = 33 * hash + *k++; + + return (hash & 511); +} + + +/* + * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as + * necessary. + */ + +static int /* O - Bitmask of fields read */ +ppd_read(cups_file_t *fp, /* I - File to read from */ + _ppd_line_t *line, /* I - Line buffer */ + char *keyword, /* O - Keyword from line */ + char *option, /* O - Option from line */ + char *text, /* O - Human-readable text from line */ + char **string, /* O - Code/string data */ + int ignoreblank, /* I - Ignore blank lines? */ + _cups_globals_t *cg) /* I - Global data */ +{ + int ch, /* Character from file */ + col, /* Column in line */ + colon, /* Colon seen? */ + endquote, /* Waiting for an end quote */ + mask, /* Mask to be returned */ + startline, /* Start line */ + textlen; /* Length of text */ + char *keyptr, /* Keyword pointer */ + *optptr, /* Option pointer */ + *textptr, /* Text pointer */ + *strptr, /* Pointer into string */ + *lineptr; /* Current position in line buffer */ + + + /* + * Now loop until we have a valid line... + */ + + *string = NULL; + col = 0; + startline = cg->ppd_line + 1; + + if (!line->buffer) + { + line->bufsize = 1024; + line->buffer = malloc(1024); + + if (!line->buffer) + return (0); + } + + do + { + /* + * Read the line... + */ + + lineptr = line->buffer; + endquote = 0; + colon = 0; + + while ((ch = cupsFileGetChar(fp)) != EOF) + { + if (lineptr >= (line->buffer + line->bufsize - 1)) + { + /* + * Expand the line buffer... + */ + + char *temp; /* Temporary line pointer */ + + + line->bufsize += 1024; + if (line->bufsize > 262144) + { + /* + * Don't allow lines longer than 256k! + */ + + cg->ppd_line = startline; + cg->ppd_status = PPD_LINE_TOO_LONG; + + return (0); + } + + temp = realloc(line->buffer, line->bufsize); + if (!temp) + { + cg->ppd_line = startline; + cg->ppd_status = PPD_LINE_TOO_LONG; + + return (0); + } + + lineptr = temp + (lineptr - line->buffer); + line->buffer = temp; + } + + if (ch == '\r' || ch == '\n') + { + /* + * Line feed or carriage return... + */ + + cg->ppd_line ++; + col = 0; + + if (ch == '\r') + { + /* + * Check for a trailing line feed... + */ + + if ((ch = cupsFilePeekChar(fp)) == EOF) + { + ch = '\n'; + break; + } + + if (ch == 0x0a) + cupsFileGetChar(fp); + } + + if (lineptr == line->buffer && ignoreblank) + continue; /* Skip blank lines */ + + ch = '\n'; + + if (!endquote) /* Continue for multi-line text */ + break; + + *lineptr++ = '\n'; + } + else if (ch < ' ' && ch != '\t' && cg->ppd_conform == PPD_CONFORM_STRICT) + { + /* + * Other control characters... + */ + + cg->ppd_line = startline; + cg->ppd_status = PPD_ILLEGAL_CHARACTER; + + return (0); + } + else if (ch != 0x1a) + { + /* + * Any other character... + */ + + *lineptr++ = ch; + col ++; + + if (col > (PPD_MAX_LINE - 1)) + { + /* + * Line is too long... + */ + + cg->ppd_line = startline; + cg->ppd_status = PPD_LINE_TOO_LONG; + + return (0); + } + + if (ch == ':' && strncmp(line->buffer, "*%", 2) != 0) + colon = 1; + + if (ch == '\"' && colon) + endquote = !endquote; + } + } + + if (endquote) + { + /* + * Didn't finish this quoted string... + */ + + while ((ch = cupsFileGetChar(fp)) != EOF) + if (ch == '\"') + break; + else if (ch == '\r' || ch == '\n') + { + cg->ppd_line ++; + col = 0; + + if (ch == '\r') + { + /* + * Check for a trailing line feed... + */ + + if ((ch = cupsFilePeekChar(fp)) == EOF) + break; + if (ch == 0x0a) + cupsFileGetChar(fp); + } + } + else if (ch < ' ' && ch != '\t' && cg->ppd_conform == PPD_CONFORM_STRICT) + { + /* + * Other control characters... + */ + + cg->ppd_line = startline; + cg->ppd_status = PPD_ILLEGAL_CHARACTER; + + return (0); + } + else if (ch != 0x1a) + { + col ++; + + if (col > (PPD_MAX_LINE - 1)) + { + /* + * Line is too long... + */ + + cg->ppd_line = startline; + cg->ppd_status = PPD_LINE_TOO_LONG; + + return (0); + } + } + } + + if (ch != '\n') + { + /* + * Didn't finish this line... + */ + + while ((ch = cupsFileGetChar(fp)) != EOF) + if (ch == '\r' || ch == '\n') + { + /* + * Line feed or carriage return... + */ + + cg->ppd_line ++; + col = 0; + + if (ch == '\r') + { + /* + * Check for a trailing line feed... + */ + + if ((ch = cupsFilePeekChar(fp)) == EOF) + break; + if (ch == 0x0a) + cupsFileGetChar(fp); + } + + break; + } + else if (ch < ' ' && ch != '\t' && cg->ppd_conform == PPD_CONFORM_STRICT) + { + /* + * Other control characters... + */ + + cg->ppd_line = startline; + cg->ppd_status = PPD_ILLEGAL_CHARACTER; + + return (0); + } + else if (ch != 0x1a) + { + col ++; + + if (col > (PPD_MAX_LINE - 1)) + { + /* + * Line is too long... + */ + + cg->ppd_line = startline; + cg->ppd_status = PPD_LINE_TOO_LONG; + + return (0); + } + } + } + + if (lineptr > line->buffer && lineptr[-1] == '\n') + lineptr --; + + *lineptr = '\0'; + + DEBUG_printf(("9ppd_read: LINE=\"%s\"", line->buffer)); + + /* + * The dynamically created PPDs for older style OS X + * drivers include a large blob of data inserted as comments + * at the end of the file. As an optimization we can stop + * reading the PPD when we get to the start of this data. + */ + + if (!strcmp(line->buffer, "*%APLWORKSET START")) + return (0); + + if (ch == EOF && lineptr == line->buffer) + return (0); + + /* + * Now parse it... + */ + + mask = 0; + lineptr = line->buffer + 1; + + keyword[0] = '\0'; + option[0] = '\0'; + text[0] = '\0'; + *string = NULL; + + if ((!line->buffer[0] || /* Blank line */ + !strncmp(line->buffer, "*%", 2) || /* Comment line */ + !strcmp(line->buffer, "*End")) && /* End of multi-line string */ + ignoreblank) /* Ignore these? */ + { + startline = cg->ppd_line + 1; + continue; + } + + if (!strcmp(line->buffer, "*")) /* (Bad) comment line */ + { + if (cg->ppd_conform == PPD_CONFORM_RELAXED) + { + startline = cg->ppd_line + 1; + continue; + } + else + { + cg->ppd_line = startline; + cg->ppd_status = PPD_ILLEGAL_MAIN_KEYWORD; + + return (0); + } + } + + if (line->buffer[0] != '*') /* All lines start with an asterisk */ + { + /* + * Allow lines consisting of just whitespace... + */ + + for (lineptr = line->buffer; *lineptr; lineptr ++) + if (*lineptr && !_cups_isspace(*lineptr)) + break; + + if (*lineptr) + { + cg->ppd_status = PPD_MISSING_ASTERISK; + return (0); + } + else if (ignoreblank) + continue; + else + return (0); + } + + /* + * Get a keyword... + */ + + keyptr = keyword; + + while (*lineptr && *lineptr != ':' && !_cups_isspace(*lineptr)) + { + if (*lineptr <= ' ' || *lineptr > 126 || *lineptr == '/' || + (keyptr - keyword) >= (PPD_MAX_NAME - 1)) + { + cg->ppd_status = PPD_ILLEGAL_MAIN_KEYWORD; + return (0); + } + + *keyptr++ = *lineptr++; + } + + *keyptr = '\0'; + + if (!strcmp(keyword, "End")) + continue; + + mask |= PPD_KEYWORD; + + if (_cups_isspace(*lineptr)) + { + /* + * Get an option name... + */ + + while (_cups_isspace(*lineptr)) + lineptr ++; + + optptr = option; + + while (*lineptr && !_cups_isspace(*lineptr) && *lineptr != ':' && + *lineptr != '/') + { + if (*lineptr <= ' ' || *lineptr > 126 || + (optptr - option) >= (PPD_MAX_NAME - 1)) + { + cg->ppd_status = PPD_ILLEGAL_OPTION_KEYWORD; + return (0); + } + + *optptr++ = *lineptr++; + } + + *optptr = '\0'; + + if (_cups_isspace(*lineptr) && cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_ILLEGAL_WHITESPACE; + return (0); + } + + while (_cups_isspace(*lineptr)) + lineptr ++; + + mask |= PPD_OPTION; + + if (*lineptr == '/') + { + /* + * Get human-readable text... + */ + + lineptr ++; + + textptr = text; + + while (*lineptr != '\0' && *lineptr != '\n' && *lineptr != ':') + { + if (((unsigned char)*lineptr < ' ' && *lineptr != '\t') || + (textptr - text) >= (PPD_MAX_LINE - 1)) + { + cg->ppd_status = PPD_ILLEGAL_TRANSLATION; + return (0); + } + + *textptr++ = *lineptr++; + } + + *textptr = '\0'; + textlen = ppd_decode(text); + + if (textlen > PPD_MAX_TEXT && cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_ILLEGAL_TRANSLATION; + return (0); + } + + mask |= PPD_TEXT; + } + } + + if (_cups_isspace(*lineptr) && cg->ppd_conform == PPD_CONFORM_STRICT) + { + cg->ppd_status = PPD_ILLEGAL_WHITESPACE; + return (0); + } + + while (_cups_isspace(*lineptr)) + lineptr ++; + + if (*lineptr == ':') + { + /* + * Get string after triming leading and trailing whitespace... + */ + + lineptr ++; + while (_cups_isspace(*lineptr)) + lineptr ++; + + strptr = lineptr + strlen(lineptr) - 1; + while (strptr >= lineptr && _cups_isspace(*strptr)) + *strptr-- = '\0'; + + if (*strptr == '\"') + { + /* + * Quoted string by itself, remove quotes... + */ + + *strptr = '\0'; + lineptr ++; + } + + *string = _cupsStrAlloc(lineptr); + + mask |= PPD_STRING; + } + } + while (mask == 0); + + return (mask); +} + + +/* + * 'ppd_update_filters()' - Update the filters array as needed. + * + * This function re-populates the filters array with cupsFilter2 entries that + * have been stripped of the destination MIME media types and any maxsize hints. + * + * (All for backwards-compatibility) + */ + +static int /* O - 1 on success, 0 on failure */ +ppd_update_filters(ppd_file_t *ppd,/* I - PPD file */ + _cups_globals_t *cg) /* I - Global data */ +{ + ppd_attr_t *attr; /* Current cupsFilter2 value */ + char srcsuper[16], /* Source MIME media type */ + srctype[256], + dstsuper[16], /* Destination MIME media type */ + dsttype[256], + program[1024], /* Command to run */ + *ptr, /* Pointer into command to run */ + buffer[1024], /* Re-written cupsFilter value */ + **filter; /* Current filter */ + int cost; /* Cost of filter */ + + + DEBUG_printf(("4ppd_update_filters(ppd=%p, cg=%p)", ppd, cg)); + + /* + * See if we have any cupsFilter2 lines... + */ + + if ((attr = ppdFindAttr(ppd, "cupsFilter2", NULL)) == NULL) + { + DEBUG_puts("5ppd_update_filters: No cupsFilter2 keywords present."); + return (1); + } + + /* + * Yes, free the cupsFilter-defined filters and re-build... + */ + + ppd_free_filters(ppd); + + do + { + /* + * Parse the cupsFilter2 string: + * + * src/type dst/type cost program + * src/type dst/type cost maxsize(n) program + */ + + DEBUG_printf(("5ppd_update_filters: cupsFilter2=\"%s\"", attr->value)); + + if (sscanf(attr->value, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]", + srcsuper, srctype, dstsuper, dsttype, &cost, program) != 6) + { + DEBUG_puts("5ppd_update_filters: Bad cupsFilter2 line."); + cg->ppd_status = PPD_BAD_VALUE; + + return (0); + } + + DEBUG_printf(("5ppd_update_filters: srcsuper=\"%s\", srctype=\"%s\", " + "dstsuper=\"%s\", dsttype=\"%s\", cost=%d, program=\"%s\"", + srcsuper, srctype, dstsuper, dsttype, cost, program)); + + if (!strncmp(program, "maxsize(", 8) && + (ptr = strchr(program + 8, ')')) != NULL) + { + DEBUG_puts("5ppd_update_filters: Found maxsize(nnn)."); + + ptr ++; + while (_cups_isspace(*ptr)) + ptr ++; + + _cups_strcpy(program, ptr); + DEBUG_printf(("5ppd_update_filters: New program=\"%s\"", program)); + } + + /* + * Convert to cupsFilter format: + * + * src/type cost program + */ + + snprintf(buffer, sizeof(buffer), "%s/%s %d %s", srcsuper, srctype, cost, + program); + DEBUG_printf(("5ppd_update_filters: Adding \"%s\".", buffer)); + + /* + * Add a cupsFilter-compatible string to the filters array. + */ + + if (ppd->num_filters == 0) + filter = malloc(sizeof(char *)); + else + filter = realloc(ppd->filters, sizeof(char *) * (ppd->num_filters + 1)); + + if (filter == NULL) + { + DEBUG_puts("5ppd_update_filters: Out of memory."); + cg->ppd_status = PPD_ALLOC_ERROR; + + return (0); + } + + ppd->filters = filter; + filter += ppd->num_filters; + ppd->num_filters ++; + + *filter = _cupsStrAlloc(buffer); + } + while ((attr = ppdFindNextAttr(ppd, "cupsFilter2", NULL)) != NULL); + + DEBUG_puts("5ppd_update_filters: Completed OK."); + return (1); +} + + +/* + * End of "$Id: ppd.c 9900 2011-08-17 20:59:46Z mike $". + */ diff --git a/cups/ppd.h b/cups/ppd.h new file mode 100644 index 0000000..3e281bd --- /dev/null +++ b/cups/ppd.h @@ -0,0 +1,480 @@ +/* + * "$Id: ppd.h 7791 2008-07-24 00:55:30Z mike $" + * + * PostScript Printer Description definitions for CUPS. + * + * THESE APIS ARE DEPRECATED. TO COMPILE WITHOUT WARNINGS ADD + * -D_PPD_DEPRECATED="" TO YOUR COMPILE OPTIONS. THIS HEADER AND THESE + * FUNCTIONS WILL BE REMOVED IN A FUTURE RELEASE OF CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 by Easy Software Products, all rights reserved. + * + * 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/". + * + * PostScript is a trademark of Adobe Systems, Inc. + * + * This code and any derivative of it may be used and distributed + * freely under the terms of the GNU General Public License when + * used with GNU Ghostscript or its derivatives. Use of the code + * (or any derivative of it) with software other than GNU + * GhostScript (or its derivatives) is governed by the CUPS license + * agreement. + * + * This file is subject to the Apple OS-Developed Software exception. + */ + +#ifndef _CUPS_PPD_H_ +# define _CUPS_PPD_H_ + +/* + * Include necessary headers... + */ + +# include +# include "cups.h" +# include "array.h" +# include "file.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Define _PPD_DEPRECATED to silence the warnings about PPD functions being + * deprecated... + */ + +# ifndef _PPD_DEPRECATED +# if defined(__APPLE__) +# if defined(MAC_OS_X_VERSION_10_8) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8 + /* Building for OS X 10.7 and earlier */ +# define _PPD_DEPRECATED +# elif !defined(MAC_OS_X_VERSION_10_8) + /* Building for OS X 10.7 and earlier */ +# define _PPD_DEPRECATED +# else +# define _PPD_DEPRECATED _CUPS_DEPRECATED +# endif /* MAC_OS_X_VERSION_10_8 && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8 */ +# else +# define _PPD_DEPRECATED _CUPS_DEPRECATED +# endif /* __APPLE__ */ +# endif /* !_PPD_DEPRECATED */ + + +/* + * PPD version... + */ + +# define PPD_VERSION 4.3 /* Kept in sync with Adobe version number */ + + +/* + * PPD size limits (defined in Adobe spec) + */ + +# define PPD_MAX_NAME 41 /* Maximum size of name + 1 for nul */ +# define PPD_MAX_TEXT 81 /* Maximum size of text + 1 for nul */ +# define PPD_MAX_LINE 256 /* Maximum size of line + 1 for nul */ + + +/* + * Types and structures... + */ + +typedef enum ppd_ui_e /**** UI Types ****/ +{ + PPD_UI_BOOLEAN, /* True or False option */ + PPD_UI_PICKONE, /* Pick one from a list */ + PPD_UI_PICKMANY /* Pick zero or more from a list */ +} ppd_ui_t; + +typedef enum ppd_section_e /**** Order dependency sections ****/ +{ + PPD_ORDER_ANY, /* Option code can be anywhere in the file */ + PPD_ORDER_DOCUMENT, /* ... must be in the DocumentSetup section */ + PPD_ORDER_EXIT, /* ... must be sent prior to the document */ + PPD_ORDER_JCL, /* ... must be sent as a JCL command */ + PPD_ORDER_PAGE, /* ... must be in the PageSetup section */ + PPD_ORDER_PROLOG /* ... must be in the Prolog section */ +} ppd_section_t; + +typedef enum ppd_cs_e /**** Colorspaces ****/ +{ + PPD_CS_CMYK = -4, /* CMYK colorspace */ + PPD_CS_CMY, /* CMY colorspace */ + PPD_CS_GRAY = 1, /* Grayscale colorspace */ + PPD_CS_RGB = 3, /* RGB colorspace */ + PPD_CS_RGBK, /* RGBK (K = gray) colorspace */ + PPD_CS_N /* DeviceN colorspace */ +} ppd_cs_t; + +typedef enum ppd_status_e /**** Status Codes @since CUPS 1.1.19/OS X 10.3@ ****/ +{ + PPD_OK = 0, /* OK */ + PPD_FILE_OPEN_ERROR, /* Unable to open PPD file */ + PPD_NULL_FILE, /* NULL PPD file pointer */ + PPD_ALLOC_ERROR, /* Memory allocation error */ + PPD_MISSING_PPDADOBE4, /* Missing PPD-Adobe-4.x header */ + PPD_MISSING_VALUE, /* Missing value string */ + PPD_INTERNAL_ERROR, /* Internal error */ + PPD_BAD_OPEN_GROUP, /* Bad OpenGroup */ + PPD_NESTED_OPEN_GROUP, /* OpenGroup without a CloseGroup first */ + PPD_BAD_OPEN_UI, /* Bad OpenUI/JCLOpenUI */ + PPD_NESTED_OPEN_UI, /* OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first */ + PPD_BAD_ORDER_DEPENDENCY, /* Bad OrderDependency */ + PPD_BAD_UI_CONSTRAINTS, /* Bad UIConstraints */ + PPD_MISSING_ASTERISK, /* Missing asterisk in column 0 */ + PPD_LINE_TOO_LONG, /* Line longer than 255 chars */ + PPD_ILLEGAL_CHARACTER, /* Illegal control character */ + PPD_ILLEGAL_MAIN_KEYWORD, /* Illegal main keyword string */ + PPD_ILLEGAL_OPTION_KEYWORD, /* Illegal option keyword string */ + PPD_ILLEGAL_TRANSLATION, /* Illegal translation string */ + PPD_ILLEGAL_WHITESPACE, /* Illegal whitespace character */ + PPD_BAD_CUSTOM_PARAM, /* Bad custom parameter */ + PPD_MISSING_OPTION_KEYWORD, /* Missing option keyword */ + PPD_BAD_VALUE, /* Bad value string */ + PPD_MISSING_CLOSE_GROUP, /* Missing CloseGroup */ + PPD_MAX_STATUS /* @private@ */ +} ppd_status_t; + +enum ppd_conform_e /**** Conformance Levels @since CUPS 1.1.19/OS X 10.3@ ****/ +{ + PPD_CONFORM_RELAXED, /* Relax whitespace and control char */ + PPD_CONFORM_STRICT /* Require strict conformance */ +}; + +typedef enum ppd_conform_e ppd_conform_t; + /**** Conformance Levels @since CUPS 1.1.19/OS X 10.3@ ****/ + +typedef struct ppd_attr_s /**** PPD Attribute Structure @since CUPS 1.1.19/OS X 10.3@ ****/ +{ + char name[PPD_MAX_NAME]; /* Name of attribute (cupsXYZ) */ + char spec[PPD_MAX_NAME]; /* Specifier string, if any */ + char text[PPD_MAX_TEXT]; /* Human-readable text, if any */ + char *value; /* Value string */ +} ppd_attr_t; + +typedef struct ppd_option_s ppd_option_t; + /**** Options ****/ + +typedef struct ppd_choice_s /**** Option choices ****/ +{ + char marked; /* 0 if not selected, 1 otherwise */ + char choice[PPD_MAX_NAME]; /* Computer-readable option name */ + char text[PPD_MAX_TEXT]; /* Human-readable option name */ + char *code; /* Code to send for this option */ + ppd_option_t *option; /* Pointer to parent option structure */ +} ppd_choice_t; + +struct ppd_option_s /**** Options ****/ +{ + char conflicted; /* 0 if no conflicts exist, 1 otherwise */ + char keyword[PPD_MAX_NAME]; /* Option keyword name ("PageSize", etc.) */ + char defchoice[PPD_MAX_NAME];/* Default option choice */ + char text[PPD_MAX_TEXT]; /* Human-readable text */ + ppd_ui_t ui; /* Type of UI option */ + ppd_section_t section; /* Section for command */ + float order; /* Order number */ + int num_choices; /* Number of option choices */ + ppd_choice_t *choices; /* Option choices */ +}; + +typedef struct ppd_group_s /**** Groups ****/ +{ + /**** Group text strings are limited to 39 chars + nul in order to + **** preserve binary compatibility and allow applications to get + **** the group's keyword name. + ****/ + char text[PPD_MAX_TEXT - PPD_MAX_NAME]; + /* Human-readable group name */ + char name[PPD_MAX_NAME]; /* Group name @since CUPS 1.1.18/OS X 10.3@ */ + int num_options; /* Number of options */ + ppd_option_t *options; /* Options */ + int num_subgroups; /* Number of sub-groups */ + struct ppd_group_s *subgroups; /* Sub-groups (max depth = 1) */ +} ppd_group_t; + +typedef struct ppd_const_s /**** Constraints ****/ +{ + char option1[PPD_MAX_NAME]; /* First keyword */ + char choice1[PPD_MAX_NAME]; /* First option/choice (blank for all) */ + char option2[PPD_MAX_NAME]; /* Second keyword */ + char choice2[PPD_MAX_NAME]; /* Second option/choice (blank for all) */ +} ppd_const_t; + +typedef struct ppd_size_s /**** Page Sizes ****/ +{ + int marked; /* Page size selected? */ + char name[PPD_MAX_NAME]; /* Media size option */ + float width; /* Width of media in points */ + float length; /* Length of media in points */ + float left; /* Left printable margin in points */ + float bottom; /* Bottom printable margin in points */ + float right; /* Right printable margin in points */ + float top; /* Top printable margin in points */ +} ppd_size_t; + +typedef struct ppd_emul_s /**** Emulators ****/ +{ + char name[PPD_MAX_NAME]; /* Emulator name */ + char *start; /* Code to switch to this emulation */ + char *stop; /* Code to stop this emulation */ +} ppd_emul_t; + +typedef struct ppd_profile_s /**** sRGB Color Profiles ****/ +{ + char resolution[PPD_MAX_NAME]; + /* Resolution or "-" */ + char media_type[PPD_MAX_NAME]; + /* Media type or "-" */ + float density; /* Ink density to use */ + float gamma; /* Gamma correction to use */ + float matrix[3][3]; /* Transform matrix */ +} ppd_profile_t; + +/**** New in CUPS 1.2/OS X 10.5 ****/ +typedef enum ppd_cptype_e /**** Custom Parameter Type @since CUPS 1.2/OS X 10.5@ ****/ +{ + PPD_CUSTOM_CURVE, /* Curve value for f(x) = x^value */ + PPD_CUSTOM_INT, /* Integer number value */ + PPD_CUSTOM_INVCURVE, /* Curve value for f(x) = x^(1/value) */ + PPD_CUSTOM_PASSCODE, /* String of (hidden) numbers */ + PPD_CUSTOM_PASSWORD, /* String of (hidden) characters */ + PPD_CUSTOM_POINTS, /* Measurement value in points */ + PPD_CUSTOM_REAL, /* Real number value */ + PPD_CUSTOM_STRING /* String of characters */ +} ppd_cptype_t; + +typedef union ppd_cplimit_u /**** Custom Parameter Limit @since CUPS 1.2/OS X 10.5@ ****/ +{ + float custom_curve; /* Gamma value */ + int custom_int; /* Integer value */ + float custom_invcurve; /* Gamma value */ + int custom_passcode; /* Passcode length */ + int custom_password; /* Password length */ + float custom_points; /* Measurement value */ + float custom_real; /* Real value */ + int custom_string; /* String length */ +} ppd_cplimit_t; + +typedef union ppd_cpvalue_u /**** Custom Parameter Value @since CUPS 1.2/OS X 10.5@ ****/ +{ + float custom_curve; /* Gamma value */ + int custom_int; /* Integer value */ + float custom_invcurve; /* Gamma value */ + char *custom_passcode; /* Passcode value */ + char *custom_password; /* Password value */ + float custom_points; /* Measurement value */ + float custom_real; /* Real value */ + char *custom_string; /* String value */ +} ppd_cpvalue_t; + +typedef struct ppd_cparam_s /**** Custom Parameter @since CUPS 1.2/OS X 10.5@ ****/ +{ + char name[PPD_MAX_NAME]; /* Parameter name */ + char text[PPD_MAX_TEXT]; /* Human-readable text */ + int order; /* Order (0 to N) */ + ppd_cptype_t type; /* Parameter type */ + ppd_cplimit_t minimum, /* Minimum value */ + maximum; /* Maximum value */ + ppd_cpvalue_t current; /* Current value */ +} ppd_cparam_t; + +typedef struct ppd_coption_s /**** Custom Option @since CUPS 1.2/OS X 10.5@ ****/ +{ + char keyword[PPD_MAX_NAME]; /* Name of option that is being extended... */ + ppd_option_t *option; /* Option that is being extended... */ + int marked; /* Extended option is marked */ + cups_array_t *params; /* Parameters */ +} ppd_coption_t; + +typedef struct _ppd_cache_s _ppd_cache_t; + /**** PPD cache and mapping data @since CUPS 1.5/OS X 10.7@ @private@ ****/ + +typedef struct ppd_file_s /**** PPD File ****/ +{ + int language_level; /* Language level of device */ + int color_device; /* 1 = color device, 0 = grayscale */ + int variable_sizes; /* 1 = supports variable sizes, 0 = doesn't */ + int accurate_screens; /* 1 = supports accurate screens, 0 = not */ + int contone_only; /* 1 = continuous tone only, 0 = not */ + int landscape; /* -90 or 90 */ + int model_number; /* Device-specific model number */ + int manual_copies; /* 1 = Copies done manually, 0 = hardware */ + int throughput; /* Pages per minute */ + ppd_cs_t colorspace; /* Default colorspace */ + char *patches; /* Patch commands to be sent to printer */ + int num_emulations; /* Number of emulations supported */ + ppd_emul_t *emulations; /* Emulations and the code to invoke them */ + char *jcl_begin; /* Start JCL commands */ + char *jcl_ps; /* Enter PostScript interpreter */ + char *jcl_end; /* End JCL commands */ + char *lang_encoding; /* Language encoding */ + char *lang_version; /* Language version (English, Spanish, etc.) */ + char *modelname; /* Model name (general) */ + char *ttrasterizer; /* Truetype rasterizer */ + char *manufacturer; /* Manufacturer name */ + char *product; /* Product name (from PS RIP/interpreter) */ + char *nickname; /* Nickname (specific) */ + char *shortnickname; /* Short version of nickname */ + int num_groups; /* Number of UI groups */ + ppd_group_t *groups; /* UI groups */ + int num_sizes; /* Number of page sizes */ + ppd_size_t *sizes; /* Page sizes */ + float custom_min[2]; /* Minimum variable page size */ + float custom_max[2]; /* Maximum variable page size */ + float custom_margins[4]; /* Margins around page */ + int num_consts; /* Number of UI/Non-UI constraints */ + ppd_const_t *consts; /* UI/Non-UI constraints */ + int num_fonts; /* Number of pre-loaded fonts */ + char **fonts; /* Pre-loaded fonts */ + int num_profiles; /* Number of sRGB color profiles @deprecated@ */ + ppd_profile_t *profiles; /* sRGB color profiles @deprecated@ */ + int num_filters; /* Number of filters */ + char **filters; /* Filter strings... */ + + /**** New in CUPS 1.1 ****/ + int flip_duplex; /* 1 = Flip page for back sides @deprecated@ */ + + /**** New in CUPS 1.1.19 ****/ + char *protocols; /* Protocols (BCP, TBCP) string @since CUPS 1.1.19/OS X 10.3@ */ + char *pcfilename; /* PCFileName string @since CUPS 1.1.19/OS X 10.3@ */ + int num_attrs; /* Number of attributes @since CUPS 1.1.19/OS X 10.3@ @private@ */ + int cur_attr; /* Current attribute @since CUPS 1.1.19/OS X 10.3@ @private@ */ + ppd_attr_t **attrs; /* Attributes @since CUPS 1.1.19/OS X 10.3@ @private@ */ + + /**** New in CUPS 1.2/OS X 10.5 ****/ + cups_array_t *sorted_attrs; /* Attribute lookup array @since CUPS 1.2/OS X 10.5@ @private@ */ + cups_array_t *options; /* Option lookup array @since CUPS 1.2/OS X 10.5@ @private@ */ + cups_array_t *coptions; /* Custom options array @since CUPS 1.2/OS X 10.5@ @private@ */ + + /**** New in CUPS 1.3/OS X 10.5 ****/ + cups_array_t *marked; /* Marked choices @since CUPS 1.3/OS X 10.5@ @private@ */ + + /**** New in CUPS 1.4/OS X 10.6 ****/ + cups_array_t *cups_uiconstraints; /* cupsUIConstraints @since CUPS 1.4/OS X 10.6@ @private@ */ + + /**** New in CUPS 1.5 ****/ + _ppd_cache_t *cache; /* PPD cache and mapping data @since CUPS 1.5/OS X 10.7@ @private@ */ +} ppd_file_t; + + +/* + * Prototypes... + */ + +extern int cupsMarkOptions(ppd_file_t *ppd, int num_options, + cups_option_t *options); +extern void ppdClose(ppd_file_t *ppd); +extern int ppdCollect(ppd_file_t *ppd, ppd_section_t section, + ppd_choice_t ***choices); +extern int ppdConflicts(ppd_file_t *ppd); +extern int ppdEmit(ppd_file_t *ppd, FILE *fp, + ppd_section_t section); +extern int ppdEmitFd(ppd_file_t *ppd, int fd, + ppd_section_t section); +extern int ppdEmitJCL(ppd_file_t *ppd, FILE *fp, int job_id, + const char *user, const char *title); +extern ppd_choice_t *ppdFindChoice(ppd_option_t *o, const char *option); +extern ppd_choice_t *ppdFindMarkedChoice(ppd_file_t *ppd, + const char *keyword); +extern ppd_option_t *ppdFindOption(ppd_file_t *ppd, const char *keyword); +extern int ppdIsMarked(ppd_file_t *ppd, const char *keyword, + const char *option); +extern void ppdMarkDefaults(ppd_file_t *ppd); +extern int ppdMarkOption(ppd_file_t *ppd, const char *keyword, + const char *option); +extern ppd_file_t *ppdOpen(FILE *fp); +extern ppd_file_t *ppdOpenFd(int fd); +extern ppd_file_t *ppdOpenFile(const char *filename); +extern float ppdPageLength(ppd_file_t *ppd, const char *name); +extern ppd_size_t *ppdPageSize(ppd_file_t *ppd, const char *name); +extern float ppdPageWidth(ppd_file_t *ppd, const char *name); + +/**** New in CUPS 1.1.19 ****/ +extern const char *ppdErrorString(ppd_status_t status) _PPD_DEPRECATED; +extern ppd_attr_t *ppdFindAttr(ppd_file_t *ppd, const char *name, + const char *spec) _PPD_DEPRECATED; +extern ppd_attr_t *ppdFindNextAttr(ppd_file_t *ppd, const char *name, + const char *spec) _PPD_DEPRECATED; +extern ppd_status_t ppdLastError(int *line) _PPD_DEPRECATED; + +/**** New in CUPS 1.1.20 ****/ +extern void ppdSetConformance(ppd_conform_t c) _PPD_DEPRECATED; + +/**** New in CUPS 1.2 ****/ +extern int ppdCollect2(ppd_file_t *ppd, ppd_section_t section, + float min_order, ppd_choice_t ***choices) + _PPD_DEPRECATED; +extern int ppdEmitAfterOrder(ppd_file_t *ppd, FILE *fp, + ppd_section_t section, int limit, + float min_order) _PPD_DEPRECATED; +extern int ppdEmitJCLEnd(ppd_file_t *ppd, FILE *fp) _PPD_DEPRECATED; +extern char *ppdEmitString(ppd_file_t *ppd, ppd_section_t section, + float min_order) _PPD_DEPRECATED; +extern ppd_coption_t *ppdFindCustomOption(ppd_file_t *ppd, + const char *keyword) _PPD_DEPRECATED; +extern ppd_cparam_t *ppdFindCustomParam(ppd_coption_t *opt, + const char *name) _PPD_DEPRECATED; +extern ppd_cparam_t *ppdFirstCustomParam(ppd_coption_t *opt) _PPD_DEPRECATED; +extern ppd_option_t *ppdFirstOption(ppd_file_t *ppd) _PPD_DEPRECATED; +extern ppd_cparam_t *ppdNextCustomParam(ppd_coption_t *opt) _PPD_DEPRECATED; +extern ppd_option_t *ppdNextOption(ppd_file_t *ppd) _PPD_DEPRECATED; +extern int ppdLocalize(ppd_file_t *ppd) _PPD_DEPRECATED; +extern ppd_file_t *ppdOpen2(cups_file_t *fp) _PPD_DEPRECATED; + +/**** New in CUPS 1.3/OS X 10.5 ****/ +extern const char *ppdLocalizeIPPReason(ppd_file_t *ppd, + const char *reason, + const char *scheme, + char *buffer, + size_t bufsize) _PPD_DEPRECATED; + +/**** New in CUPS 1.4/OS X 10.6 ****/ +extern int cupsGetConflicts(ppd_file_t *ppd, const char *option, + const char *choice, + cups_option_t **options) + _PPD_DEPRECATED; +extern int cupsResolveConflicts(ppd_file_t *ppd, + const char *option, + const char *choice, + int *num_options, + cups_option_t **options) + _PPD_DEPRECATED; +extern int ppdInstallableConflict(ppd_file_t *ppd, + const char *option, + const char *choice) + _PPD_DEPRECATED; +extern ppd_attr_t *ppdLocalizeAttr(ppd_file_t *ppd, const char *keyword, + const char *spec) _PPD_DEPRECATED; +extern const char *ppdLocalizeMarkerName(ppd_file_t *ppd, + const char *name) + _PPD_DEPRECATED; +extern int ppdPageSizeLimits(ppd_file_t *ppd, + ppd_size_t *minimum, + ppd_size_t *maximum) _PPD_DEPRECATED; + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_PPD_H_ */ + +/* + * End of "$Id: ppd.h 7791 2008-07-24 00:55:30Z mike $". + */ diff --git a/cups/pwg-media.c b/cups/pwg-media.c new file mode 100644 index 0000000..0c56355 --- /dev/null +++ b/cups/pwg-media.c @@ -0,0 +1,928 @@ +/* + * "$Id: pwg-media.c 10979 2013-05-13 17:39:19Z msweet $" + * + * PWG media name API implementation for CUPS. + * + * Copyright 2009-2012 by Apple Inc. + * + * 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. + * + * Contents: + * + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include + + +/* + * Local macros... + */ + +#define _PWG_MEDIA_IN(p,l,a,x,y) {p, l, a, (int)(x * 2540), (int)(y * 2540)} +#define _PWG_MEDIA_MM(p,l,a,x,y) {p, l, a, (int)(x * 100), (int)(y * 100)} + + +/* + * Local functions... + */ + +static int pwg_compare_legacy(_pwg_media_t *a, _pwg_media_t *b); +static int pwg_compare_pwg(_pwg_media_t *a, _pwg_media_t *b); +static int pwg_compare_ppd(_pwg_media_t *a, _pwg_media_t *b); + + +/* + * Local globals... + */ + +static _pwg_media_t const cups_pwg_media[] = +{ /* Media size lookup table */ + /* North American Standard Sheet Media Sizes */ + _PWG_MEDIA_IN("na_index-3x5_3x5in", NULL, "3x5", 3, 5), + _PWG_MEDIA_IN("na_personal_3.625x6.5in", NULL, "EnvPersonal", 3.625, 6.5), + _PWG_MEDIA_IN("na_monarch_3.875x7.5in", "monarch-envelope", "EnvMonarch", 3.875, 7.5), + _PWG_MEDIA_IN("na_number-9_3.875x8.875in", "na-number-9-envelope", "Env9", 3.875, 8.875), + _PWG_MEDIA_IN("na_index-4x6_4x6in", NULL, "4x6", 4, 6), + _PWG_MEDIA_IN("na_number-10_4.125x9.5in", "na-number-10-envelope", "Env10", 4.125, 9.5), + _PWG_MEDIA_IN("na_a2_4.375x5.75in", NULL, "EnvA2", 4.375, 5.75), + _PWG_MEDIA_IN("na_number-11_4.5x10.375in", NULL, "Env11", 4.5, 10.375), + _PWG_MEDIA_IN("na_number-12_4.75x11in", NULL, "Env12", 4.75, 11), + _PWG_MEDIA_IN("na_5x7_5x7in", NULL, "5x7", 5, 7), + _PWG_MEDIA_IN("na_index-5x8_5x8in", NULL, "5x8", 5, 8), + _PWG_MEDIA_IN("na_number-14_5x11.5in", NULL, "Env14", 5, 11.5), + _PWG_MEDIA_IN("na_invoice_5.5x8.5in", "invoice", "Statement", 5.5, 8.5), + _PWG_MEDIA_IN("na_index-4x6-ext_6x8in", NULL, NULL, 6, 8), + _PWG_MEDIA_IN("na_6x9_6x9in", "na-6x9-envelope", "6x9", 6, 9), + _PWG_MEDIA_IN("na_c5_6.5x9.5in", NULL, "6.5x9.5", 6.5, 9.5), + _PWG_MEDIA_IN("na_7x9_7x9in", "na-7x9-envelope", "7x9", 7, 9), + _PWG_MEDIA_IN("na_executive_7.25x10.5in", "executive", "Executive", 7.25, 10.5), + _PWG_MEDIA_IN("na_govt-letter_8x10in", "na-8x10", "8x10", 8, 10), + _PWG_MEDIA_IN("na_govt-legal_8x13in", NULL, "8x13", 8, 13), + _PWG_MEDIA_IN("na_quarto_8.5x10.83in", "quarto", "Quarto", 8.5, 10.83), + _PWG_MEDIA_IN("na_letter_8.5x11in", "na-letter", "Letter", 8.5, 11), + _PWG_MEDIA_IN("na_fanfold-eur_8.5x12in", NULL, "FanFoldGerman", 8.5, 12), + _PWG_MEDIA_IN("na_letter-plus_8.5x12.69in", NULL, "LetterPlus", 8.5, 12.69), + _PWG_MEDIA_IN("na_foolscap_8.5x13in", NULL, "FanFoldGermanLegal", 8.5, 13), + _PWG_MEDIA_IN("na_legal_8.5x14in", "na-legal", "Legal", 8.5, 14), + _PWG_MEDIA_IN("na_super-a_8.94x14in", NULL, "SuperA", 8.94, 14), + _PWG_MEDIA_IN("na_9x11_9x11in", "na-9x11-envelope", "9x11", 9, 11), + _PWG_MEDIA_IN("na_arch-a_9x12in", "arch-a", "ARCHA", 9, 12), + _PWG_MEDIA_IN("na_letter-extra_9.5x12in", NULL, "LetterExtra", 9.5, 12), + _PWG_MEDIA_IN("na_legal-extra_9.5x15in", NULL, "LegalExtra", 9.5, 15), + _PWG_MEDIA_IN("na_10x11_10x11in", NULL, "10x11", 10, 11), + _PWG_MEDIA_IN("na_10x13_10x13in", "na-10x13-envelope", "10x13", 10, 13), + _PWG_MEDIA_IN("na_10x14_10x14in", "na-10x14-envelope", "10x14", 10, 14), + _PWG_MEDIA_IN("na_10x15_10x15in", "na-10x15-envelope", "10x15", 10, 15), + _PWG_MEDIA_IN("na_11x12_11x12in", NULL, "11x12", 11, 12), + _PWG_MEDIA_IN("na_edp_11x14in", NULL, "11x14", 11, 14), + _PWG_MEDIA_IN("na_fanfold-us_11x14.875in", NULL, NULL, 11, 14.875), + _PWG_MEDIA_IN("na_11x15_11x15in", NULL, "11x15", 11, 15), + _PWG_MEDIA_IN("na_ledger_11x17in", "tabloid", "Tabloid", 11, 17), + _PWG_MEDIA_IN("na_eur-edp_12x14in", NULL, NULL, 12, 14), + _PWG_MEDIA_IN("na_arch-b_12x18in", "arch-b", "ARCHB", 12, 18), + _PWG_MEDIA_IN("na_12x19_12x19in", NULL, "12x19", 12, 19), + _PWG_MEDIA_IN("na_b-plus_12x19.17in", NULL, "SuperB", 12, 19.17), + _PWG_MEDIA_IN("na_super-b_13x19in", "super-b", "13x19", 13, 19), + _PWG_MEDIA_IN("na_c_17x22in", "c", "AnsiC", 17, 22), + _PWG_MEDIA_IN("na_arch-c_18x24in", "arch-c", "ARCHC", 18, 24), + _PWG_MEDIA_IN("na_d_22x34in", "d", "AnsiD", 22, 34), + _PWG_MEDIA_IN("na_arch-d_24x36in", "arch-d", "ARCHD", 24, 36), + _PWG_MEDIA_IN("asme_f_28x40in", "f", NULL, 28, 40), + _PWG_MEDIA_IN("na_wide-format_30x42in", NULL, NULL, 30, 42), + _PWG_MEDIA_IN("na_e_34x44in", "e", "AnsiE", 34, 44), + _PWG_MEDIA_IN("na_arch-e_36x48in", "arch-e", "ARCHE", 36, 48), + _PWG_MEDIA_IN("na_f_44x68in", NULL, "AnsiF", 44, 68), + + /* Chinese Standard Sheet Media Inch Sizes */ + _PWG_MEDIA_IN("roc_16k_7.75x10.75in", NULL, "roc16k", 7.75, 10.75), + _PWG_MEDIA_IN("roc_8k_10.75x15.5in", NULL, "roc8k", 10.75, 15.5), + + /* ISO Standard Sheet Media Sizes */ + _PWG_MEDIA_MM("iso_a10_26x37mm", "iso-a10", "A10", 26, 37), + _PWG_MEDIA_MM("iso_a9_37x52mm", "iso-a9", "A9", 37, 52), + _PWG_MEDIA_MM("iso_a8_52x74mm", "iso-a8", "A8", 52, 74), + _PWG_MEDIA_MM("iso_a7_74x105mm", "iso-a7", "A7", 74, 105), + _PWG_MEDIA_MM("iso_a6_105x148mm", "iso-a6", "A6", 105, 148), + _PWG_MEDIA_MM("iso_a5_148x210mm", "iso-a5", "A5", 148, 210), + _PWG_MEDIA_MM("iso_a5-extra_174x235mm", NULL, "A5Extra", 174, 235), + _PWG_MEDIA_MM("iso_a4_210x297mm", "iso-a4", "A4", 210, 297), + _PWG_MEDIA_MM("iso_a4-tab_225x297mm", NULL, "A4Tab", 225, 297), + _PWG_MEDIA_MM("iso_a4-extra_235.5x322.3mm", NULL, "A4Extra", 235.5, 322.3), + _PWG_MEDIA_MM("iso_a3_297x420mm", "iso-a3", "A3", 297, 420), + _PWG_MEDIA_MM("iso_a4x3_297x630mm", "iso-a4x3", NULL, 297, 630), + _PWG_MEDIA_MM("iso_a4x4_297x841mm", "iso-a4x4", NULL, 297, 841), + _PWG_MEDIA_MM("iso_a4x5_297x1051mm", "iso-a4x5", NULL, 297, 1051), + _PWG_MEDIA_MM("iso_a4x6_297x1261mm", "iso-a4x6", NULL, 297, 1261), + _PWG_MEDIA_MM("iso_a4x7_297x1471mm", "iso-a4x7", NULL, 297, 1471), + _PWG_MEDIA_MM("iso_a4x8_297x1682mm", "iso-a4x8", NULL, 297, 1682), + _PWG_MEDIA_MM("iso_a4x9_297x1892mm", "iso-a4x9", NULL, 297, 1892), + _PWG_MEDIA_MM("iso_a3-extra_322x445mm", "iso-a3-extra", "A3Extra", 322, 445), + _PWG_MEDIA_MM("iso_a2_420x594mm", "iso-a2", "A2", 420, 594), + _PWG_MEDIA_MM("iso_a3x3_420x891mm", "iso-a3x3", NULL, 420, 891), + _PWG_MEDIA_MM("iso_a3x4_420x1189mm", "iso-a3x4", NULL, 420, 1189), + _PWG_MEDIA_MM("iso_a3x5_420x1486mm", "iso-a3x5", NULL, 420, 1486), + _PWG_MEDIA_MM("iso_a3x6_420x1783mm", "iso-a3x6", NULL, 420, 1783), + _PWG_MEDIA_MM("iso_a3x7_420x2080mm", "iso-a3x7", NULL, 420, 2080), + _PWG_MEDIA_MM("iso_a1_594x841mm", "iso-a1", "A1", 594, 841), + _PWG_MEDIA_MM("iso_a2x3_594x1261mm", "iso-a2x3", NULL, 594, 1261), + _PWG_MEDIA_MM("iso_a2x4_594x1682mm", "iso-a2x4", NULL, 594, 1682), + _PWG_MEDIA_MM("iso_a2x5_594x2102mm", "iso-a2x5", NULL, 594, 2102), + _PWG_MEDIA_MM("iso_a0_841x1189mm", "iso-a0", "A0", 841, 1189), + _PWG_MEDIA_MM("iso_a1x3_841x1783mm", "iso-a1x3", NULL, 841, 1783), + _PWG_MEDIA_MM("iso_a1x4_841x2378mm", "iso-a1x4", NULL, 841, 2378), + _PWG_MEDIA_MM("iso_2a0_1189x1682mm", NULL, NULL, 1189, 1682), + _PWG_MEDIA_MM("iso_a0x3_1189x2523mm", NULL, NULL, 1189, 2523), + _PWG_MEDIA_MM("iso_b10_31x44mm", "iso-b10", "ISOB10", 31, 44), + _PWG_MEDIA_MM("iso_b9_44x62mm", "iso-b9", "ISOB9", 44, 62), + _PWG_MEDIA_MM("iso_b8_62x88mm", "iso-b8", "ISOB8", 62, 88), + _PWG_MEDIA_MM("iso_b7_88x125mm", "iso-b7", "ISOB7", 88, 125), + _PWG_MEDIA_MM("iso_b6_125x176mm", "iso-b6", "ISOB6", 125, 176), + _PWG_MEDIA_MM("iso_b6c4_125x324mm", NULL, NULL, 125, 324), + _PWG_MEDIA_MM("iso_b5_176x250mm", "iso-b5", "ISOB5", 176, 250), + _PWG_MEDIA_MM("iso_b5-extra_201x276mm", NULL, "ISOB5Extra", 201, 276), + _PWG_MEDIA_MM("iso_b4_250x353mm", "iso-b4", "ISOB4", 250, 353), + _PWG_MEDIA_MM("iso_b3_353x500mm", "iso-b3", "ISOB3", 353, 500), + _PWG_MEDIA_MM("iso_b2_500x707mm", "iso-b2", "ISOB2", 500, 707), + _PWG_MEDIA_MM("iso_b1_707x1000mm", "iso-b1", "ISOB1", 707, 1000), + _PWG_MEDIA_MM("iso_b0_1000x1414mm", "iso-b0", "ISOB0", 1000, 1414), + _PWG_MEDIA_MM("iso_c10_28x40mm", "iso-c10", NULL, 28, 40), + _PWG_MEDIA_MM("iso_c9_40x57mm", "iso-c9", NULL, 40, 57), + _PWG_MEDIA_MM("iso_c8_57x81mm", "iso-c8", NULL, 57, 81), + _PWG_MEDIA_MM("iso_c7_81x114mm", "iso-c7", "EnvC7", 81, 114), + _PWG_MEDIA_MM("iso_c7c6_81x162mm", NULL, NULL, 81, 162), + _PWG_MEDIA_MM("iso_c6_114x162mm", "iso-c6", "EnvC6", 114, 162), + _PWG_MEDIA_MM("iso_c6c5_114x229mm", NULL, "EnvC65", 114, 229), + _PWG_MEDIA_MM("iso_c5_162x229mm", "iso-c5", "EnvC5", 162, 229), + _PWG_MEDIA_MM("iso_c4_229x324mm", "iso-c4", "EnvC4", 229, 324), + _PWG_MEDIA_MM("iso_c3_324x458mm", "iso-c3", "EnvC3", 324, 458), + _PWG_MEDIA_MM("iso_c2_458x648mm", "iso-c2", "EnvC2", 458, 648), + _PWG_MEDIA_MM("iso_c1_648x917mm", "iso-c1", "EnvC1", 648, 917), + _PWG_MEDIA_MM("iso_c0_917x1297mm", "iso-c0", "EnvC0", 917, 1297), + _PWG_MEDIA_MM("iso_dl_110x220mm", "iso-designated", "EnvDL", 110, 220), + _PWG_MEDIA_MM("iso_ra2_430x610mm", "iso-ra2", NULL, 430, 610), + _PWG_MEDIA_MM("iso_sra2_450x640mm", "iso-sra2", NULL, 450, 640), + _PWG_MEDIA_MM("iso_ra1_610x860mm", "iso-ra1", NULL, 610, 860), + _PWG_MEDIA_MM("iso_sra1_640x900mm", "iso-sra1", NULL, 640, 900), + _PWG_MEDIA_MM("iso_ra0_860x1220mm", "iso-ra0", NULL, 860, 1220), + _PWG_MEDIA_MM("iso_sra0_900x1280mm", "iso-sra0", NULL, 900, 1280), + + /* Japanese Standard Sheet Media Sizes */ + _PWG_MEDIA_MM("jis_b10_32x45mm", "jis-b10", "B10", 32, 45), + _PWG_MEDIA_MM("jis_b9_45x64mm", "jis-b9", "B9", 45, 64), + _PWG_MEDIA_MM("jis_b8_64x91mm", "jis-b8", "B8", 64, 91), + _PWG_MEDIA_MM("jis_b7_91x128mm", "jis-b7", "B7", 91, 128), + _PWG_MEDIA_MM("jis_b6_128x182mm", "jis-b6", "B6", 128, 182), + _PWG_MEDIA_MM("jis_b5_182x257mm", "jis-b5", "B5", 182, 257), + _PWG_MEDIA_MM("jis_b4_257x364mm", "jis-b4", "B4", 257, 364), + _PWG_MEDIA_MM("jis_b3_364x515mm", "jis-b3", "B3", 364, 515), + _PWG_MEDIA_MM("jis_b2_515x728mm", "jis-b2", "B2", 515, 728), + _PWG_MEDIA_MM("jis_b1_728x1030mm", "jis-b1", "B1", 728, 1030), + _PWG_MEDIA_MM("jis_b0_1030x1456mm", "jis-b0", "B0", 1030, 1456), + _PWG_MEDIA_MM("jis_exec_216x330mm", NULL, NULL, 216, 330), + _PWG_MEDIA_MM("jpn_chou4_90x205mm", NULL, "EnvChou4", 90, 205), + _PWG_MEDIA_MM("jpn_hagaki_100x148mm", NULL, "Postcard", 100, 148), + _PWG_MEDIA_MM("jpn_you4_105x235mm", NULL, "EnvYou4", 105, 235), + _PWG_MEDIA_MM("jpn_you6_98x190mm", NULL, "EnvYou6", 98, 190), + _PWG_MEDIA_MM("jpn_chou2_111.1x146mm", NULL, NULL, 111.1, 146), + _PWG_MEDIA_MM("jpn_chou3_120x235mm", NULL, "EnvChou3", 120, 235), + _PWG_MEDIA_MM("jpn_oufuku_148x200mm", NULL, "DoublePostcardRotated", 148, 200), + _PWG_MEDIA_MM("jpn_kahu_240x322.1mm", NULL, NULL, 240, 322.1), + _PWG_MEDIA_MM("jpn_kaku2_240x332mm", NULL, "EnvKaku2", 240, 332), + + /* Chinese Standard Sheet Media Sizes */ + _PWG_MEDIA_MM("prc_32k_97x151mm", NULL, "PRC32K", 97, 151), + _PWG_MEDIA_MM("prc_1_102x165mm", NULL, "EnvPRC1", 102, 165), + _PWG_MEDIA_MM("prc_2_102x176mm", NULL, "EnvPRC2", 102, 176), + _PWG_MEDIA_MM("prc_4_110x208mm", NULL, "EnvPRC4", 110, 208), + _PWG_MEDIA_MM("prc_5_110x220mm", NULL, "EnvPRC5", 110, 220), + _PWG_MEDIA_MM("prc_8_120x309mm", NULL, "EnvPRC8", 120, 309), + _PWG_MEDIA_MM("prc_6_120x320mm", NULL, NULL, 120, 320), + _PWG_MEDIA_MM("prc_3_125x176mm", NULL, "EnvPRC3", 125, 176), + _PWG_MEDIA_MM("prc_16k_146x215mm", NULL, "PRC16K", 146, 215), + _PWG_MEDIA_MM("prc_7_160x230mm", NULL, "EnvPRC7", 160, 230), + _PWG_MEDIA_MM("om_juuro-ku-kai_198x275mm", NULL, NULL, 198, 275), + _PWG_MEDIA_MM("om_pa-kai_267x389mm", NULL, NULL, 267, 389), + _PWG_MEDIA_MM("om_dai-pa-kai_275x395mm", NULL, NULL, 275, 395), + _PWG_MEDIA_MM("prc_10_324x458mm", NULL, "EnvPRC10", 324, 458), + + /* Other English Standard Sheet Media Sizes */ + _PWG_MEDIA_IN("oe_photo-l_3.5x5in", NULL, "3.5x5", 3.5, 5), + + /* Other Metric Standard Sheet Media Sizes */ + _PWG_MEDIA_MM("om_small-photo_100x150mm", NULL, "om_small-photo", 100, 150), + _PWG_MEDIA_MM("om_italian_110x230mm", NULL, "EnvItalian", 110, 230), + _PWG_MEDIA_MM("om_postfix_114x229mm", NULL, NULL, 114, 229), + _PWG_MEDIA_MM("om_large-photo_200x300", NULL, "om_large-photo", 200, 300), + _PWG_MEDIA_MM("om_folio_210x330mm", "folio", "Folio", 210, 330), + _PWG_MEDIA_MM("om_folio-sp_215x315mm", NULL, "FolioSP", 215, 315), + _PWG_MEDIA_MM("om_invite_220x220mm", NULL, "EnvInvite", 220, 220) +}; + + +/* + * '_pwgFormatInches()' - Convert and format PWG units as inches. + */ + +char * /* O - String */ +_pwgFormatInches(char *buf, /* I - Buffer */ + size_t bufsize, /* I - Size of buffer */ + int val) /* I - Value in hundredths of millimeters */ +{ + int thousandths, /* Thousandths of inches */ + integer, /* Integer portion */ + fraction; /* Fractional portion */ + + + /* + * Convert hundredths of millimeters to thousandths of inches and round to + * the nearest thousandth. + */ + + thousandths = (val * 1000 + 1270) / 2540; + integer = thousandths / 1000; + fraction = thousandths % 1000; + + /* + * Format as a pair of integers (avoids locale stuff), avoiding trailing + * zeros... + */ + + if (fraction == 0) + snprintf(buf, bufsize, "%d", integer); + else if (fraction % 10) + snprintf(buf, bufsize, "%d.%03d", integer, fraction); + else if (fraction % 100) + snprintf(buf, bufsize, "%d.%02d", integer, fraction / 10); + else + snprintf(buf, bufsize, "%d.%01d", integer, fraction / 100); + + return (buf); +} + + +/* + * '_pwgFormatMillimeters()' - Convert and format PWG units as millimeters. + */ + +char * /* O - String */ +_pwgFormatMillimeters(char *buf, /* I - Buffer */ + size_t bufsize, /* I - Size of buffer */ + int val) /* I - Value in hundredths of millimeters */ +{ + int integer, /* Integer portion */ + fraction; /* Fractional portion */ + + + /* + * Convert hundredths of millimeters to integer and fractional portions. + */ + + integer = val / 100; + fraction = val % 100; + + /* + * Format as a pair of integers (avoids locale stuff), avoiding trailing + * zeros... + */ + + if (fraction == 0) + snprintf(buf, bufsize, "%d", integer); + else if (fraction % 10) + snprintf(buf, bufsize, "%d.%02d", integer, fraction); + else + snprintf(buf, bufsize, "%d.%01d", integer, fraction / 10); + + return (buf); +} + + +/* + * '_pwgGenerateSize()' - Generate a PWG size keyword. + */ + +void +_pwgGenerateSize(char *keyword, /* I - Keyword buffer */ + size_t keysize, /* I - Size of keyword buffer */ + const char *prefix, /* I - Prefix for PWG size or NULL */ + const char *name, /* I - Size name or NULL */ + int width, /* I - Width of page in 2540ths */ + int length) /* I - Length of page in 2540ths */ +{ + const char *units; /* Units to report */ + char usize[12 + 1 + 12 + 3], /* Unit size: NNNNNNNNNNNNxNNNNNNNNNNNNuu */ + *uptr; /* Pointer into unit size */ + char *(*format)(char *, size_t, int); + /* Formatting function */ + + + if ((width % 635) == 0 && (length % 635) == 0) + { + /* + * Use inches since the size is a multiple of 1/4 inch. + */ + + units = "in"; + format = _pwgFormatInches; + + if (!prefix) + prefix = "oe"; + } + else + { + /* + * Use millimeters since the size is not a multiple of 1/4 inch. + */ + + units = "mm"; + format = _pwgFormatMillimeters; + + if (!prefix) + prefix = "om"; + } + + uptr = usize; + (*format)(uptr, sizeof(usize) - (uptr - usize), width); + uptr += strlen(uptr); + *uptr++ = 'x'; + (*format)(uptr, sizeof(usize) - (uptr - usize), length); + uptr += strlen(uptr); + + /* + * Safe because usize can hold up to 12 + 1 + 12 + 4 bytes. + */ + + memcpy(uptr, units, 3); + + if (!name) + name = usize; + + /* + * Format the name... + */ + + snprintf(keyword, keysize, "%s_%s_%s", prefix, name, usize); +} + + +/* + * '_pwgInitSize()' - Initialize a PWG size using IPP job template attributes. + */ + +int /* O - 1 if size was initialize, 0 otherwise */ +_pwgInitSize(_pwg_size_t *size, /* I - Size to initialize */ + ipp_t *job, /* I - Job template attributes */ + int *margins_set) /* O - 1 if margins were set, 0 otherwise */ +{ + ipp_attribute_t *media, /* media attribute */ + *media_bottom_margin, /* media-bottom-margin member attribute */ + *media_col, /* media-col attribute */ + *media_left_margin, /* media-left-margin member attribute */ + *media_right_margin, /* media-right-margin member attribute */ + *media_size, /* media-size member attribute */ + *media_top_margin, /* media-top-margin member attribute */ + *x_dimension, /* x-dimension member attribute */ + *y_dimension; /* y-dimension member attribute */ + _pwg_media_t *pwg; /* PWG media value */ + + + /* + * Range check input... + */ + + if (!size || !job || !margins_set) + return (0); + + /* + * Look for media-col and then media... + */ + + memset(size, 0, sizeof(_pwg_size_t)); + *margins_set = 0; + + if ((media_col = ippFindAttribute(job, "media-col", + IPP_TAG_BEGIN_COLLECTION)) != NULL) + { + /* + * Got media-col, look for media-size member attribute... + */ + + if ((media_size = ippFindAttribute(media_col->values[0].collection, + "media-size", + IPP_TAG_BEGIN_COLLECTION)) != NULL) + { + /* + * Got media-size, look for x-dimension and y-dimension member + * attributes... + */ + + x_dimension = ippFindAttribute(media_size->values[0].collection, + "x-dimension", IPP_TAG_INTEGER); + y_dimension = ippFindAttribute(media_size->values[0].collection, + "y-dimension", IPP_TAG_INTEGER); + + if (x_dimension && y_dimension) + { + size->width = x_dimension->values[0].integer; + size->length = y_dimension->values[0].integer; + } + else if (!x_dimension) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("Missing x-dimension in media-size."), 1); + return (0); + } + else if (!y_dimension) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("Missing y-dimension in media-size."), 1); + return (0); + } + } + else + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Missing media-size in media-col."), + 1); + return (0); + } + + /* media-*-margin */ + media_bottom_margin = ippFindAttribute(media_col->values[0].collection, + "media-bottom-margin", + IPP_TAG_INTEGER); + media_left_margin = ippFindAttribute(media_col->values[0].collection, + "media-left-margin", + IPP_TAG_INTEGER); + media_right_margin = ippFindAttribute(media_col->values[0].collection, + "media-right-margin", + IPP_TAG_INTEGER); + media_top_margin = ippFindAttribute(media_col->values[0].collection, + "media-top-margin", + IPP_TAG_INTEGER); + if (media_bottom_margin && media_left_margin && media_right_margin && + media_top_margin) + { + *margins_set = 1; + size->bottom = media_bottom_margin->values[0].integer; + size->left = media_left_margin->values[0].integer; + size->right = media_right_margin->values[0].integer; + size->top = media_top_margin->values[0].integer; + } + } + else + { + if ((media = ippFindAttribute(job, "media", IPP_TAG_NAME)) == NULL) + if ((media = ippFindAttribute(job, "media", IPP_TAG_KEYWORD)) == NULL) + if ((media = ippFindAttribute(job, "PageSize", IPP_TAG_NAME)) == NULL) + media = ippFindAttribute(job, "PageRegion", IPP_TAG_NAME); + + if (media) + { + const char *name = media->values[0].string.text; + /* Name string */ + + if ((pwg = _pwgMediaForPWG(name)) == NULL) + { + /* + * Not a PWG name, try a legacy name... + */ + + if ((pwg = _pwgMediaForLegacy(name)) == NULL) + { + /* + * Not a legacy name, try a PPD name... + */ + + const char *suffix; /* Suffix on media string */ + + pwg = _pwgMediaForPPD(name); + if (pwg && + (suffix = name + strlen(name) - 10 /* .FullBleed */) > name && + !_cups_strcasecmp(suffix, ".FullBleed")) + { + /* + * Indicate that margins are set with the default values of 0. + */ + + *margins_set = 1; + } + } + } + + if (pwg) + { + size->width = pwg->width; + size->length = pwg->length; + } + else + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Unsupported media value."), 1); + return (0); + } + } + else + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Missing media or media-col."), 1); + return (0); + } + } + + return (1); +} + + +/* + * '_pwgMediaForLegacy()' - Find a PWG media size by ISO/IPP legacy name. + */ + +_pwg_media_t * /* O - Matching size or NULL */ +_pwgMediaForLegacy( + const char *legacy) /* I - Legacy size name */ +{ + _pwg_media_t key; /* Search key */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + /* + * Range check input... + */ + + if (!legacy) + return (NULL); + + /* + * Build the lookup table for PWG names as needed... + */ + + if (!cg->leg_size_lut) + { + int i; /* Looping var */ + _pwg_media_t *size; /* Current size */ + + cg->leg_size_lut = cupsArrayNew((cups_array_func_t)pwg_compare_legacy, + NULL); + + for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), + size = (_pwg_media_t *)cups_pwg_media; + i > 0; + i --, size ++) + if (size->legacy) + cupsArrayAdd(cg->leg_size_lut, size); + } + + /* + * Lookup the name... + */ + + key.legacy = legacy; + return ((_pwg_media_t *)cupsArrayFind(cg->leg_size_lut, &key)); +} + + +/* + * '_pwgMediaForPPD()' - Find a PWG media size by Adobe PPD name. + */ + +_pwg_media_t * /* O - Matching size or NULL */ +_pwgMediaForPPD(const char *ppd) /* I - PPD size name */ +{ + _pwg_media_t key, /* Search key */ + *size; /* Matching size */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + /* + * Range check input... + */ + + if (!ppd) + return (NULL); + + /* + * Build the lookup table for PWG names as needed... + */ + + if (!cg->ppd_size_lut) + { + int i; /* Looping var */ + + cg->ppd_size_lut = cupsArrayNew((cups_array_func_t)pwg_compare_ppd, NULL); + + for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), + size = (_pwg_media_t *)cups_pwg_media; + i > 0; + i --, size ++) + if (size->ppd) + cupsArrayAdd(cg->ppd_size_lut, size); + } + + /* + * Lookup the name... + */ + + key.ppd = ppd; + if ((size = (_pwg_media_t *)cupsArrayFind(cg->ppd_size_lut, &key)) == NULL) + { + /* + * See if the name is of the form: + * + * [Custom.]WIDTHxLENGTH[.FullBleed] - Size in points/inches [borderless] + * [Custom.]WIDTHxLENGTHcm[.FullBleed] - Size in centimeters [borderless] + * [Custom.]WIDTHxLENGTHft[.FullBleed] - Size in feet [borderless] + * [Custom.]WIDTHxLENGTHin[.FullBleed] - Size in inches [borderless] + * [Custom.]WIDTHxLENGTHm[.FullBleed] - Size in meters [borderless] + * [Custom.]WIDTHxLENGTHmm[.FullBleed] - Size in millimeters [borderless] + * [Custom.]WIDTHxLENGTHpt[.FullBleed] - Size in points [borderless] + */ + + double w, l, /* Width and length of page */ + factor; /* Unit scaling factor */ + char *ptr; /* Pointer into name */ + struct lconv *loc; /* Locale data */ + int custom; /* Custom page size? */ + + if (!_cups_strncasecmp(ppd, "Custom.", 7)) + { + custom = 1; + factor = 2540.0 / 72.0; + ptr = (char *)ppd + 7; + } + else + { + custom = 0; + factor = 2540.0; + ptr = (char *)ppd; + } + + loc = localeconv(); + w = _cupsStrScand(ptr, &ptr, loc); + + if (ptr && ptr > ppd && *ptr == 'x') + { + l = _cupsStrScand(ptr + 1, &ptr, loc); + + if (ptr && + (!*ptr || + !_cups_strcasecmp(ptr, "FullBleed") || + !_cups_strcasecmp(ptr, ".FullBleed") || + !_cups_strcasecmp(ptr, "cm") || + !_cups_strcasecmp(ptr, "cm.FullBleed") || + !_cups_strcasecmp(ptr, "ft") || + !_cups_strcasecmp(ptr, "ft.FullBleed") || + !_cups_strcasecmp(ptr, "in") || + !_cups_strcasecmp(ptr, "in.FullBleed") || + !_cups_strcasecmp(ptr, "m") || + !_cups_strcasecmp(ptr, "m.FullBleed") || + !_cups_strcasecmp(ptr, "mm") || + !_cups_strcasecmp(ptr, "mm.FullBleed") || + !_cups_strcasecmp(ptr, "pt") || + !_cups_strcasecmp(ptr, "pt.FullBleed"))) + { + size = &(cg->pwg_media); + + if (!_cups_strncasecmp(ptr, "cm", 2)) + factor = 1000.0; + else if (!_cups_strncasecmp(ptr, "ft", 2)) + factor = 2540.0 * 12.0; + else if (!_cups_strncasecmp(ptr, "in", 2)) + factor = 2540.0; + else if (!_cups_strncasecmp(ptr, "mm", 2)) + factor = 100.0; + else if (*ptr == 'm' || *ptr == 'M') + factor = 100000.0; + else if (!_cups_strncasecmp(ptr, "pt", 2)) + factor = 2540.0 / 72.0; + + /* + * Not a standard size; convert it to a PWG custom name of the form: + * + * [oe|om]_WIDTHxHEIGHTuu_WIDTHxHEIGHTuu + */ + + size->width = (int)(w * factor); + size->length = (int)(l * factor); + size->pwg = cg->pwg_name; + + _pwgGenerateSize(cg->pwg_name, sizeof(cg->pwg_name), + custom ? "custom" : NULL, custom ? ppd + 7 : NULL, + size->width, size->length); + } + } + } + + return (size); +} + + +/* + * '_pwgMediaForPWG()' - Find a PWG media size by 5101.1 self-describing name. + */ + +_pwg_media_t * /* O - Matching size or NULL */ +_pwgMediaForPWG(const char *pwg) /* I - PWG size name */ +{ + char *ptr; /* Pointer into name */ + _pwg_media_t key, /* Search key */ + *size; /* Matching size */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + /* + * Range check input... + */ + + if (!pwg) + return (NULL); + + /* + * Build the lookup table for PWG names as needed... + */ + + if (!cg->pwg_size_lut) + { + int i; /* Looping var */ + + cg->pwg_size_lut = cupsArrayNew((cups_array_func_t)pwg_compare_pwg, NULL); + + for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), + size = (_pwg_media_t *)cups_pwg_media; + i > 0; + i --, size ++) + cupsArrayAdd(cg->pwg_size_lut, size); + } + + /* + * Lookup the name... + */ + + key.pwg = pwg; + if ((size = (_pwg_media_t *)cupsArrayFind(cg->pwg_size_lut, &key)) == NULL && + (ptr = (char *)strchr(pwg, '_')) != NULL && + (ptr = (char *)strchr(ptr + 1, '_')) != NULL) + { + /* + * Try decoding the self-describing name of the form: + * + * class_name_WWWxHHHin + * class_name_WWWxHHHmm + */ + + double w, l; /* Width and length of page */ + struct lconv *loc; /* Locale data */ + + ptr ++; + loc = localeconv(); + w = _cupsStrScand(ptr, &ptr, loc); + + if (ptr && *ptr == 'x') + { + l = _cupsStrScand(ptr + 1, &ptr, loc); + + if (ptr && (!strcmp(ptr, "in") || !strcmp(ptr, "mm"))) + { + size = &(cg->pwg_media); + + if (!strcmp(ptr, "mm")) + { + size->width = (int)(w * 100 + 0.5); + size->length = (int)(l * 100 + 0.5); + } + else + { + size->width = (int)(w * 2540 + 0.5); + size->length = (int)(l * 2540 + 0.5); + } + + strlcpy(cg->pwg_name, pwg, sizeof(cg->pwg_name)); + size->pwg = cg->pwg_name; + } + } + } + + return (size); +} + + +/* + * '_pwgMediaForSize()' - Get the PWG media name for a given size. + */ + +_pwg_media_t * /* O - PWG media name */ +_pwgMediaForSize(int width, /* I - Width in 2540ths */ + int length) /* I - Length in 2540ths */ +{ + int i; /* Looping var */ + _pwg_media_t *media, /* Current media */ + *best_media = NULL; /* Best match */ + int dw, dl, /* Difference in width and length */ + best_dw = 999, /* Best difference in width and length */ + best_dl = 999; + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + /* + * Range check input... + */ + + if (width <= 0 || length <= 0) + return (NULL); + + /* + * Look for a standard size... + */ + + for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), + media = (_pwg_media_t *)cups_pwg_media; + i > 0; + i --, media ++) + { + /* + * Adobe uses a size matching algorithm with an epsilon of 5 points, which + * is just about 176/2540ths... + */ + + dw = abs(media->width - width); + dl = abs(media->length - length); + + if (!dw && !dl) + return (media); + else if (dw < 176 && dl < 176) + { + if (dw <= best_dw && dl <= best_dl) + { + best_media = media; + best_dw = dw; + best_dl = dl; + } + } + } + + if (best_media) + return (best_media); + + /* + * Not a standard size; convert it to a PWG custom name of the form: + * + * custom_WIDTHxHEIGHTuu_WIDTHxHEIGHTuu + */ + + _pwgGenerateSize(cg->pwg_name, sizeof(cg->pwg_name), "custom", NULL, width, + length); + + cg->pwg_media.pwg = cg->pwg_name; + cg->pwg_media.width = width; + cg->pwg_media.length = length; + + return (&(cg->pwg_media)); +} + + +/* + * 'pwg_compare_legacy()' - Compare two sizes using the legacy names. + */ + +static int /* O - Result of comparison */ +pwg_compare_legacy(_pwg_media_t *a, /* I - First size */ + _pwg_media_t *b) /* I - Second size */ +{ + return (strcmp(a->legacy, b->legacy)); +} + + +/* + * 'pwg_compare_ppd()' - Compare two sizes using the PPD names. + */ + +static int /* O - Result of comparison */ +pwg_compare_ppd(_pwg_media_t *a, /* I - First size */ + _pwg_media_t *b) /* I - Second size */ +{ + return (strcmp(a->ppd, b->ppd)); +} + + +/* + * 'pwg_compare_pwg()' - Compare two sizes using the PWG names. + */ + +static int /* O - Result of comparison */ +pwg_compare_pwg(_pwg_media_t *a, /* I - First size */ + _pwg_media_t *b) /* I - Second size */ +{ + return (strcmp(a->pwg, b->pwg)); +} + + +/* + * End of "$Id: pwg-media.c 10979 2013-05-13 17:39:19Z msweet $". + */ diff --git a/cups/pwg-private.h b/cups/pwg-private.h new file mode 100644 index 0000000..3c7a1a7 --- /dev/null +++ b/cups/pwg-private.h @@ -0,0 +1,104 @@ +/* + * "$Id: pwg-private.h 3833 2012-05-23 22:51:18Z msweet $" + * + * Private PWG media API definitions for CUPS. + * + * Copyright 2009-2012 by Apple Inc. + * + * 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. + */ + +#ifndef _CUPS_PWG_PRIVATE_H_ +# define _CUPS_PWG_PRIVATE_H_ + + +/* + * Include necessary headers... + */ + +# include + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Macros... + */ + +/* Convert from points to 2540ths */ +# define _PWG_FROMPTS(n) (int)(((n) * 2540 + 36) / 72) +/* Convert from 2540ths to points */ +# define _PWG_TOPTS(n) ((n) * 72.0 / 2540.0) + + +/* + * Types and structures... + */ + +typedef struct _pwg_map_s /**** Map element - PPD to/from PWG */ +{ + char *pwg, /* PWG media keyword */ + *ppd; /* PPD option keyword */ +} _pwg_map_t; + +typedef struct _pwg_media_s /**** Common media size data ****/ +{ + const char *pwg, /* PWG 5101.1 "self describing" name */ + *legacy, /* IPP/ISO legacy name */ + *ppd; /* Standard Adobe PPD name */ + int width, /* Width in 2540ths */ + length; /* Length in 2540ths */ +} _pwg_media_t; + +typedef struct _pwg_size_s /**** Size element - PPD to/from PWG */ +{ + _pwg_map_t map; /* Map element */ + int width, /* Width in 2540ths */ + length, /* Length in 2540ths */ + left, /* Left margin in 2540ths */ + bottom, /* Bottom margin in 2540ths */ + right, /* Right margin in 2540ths */ + top; /* Top margin in 2540ths */ +} _pwg_size_t; + + +/* + * Functions... + */ + +extern char *_pwgFormatInches(char *buf, size_t bufsize, int val); +extern char *_pwgFormatMillimeters(char *buf, size_t bufsize, + int val); +extern void _pwgGenerateSize(char *keyword, size_t keysize, + const char *prefix, + const char *name, + int width, int length); +extern int _pwgInitSize(_pwg_size_t *size, ipp_t *job, + int *margins_set); +extern _pwg_media_t *_pwgMediaForLegacy(const char *legacy); +extern _pwg_media_t *_pwgMediaForPPD(const char *ppd); +extern _pwg_media_t *_pwgMediaForPWG(const char *pwg); +extern _pwg_media_t *_pwgMediaForSize(int width, int length); + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_PWG_PRIVATE_H_ */ + +/* + * End of "$Id: pwg-private.h 3833 2012-05-23 22:51:18Z msweet $". + */ diff --git a/cups/raster-private.h b/cups/raster-private.h new file mode 100644 index 0000000..ebd5d72 --- /dev/null +++ b/cups/raster-private.h @@ -0,0 +1,66 @@ +/* + * "$Id: raster-private.h 3794 2012-04-23 22:44:16Z msweet $" + * + * Private image library definitions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1993-2006 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. + */ + +#ifndef _CUPS_RASTER_PRIVATE_H_ +# define _CUPS_RASTER_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include "raster.h" +# include +# include +# include +# ifdef WIN32 +# include +# include /* for htonl() definition */ +# else +# include +# include +# endif /* WIN32 */ + + +/* + * min/max macros... + */ + +# ifndef max +# define max(a,b) ((a) > (b) ? (a) : (b)) +# endif /* !max */ +# ifndef min +# define min(a,b) ((a) < (b) ? (a) : (b)) +# endif /* !min */ + + +/* + * Prototypes... + */ + +extern int _cupsRasterExecPS(cups_page_header2_t *h, + int *preferred_bits, + const char *code) + __attribute__((nonnull(3))); +extern void _cupsRasterAddError(const char *f, ...) + __attribute__((__format__(__printf__, 1, 2))); +extern void _cupsRasterClearError(void); + +#endif /* !_CUPS_RASTER_PRIVATE_H_ */ + +/* + * End of "$Id: raster-private.h 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/raster.h b/cups/raster.h new file mode 100644 index 0000000..a0bbdda --- /dev/null +++ b/cups/raster.h @@ -0,0 +1,405 @@ +/* + * "$Id: raster.h 3794 2012-04-23 22:44:16Z msweet $" + * + * Raster file definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products. + * + * This file is part of the CUPS Imaging library. + * + * 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. + */ + +#ifndef _CUPS_RASTER_H_ +# define _CUPS_RASTER_H_ + +/* + * Include necessary headers... + */ + +# include "cups.h" +# include "ppd.h" + + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + +/* + * Every non-PostScript printer driver that supports raster images + * should use the application/vnd.cups-raster image file format. + * Since both the PostScript RIP (pstoraster, based on GNU/GPL + * Ghostscript) and Image RIP (imagetoraster, located in the filter + * directory) use it, using this format saves you a lot of work. + * Also, the PostScript RIP passes any printer options that are in + * a PS file to your driver this way as well... + */ + +/* + * Constants... + */ + +# define CUPS_RASTER_SYNC 0x52615333 /* RaS3 */ +# define CUPS_RASTER_REVSYNC 0x33536152 /* 3SaR */ + +# define CUPS_RASTER_SYNCv1 0x52615374 /* RaSt */ +# define CUPS_RASTER_REVSYNCv1 0x74536152 /* tSaR */ + +# define CUPS_RASTER_SYNCv2 0x52615332 /* RaS2 */ +# define CUPS_RASTER_REVSYNCv2 0x32536152 /* 2SaR */ + +# define CUPS_RASTER_SYNC_PWG CUPS_RASTER_SYNCv2 + + +/* + * The following definition can be used to determine if the + * colorimetric colorspaces (CIEXYZ, CIELAB, and ICCn) are + * defined... + */ + +# define CUPS_RASTER_HAVE_COLORIMETRIC 1 + +/* + * The following definition can be used to determine if the + * device colorspaces (DEVICEn) are defined... + */ + +# define CUPS_RASTER_HAVE_DEVICE 1 + +/* + * The following definition can be used to determine if PWG Raster is supported. + */ + +# define CUPS_RASTER_HAVE_PWGRASTER 1 + + +/* + * Types... + */ + +typedef enum cups_adv_e /**** AdvanceMedia attribute values ****/ +{ + CUPS_ADVANCE_NONE = 0, /* Never advance the roll */ + CUPS_ADVANCE_FILE = 1, /* Advance the roll after this file */ + CUPS_ADVANCE_JOB = 2, /* Advance the roll after this job */ + CUPS_ADVANCE_SET = 3, /* Advance the roll after this set */ + CUPS_ADVANCE_PAGE = 4 /* Advance the roll after this page */ +} cups_adv_t; + +typedef enum cups_bool_e /**** Boolean type ****/ +{ + CUPS_FALSE = 0, /* Logical false */ + CUPS_TRUE = 1 /* Logical true */ +} cups_bool_t; + +typedef enum cups_cspace_e /**** cupsColorSpace attribute values ****/ +{ + CUPS_CSPACE_W = 0, /* Luminance (DeviceGray, gamma 2.2 by default) */ + CUPS_CSPACE_RGB = 1, /* Red, green, blue (DeviceRGB, sRGB by default) */ + CUPS_CSPACE_RGBA = 2, /* Red, green, blue, alpha (DeviceRGB, sRGB by default) */ + CUPS_CSPACE_K = 3, /* Black (DeviceK) */ + CUPS_CSPACE_CMY = 4, /* Cyan, magenta, yellow (DeviceCMY) */ + CUPS_CSPACE_YMC = 5, /* Yellow, magenta, cyan @deprecated@ */ + CUPS_CSPACE_CMYK = 6, /* Cyan, magenta, yellow, black (DeviceCMYK) */ + CUPS_CSPACE_YMCK = 7, /* Yellow, magenta, cyan, black @deprecated@ */ + CUPS_CSPACE_KCMY = 8, /* Black, cyan, magenta, yellow @deprecated@ */ + CUPS_CSPACE_KCMYcm = 9, /* Black, cyan, magenta, yellow, light-cyan, light-magenta @deprecated@ */ + CUPS_CSPACE_GMCK = 10, /* Gold, magenta, yellow, black @deprecated@ */ + CUPS_CSPACE_GMCS = 11, /* Gold, magenta, yellow, silver @deprecated@ */ + CUPS_CSPACE_WHITE = 12, /* White ink (as black) @deprecated@ */ + CUPS_CSPACE_GOLD = 13, /* Gold foil @deprecated@ */ + CUPS_CSPACE_SILVER = 14, /* Silver foil @deprecated@ */ + + CUPS_CSPACE_CIEXYZ = 15, /* CIE XYZ @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_CIELab = 16, /* CIE Lab @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_RGBW = 17, /* Red, green, blue, white (DeviceRGB, sRGB by default) @since CUPS 1.2/OS X 10.5@ */ + CUPS_CSPACE_SW = 18, /* Luminance (gamma 2.2) @since CUPS 1.4.5@ */ + CUPS_CSPACE_SRGB = 19, /* Red, green, blue (sRGB) @since CUPS 1.4.5@ */ + CUPS_CSPACE_ADOBERGB = 20, /* Red, green, blue (Adobe RGB) @since CUPS 1.4.5@ */ + + CUPS_CSPACE_ICC1 = 32, /* ICC-based, 1 color @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC2 = 33, /* ICC-based, 2 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC3 = 34, /* ICC-based, 3 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC4 = 35, /* ICC-based, 4 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC5 = 36, /* ICC-based, 5 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC6 = 37, /* ICC-based, 6 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC7 = 38, /* ICC-based, 7 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC8 = 39, /* ICC-based, 8 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICC9 = 40, /* ICC-based, 9 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICCA = 41, /* ICC-based, 10 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICCB = 42, /* ICC-based, 11 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICCC = 43, /* ICC-based, 12 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICCD = 44, /* ICC-based, 13 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICCE = 45, /* ICC-based, 14 colors @since CUPS 1.1.19/OS X 10.3@ */ + CUPS_CSPACE_ICCF = 46, /* ICC-based, 15 colors @since CUPS 1.1.19/OS X 10.3@ */ + + CUPS_CSPACE_DEVICE1 = 48, /* DeviceN, 1 color @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE2 = 49, /* DeviceN, 2 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE3 = 50, /* DeviceN, 3 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE4 = 51, /* DeviceN, 4 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE5 = 52, /* DeviceN, 5 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE6 = 53, /* DeviceN, 6 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE7 = 54, /* DeviceN, 7 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE8 = 55, /* DeviceN, 8 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICE9 = 56, /* DeviceN, 9 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICEA = 57, /* DeviceN, 10 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICEB = 58, /* DeviceN, 11 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICEC = 59, /* DeviceN, 12 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICED = 60, /* DeviceN, 13 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICEE = 61, /* DeviceN, 14 colors @since CUPS 1.4.5@ */ + CUPS_CSPACE_DEVICEF = 62 /* DeviceN, 15 colors @since CUPS 1.4.5@ */ +} cups_cspace_t; + +typedef enum cups_cut_e /**** CutMedia attribute values ****/ +{ + CUPS_CUT_NONE = 0, /* Never cut the roll */ + CUPS_CUT_FILE = 1, /* Cut the roll after this file */ + CUPS_CUT_JOB = 2, /* Cut the roll after this job */ + CUPS_CUT_SET = 3, /* Cut the roll after this set */ + CUPS_CUT_PAGE = 4 /* Cut the roll after this page */ +} cups_cut_t; + +typedef enum cups_edge_e /**** LeadingEdge attribute values ****/ +{ + CUPS_EDGE_TOP = 0, /* Leading edge is the top of the page */ + CUPS_EDGE_RIGHT = 1, /* Leading edge is the right of the page */ + CUPS_EDGE_BOTTOM = 2, /* Leading edge is the bottom of the page */ + CUPS_EDGE_LEFT = 3 /* Leading edge is the left of the page */ +} cups_edge_t; + +typedef enum cups_jog_e /**** Jog attribute values ****/ +{ + CUPS_JOG_NONE = 0, /* Never move pages */ + CUPS_JOG_FILE = 1, /* Move pages after this file */ + CUPS_JOG_JOB = 2, /* Move pages after this job */ + CUPS_JOG_SET = 3 /* Move pages after this set */ +} cups_jog_t; + +enum cups_mode_e /**** cupsRasterOpen modes ****/ +{ + CUPS_RASTER_READ = 0, /* Open stream for reading */ + CUPS_RASTER_WRITE = 1, /* Open stream for writing */ + CUPS_RASTER_WRITE_COMPRESSED = 2, /* Open stream for compressed writing @since CUPS 1.3/OS X 10.5@ */ + CUPS_RASTER_WRITE_PWG = 3 /* Open stream for compressed writing in PWG mode @since CUPS 1.5/OS X 10.7@ */ +}; + +typedef enum cups_mode_e cups_mode_t; /**** cupsRasterOpen modes ****/ + +typedef enum cups_order_e /**** cupsColorOrder attribute values ****/ +{ + CUPS_ORDER_CHUNKED = 0, /* CMYK CMYK CMYK ... */ + CUPS_ORDER_BANDED = 1, /* CCC MMM YYY KKK ... */ + CUPS_ORDER_PLANAR = 2 /* CCC ... MMM ... YYY ... KKK ... */ +} cups_order_t; + +typedef enum cups_orient_e /**** Orientation attribute values ****/ +{ + CUPS_ORIENT_0 = 0, /* Don't rotate the page */ + CUPS_ORIENT_90 = 1, /* Rotate the page counter-clockwise */ + CUPS_ORIENT_180 = 2, /* Turn the page upside down */ + CUPS_ORIENT_270 = 3 /* Rotate the page clockwise */ +} cups_orient_t; + + +/* + * The page header structure contains the standard PostScript page device + * dictionary, along with some CUPS-specific parameters that are provided + * by the RIPs... + * + * The API supports a "version 1" (from CUPS 1.0 and 1.1) and a "version 2" + * (from CUPS 1.2 and higher) page header, for binary compatibility. + */ + +typedef struct cups_page_header_s /**** Version 1 page header @deprecated@ ****/ +{ + /**** Standard Page Device Dictionary String Values ****/ + char MediaClass[64]; /* MediaClass string */ + char MediaColor[64]; /* MediaColor string */ + char MediaType[64]; /* MediaType string */ + char OutputType[64]; /* OutputType string */ + + /**** Standard Page Device Dictionary Integer Values ****/ + unsigned AdvanceDistance; /* AdvanceDistance value in points */ + cups_adv_t AdvanceMedia; /* AdvanceMedia value (@link cups_adv_t@) */ + cups_bool_t Collate; /* Collated copies value */ + cups_cut_t CutMedia; /* CutMedia value (@link cups_cut_t@) */ + cups_bool_t Duplex; /* Duplexed (double-sided) value */ + unsigned HWResolution[2]; /* Resolution in dots-per-inch */ + unsigned ImagingBoundingBox[4]; /* Pixel region that is painted (points, left, bottom, right, top) */ + cups_bool_t InsertSheet; /* InsertSheet value */ + cups_jog_t Jog; /* Jog value (@link cups_jog_t@) */ + cups_edge_t LeadingEdge; /* LeadingEdge value (@link cups_edge_t@) */ + unsigned Margins[2]; /* Lower-lefthand margins in points */ + cups_bool_t ManualFeed; /* ManualFeed value */ + unsigned MediaPosition; /* MediaPosition value */ + unsigned MediaWeight; /* MediaWeight value in grams/m^2 */ + cups_bool_t MirrorPrint; /* MirrorPrint value */ + cups_bool_t NegativePrint; /* NegativePrint value */ + unsigned NumCopies; /* Number of copies to produce */ + cups_orient_t Orientation; /* Orientation value (@link cups_orient_t@) */ + cups_bool_t OutputFaceUp; /* OutputFaceUp value */ + unsigned PageSize[2]; /* Width and length of page in points */ + cups_bool_t Separations; /* Separations value */ + cups_bool_t TraySwitch; /* TraySwitch value */ + cups_bool_t Tumble; /* Tumble value */ + + /**** CUPS Page Device Dictionary Values ****/ + unsigned cupsWidth; /* Width of page image in pixels */ + unsigned cupsHeight; /* Height of page image in pixels */ + unsigned cupsMediaType; /* Media type code */ + unsigned cupsBitsPerColor; /* Number of bits for each color */ + unsigned cupsBitsPerPixel; /* Number of bits for each pixel */ + unsigned cupsBytesPerLine; /* Number of bytes per line */ + cups_order_t cupsColorOrder; /* Order of colors */ + cups_cspace_t cupsColorSpace; /* True colorspace */ + unsigned cupsCompression; /* Device compression to use */ + unsigned cupsRowCount; /* Rows per band */ + unsigned cupsRowFeed; /* Feed between bands */ + unsigned cupsRowStep; /* Spacing between lines */ +} cups_page_header_t; + +/**** New in CUPS 1.2 ****/ +typedef struct cups_page_header2_s /**** Version 2 page header @since CUPS 1.2/OS X 10.5@ ****/ +{ + /**** Standard Page Device Dictionary String Values ****/ + char MediaClass[64]; /* MediaClass string */ + char MediaColor[64]; /* MediaColor string */ + char MediaType[64]; /* MediaType string */ + char OutputType[64]; /* OutputType string */ + + /**** Standard Page Device Dictionary Integer Values ****/ + unsigned AdvanceDistance; /* AdvanceDistance value in points */ + cups_adv_t AdvanceMedia; /* AdvanceMedia value (@link cups_adv_t@) */ + cups_bool_t Collate; /* Collated copies value */ + cups_cut_t CutMedia; /* CutMedia value (@link cups_cut_t@) */ + cups_bool_t Duplex; /* Duplexed (double-sided) value */ + unsigned HWResolution[2]; /* Resolution in dots-per-inch */ + unsigned ImagingBoundingBox[4]; /* Pixel region that is painted (points, left, bottom, right, top) */ + cups_bool_t InsertSheet; /* InsertSheet value */ + cups_jog_t Jog; /* Jog value (@link cups_jog_t@) */ + cups_edge_t LeadingEdge; /* LeadingEdge value (@link cups_edge_t@) */ + unsigned Margins[2]; /* Lower-lefthand margins in points */ + cups_bool_t ManualFeed; /* ManualFeed value */ + unsigned MediaPosition; /* MediaPosition value */ + unsigned MediaWeight; /* MediaWeight value in grams/m^2 */ + cups_bool_t MirrorPrint; /* MirrorPrint value */ + cups_bool_t NegativePrint; /* NegativePrint value */ + unsigned NumCopies; /* Number of copies to produce */ + cups_orient_t Orientation; /* Orientation value (@link cups_orient_t@) */ + cups_bool_t OutputFaceUp; /* OutputFaceUp value */ + unsigned PageSize[2]; /* Width and length of page in points */ + cups_bool_t Separations; /* Separations value */ + cups_bool_t TraySwitch; /* TraySwitch value */ + cups_bool_t Tumble; /* Tumble value */ + + /**** CUPS Page Device Dictionary Values ****/ + unsigned cupsWidth; /* Width of page image in pixels */ + unsigned cupsHeight; /* Height of page image in pixels */ + unsigned cupsMediaType; /* Media type code */ + unsigned cupsBitsPerColor; /* Number of bits for each color */ + unsigned cupsBitsPerPixel; /* Number of bits for each pixel */ + unsigned cupsBytesPerLine; /* Number of bytes per line */ + cups_order_t cupsColorOrder; /* Order of colors */ + cups_cspace_t cupsColorSpace; /* True colorspace */ + unsigned cupsCompression; /* Device compression to use */ + unsigned cupsRowCount; /* Rows per band */ + unsigned cupsRowFeed; /* Feed between bands */ + unsigned cupsRowStep; /* Spacing between lines */ + + /**** Version 2 Dictionary Values ****/ + unsigned cupsNumColors; /* Number of color compoents @since CUPS 1.2/OS X 10.5@ */ + float cupsBorderlessScalingFactor; + /* Scaling that was applied to page data @since CUPS 1.2/OS X 10.5@ */ + float cupsPageSize[2]; /* Floating point PageSize (scaling * + * factor not applied) @since CUPS 1.2/OS X 10.5@ */ + float cupsImagingBBox[4]; /* Floating point ImagingBoundingBox + * (scaling factor not applied, left, + * bottom, right, top) @since CUPS 1.2/OS X 10.5@ */ + unsigned cupsInteger[16]; /* User-defined integer values @since CUPS 1.2/OS X 10.5@ */ + float cupsReal[16]; /* User-defined floating-point values @since CUPS 1.2/OS X 10.5@ */ + char cupsString[16][64]; /* User-defined string values @since CUPS 1.2/OS X 10.5@ */ + char cupsMarkerType[64]; /* Ink/toner type @since CUPS 1.2/OS X 10.5@ */ + char cupsRenderingIntent[64];/* Color rendering intent @since CUPS 1.2/OS X 10.5@ */ + char cupsPageSizeName[64]; /* PageSize name @since CUPS 1.2/OS X 10.5@ */ +} cups_page_header2_t; + +typedef struct _cups_raster_s cups_raster_t; + /**** Raster stream data ****/ + +typedef int (*cups_interpret_cb_t)(cups_page_header2_t *header, int preferred_bits); + /**** cupsRasterInterpretPPD callback function + * + * This function is called by + * @link cupsRasterInterpretPPD@ to + * validate (and update, as needed) + * the page header attributes. The + * "preferred_bits" argument provides + * the value of the + * @code cupsPreferredBitsPerColor@ + * key from the PostScript page device + * dictionary and is 0 if undefined. + ****/ + +/**** New in CUPS 1.5 ****/ +typedef ssize_t (*cups_raster_iocb_t)(void *ctx, unsigned char *buffer, size_t length); + /**** cupsRasterOpenIO callback function + * + * This function is specified when + * creating a raster stream with + * @link cupsRasterOpenIO@ and handles + * generic reading and writing of raster + * data. It must return -1 on error or + * the number of bytes specified by + * "length" on success. + ****/ + + +/* + * Prototypes... + */ + +extern void cupsRasterClose(cups_raster_t *r); +extern cups_raster_t *cupsRasterOpen(int fd, cups_mode_t mode); +extern unsigned cupsRasterReadHeader(cups_raster_t *r, + cups_page_header_t *h) _CUPS_DEPRECATED; +extern unsigned cupsRasterReadPixels(cups_raster_t *r, + unsigned char *p, unsigned len); +extern unsigned cupsRasterWriteHeader(cups_raster_t *r, + cups_page_header_t *h) _CUPS_DEPRECATED; +extern unsigned cupsRasterWritePixels(cups_raster_t *r, + unsigned char *p, unsigned len); + +/**** New in CUPS 1.2 ****/ +extern int cupsRasterInterpretPPD(cups_page_header2_t *h, + ppd_file_t *ppd, + int num_options, + cups_option_t *options, + cups_interpret_cb_t func) _CUPS_API_1_2; +extern unsigned cupsRasterReadHeader2(cups_raster_t *r, + cups_page_header2_t *h) _CUPS_API_1_2; +extern unsigned cupsRasterWriteHeader2(cups_raster_t *r, + cups_page_header2_t *h) _CUPS_API_1_2; + +/**** New in CUPS 1.3 ****/ +extern const char *cupsRasterErrorString(void) _CUPS_API_1_3; + +/**** New in CUPS 1.5 ****/ +extern cups_raster_t *cupsRasterOpenIO(cups_raster_iocb_t iocb, void *ctx, + cups_mode_t mode); + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_RASTER_H_ */ + +/* + * End of "$Id: raster.h 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/request.c b/cups/request.c new file mode 100644 index 0000000..59ac6dd --- /dev/null +++ b/cups/request.c @@ -0,0 +1,1168 @@ +/* + * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $" + * + * IPP utilities for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * cupsDoFileRequest() - Do an IPP request with a file. + * cupsDoIORequest() - Do an IPP request with file descriptors. + * cupsDoRequest() - Do an IPP request. + * cupsGetResponse() - Get a response to an IPP request. + * cupsLastError() - Return the last IPP status code. + * cupsLastErrorString() - Return the last IPP status-message. + * _cupsNextDelay() - Return the next retry delay value. + * cupsReadResponseData() - Read additional data after the IPP response. + * cupsSendRequest() - Send an IPP request. + * cupsWriteRequestData() - Write additional data after an IPP request. + * _cupsConnect() - Get the default server connection... + * _cupsSetError() - Set the last IPP status code and status-message. + * _cupsSetHTTPError() - Set the last error using the HTTP status. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#if defined(WIN32) || defined(__EMX__) +# include +#else +# include +#endif /* WIN32 || __EMX__ */ +#ifndef O_BINARY +# define O_BINARY 0 +#endif /* O_BINARY */ + + +/* + * 'cupsDoFileRequest()' - Do an IPP request with a file. + * + * This function sends the IPP request and attached file to the specified + * server, retrying and authenticating as necessary. The request is freed with + * @link ippDelete@. + */ + +ipp_t * /* O - Response data */ +cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + ipp_t *request, /* I - IPP request */ + const char *resource, /* I - HTTP resource for POST */ + const char *filename) /* I - File to send or @code NULL@ for none */ +{ + ipp_t *response; /* IPP response data */ + int infile; /* Input file */ + + + DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", " + "filename=\"%s\")", http, request, + request ? ippOpString(request->request.op.operation_id) : "?", + resource, filename)); + + if (filename) + { + if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0) + { + /* + * Can't get file information! + */ + + _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED, + NULL, 0); + + ippDelete(request); + + return (NULL); + } + } + else + infile = -1; + + response = cupsDoIORequest(http, request, resource, infile, -1); + + if (infile >= 0) + close(infile); + + return (response); +} + + +/* + * 'cupsDoIORequest()' - Do an IPP request with file descriptors. + * + * This function sends the IPP request with the optional input file "infile" to + * the specified server, retrying and authenticating as necessary. The request + * is freed with @link ippDelete@. + * + * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies + * all of the data from the file after the IPP request message. + * + * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies + * all of the data after the IPP response message to the file. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +ipp_t * /* O - Response data */ +cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + ipp_t *request, /* I - IPP request */ + const char *resource, /* I - HTTP resource for POST */ + int infile, /* I - File to read from or -1 for none */ + int outfile) /* I - File to write to or -1 for none */ +{ + ipp_t *response = NULL; /* IPP response data */ + size_t length = 0; /* Content-Length value */ + http_status_t status; /* Status of HTTP request */ + struct stat fileinfo; /* File information */ + int bytes; /* Number of bytes read/written */ + char buffer[32768]; /* Output buffer */ + + + DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", " + "infile=%d, outfile=%d)", http, request, + request ? ippOpString(request->request.op.operation_id) : "?", + resource, infile, outfile)); + + /* + * Range check input... + */ + + if (!request || !resource) + { + ippDelete(request); + + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + return (NULL); + } + + /* + * Get the default connection as needed... + */ + + if (!http) + if ((http = _cupsConnect()) == NULL) + { + ippDelete(request); + + return (NULL); + } + + /* + * See if we have a file to send... + */ + + if (infile >= 0) + { + if (fstat(infile, &fileinfo)) + { + /* + * Can't get file information! + */ + + _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED, + NULL, 0); + + ippDelete(request); + + return (NULL); + } + +#ifdef WIN32 + if (fileinfo.st_mode & _S_IFDIR) +#else + if (S_ISDIR(fileinfo.st_mode)) +#endif /* WIN32 */ + { + /* + * Can't send a directory... + */ + + ippDelete(request); + + _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR), 0); + + return (NULL); + } + +#ifndef WIN32 + if (!S_ISREG(fileinfo.st_mode)) + length = 0; /* Chunk when piping */ + else +#endif /* !WIN32 */ + length = ippLength(request) + fileinfo.st_size; + } + else + length = ippLength(request); + + DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld", + (long)ippLength(request), (long)length)); + + /* + * Clear any "Local" authentication data since it is probably stale... + */ + + if (http->authstring && !strncmp(http->authstring, "Local ", 6)) + httpSetAuthString(http, NULL, NULL); + + /* + * Loop until we can send the request without authorization problems. + */ + + while (response == NULL) + { + DEBUG_puts("2cupsDoIORequest: setup..."); + + /* + * Send the request... + */ + + status = cupsSendRequest(http, request, resource, length); + + DEBUG_printf(("2cupsDoIORequest: status=%d", status)); + + if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0) + { + DEBUG_puts("2cupsDoIORequest: file write..."); + + /* + * Send the file with the request... + */ + +#ifndef WIN32 + if (S_ISREG(fileinfo.st_mode)) +#endif /* WIN32 */ + lseek(infile, 0, SEEK_SET); + + while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0) + { + if ((status = cupsWriteRequestData(http, buffer, bytes)) + != HTTP_CONTINUE) + break; + } + } + + /* + * Get the server's response... + */ + + if (status != HTTP_ERROR) + { + response = cupsGetResponse(http, resource); + status = httpGetStatus(http); + } + + DEBUG_printf(("2cupsDoIORequest: status=%d", status)); + + if (status == HTTP_ERROR || + (status >= HTTP_BAD_REQUEST && status != HTTP_UNAUTHORIZED && + status != HTTP_UPGRADE_REQUIRED)) + { + _cupsSetHTTPError(status); + break; + } + + if (response && outfile >= 0) + { + /* + * Write trailing data to file... + */ + + while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0) + if (write(outfile, buffer, bytes) < bytes) + break; + } + + if (http->state != HTTP_WAITING) + { + /* + * Flush any remaining data... + */ + + httpFlush(http); + } + } + + /* + * Delete the original request and return the response... + */ + + ippDelete(request); + + return (response); +} + + +/* + * 'cupsDoRequest()' - Do an IPP request. + * + * This function sends the IPP request to the specified server, retrying + * and authenticating as necessary. The request is freed with @link ippDelete@. + */ + +ipp_t * /* O - Response data */ +cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + ipp_t *request, /* I - IPP request */ + const char *resource) /* I - HTTP resource for POST */ +{ + DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")", + http, request, + request ? ippOpString(request->request.op.operation_id) : "?", + resource)); + + return (cupsDoIORequest(http, request, resource, -1, -1)); +} + + +/* + * 'cupsGetResponse()' - Get a response to an IPP request. + * + * Use this function to get the response for an IPP request sent using + * @link cupsSendRequest@. For requests that return additional data, use + * @link cupsReadResponseData@ after getting a successful response, + * otherwise call @link httpFlush@ to complete the response processing. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +ipp_t * /* O - Response or @code NULL@ on HTTP error */ +cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *resource) /* I - HTTP resource for POST */ +{ + http_status_t status; /* HTTP status */ + ipp_state_t state; /* IPP read state */ + ipp_t *response = NULL; /* IPP response */ + + + DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http, resource)); + + /* + * Connect to the default server as needed... + */ + + if (!http) + http = _cupsConnect(); + + if (!http || (http->state != HTTP_POST_RECV && http->state != HTTP_POST_SEND)) + return (NULL); + + /* + * Check for an unfinished chunked request... + */ + + if (http->data_encoding == HTTP_ENCODE_CHUNKED) + { + /* + * Send a 0-length chunk to finish off the request... + */ + + DEBUG_puts("2cupsGetResponse: Finishing chunked POST..."); + + if (httpWrite2(http, "", 0) < 0) + return (NULL); + } + + /* + * Wait for a response from the server... + */ + + DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...", + http->status)); + + do + { + status = httpUpdate(http); + } + while (status != HTTP_ERROR && http->state == HTTP_POST_RECV); + + DEBUG_printf(("2cupsGetResponse: status=%d", status)); + + if (status == HTTP_OK) + { + /* + * Get the IPP response... + */ + + response = ippNew(); + + while ((state = ippRead(http, response)) != IPP_DATA) + if (state == IPP_ERROR) + break; + + if (state == IPP_ERROR) + { + /* + * Flush remaining data and delete the response... + */ + + DEBUG_puts("1cupsGetResponse: IPP read error!"); + + httpFlush(http); + + ippDelete(response); + response = NULL; + + http->status = status = HTTP_ERROR; + http->error = EINVAL; + } + } + else if (status != HTTP_ERROR) + { + /* + * Flush any error message... + */ + + httpFlush(http); + + /* + * Then handle encryption and authentication... + */ + + if (status == HTTP_UNAUTHORIZED) + { + /* + * See if we can do authentication... + */ + + DEBUG_puts("2cupsGetResponse: Need authorization..."); + + if (!cupsDoAuthentication(http, "POST", resource)) + httpReconnect(http); + else + http->status = status = HTTP_AUTHORIZATION_CANCELED; + } + +#ifdef HAVE_SSL + else if (status == HTTP_UPGRADE_REQUIRED) + { + /* + * Force a reconnect with encryption... + */ + + DEBUG_puts("2cupsGetResponse: Need encryption..."); + + if (!httpReconnect(http)) + httpEncryption(http, HTTP_ENCRYPT_REQUIRED); + } +#endif /* HAVE_SSL */ + } + + if (response) + { + ipp_attribute_t *attr; /* status-message attribute */ + + + attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); + + DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"", + ippErrorString(response->request.status.status_code), + attr ? attr->values[0].string.text : "")); + + _cupsSetError(response->request.status.status_code, + attr ? attr->values[0].string.text : + ippErrorString(response->request.status.status_code), 0); + } + + return (response); +} + + +/* + * 'cupsLastError()' - Return the last IPP status code received on the current + * thread. + */ + +ipp_status_t /* O - IPP status code from last request */ +cupsLastError(void) +{ + return (_cupsGlobals()->last_error); +} + + +/* + * 'cupsLastErrorString()' - Return the last IPP status-message received on the + * current thread. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +const char * /* O - status-message text from last request */ +cupsLastErrorString(void) +{ + return (_cupsGlobals()->last_status_message); +} + + +/* + * '_cupsNextDelay()' - Return the next retry delay value. + * + * This function currently returns the Fibonacci sequence 1 1 2 3 5 8. + * + * Pass 0 for the current delay value to initialize the sequence. + */ + +int /* O - Next delay value */ +_cupsNextDelay(int current, /* I - Current delay value or 0 */ + int *previous) /* IO - Previous delay value */ +{ + int next; /* Next delay value */ + + + if (current > 0) + { + next = (current + *previous) % 12; + *previous = next < current ? 0 : current; + } + else + { + next = 1; + *previous = 0; + } + + return (next); +} + + +/* + * 'cupsReadResponseData()' - Read additional data after the IPP response. + * + * This function is used after @link cupsGetResponse@ to read the PPD or document + * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests, + * respectively. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +ssize_t /* O - Bytes read, 0 on EOF, -1 on error */ +cupsReadResponseData( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + char *buffer, /* I - Buffer to use */ + size_t length) /* I - Number of bytes to read */ +{ + /* + * Get the default connection as needed... + */ + + DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, " + "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length)); + + if (!http) + { + _cups_globals_t *cg = _cupsGlobals(); + /* Pointer to library globals */ + + if ((http = cg->http) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1); + return (-1); + } + } + + /* + * Then read from the HTTP connection... + */ + + return (httpRead2(http, buffer, length)); +} + + +/* + * 'cupsSendRequest()' - Send an IPP request. + * + * Use @link cupsWriteRequestData@ to write any additional data (document, PPD + * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response, + * and @link cupsReadResponseData@ to read any additional data following the + * response. Only one request can be sent/queued at a time per @code http_t@ + * connection. + * + * Returns the initial HTTP status code, which will be @code HTTP_CONTINUE@ + * on a successful send of the request. + * + * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and + * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +http_status_t /* O - Initial HTTP status */ +cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + ipp_t *request, /* I - IPP request */ + const char *resource, /* I - Resource path */ + size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */ +{ + http_status_t status; /* Status of HTTP request */ + int got_status; /* Did we get the status? */ + ipp_state_t state; /* State of IPP processing */ + http_status_t expect; /* Expect: header to use */ + + + DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", " + "length=" CUPS_LLFMT ")", http, request, + request ? ippOpString(request->request.op.operation_id) : "?", + resource, CUPS_LLCAST length)); + + /* + * Range check input... + */ + + if (!request || !resource) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + return (HTTP_ERROR); + } + + /* + * Get the default connection as needed... + */ + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (HTTP_SERVICE_UNAVAILABLE); + + /* + * If the prior request was not flushed out, do so now... + */ + + if (http->state == HTTP_GET_SEND || + http->state == HTTP_POST_SEND) + { + DEBUG_puts("2cupsSendRequest: Flush prior response."); + httpFlush(http); + } + else if (http->state != HTTP_WAITING) + { + DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), " + "reconnecting.", http->state)); + if (httpReconnect(http)) + return (HTTP_ERROR); + } + +#ifdef HAVE_SSL + /* + * See if we have an auth-info attribute and are communicating over + * a non-local link. If so, encrypt the link so that we can pass + * the authentication information securely... + */ + + if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) && + !httpAddrLocalhost(http->hostaddr) && !http->tls && + httpEncryption(http, HTTP_ENCRYPT_REQUIRED)) + { + DEBUG_puts("1cupsSendRequest: Unable to encrypt connection."); + return (HTTP_SERVICE_UNAVAILABLE); + } +#endif /* HAVE_SSL */ + + /* + * Reconnect if the last response had a "Connection: close"... + */ + + if (!_cups_strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close")) + { + DEBUG_puts("2cupsSendRequest: Connection: close"); + httpClearFields(http); + if (httpReconnect(http)) + { + DEBUG_puts("1cupsSendRequest: Unable to reconnect."); + return (HTTP_SERVICE_UNAVAILABLE); + } + } + + /* + * Loop until we can send the request without authorization problems. + */ + + expect = HTTP_CONTINUE; + + for (;;) + { + DEBUG_puts("2cupsSendRequest: Setup..."); + + /* + * Setup the HTTP variables needed... + */ + + httpClearFields(http); + httpSetExpect(http, expect); + httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp"); + httpSetLength(http, length); + +#ifdef HAVE_GSSAPI + if (http->authstring && !strncmp(http->authstring, "Negotiate", 9)) + { + /* + * Do not use cached Kerberos credentials since they will look like a + * "replay" attack... + */ + + _cupsSetNegotiateAuthString(http, "POST", resource); + } +#endif /* HAVE_GSSAPI */ + + httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); + + DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http->authstring)); + + /* + * Try the request... + */ + + DEBUG_puts("2cupsSendRequest: Sending HTTP POST..."); + + if (httpPost(http, resource)) + { + DEBUG_puts("2cupsSendRequest: POST failed, reconnecting."); + if (httpReconnect(http)) + { + DEBUG_puts("1cupsSendRequest: Unable to reconnect."); + return (HTTP_SERVICE_UNAVAILABLE); + } + else + continue; + } + + /* + * Send the IPP data... + */ + + DEBUG_puts("2cupsSendRequest: Writing IPP request..."); + + request->state = IPP_IDLE; + status = HTTP_CONTINUE; + got_status = 0; + + while ((state = ippWrite(http, request)) != IPP_DATA) + if (state == IPP_ERROR) + break; + else if (httpCheck(http)) + { + got_status = 1; + + _httpUpdate(http, &status); + if (status >= HTTP_MULTIPLE_CHOICES) + break; + } + + if (state == IPP_ERROR) + { + DEBUG_puts("1cupsSendRequest: Unable to send IPP request."); + + http->status = HTTP_ERROR; + http->state = HTTP_WAITING; + + return (HTTP_ERROR); + } + + /* + * Wait up to 1 second to get the 100-continue response as needed... + */ + + if (!got_status) + { + if (expect == HTTP_CONTINUE) + { + DEBUG_puts("2cupsSendRequest: Waiting for 100-continue..."); + + if (httpWait(http, 1000)) + _httpUpdate(http, &status); + } + else if (httpCheck(http)) + _httpUpdate(http, &status); + } + + DEBUG_printf(("2cupsSendRequest: status=%d", status)); + + /* + * Process the current HTTP status... + */ + + if (status >= HTTP_MULTIPLE_CHOICES) + { + _cupsSetHTTPError(status); + + do + { + status = httpUpdate(http); + } + while (status != HTTP_ERROR && http->state == HTTP_POST_RECV); + + httpFlush(http); + } + + switch (status) + { + case HTTP_ERROR : + case HTTP_CONTINUE : + case HTTP_OK : + DEBUG_printf(("1cupsSendRequest: Returning %d.", status)); + return (status); + + case HTTP_UNAUTHORIZED : + if (cupsDoAuthentication(http, "POST", resource)) + { + DEBUG_puts("1cupsSendRequest: Returning HTTP_AUTHORIZATION_CANCELED."); + return (HTTP_AUTHORIZATION_CANCELED); + } + + DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_UNAUTHORIZED."); + + if (httpReconnect(http)) + { + DEBUG_puts("1cupsSendRequest: Unable to reconnect."); + return (HTTP_SERVICE_UNAVAILABLE); + } + break; + +#ifdef HAVE_SSL + case HTTP_UPGRADE_REQUIRED : + /* + * Flush any error message, reconnect, and then upgrade with + * encryption... + */ + + DEBUG_puts("2cupsSendRequest: Reconnecting after " + "HTTP_UPGRADE_REQUIRED."); + + if (httpReconnect(http)) + { + DEBUG_puts("1cupsSendRequest: Unable to reconnect."); + return (HTTP_SERVICE_UNAVAILABLE); + } + + DEBUG_puts("2cupsSendRequest: Upgrading to TLS."); + if (httpEncryption(http, HTTP_ENCRYPT_REQUIRED)) + { + DEBUG_puts("1cupsSendRequest: Unable to encrypt connection."); + return (HTTP_SERVICE_UNAVAILABLE); + } + break; +#endif /* HAVE_SSL */ + + case HTTP_EXPECTATION_FAILED : + /* + * Don't try using the Expect: header the next time around... + */ + + expect = (http_status_t)0; + + DEBUG_puts("2cupsSendRequest: Reconnecting after " + "HTTP_EXPECTATION_FAILED."); + + if (httpReconnect(http)) + { + DEBUG_puts("1cupsSendRequest: Unable to reconnect."); + return (HTTP_SERVICE_UNAVAILABLE); + } + break; + + default : + /* + * Some other error... + */ + + return (status); + } + } +} + + +/* + * 'cupsWriteRequestData()' - Write additional data after an IPP request. + * + * This function is used after @link cupsSendRequest@ to provide a PPD and + * after @link cupsStartDocument@ to provide a document file. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */ +cupsWriteRequestData( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *buffer, /* I - Bytes to write */ + size_t length) /* I - Number of bytes to write */ +{ + int wused; /* Previous bytes in buffer */ + + + /* + * Get the default connection as needed... + */ + + DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, " + "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length)); + + if (!http) + { + _cups_globals_t *cg = _cupsGlobals(); + /* Pointer to library globals */ + + if ((http = cg->http) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1); + DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR."); + return (HTTP_ERROR); + } + } + + /* + * Then write to the HTTP connection... + */ + + wused = http->wused; + + if (httpWrite2(http, buffer, length) < 0) + { + DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR."); + _cupsSetError(IPP_INTERNAL_ERROR, strerror(http->error), 0); + return (HTTP_ERROR); + } + + /* + * Finally, check if we have any pending data from the server... + */ + + if (length >= HTTP_MAX_BUFFER || + http->wused < wused || + (wused > 0 && http->wused == length)) + { + /* + * We've written something to the server, so check for response data... + */ + + if (_httpWait(http, 0, 1)) + { + http_status_t status; /* Status from _httpUpdate */ + + _httpUpdate(http, &status); + if (status >= HTTP_MULTIPLE_CHOICES) + { + _cupsSetHTTPError(status); + + do + { + status = httpUpdate(http); + } + while (status != HTTP_ERROR && http->state == HTTP_POST_RECV); + + httpFlush(http); + } + + DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status)); + return (status); + } + } + + DEBUG_puts("1cupsWriteRequestData: Returning HTTP_CONTINUE."); + return (HTTP_CONTINUE); +} + + +/* + * '_cupsConnect()' - Get the default server connection... + */ + +http_t * /* O - HTTP connection */ +_cupsConnect(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * See if we are connected to the same server... + */ + + if (cg->http) + { + /* + * Compare the connection hostname, port, and encryption settings to + * the cached defaults; these were initialized the first time we + * connected... + */ + + if (strcmp(cg->http->hostname, cg->server) || + cg->ipp_port != _httpAddrPort(cg->http->hostaddr) || + (cg->http->encryption != cg->encryption && + cg->http->encryption == HTTP_ENCRYPT_NEVER)) + { + /* + * Need to close the current connection because something has changed... + */ + + httpClose(cg->http); + cg->http = NULL; + } + else + { + /* + * Same server, see if the connection is still established... + */ + + char ch; /* Connection check byte */ + + if (recv(cg->http->fd, &ch, 1, MSG_PEEK | MSG_DONTWAIT) < 0 && + errno != EWOULDBLOCK) + { + /* + * Nope, close the connection... + */ + + httpClose(cg->http); + cg->http = NULL; + } + } + } + + /* + * (Re)connect as needed... + */ + + if (!cg->http) + { + if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(), + cupsEncryption())) == NULL) + { + if (errno) + _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); + else + _cupsSetError(IPP_SERVICE_UNAVAILABLE, + _("Unable to connect to host."), 1); + } + } + + /* + * Return the cached connection... + */ + + return (cg->http); +} + + +/* + * '_cupsSetError()' - Set the last IPP status code and status-message. + */ + +void +_cupsSetError(ipp_status_t status, /* I - IPP status code */ + const char *message, /* I - status-message value */ + int localize) /* I - Localize the message? */ +{ + _cups_globals_t *cg; /* Global data */ + + + if (!message && errno) + { + message = strerror(errno); + localize = 0; + } + + cg = _cupsGlobals(); + cg->last_error = status; + + if (cg->last_status_message) + { + _cupsStrFree(cg->last_status_message); + + cg->last_status_message = NULL; + } + + if (message) + { + if (localize) + { + /* + * Get the message catalog... + */ + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default, + message)); + } + else + cg->last_status_message = _cupsStrAlloc(message); + } + + DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"", + ippErrorString(cg->last_error), cg->last_status_message)); +} + + +/* + * '_cupsSetHTTPError()' - Set the last error using the HTTP status. + */ + +void +_cupsSetHTTPError(http_status_t status) /* I - HTTP status code */ +{ + switch (status) + { + case HTTP_NOT_FOUND : + _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0); + break; + + case HTTP_UNAUTHORIZED : + _cupsSetError(IPP_NOT_AUTHENTICATED, httpStatus(status), 0); + break; + + case HTTP_AUTHORIZATION_CANCELED : + _cupsSetError(IPP_AUTHENTICATION_CANCELED, httpStatus(status), 0); + break; + + case HTTP_FORBIDDEN : + _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0); + break; + + case HTTP_BAD_REQUEST : + _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0); + break; + + case HTTP_REQUEST_TOO_LARGE : + _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0); + break; + + case HTTP_NOT_IMPLEMENTED : + _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0); + break; + + case HTTP_NOT_SUPPORTED : + _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0); + break; + + case HTTP_UPGRADE_REQUIRED : + _cupsSetError(IPP_UPGRADE_REQUIRED, httpStatus(status), 0); + break; + + case HTTP_PKI_ERROR : + _cupsSetError(IPP_PKI_ERROR, httpStatus(status), 0); + break; + + case HTTP_ERROR : + _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0); + break; + + default : + DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to " + "IPP_SERVICE_UNAVAILABLE!", status)); + _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0); + break; + } +} + + +/* + * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $". + */ diff --git a/cups/sidechannel.c b/cups/sidechannel.c new file mode 100644 index 0000000..7bd626e --- /dev/null +++ b/cups/sidechannel.c @@ -0,0 +1,642 @@ +/* + * "$Id: sidechannel.c 7720 2008-07-11 22:46:21Z mike $" + * + * Side-channel API code for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2006 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. + * + * Contents: + * + * cupsSideChannelDoRequest() - Send a side-channel command to a backend and + * wait for a response. + * cupsSideChannelRead() - Read a side-channel message. + * cupsSideChannelSNMPGet() - Query a SNMP OID's value. + * cupsSideChannelSNMPWalk() - Query multiple SNMP OID values. + * cupsSideChannelWrite() - Write a side-channel message. + */ + +/* + * Include necessary headers... + */ + +#include "sidechannel.h" +#include "cups-private.h" +#ifdef WIN32 +# include +#else +# include +#endif /* WIN32 */ +#ifdef __hpux +# include +#elif !defined(WIN32) +# include +#endif /* __hpux */ +#ifndef WIN32 +# include +#endif /* !WIN32 */ +#ifdef HAVE_POLL +# include +#endif /* HAVE_POLL */ + + +/* + * Buffer size for side-channel requests... + */ + +#define _CUPS_SC_MAX_DATA 65535 +#define _CUPS_SC_MAX_BUFFER 65540 + + +/* + * 'cupsSideChannelDoRequest()' - Send a side-channel command to a backend and wait for a response. + * + * This function is normally only called by filters, drivers, or port + * monitors in order to communicate with the backend used by the current + * printer. Programs must be prepared to handle timeout or "not + * implemented" status codes, which indicate that the backend or device + * do not support the specified side-channel command. + * + * The "datalen" parameter must be initialized to the size of the buffer + * pointed to by the "data" parameter. cupsSideChannelDoRequest() will + * update the value to contain the number of data bytes in the buffer. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +cups_sc_status_t /* O - Status of command */ +cupsSideChannelDoRequest( + cups_sc_command_t command, /* I - Command to send */ + char *data, /* O - Response data buffer pointer */ + int *datalen, /* IO - Size of data buffer on entry, number of bytes in buffer on return */ + double timeout) /* I - Timeout in seconds */ +{ + cups_sc_status_t status; /* Status of command */ + cups_sc_command_t rcommand; /* Response command */ + + + if (cupsSideChannelWrite(command, CUPS_SC_STATUS_NONE, NULL, 0, timeout)) + return (CUPS_SC_STATUS_TIMEOUT); + + if (cupsSideChannelRead(&rcommand, &status, data, datalen, timeout)) + return (CUPS_SC_STATUS_TIMEOUT); + + if (rcommand != command) + return (CUPS_SC_STATUS_BAD_MESSAGE); + + return (status); +} + + +/* + * 'cupsSideChannelRead()' - Read a side-channel message. + * + * This function is normally only called by backend programs to read + * commands from a filter, driver, or port monitor program. The + * caller must be prepared to handle incomplete or invalid messages + * and return the corresponding status codes. + * + * The "datalen" parameter must be initialized to the size of the buffer + * pointed to by the "data" parameter. cupsSideChannelDoRequest() will + * update the value to contain the number of data bytes in the buffer. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +cupsSideChannelRead( + cups_sc_command_t *command, /* O - Command code */ + cups_sc_status_t *status, /* O - Status code */ + char *data, /* O - Data buffer pointer */ + int *datalen, /* IO - Size of data buffer on entry, number of bytes in buffer on return */ + double timeout) /* I - Timeout in seconds */ +{ + char *buffer; /* Message buffer */ + int bytes; /* Bytes read */ + int templen; /* Data length from message */ + int nfds; /* Number of file descriptors */ +#ifdef HAVE_POLL + struct pollfd pfd; /* Poll structure for poll() */ +#else /* select() */ + fd_set input_set; /* Input set for select() */ + struct timeval stimeout; /* Timeout value for select() */ +#endif /* HAVE_POLL */ + + + DEBUG_printf(("cupsSideChannelRead(command=%p, status=%p, data=%p, " + "datalen=%p(%d), timeout=%.3f)", command, status, data, + datalen, datalen ? *datalen : -1, timeout)); + + /* + * Range check input... + */ + + if (!command || !status) + return (-1); + + /* + * See if we have pending data on the side-channel socket... + */ + +#ifdef HAVE_POLL + pfd.fd = CUPS_SC_FD; + pfd.events = POLLIN; + + while ((nfds = poll(&pfd, 1, + timeout < 0.0 ? -1 : (long)(timeout * 1000))) < 0 && + (errno == EINTR || errno == EAGAIN)) + ; + +#else /* select() */ + FD_ZERO(&input_set); + FD_SET(CUPS_SC_FD, &input_set); + + stimeout.tv_sec = (int)timeout; + stimeout.tv_usec = (int)(timeout * 1000000) % 1000000; + + while ((nfds = select(CUPS_SC_FD + 1, &input_set, NULL, NULL, + timeout < 0.0 ? NULL : &stimeout)) < 0 && + (errno == EINTR || errno == EAGAIN)) + ; + +#endif /* HAVE_POLL */ + + if (nfds < 1) + { + *command = CUPS_SC_CMD_NONE; + *status = nfds==0 ? CUPS_SC_STATUS_TIMEOUT : CUPS_SC_STATUS_IO_ERROR; + return (-1); + } + + /* + * Read a side-channel message for the format: + * + * Byte(s) Description + * ------- ------------------------------------------- + * 0 Command code + * 1 Status code + * 2-3 Data length (network byte order) + * 4-N Data + */ + + if ((buffer = _cupsBufferGet(_CUPS_SC_MAX_BUFFER)) == NULL) + { + *command = CUPS_SC_CMD_NONE; + *status = CUPS_SC_STATUS_TOO_BIG; + + return (-1); + } + + while ((bytes = read(CUPS_SC_FD, buffer, _CUPS_SC_MAX_BUFFER)) < 0) + if (errno != EINTR && errno != EAGAIN) + { + DEBUG_printf(("1cupsSideChannelRead: Read error: %s", strerror(errno))); + + _cupsBufferRelease(buffer); + + *command = CUPS_SC_CMD_NONE; + *status = CUPS_SC_STATUS_IO_ERROR; + + return (-1); + } + + /* + * Watch for EOF or too few bytes... + */ + + if (bytes < 4) + { + DEBUG_printf(("1cupsSideChannelRead: Short read of %d bytes", bytes)); + + _cupsBufferRelease(buffer); + + *command = CUPS_SC_CMD_NONE; + *status = CUPS_SC_STATUS_BAD_MESSAGE; + + return (-1); + } + + /* + * Validate the command code in the message... + */ + + if (buffer[0] < CUPS_SC_CMD_SOFT_RESET || + buffer[0] >= CUPS_SC_CMD_MAX) + { + DEBUG_printf(("1cupsSideChannelRead: Bad command %d!", buffer[0])); + + _cupsBufferRelease(buffer); + + *command = CUPS_SC_CMD_NONE; + *status = CUPS_SC_STATUS_BAD_MESSAGE; + + return (-1); + } + + *command = (cups_sc_command_t)buffer[0]; + + /* + * Validate the data length in the message... + */ + + templen = ((buffer[2] & 255) << 8) | (buffer[3] & 255); + + if (templen > 0 && (!data || !datalen)) + { + /* + * Either the response is bigger than the provided buffer or the + * response is bigger than we've read... + */ + + *status = CUPS_SC_STATUS_TOO_BIG; + } + else if (!datalen || templen > *datalen || templen > (bytes - 4)) + { + /* + * Either the response is bigger than the provided buffer or the + * response is bigger than we've read... + */ + + *status = CUPS_SC_STATUS_TOO_BIG; + } + else + { + /* + * The response data will fit, copy it over and provide the actual + * length... + */ + + *status = (cups_sc_status_t)buffer[1]; + *datalen = templen; + + memcpy(data, buffer + 4, templen); + } + + _cupsBufferRelease(buffer); + + DEBUG_printf(("1cupsSideChannelRead: Returning status=%d", *status)); + + return (0); +} + + +/* + * 'cupsSideChannelSNMPGet()' - Query a SNMP OID's value. + * + * This function asks the backend to do a SNMP OID query on behalf of the + * filter, port monitor, or backend using the default community name. + * + * "oid" contains a numeric OID consisting of integers separated by periods, + * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not + * supported and must be converted to their numeric forms. + * + * On input, "data" and "datalen" provide the location and size of the + * buffer to hold the OID value as a string. HEX-String (binary) values are + * converted to hexadecimal strings representing the binary data, while + * NULL-Value and unknown OID types are returned as the empty string. + * The returned "datalen" does not include the trailing nul. + * + * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not + * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when + * the printer does not respond to the SNMP query. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +cups_sc_status_t /* O - Query status */ +cupsSideChannelSNMPGet( + const char *oid, /* I - OID to query */ + char *data, /* I - Buffer for OID value */ + int *datalen, /* IO - Size of OID buffer on entry, size of value on return */ + double timeout) /* I - Timeout in seconds */ +{ + cups_sc_status_t status; /* Status of command */ + cups_sc_command_t rcommand; /* Response command */ + char *real_data; /* Real data buffer for response */ + int real_datalen, /* Real length of data buffer */ + real_oidlen; /* Length of returned OID string */ + + + DEBUG_printf(("cupsSideChannelSNMPGet(oid=\"%s\", data=%p, datalen=%p(%d), " + "timeout=%.3f)", oid, data, datalen, datalen ? *datalen : -1, + timeout)); + + /* + * Range check input... + */ + + if (!oid || !*oid || !data || !datalen || *datalen < 2) + return (CUPS_SC_STATUS_BAD_MESSAGE); + + *data = '\0'; + + /* + * Send the request to the backend and wait for a response... + */ + + if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET, CUPS_SC_STATUS_NONE, oid, + (int)strlen(oid) + 1, timeout)) + return (CUPS_SC_STATUS_TIMEOUT); + + if ((real_data = _cupsBufferGet(_CUPS_SC_MAX_BUFFER)) == NULL) + return (CUPS_SC_STATUS_TOO_BIG); + + real_datalen = _CUPS_SC_MAX_BUFFER; + if (cupsSideChannelRead(&rcommand, &status, real_data, &real_datalen, timeout)) + { + _cupsBufferRelease(real_data); + return (CUPS_SC_STATUS_TIMEOUT); + } + + if (rcommand != CUPS_SC_CMD_SNMP_GET) + { + _cupsBufferRelease(real_data); + return (CUPS_SC_STATUS_BAD_MESSAGE); + } + + if (status == CUPS_SC_STATUS_OK) + { + /* + * Parse the response of the form "oid\0value"... + */ + + real_oidlen = strlen(real_data) + 1; + real_datalen -= real_oidlen; + + if ((real_datalen + 1) > *datalen) + { + _cupsBufferRelease(real_data); + return (CUPS_SC_STATUS_TOO_BIG); + } + + memcpy(data, real_data + real_oidlen, real_datalen); + data[real_datalen] = '\0'; + + *datalen = real_datalen; + } + + _cupsBufferRelease(real_data); + + return (status); +} + + +/* + * 'cupsSideChannelSNMPWalk()' - Query multiple SNMP OID values. + * + * This function asks the backend to do multiple SNMP OID queries on behalf + * of the filter, port monitor, or backend using the default community name. + * All OIDs under the "parent" OID are queried and the results are sent to + * the callback function you provide. + * + * "oid" contains a numeric OID consisting of integers separated by periods, + * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not + * supported and must be converted to their numeric forms. + * + * "timeout" specifies the timeout for each OID query. The total amount of + * time will depend on the number of OID values found and the time required + * for each query. + * + * "cb" provides a function to call for every value that is found. "context" + * is an application-defined pointer that is sent to the callback function + * along with the OID and current data. The data passed to the callback is the + * same as returned by @link cupsSideChannelSNMPGet@. + * + * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not + * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when + * the printer does not respond to the first SNMP query. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +cups_sc_status_t /* O - Status of first query of @code CUPS_SC_STATUS_OK@ on success */ +cupsSideChannelSNMPWalk( + const char *oid, /* I - First numeric OID to query */ + double timeout, /* I - Timeout for each query in seconds */ + cups_sc_walk_func_t cb, /* I - Function to call with each value */ + void *context) /* I - Application-defined pointer to send to callback */ +{ + cups_sc_status_t status; /* Status of command */ + cups_sc_command_t rcommand; /* Response command */ + char *real_data; /* Real data buffer for response */ + int real_datalen, /* Real length of data buffer */ + real_oidlen, /* Length of returned OID string */ + oidlen; /* Length of first OID */ + const char *current_oid; /* Current OID */ + char last_oid[2048]; /* Last OID */ + + + DEBUG_printf(("cupsSideChannelSNMPWalk(oid=\"%s\", timeout=%.3f, cb=%p, " + "context=%p)", oid, timeout, cb, context)); + + /* + * Range check input... + */ + + if (!oid || !*oid || !cb) + return (CUPS_SC_STATUS_BAD_MESSAGE); + + if ((real_data = _cupsBufferGet(_CUPS_SC_MAX_BUFFER)) == NULL) + return (CUPS_SC_STATUS_TOO_BIG); + + /* + * Loop until the OIDs don't match... + */ + + current_oid = oid; + oidlen = (int)strlen(oid); + last_oid[0] = '\0'; + + do + { + /* + * Send the request to the backend and wait for a response... + */ + + if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET_NEXT, CUPS_SC_STATUS_NONE, + current_oid, (int)strlen(current_oid) + 1, timeout)) + { + _cupsBufferRelease(real_data); + return (CUPS_SC_STATUS_TIMEOUT); + } + + real_datalen = _CUPS_SC_MAX_BUFFER; + if (cupsSideChannelRead(&rcommand, &status, real_data, &real_datalen, + timeout)) + { + _cupsBufferRelease(real_data); + return (CUPS_SC_STATUS_TIMEOUT); + } + + if (rcommand != CUPS_SC_CMD_SNMP_GET_NEXT) + { + _cupsBufferRelease(real_data); + return (CUPS_SC_STATUS_BAD_MESSAGE); + } + + if (status == CUPS_SC_STATUS_OK) + { + /* + * Parse the response of the form "oid\0value"... + */ + + if (strncmp(real_data, oid, oidlen) || real_data[oidlen] != '.' || + !strcmp(real_data, last_oid)) + { + /* + * Done with this set of OIDs... + */ + + _cupsBufferRelease(real_data); + return (CUPS_SC_STATUS_OK); + } + + if (real_datalen < sizeof(real_data)) + real_data[real_datalen] = '\0'; + + real_oidlen = strlen(real_data) + 1; + real_datalen -= real_oidlen; + + /* + * Call the callback with the OID and data... + */ + + (*cb)(real_data, real_data + real_oidlen, real_datalen, context); + + /* + * Update the current OID... + */ + + current_oid = real_data; + strlcpy(last_oid, current_oid, sizeof(last_oid)); + } + } + while (status == CUPS_SC_STATUS_OK); + + _cupsBufferRelease(real_data); + + return (status); +} + + +/* + * 'cupsSideChannelWrite()' - Write a side-channel message. + * + * This function is normally only called by backend programs to send + * responses to a filter, driver, or port monitor program. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on error */ +cupsSideChannelWrite( + cups_sc_command_t command, /* I - Command code */ + cups_sc_status_t status, /* I - Status code */ + const char *data, /* I - Data buffer pointer */ + int datalen, /* I - Number of bytes of data */ + double timeout) /* I - Timeout in seconds */ +{ + char *buffer; /* Message buffer */ + int bytes; /* Bytes written */ +#ifdef HAVE_POLL + struct pollfd pfd; /* Poll structure for poll() */ +#else /* select() */ + fd_set output_set; /* Output set for select() */ + struct timeval stimeout; /* Timeout value for select() */ +#endif /* HAVE_POLL */ + + + /* + * Range check input... + */ + + if (command < CUPS_SC_CMD_SOFT_RESET || command >= CUPS_SC_CMD_MAX || + datalen < 0 || datalen > _CUPS_SC_MAX_DATA || (datalen > 0 && !data)) + return (-1); + + /* + * See if we can safely write to the side-channel socket... + */ + +#ifdef HAVE_POLL + pfd.fd = CUPS_SC_FD; + pfd.events = POLLOUT; + + if (timeout < 0.0) + { + if (poll(&pfd, 1, -1) < 1) + return (-1); + } + else if (poll(&pfd, 1, (long)(timeout * 1000)) < 1) + return (-1); + +#else /* select() */ + FD_ZERO(&output_set); + FD_SET(CUPS_SC_FD, &output_set); + + if (timeout < 0.0) + { + if (select(CUPS_SC_FD + 1, NULL, &output_set, NULL, NULL) < 1) + return (-1); + } + else + { + stimeout.tv_sec = (int)timeout; + stimeout.tv_usec = (int)(timeout * 1000000) % 1000000; + + if (select(CUPS_SC_FD + 1, NULL, &output_set, NULL, &stimeout) < 1) + return (-1); + } +#endif /* HAVE_POLL */ + + /* + * Write a side-channel message in the format: + * + * Byte(s) Description + * ------- ------------------------------------------- + * 0 Command code + * 1 Status code + * 2-3 Data length (network byte order) <= 16384 + * 4-N Data + */ + + if ((buffer = _cupsBufferGet(datalen + 4)) == NULL) + return (-1); + + buffer[0] = command; + buffer[1] = status; + buffer[2] = datalen >> 8; + buffer[3] = datalen & 255; + + bytes = 4; + + if (datalen > 0) + { + memcpy(buffer + 4, data, datalen); + bytes += datalen; + } + + while (write(CUPS_SC_FD, buffer, bytes) < 0) + if (errno != EINTR && errno != EAGAIN) + { + _cupsBufferRelease(buffer); + return (-1); + } + + _cupsBufferRelease(buffer); + + return (0); +} + + +/* + * End of "$Id: sidechannel.c 7720 2008-07-11 22:46:21Z mike $". + */ diff --git a/cups/sidechannel.h b/cups/sidechannel.h new file mode 100644 index 0000000..5f97257 --- /dev/null +++ b/cups/sidechannel.h @@ -0,0 +1,147 @@ +/* + * "$Id: sidechannel.h 7616 2008-05-28 00:34:13Z mike $" + * + * Side-channel API definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2006 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. + */ + +#ifndef _CUPS_SIDECHANNEL_H_ +# define _CUPS_SIDECHANNEL_H_ + +/* + * Include necessary headers... + */ + +# include "versioning.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Constants... + */ + +#define CUPS_SC_FD 4 /* File descriptor for select/poll */ + + +/* + * Enumerations... + */ + +enum cups_sc_bidi_e /**** Bidirectional capability values ****/ +{ + CUPS_SC_BIDI_NOT_SUPPORTED = 0, /* Bidirectional I/O is not supported */ + CUPS_SC_BIDI_SUPPORTED = 1 /* Bidirectional I/O is supported */ +}; +typedef enum cups_sc_bidi_e cups_sc_bidi_t; + /**** Bidirectional capabilities ****/ + +enum cups_sc_command_e /**** Request command codes ****/ +{ + CUPS_SC_CMD_NONE = 0, /* No command @private@ */ + CUPS_SC_CMD_SOFT_RESET = 1, /* Do a soft reset */ + CUPS_SC_CMD_DRAIN_OUTPUT = 2, /* Drain all pending output */ + CUPS_SC_CMD_GET_BIDI = 3, /* Return bidirectional capabilities */ + CUPS_SC_CMD_GET_DEVICE_ID = 4, /* Return the IEEE-1284 device ID */ + CUPS_SC_CMD_GET_STATE = 5, /* Return the device state */ + CUPS_SC_CMD_SNMP_GET = 6, /* Query an SNMP OID @since CUPS 1.4/OS X 10.6@ */ + CUPS_SC_CMD_SNMP_GET_NEXT = 7, /* Query the next SNMP OID @since CUPS 1.4/OS X 10.6@ */ + CUPS_SC_CMD_GET_CONNECTED = 8, /* Return whether the backend is "connected" to the printer @since CUPS 1.5/OS X 10.7@ */ + CUPS_SC_CMD_MAX /* End of valid values @private@ */ +}; +typedef enum cups_sc_command_e cups_sc_command_t; + /**** Request command codes ****/ + +enum cups_sc_connected_e /**** Connectivity values ****/ +{ + CUPS_SC_NOT_CONNECTED = 0, /* Backend is not "connected" to printer */ + CUPS_SC_CONNECTED = 1 /* Backend is "connected" to printer */ +}; +typedef enum cups_sc_connected_e cups_sc_connected_t; + /**** Connectivity values ****/ + + +enum cups_sc_state_e /**** Printer state bits ****/ +{ + CUPS_SC_STATE_OFFLINE = 0, /* Device is offline */ + CUPS_SC_STATE_ONLINE = 1, /* Device is online */ + CUPS_SC_STATE_BUSY = 2, /* Device is busy */ + CUPS_SC_STATE_ERROR = 4, /* Other error condition */ + CUPS_SC_STATE_MEDIA_LOW = 16, /* Paper low condition */ + CUPS_SC_STATE_MEDIA_EMPTY = 32, /* Paper out condition */ + CUPS_SC_STATE_MARKER_LOW = 64, /* Toner/ink low condition */ + CUPS_SC_STATE_MARKER_EMPTY = 128 /* Toner/ink out condition */ +}; +typedef enum cups_sc_state_e cups_sc_state_t; + /**** Printer state bits ****/ + +enum cups_sc_status_e /**** Response status codes ****/ +{ + CUPS_SC_STATUS_NONE, /* No status */ + CUPS_SC_STATUS_OK, /* Operation succeeded */ + CUPS_SC_STATUS_IO_ERROR, /* An I/O error occurred */ + CUPS_SC_STATUS_TIMEOUT, /* The backend did not respond */ + CUPS_SC_STATUS_NO_RESPONSE, /* The device did not respond */ + CUPS_SC_STATUS_BAD_MESSAGE, /* The command/response message was invalid */ + CUPS_SC_STATUS_TOO_BIG, /* Response too big */ + CUPS_SC_STATUS_NOT_IMPLEMENTED /* Command not implemented */ +}; +typedef enum cups_sc_status_e cups_sc_status_t; + /**** Response status codes ****/ + +typedef void (*cups_sc_walk_func_t)(const char *oid, const char *data, + int datalen, void *context); + /**** SNMP walk callback ****/ + + +/* + * Prototypes... + */ + +extern cups_sc_status_t cupsSideChannelDoRequest(cups_sc_command_t command, + char *data, int *datalen, + double timeout) _CUPS_API_1_3; +extern int cupsSideChannelRead(cups_sc_command_t *command, + cups_sc_status_t *status, + char *data, int *datalen, + double timeout) _CUPS_API_1_3; +extern int cupsSideChannelWrite(cups_sc_command_t command, + cups_sc_status_t status, + const char *data, int datalen, + double timeout) _CUPS_API_1_3; + +/**** New in CUPS 1.4 ****/ +extern cups_sc_status_t cupsSideChannelSNMPGet(const char *oid, char *data, + int *datalen, double timeout) + _CUPS_API_1_4; +extern cups_sc_status_t cupsSideChannelSNMPWalk(const char *oid, double timeout, + cups_sc_walk_func_t cb, + void *context) _CUPS_API_1_4; + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_SIDECHANNEL_H_ */ + +/* + * End of "$Id: sidechannel.h 7616 2008-05-28 00:34:13Z mike $". + */ diff --git a/cups/snmp-private.h b/cups/snmp-private.h new file mode 100644 index 0000000..9661243 --- /dev/null +++ b/cups/snmp-private.h @@ -0,0 +1,146 @@ +/* + * "$Id: snmp-private.h 3794 2012-04-23 22:44:16Z msweet $" + * + * Private SNMP definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2006-2007 by Easy Software Products, all rights reserved. + * + * 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" + * "LICENSE" which should have been included with this file. If this + * file is missing or damaged, see the license at "http://www.cups.org/". + * + * This file is subject to the Apple OS-Developed Software exception. + */ + +#ifndef _CUPS_SNMP_PRIVATE_H_ +# define _CUPS_SNMP_PRIVATE_H_ + + +/* + * Include necessary headers. + */ + +#include + + +/* + * Constants... + */ + +#define CUPS_SNMP_PORT 161 /* SNMP well-known port */ +#define CUPS_SNMP_MAX_COMMUNITY 512 /* Maximum size of community name */ +#define CUPS_SNMP_MAX_OID 128 /* Maximum number of OID numbers */ +#define CUPS_SNMP_MAX_PACKET 1472 /* Maximum size of SNMP packet */ +#define CUPS_SNMP_MAX_STRING 1024 /* Maximum size of string */ +#define CUPS_SNMP_VERSION_1 0 /* SNMPv1 */ + + +/* + * Types... + */ + +enum cups_asn1_e /**** ASN1 request/object types ****/ +{ + CUPS_ASN1_END_OF_CONTENTS = 0x00, /* End-of-contents */ + CUPS_ASN1_BOOLEAN = 0x01, /* BOOLEAN */ + CUPS_ASN1_INTEGER = 0x02, /* INTEGER or ENUMERATION */ + CUPS_ASN1_BIT_STRING = 0x03, /* BIT STRING */ + CUPS_ASN1_OCTET_STRING = 0x04, /* OCTET STRING */ + CUPS_ASN1_NULL_VALUE = 0x05, /* NULL VALUE */ + CUPS_ASN1_OID = 0x06, /* OBJECT IDENTIFIER */ + CUPS_ASN1_SEQUENCE = 0x30, /* SEQUENCE */ + CUPS_ASN1_HEX_STRING = 0x40, /* Binary string aka Hex-STRING */ + CUPS_ASN1_COUNTER = 0x41, /* 32-bit unsigned aka Counter32 */ + CUPS_ASN1_GAUGE = 0x42, /* 32-bit unsigned aka Gauge32 */ + CUPS_ASN1_TIMETICKS = 0x43, /* 32-bit unsigned aka Timeticks32 */ + CUPS_ASN1_GET_REQUEST = 0xa0, /* GetRequest-PDU */ + CUPS_ASN1_GET_NEXT_REQUEST = 0xa1, /* GetNextRequest-PDU */ + CUPS_ASN1_GET_RESPONSE = 0xa2 /* GetResponse-PDU */ +}; +typedef enum cups_asn1_e cups_asn1_t; /**** ASN1 request/object types ****/ + +typedef struct cups_snmp_string_s /**** String value ****/ +{ + unsigned char bytes[CUPS_SNMP_MAX_STRING]; + /* Bytes in string */ + int num_bytes; /* Number of bytes */ +} cups_snmp_string_t; + +union cups_snmp_value_u /**** Object value ****/ +{ + int boolean; /* Boolean value */ + int integer; /* Integer value */ + unsigned counter; /* Counter value */ + unsigned gauge; /* Gauge value */ + unsigned timeticks; /* Timeticks value */ + int oid[CUPS_SNMP_MAX_OID]; /* OID value */ + cups_snmp_string_t string; /* String value */ +}; + +typedef struct cups_snmp_s /**** SNMP data packet ****/ +{ + const char *error; /* Encode/decode error */ + http_addr_t address; /* Source address */ + int version; /* Version number */ + char community[CUPS_SNMP_MAX_COMMUNITY]; + /* Community name */ + cups_asn1_t request_type; /* Request type */ + int request_id; /* request-id value */ + int error_status; /* error-status value */ + int error_index; /* error-index value */ + int object_name[CUPS_SNMP_MAX_OID]; + /* object-name value */ + cups_asn1_t object_type; /* object-value type */ + union cups_snmp_value_u + object_value; /* object-value value */ +} cups_snmp_t; + +typedef void (*cups_snmp_cb_t)(cups_snmp_t *packet, void *data); + +/* + * Prototypes... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + +extern void _cupsSNMPClose(int fd) _CUPS_API_1_4; +extern int *_cupsSNMPCopyOID(int *dst, const int *src, int dstsize) + _CUPS_API_1_4; +extern const char *_cupsSNMPDefaultCommunity(void) _CUPS_API_1_4; +extern int _cupsSNMPIsOID(cups_snmp_t *packet, const int *oid) + _CUPS_API_1_4; +extern int _cupsSNMPIsOIDPrefixed(cups_snmp_t *packet, + const int *prefix) _CUPS_API_1_4; +extern char *_cupsSNMPOIDToString(const int *src, char *dst, + size_t dstsize) _CUPS_API_1_4; +extern int _cupsSNMPOpen(int family) _CUPS_API_1_4; +extern cups_snmp_t *_cupsSNMPRead(int fd, cups_snmp_t *packet, + double timeout) _CUPS_API_1_4; +extern void _cupsSNMPSetDebug(int level) _CUPS_API_1_4; +extern int *_cupsSNMPStringToOID(const char *src, + int *dst, int dstsize) + _CUPS_API_1_4; +extern int _cupsSNMPWalk(int fd, http_addr_t *address, int version, + const char *community, const int *prefix, + double timeout, cups_snmp_cb_t cb, + void *data) _CUPS_API_1_4; +extern int _cupsSNMPWrite(int fd, http_addr_t *address, int version, + const char *community, + cups_asn1_t request_type, + const unsigned request_id, + const int *oid) _CUPS_API_1_4; + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_SNMP_PRIVATE_H_ */ + + +/* + * End of "$Id: snmp-private.h 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/snmp.c b/cups/snmp.c new file mode 100644 index 0000000..0c0e520 --- /dev/null +++ b/cups/snmp.c @@ -0,0 +1,1737 @@ +/* + * "$Id: snmp.c 3794 2012-04-23 22:44:16Z msweet $" + * + * SNMP functions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 2006-2007 by Easy Software Products, all rights reserved. + * + * 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" + * "LICENSE" which should have been included with this file. If this + * file is missing or damaged, see the license at "http://www.cups.org/". + * + * This file is subject to the Apple OS-Developed Software exception. + * + * Contents: + * + * _cupsSNMPClose() - Close a SNMP socket. + * _cupsSNMPCopyOID() - Copy an OID. + * _cupsSNMPDefaultCommunity() - Get the default SNMP community name. + * _cupsSNMPIsOID() - Test whether a SNMP response contains the + * specified OID. + * _cupsSNMPIsOIDPrefixed() - Test whether a SNMP response uses the + * specified OID prefix. + * _cupsSNMPOIDToString() - Convert an OID to a string. + * _cupsSNMPOpen() - Open a SNMP socket. + * _cupsSNMPRead() - Read and parse a SNMP response. + * _cupsSNMPSetDebug() - Enable/disable debug logging to stderr. + * _cupsSNMPStringToOID() - Convert a numeric OID string to an OID array. + * _cupsSNMPWalk() - Enumerate a group of OIDs. + * _cupsSNMPWrite() - Send an SNMP query packet. + * asn1_debug() - Decode an ASN1-encoded message. + * asn1_decode_snmp() - Decode a SNMP packet. + * asn1_encode_snmp() - Encode a SNMP packet. + * asn1_get_integer() - Get an integer value. + * asn1_get_length() - Get a value length. + * asn1_get_oid() - Get an OID value. + * asn1_get_packed() - Get a packed integer value. + * asn1_get_string() - Get a string value. + * asn1_get_type() - Get a value type. + * asn1_set_integer() - Set an integer value. + * asn1_set_length() - Set a value length. + * asn1_set_oid() - Set an OID value. + * asn1_set_packed() - Set a packed integer value. + * asn1_size_integer() - Figure out the number of bytes needed for an + * integer value. + * asn1_size_length() - Figure out the number of bytes needed for a + * length value. + * asn1_size_oid() - Figure out the numebr of bytes needed for an + * OID value. + * asn1_size_packed() - Figure out the number of bytes needed for a + * packed integer value. + * snmp_set_error() - Set the localized error for a packet. + */ + +/* + * Include necessary headers. + */ + +#include "cups-private.h" +#include "snmp-private.h" +#ifdef HAVE_POLL +# include +#endif /* HAVE_POLL */ + + +/* + * Local functions... + */ + +static void asn1_debug(const char *prefix, unsigned char *buffer, + size_t len, int indent); +static int asn1_decode_snmp(unsigned char *buffer, size_t len, + cups_snmp_t *packet); +static int asn1_encode_snmp(unsigned char *buffer, size_t len, + cups_snmp_t *packet); +static int asn1_get_integer(unsigned char **buffer, + unsigned char *bufend, + int length); +static int asn1_get_oid(unsigned char **buffer, + unsigned char *bufend, + int length, int *oid, int oidsize); +static int asn1_get_packed(unsigned char **buffer, + unsigned char *bufend); +static char *asn1_get_string(unsigned char **buffer, + unsigned char *bufend, + int length, char *string, + int strsize); +static unsigned asn1_get_length(unsigned char **buffer, + unsigned char *bufend); +static int asn1_get_type(unsigned char **buffer, + unsigned char *bufend); +static void asn1_set_integer(unsigned char **buffer, + int integer); +static void asn1_set_length(unsigned char **buffer, + unsigned length); +static void asn1_set_oid(unsigned char **buffer, + const int *oid); +static void asn1_set_packed(unsigned char **buffer, + int integer); +static int asn1_size_integer(int integer); +static int asn1_size_length(int length); +static int asn1_size_oid(const int *oid); +static int asn1_size_packed(int integer); +static void snmp_set_error(cups_snmp_t *packet, + const char *message); + + +/* + * '_cupsSNMPClose()' - Close a SNMP socket. + */ + +void +_cupsSNMPClose(int fd) /* I - SNMP socket file descriptor */ +{ + DEBUG_printf(("4_cupsSNMPClose(fd=%d)", fd)); + +#ifdef WIN32 + closesocket(fd); +#else + close(fd); +#endif /* WIN32 */ +} + + +/* + * '_cupsSNMPCopyOID()' - Copy an OID. + * + * The array pointed to by "src" is terminated by the value -1. + */ + +int * /* O - New OID */ +_cupsSNMPCopyOID(int *dst, /* I - Destination OID */ + const int *src, /* I - Source OID */ + int dstsize) /* I - Number of integers in dst */ +{ + int i; /* Looping var */ + + + DEBUG_printf(("4_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)", dst, src, + dstsize)); + + for (i = 0, dstsize --; src[i] >= 0 && i < dstsize; i ++) + dst[i] = src[i]; + + dst[i] = -1; + + return (dst); +} + + +/* + * '_cupsSNMPDefaultCommunity()' - Get the default SNMP community name. + * + * The default community name is the first community name found in the + * snmp.conf file. If no community name is defined there, "public" is used. + */ + +const char * /* O - Default community name */ +_cupsSNMPDefaultCommunity(void) +{ + cups_file_t *fp; /* snmp.conf file */ + char line[1024], /* Line from file */ + *value; /* Value from file */ + int linenum; /* Line number in file */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + DEBUG_puts("4_cupsSNMPDefaultCommunity()"); + + if (!cg->snmp_community[0]) + { + strlcpy(cg->snmp_community, "public", sizeof(cg->snmp_community)); + + snprintf(line, sizeof(line), "%s/snmp.conf", cg->cups_serverroot); + if ((fp = cupsFileOpen(line, "r")) != NULL) + { + linenum = 0; + while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) + if (!_cups_strcasecmp(line, "Community") && value) + { + strlcpy(cg->snmp_community, value, sizeof(cg->snmp_community)); + break; + } + + cupsFileClose(fp); + } + } + + DEBUG_printf(("5_cupsSNMPDefaultCommunity: Returning \"%s\"", + cg->snmp_community)); + + return (cg->snmp_community); +} + + +/* + * '_cupsSNMPIsOID()' - Test whether a SNMP response contains the specified OID. + * + * The array pointed to by "oid" is terminated by the value -1. + */ + +int /* O - 1 if equal, 0 if not equal */ +_cupsSNMPIsOID(cups_snmp_t *packet, /* I - Response packet */ + const int *oid) /* I - OID */ +{ + int i; /* Looping var */ + + + /* + * Range check input... + */ + + DEBUG_printf(("4_cupsSNMPIsOID(packet=%p, oid=%p)", packet, oid)); + + if (!packet || !oid) + { + DEBUG_puts("5_cupsSNMPIsOID: Returning 0"); + + return (0); + } + + /* + * Compare OIDs... + */ + + for (i = 0; + i < CUPS_SNMP_MAX_OID && oid[i] >= 0 && packet->object_name[i] >= 0; + i ++) + if (oid[i] != packet->object_name[i]) + { + DEBUG_puts("5_cupsSNMPIsOID: Returning 0"); + + return (0); + } + + DEBUG_printf(("5_cupsSNMPIsOID: Returning %d", + i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i])); + + return (i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]); +} + + +/* + * '_cupsSNMPIsOIDPrefixed()' - Test whether a SNMP response uses the specified + * OID prefix. + * + * The array pointed to by "prefix" is terminated by the value -1. + */ + +int /* O - 1 if prefixed, 0 if not prefixed */ +_cupsSNMPIsOIDPrefixed( + cups_snmp_t *packet, /* I - Response packet */ + const int *prefix) /* I - OID prefix */ +{ + int i; /* Looping var */ + + + /* + * Range check input... + */ + + DEBUG_printf(("4_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)", packet, + prefix)); + + if (!packet || !prefix) + { + DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0"); + + return (0); + } + + /* + * Compare OIDs... + */ + + for (i = 0; + i < CUPS_SNMP_MAX_OID && prefix[i] >= 0 && packet->object_name[i] >= 0; + i ++) + if (prefix[i] != packet->object_name[i]) + { + DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0"); + + return (0); + } + + DEBUG_printf(("5_cupsSNMPIsOIDPrefixed: Returning %d", + i < CUPS_SNMP_MAX_OID)); + + return (i < CUPS_SNMP_MAX_OID); +} + + +/* + * '_cupsSNMPOIDToString()' - Convert an OID to a string. + */ + + +char * /* O - New string or @code NULL@ on error */ +_cupsSNMPOIDToString(const int *src, /* I - OID */ + char *dst, /* I - String buffer */ + size_t dstsize) /* I - Size of string buffer */ +{ + char *dstptr, /* Pointer into string buffer */ + *dstend; /* End of string buffer */ + + + DEBUG_printf(("4_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT ")", + src, dst, CUPS_LLCAST dstsize)); + + /* + * Range check input... + */ + + if (!src || !dst || dstsize < 4) + return (NULL); + + /* + * Loop through the OID array and build a string... + */ + + for (dstptr = dst, dstend = dstptr + dstsize - 1; + *src >= 0 && dstptr < dstend; + src ++, dstptr += strlen(dstptr)) + snprintf(dstptr, dstend - dstptr + 1, ".%d", *src); + + if (*src >= 0) + return (NULL); + else + return (dst); +} + + +/* + * '_cupsSNMPOpen()' - Open a SNMP socket. + */ + +int /* O - SNMP socket file descriptor */ +_cupsSNMPOpen(int family) /* I - Address family - @code AF_INET@ or @code AF_INET6@ */ +{ + int fd; /* SNMP socket file descriptor */ + int val; /* Socket option value */ + + + /* + * Create the SNMP socket... + */ + + DEBUG_printf(("4_cupsSNMPOpen(family=%d)", family)); + + if ((fd = socket(family, SOCK_DGRAM, 0)) < 0) + { + DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno))); + + return (-1); + } + + /* + * Set the "broadcast" flag... + */ + + val = 1; + +#ifdef WIN32 + if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *)&val, sizeof(val))) +#else + if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) +#endif /* WIN32 */ + { + DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno))); + + close(fd); + + return (-1); + } + + DEBUG_printf(("5_cupsSNMPOpen: Returning %d", fd)); + + return (fd); +} + + +/* + * '_cupsSNMPRead()' - Read and parse a SNMP response. + * + * If "timeout" is negative, @code _cupsSNMPRead@ will wait for a response + * indefinitely. + */ + +cups_snmp_t * /* O - SNMP packet or @code NULL@ if none */ +_cupsSNMPRead(int fd, /* I - SNMP socket file descriptor */ + cups_snmp_t *packet, /* I - SNMP packet buffer */ + double timeout) /* I - Timeout in seconds */ +{ + unsigned char buffer[CUPS_SNMP_MAX_PACKET]; + /* Data packet */ + int bytes; /* Number of bytes received */ + socklen_t addrlen; /* Source address length */ + http_addr_t address; /* Source address */ + + + /* + * Range check input... + */ + + DEBUG_printf(("4_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)", fd, packet, + timeout)); + + if (fd < 0 || !packet) + { + DEBUG_puts("5_cupsSNMPRead: Returning NULL"); + + return (NULL); + } + + /* + * Optionally wait for a response... + */ + + if (timeout >= 0.0) + { + int ready; /* Data ready on socket? */ +#ifdef HAVE_POLL + struct pollfd pfd; /* Polled file descriptor */ + + pfd.fd = fd; + pfd.events = POLLIN; + + while ((ready = poll(&pfd, 1, (int)(timeout * 1000.0))) < 0 && + (errno == EINTR || errno == EAGAIN)); + +#else + fd_set input_set; /* select() input set */ + struct timeval stimeout; /* select() timeout */ + + do + { + FD_ZERO(&input_set); + FD_SET(fd, &input_set); + + stimeout.tv_sec = (int)timeout; + stimeout.tv_usec = (int)((timeout - stimeout.tv_sec) * 1000000); + + ready = select(fd + 1, &input_set, NULL, NULL, &stimeout); + } +# ifdef WIN32 + while (ready < 0 && WSAGetLastError() == WSAEINTR); +# else + while (ready < 0 && (errno == EINTR || errno == EAGAIN)); +# endif /* WIN32 */ +#endif /* HAVE_POLL */ + + /* + * If we don't have any data ready, return right away... + */ + + if (ready <= 0) + { + DEBUG_puts("5_cupsSNMPRead: Returning NULL (timeout)"); + + return (NULL); + } + } + + /* + * Read the response data... + */ + + addrlen = sizeof(address); + + if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address, + &addrlen)) < 0) + { + DEBUG_printf(("5_cupsSNMPRead: Returning NULL (%s)", strerror(errno))); + + return (NULL); + } + + /* + * Look for the response status code in the SNMP message header... + */ + + asn1_debug("DEBUG: IN ", buffer, bytes, 0); + + asn1_decode_snmp(buffer, bytes, packet); + + memcpy(&(packet->address), &address, sizeof(packet->address)); + + /* + * Return decoded data packet... + */ + + DEBUG_puts("5_cupsSNMPRead: Returning packet"); + + return (packet); +} + + +/* + * '_cupsSNMPSetDebug()' - Enable/disable debug logging to stderr. + */ + +void +_cupsSNMPSetDebug(int level) /* I - 1 to enable debug output, 0 otherwise */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + DEBUG_printf(("4_cupsSNMPSetDebug(level=%d)", level)); + + cg->snmp_debug = level; +} + + +/* + * '_cupsSNMPStringToOID()' - Convert a numeric OID string to an OID array. + * + * This function converts a string of the form ".N.N.N.N.N" to the + * corresponding OID array terminated by -1. + * + * @code NULL@ is returned if the array is not large enough or the string is + * not a valid OID number. + */ + +int * /* O - Pointer to OID array or @code NULL@ on error */ +_cupsSNMPStringToOID(const char *src, /* I - OID string */ + int *dst, /* I - OID array */ + int dstsize)/* I - Number of integers in OID array */ +{ + int *dstptr, /* Pointer into OID array */ + *dstend; /* End of OID array */ + + + DEBUG_printf(("4_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)", + src, dst, dstsize)); + + /* + * Range check input... + */ + + if (!src || !dst || dstsize < 2) + return (NULL); + + /* + * Skip leading "."... + */ + + if (*src == '.') + src ++; + + /* + * Loop to the end of the string... + */ + + for (dstend = dst + dstsize - 1, dstptr = dst, *dstptr = 0; + *src && dstptr < dstend; + src ++) + { + if (*src == '.') + { + dstptr ++; + *dstptr = 0; + } + else if (isdigit(*src & 255)) + *dstptr = *dstptr * 10 + *src - '0'; + else + break; + } + + if (*src) + return (NULL); + + /* + * Terminate the end of the OID array and return... + */ + + dstptr[1] = -1; + + return (dst); +} + + +/* + * '_cupsSNMPWalk()' - Enumerate a group of OIDs. + * + * This function queries all of the OIDs with the specified OID prefix, + * calling the "cb" function for every response that is received. + * + * The array pointed to by "prefix" is terminated by the value -1. + * + * If "timeout" is negative, @code _cupsSNMPWalk@ will wait for a response + * indefinitely. + */ + +int /* O - Number of OIDs found or -1 on error */ +_cupsSNMPWalk(int fd, /* I - SNMP socket */ + http_addr_t *address, /* I - Address to query */ + int version, /* I - SNMP version */ + const char *community,/* I - Community name */ + const int *prefix, /* I - OID prefix */ + double timeout, /* I - Timeout for each response in seconds */ + cups_snmp_cb_t cb, /* I - Function to call for each response */ + void *data) /* I - User data pointer that is passed to the callback function */ +{ + int count = 0; /* Number of OIDs found */ + int request_id = 0; /* Current request ID */ + cups_snmp_t packet; /* Current response packet */ + int lastoid[CUPS_SNMP_MAX_OID]; + /* Last OID we got */ + + + /* + * Range check input... + */ + + DEBUG_printf(("4_cupsSNMPWalk(fd=%d, address=%p, version=%d, " + "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)", + fd, address, version, community, prefix, timeout, cb, data)); + + if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || + !prefix || !cb) + { + DEBUG_puts("5_cupsSNMPWalk: Returning -1"); + + return (-1); + } + + /* + * Copy the OID prefix and then loop until we have no more OIDs... + */ + + _cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID); + lastoid[0] = -1; + + for (;;) + { + request_id ++; + + if (!_cupsSNMPWrite(fd, address, version, community, + CUPS_ASN1_GET_NEXT_REQUEST, request_id, + packet.object_name)) + { + DEBUG_puts("5_cupsSNMPWalk: Returning -1"); + + return (-1); + } + + if (!_cupsSNMPRead(fd, &packet, timeout)) + { + DEBUG_puts("5_cupsSNMPWalk: Returning -1"); + + return (-1); + } + + if (!_cupsSNMPIsOIDPrefixed(&packet, prefix) || + _cupsSNMPIsOID(&packet, lastoid)) + { + DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count)); + + return (count); + } + + if (packet.error || packet.error_status) + { + DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count > 0 ? count : -1)); + + return (count > 0 ? count : -1); + } + + _cupsSNMPCopyOID(lastoid, packet.object_name, CUPS_SNMP_MAX_OID); + + count ++; + + (*cb)(&packet, data); + } +} + + +/* + * '_cupsSNMPWrite()' - Send an SNMP query packet. + * + * The array pointed to by "oid" is terminated by the value -1. + */ + +int /* O - 1 on success, 0 on error */ +_cupsSNMPWrite( + int fd, /* I - SNMP socket */ + http_addr_t *address, /* I - Address to send to */ + int version, /* I - SNMP version */ + const char *community, /* I - Community name */ + cups_asn1_t request_type, /* I - Request type */ + const unsigned request_id, /* I - Request ID */ + const int *oid) /* I - OID */ +{ + int i; /* Looping var */ + cups_snmp_t packet; /* SNMP message packet */ + unsigned char buffer[CUPS_SNMP_MAX_PACKET]; + /* SNMP message buffer */ + int bytes; /* Size of message */ + http_addr_t temp; /* Copy of address */ + + + /* + * Range check input... + */ + + DEBUG_printf(("4_cupsSNMPWrite(fd=%d, address=%p, version=%d, " + "community=\"%s\", request_type=%d, request_id=%u, oid=%p)", + fd, address, version, community, request_type, request_id, oid)); + + if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || + (request_type != CUPS_ASN1_GET_REQUEST && + request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid) + { + DEBUG_puts("5_cupsSNMPWrite: Returning 0 (bad arguments)"); + + return (0); + } + + /* + * Create the SNMP message... + */ + + memset(&packet, 0, sizeof(packet)); + + packet.version = version; + packet.request_type = request_type; + packet.request_id = request_id; + packet.object_type = CUPS_ASN1_NULL_VALUE; + + strlcpy(packet.community, community, sizeof(packet.community)); + + for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++) + packet.object_name[i] = oid[i]; + packet.object_name[i] = -1; + + if (oid[i] >= 0) + { + DEBUG_puts("5_cupsSNMPWrite: Returning 0 (OID too big)"); + + errno = E2BIG; + return (0); + } + + bytes = asn1_encode_snmp(buffer, sizeof(buffer), &packet); + + if (bytes < 0) + { + DEBUG_puts("5_cupsSNMPWrite: Returning 0 (request too big)"); + + errno = E2BIG; + return (0); + } + + asn1_debug("DEBUG: OUT ", buffer, bytes, 0); + + /* + * Send the message... + */ + + temp = *address; + + _httpAddrSetPort(&temp, CUPS_SNMP_PORT); + + return (sendto(fd, buffer, bytes, 0, (void *)&temp, + httpAddrLength(&temp)) == bytes); +} + + +/* + * 'asn1_debug()' - Decode an ASN1-encoded message. + */ + +static void +asn1_debug(const char *prefix, /* I - Prefix string */ + unsigned char *buffer, /* I - Buffer */ + size_t len, /* I - Length of buffer */ + int indent) /* I - Indentation */ +{ + int i; /* Looping var */ + unsigned char *bufend; /* End of buffer */ + int integer; /* Number value */ + int oid[CUPS_SNMP_MAX_OID]; /* OID value */ + char string[CUPS_SNMP_MAX_STRING]; + /* String value */ + unsigned char value_type; /* Type of value */ + int value_length; /* Length of value */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + if (cg->snmp_debug <= 0) + return; + + if (cg->snmp_debug > 1 && indent == 0) + { + /* + * Do a hex dump of the packet... + */ + + int j; + + fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len); + + for (i = 0; i < (int)len; i += 16) + { + fprintf(stderr, "%s%04x:", prefix, i); + + for (j = 0; j < 16 && (i + j) < (int)len; j ++) + { + if (j && !(j & 3)) + fprintf(stderr, " %02x", buffer[i + j]); + else + fprintf(stderr, " %02x", buffer[i + j]); + } + + while (j < 16) + { + if (j && !(j & 3)) + fputs(" ", stderr); + else + fputs(" ", stderr); + + j ++; + } + + fputs(" ", stderr); + + for (j = 0; j < 16 && (i + j) < (int)len; j ++) + if (buffer[i + j] < ' ' || buffer[i + j] >= 0x7f) + putc('.', stderr); + else + putc(buffer[i + j], stderr); + + putc('\n', stderr); + } + } + + if (indent == 0) + fprintf(stderr, "%sMessage:\n", prefix); + + bufend = buffer + len; + + while (buffer < bufend) + { + /* + * Get value type... + */ + + value_type = asn1_get_type(&buffer, bufend); + value_length = asn1_get_length(&buffer, bufend); + + switch (value_type) + { + case CUPS_ASN1_BOOLEAN : + integer = asn1_get_integer(&buffer, bufend, value_length); + + fprintf(stderr, "%s%*sBOOLEAN %d bytes %d\n", prefix, indent, "", + value_length, integer); + break; + + case CUPS_ASN1_INTEGER : + integer = asn1_get_integer(&buffer, bufend, value_length); + + fprintf(stderr, "%s%*sINTEGER %d bytes %d\n", prefix, indent, "", + value_length, integer); + break; + + case CUPS_ASN1_COUNTER : + integer = asn1_get_integer(&buffer, bufend, value_length); + + fprintf(stderr, "%s%*sCOUNTER %d bytes %u\n", prefix, indent, "", + value_length, (unsigned)integer); + break; + + case CUPS_ASN1_GAUGE : + integer = asn1_get_integer(&buffer, bufend, value_length); + + fprintf(stderr, "%s%*sGAUGE %d bytes %u\n", prefix, indent, "", + value_length, (unsigned)integer); + break; + + case CUPS_ASN1_TIMETICKS : + integer = asn1_get_integer(&buffer, bufend, value_length); + + fprintf(stderr, "%s%*sTIMETICKS %d bytes %u\n", prefix, indent, "", + value_length, (unsigned)integer); + break; + + case CUPS_ASN1_OCTET_STRING : + fprintf(stderr, "%s%*sOCTET STRING %d bytes \"%s\"\n", prefix, + indent, "", value_length, + asn1_get_string(&buffer, bufend, value_length, string, + sizeof(string))); + break; + + case CUPS_ASN1_HEX_STRING : + asn1_get_string(&buffer, bufend, value_length, string, + sizeof(string)); + fprintf(stderr, "%s%*sHex-STRING %d bytes", prefix, + indent, "", value_length); + for (i = 0; i < value_length; i ++) + fprintf(stderr, " %02X", string[i] & 255); + putc('\n', stderr); + break; + + case CUPS_ASN1_NULL_VALUE : + fprintf(stderr, "%s%*sNULL VALUE %d bytes\n", prefix, indent, "", + value_length); + + buffer += value_length; + break; + + case CUPS_ASN1_OID : + integer = asn1_get_oid(&buffer, bufend, value_length, oid, + CUPS_SNMP_MAX_OID); + + fprintf(stderr, "%s%*sOID %d bytes ", prefix, indent, "", + value_length); + for (i = 0; i < integer; i ++) + fprintf(stderr, ".%d", oid[i]); + putc('\n', stderr); + break; + + case CUPS_ASN1_SEQUENCE : + fprintf(stderr, "%s%*sSEQUENCE %d bytes\n", prefix, indent, "", + value_length); + asn1_debug(prefix, buffer, value_length, indent + 4); + + buffer += value_length; + break; + + case CUPS_ASN1_GET_NEXT_REQUEST : + fprintf(stderr, "%s%*sGet-Next-Request-PDU %d bytes\n", prefix, + indent, "", value_length); + asn1_debug(prefix, buffer, value_length, indent + 4); + + buffer += value_length; + break; + + case CUPS_ASN1_GET_REQUEST : + fprintf(stderr, "%s%*sGet-Request-PDU %d bytes\n", prefix, indent, "", + value_length); + asn1_debug(prefix, buffer, value_length, indent + 4); + + buffer += value_length; + break; + + case CUPS_ASN1_GET_RESPONSE : + fprintf(stderr, "%s%*sGet-Response-PDU %d bytes\n", prefix, indent, + "", value_length); + asn1_debug(prefix, buffer, value_length, indent + 4); + + buffer += value_length; + break; + + default : + fprintf(stderr, "%s%*sUNKNOWN(%x) %d bytes\n", prefix, indent, "", + value_type, value_length); + + buffer += value_length; + break; + } + } +} + + +/* + * 'asn1_decode_snmp()' - Decode a SNMP packet. + */ + +static int /* O - 0 on success, -1 on error */ +asn1_decode_snmp(unsigned char *buffer, /* I - Buffer */ + size_t len, /* I - Size of buffer */ + cups_snmp_t *packet) /* I - SNMP packet */ +{ + unsigned char *bufptr, /* Pointer into the data */ + *bufend; /* End of data */ + int length; /* Length of value */ + + + /* + * Initialize the decoding... + */ + + memset(packet, 0, sizeof(cups_snmp_t)); + packet->object_name[0] = -1; + + bufptr = buffer; + bufend = buffer + len; + + if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) + snmp_set_error(packet, _("Packet does not start with SEQUENCE")); + else if (asn1_get_length(&bufptr, bufend) == 0) + snmp_set_error(packet, _("SEQUENCE uses indefinite length")); + else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) + snmp_set_error(packet, _("No version number")); + else if ((length = asn1_get_length(&bufptr, bufend)) == 0) + snmp_set_error(packet, _("Version uses indefinite length")); + else if ((packet->version = asn1_get_integer(&bufptr, bufend, length)) + != CUPS_SNMP_VERSION_1) + snmp_set_error(packet, _("Bad SNMP version number")); + else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OCTET_STRING) + snmp_set_error(packet, _("No community name")); + else if ((length = asn1_get_length(&bufptr, bufend)) == 0) + snmp_set_error(packet, _("Community name uses indefinite length")); + else + { + asn1_get_string(&bufptr, bufend, length, packet->community, + sizeof(packet->community)); + + if ((packet->request_type = asn1_get_type(&bufptr, bufend)) + != CUPS_ASN1_GET_RESPONSE) + snmp_set_error(packet, _("Packet does not contain a Get-Response-PDU")); + else if (asn1_get_length(&bufptr, bufend) == 0) + snmp_set_error(packet, _("Get-Response-PDU uses indefinite length")); + else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) + snmp_set_error(packet, _("No request-id")); + else if ((length = asn1_get_length(&bufptr, bufend)) == 0) + snmp_set_error(packet, _("request-id uses indefinite length")); + else + { + packet->request_id = asn1_get_integer(&bufptr, bufend, length); + + if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) + snmp_set_error(packet, _("No error-status")); + else if ((length = asn1_get_length(&bufptr, bufend)) == 0) + snmp_set_error(packet, _("error-status uses indefinite length")); + else + { + packet->error_status = asn1_get_integer(&bufptr, bufend, length); + + if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) + snmp_set_error(packet, _("No error-index")); + else if ((length = asn1_get_length(&bufptr, bufend)) == 0) + snmp_set_error(packet, _("error-index uses indefinite length")); + else + { + packet->error_index = asn1_get_integer(&bufptr, bufend, length); + + if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) + snmp_set_error(packet, _("No variable-bindings SEQUENCE")); + else if (asn1_get_length(&bufptr, bufend) == 0) + snmp_set_error(packet, + _("variable-bindings uses indefinite length")); + else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) + snmp_set_error(packet, _("No VarBind SEQUENCE")); + else if (asn1_get_length(&bufptr, bufend) == 0) + snmp_set_error(packet, _("VarBind uses indefinite length")); + else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OID) + snmp_set_error(packet, _("No name OID")); + else if ((length = asn1_get_length(&bufptr, bufend)) == 0) + snmp_set_error(packet, _("Name OID uses indefinite length")); + else + { + asn1_get_oid(&bufptr, bufend, length, packet->object_name, + CUPS_SNMP_MAX_OID); + + packet->object_type = asn1_get_type(&bufptr, bufend); + + if ((length = asn1_get_length(&bufptr, bufend)) == 0 && + packet->object_type != CUPS_ASN1_NULL_VALUE && + packet->object_type != CUPS_ASN1_OCTET_STRING) + snmp_set_error(packet, _("Value uses indefinite length")); + else + { + switch (packet->object_type) + { + case CUPS_ASN1_BOOLEAN : + packet->object_value.boolean = + asn1_get_integer(&bufptr, bufend, length); + break; + + case CUPS_ASN1_INTEGER : + packet->object_value.integer = + asn1_get_integer(&bufptr, bufend, length); + break; + + case CUPS_ASN1_NULL_VALUE : + break; + + case CUPS_ASN1_OCTET_STRING : + case CUPS_ASN1_BIT_STRING : + case CUPS_ASN1_HEX_STRING : + packet->object_value.string.num_bytes = length; + asn1_get_string(&bufptr, bufend, length, + (char *)packet->object_value.string.bytes, + sizeof(packet->object_value.string.bytes)); + break; + + case CUPS_ASN1_OID : + asn1_get_oid(&bufptr, bufend, length, + packet->object_value.oid, CUPS_SNMP_MAX_OID); + break; + + case CUPS_ASN1_COUNTER : + packet->object_value.counter = + asn1_get_integer(&bufptr, bufend, length); + break; + + case CUPS_ASN1_GAUGE : + packet->object_value.gauge = + asn1_get_integer(&bufptr, bufend, length); + break; + + case CUPS_ASN1_TIMETICKS : + packet->object_value.timeticks = + asn1_get_integer(&bufptr, bufend, length); + break; + + default : + snmp_set_error(packet, _("Unsupported value type")); + break; + } + } + } + } + } + } + } + + return (packet->error ? -1 : 0); +} + + +/* + * 'asn1_encode_snmp()' - Encode a SNMP packet. + */ + +static int /* O - Length on success, -1 on error */ +asn1_encode_snmp(unsigned char *buffer, /* I - Buffer */ + size_t bufsize, /* I - Size of buffer */ + cups_snmp_t *packet) /* I - SNMP packet */ +{ + unsigned char *bufptr; /* Pointer into buffer */ + int total, /* Total length */ + msglen, /* Length of entire message */ + commlen, /* Length of community string */ + reqlen, /* Length of request */ + listlen, /* Length of variable list */ + varlen, /* Length of variable */ + namelen, /* Length of object name OID */ + valuelen; /* Length of object value */ + + + /* + * Get the lengths of the community string, OID, and message... + */ + + + namelen = asn1_size_oid(packet->object_name); + + switch (packet->object_type) + { + case CUPS_ASN1_NULL_VALUE : + valuelen = 0; + break; + + case CUPS_ASN1_BOOLEAN : + valuelen = asn1_size_integer(packet->object_value.boolean); + break; + + case CUPS_ASN1_INTEGER : + valuelen = asn1_size_integer(packet->object_value.integer); + break; + + case CUPS_ASN1_OCTET_STRING : + valuelen = packet->object_value.string.num_bytes; + break; + + case CUPS_ASN1_OID : + valuelen = asn1_size_oid(packet->object_value.oid); + break; + + default : + packet->error = "Unknown object type"; + return (-1); + } + + varlen = 1 + asn1_size_length(namelen) + namelen + + 1 + asn1_size_length(valuelen) + valuelen; + listlen = 1 + asn1_size_length(varlen) + varlen; + reqlen = 2 + asn1_size_integer(packet->request_id) + + 2 + asn1_size_integer(packet->error_status) + + 2 + asn1_size_integer(packet->error_index) + + 1 + asn1_size_length(listlen) + listlen; + commlen = strlen(packet->community); + msglen = 2 + asn1_size_integer(packet->version) + + 1 + asn1_size_length(commlen) + commlen + + 1 + asn1_size_length(reqlen) + reqlen; + total = 1 + asn1_size_length(msglen) + msglen; + + if (total > (int)bufsize) + { + packet->error = "Message too large for buffer"; + return (-1); + } + + /* + * Then format the message... + */ + + bufptr = buffer; + + *bufptr++ = CUPS_ASN1_SEQUENCE; /* SNMPv1 message header */ + asn1_set_length(&bufptr, msglen); + + asn1_set_integer(&bufptr, packet->version); + /* version */ + + *bufptr++ = CUPS_ASN1_OCTET_STRING; /* community */ + asn1_set_length(&bufptr, commlen); + memcpy(bufptr, packet->community, commlen); + bufptr += commlen; + + *bufptr++ = packet->request_type; /* Get-Request-PDU/Get-Next-Request-PDU */ + asn1_set_length(&bufptr, reqlen); + + asn1_set_integer(&bufptr, packet->request_id); + + asn1_set_integer(&bufptr, packet->error_status); + + asn1_set_integer(&bufptr, packet->error_index); + + *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable-bindings */ + asn1_set_length(&bufptr, listlen); + + *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable */ + asn1_set_length(&bufptr, varlen); + + asn1_set_oid(&bufptr, packet->object_name); + /* ObjectName */ + + switch (packet->object_type) + { + case CUPS_ASN1_NULL_VALUE : + *bufptr++ = CUPS_ASN1_NULL_VALUE; + /* ObjectValue */ + *bufptr++ = 0; /* Length */ + break; + + case CUPS_ASN1_BOOLEAN : + asn1_set_integer(&bufptr, packet->object_value.boolean); + break; + + case CUPS_ASN1_INTEGER : + asn1_set_integer(&bufptr, packet->object_value.integer); + break; + + case CUPS_ASN1_OCTET_STRING : + *bufptr++ = CUPS_ASN1_OCTET_STRING; + asn1_set_length(&bufptr, valuelen); + memcpy(bufptr, packet->object_value.string.bytes, valuelen); + bufptr += valuelen; + break; + + case CUPS_ASN1_OID : + asn1_set_oid(&bufptr, packet->object_value.oid); + break; + + default : + break; + } + + return (bufptr - buffer); +} + + +/* + * 'asn1_get_integer()' - Get an integer value. + */ + +static int /* O - Integer value */ +asn1_get_integer( + unsigned char **buffer, /* IO - Pointer in buffer */ + unsigned char *bufend, /* I - End of buffer */ + int length) /* I - Length of value */ +{ + int value; /* Integer value */ + + + if (length > sizeof(int)) + { + (*buffer) += length; + return (0); + } + + for (value = (**buffer & 0x80) ? -1 : 0; + length > 0 && *buffer < bufend; + length --, (*buffer) ++) + value = (value << 8) | **buffer; + + return (value); +} + + +/* + * 'asn1_get_length()' - Get a value length. + */ + +static unsigned /* O - Length */ +asn1_get_length(unsigned char **buffer, /* IO - Pointer in buffer */ + unsigned char *bufend) /* I - End of buffer */ +{ + unsigned length; /* Length */ + + + length = **buffer; + (*buffer) ++; + + if (length & 128) + { + int count; /* Number of bytes for length */ + + + if ((count = length & 127) > sizeof(unsigned)) + { + (*buffer) += count; + return (0); + } + + for (length = 0; + count > 0 && *buffer < bufend; + count --, (*buffer) ++) + length = (length << 8) | **buffer; + } + + return (length); +} + + +/* + * 'asn1_get_oid()' - Get an OID value. + */ + +static int /* O - Number of OIDs */ +asn1_get_oid( + unsigned char **buffer, /* IO - Pointer in buffer */ + unsigned char *bufend, /* I - End of buffer */ + int length, /* I - Length of value */ + int *oid, /* I - OID buffer */ + int oidsize) /* I - Size of OID buffer */ +{ + unsigned char *valend; /* End of value */ + int *oidptr, /* Current OID */ + *oidend; /* End of OID buffer */ + int number; /* OID number */ + + + valend = *buffer + length; + oidptr = oid; + oidend = oid + oidsize - 1; + + if (valend > bufend) + valend = bufend; + + number = asn1_get_packed(buffer, bufend); + + if (number < 80) + { + *oidptr++ = number / 40; + number = number % 40; + *oidptr++ = number; + } + else + { + *oidptr++ = 2; + number -= 80; + *oidptr++ = number; + } + + while (*buffer < valend) + { + number = asn1_get_packed(buffer, bufend); + + if (oidptr < oidend) + *oidptr++ = number; + } + + *oidptr = -1; + + return (oidptr - oid); +} + + +/* + * 'asn1_get_packed()' - Get a packed integer value. + */ + +static int /* O - Value */ +asn1_get_packed( + unsigned char **buffer, /* IO - Pointer in buffer */ + unsigned char *bufend) /* I - End of buffer */ +{ + int value; /* Value */ + + + value = 0; + + while ((**buffer & 128) && *buffer < bufend) + { + value = (value << 7) | (**buffer & 127); + (*buffer) ++; + } + + if (*buffer < bufend) + { + value = (value << 7) | **buffer; + (*buffer) ++; + } + + return (value); +} + + +/* + * 'asn1_get_string()' - Get a string value. + */ + +static char * /* O - String */ +asn1_get_string( + unsigned char **buffer, /* IO - Pointer in buffer */ + unsigned char *bufend, /* I - End of buffer */ + int length, /* I - Value length */ + char *string, /* I - String buffer */ + int strsize) /* I - String buffer size */ +{ + if (length > (bufend - *buffer)) + length = bufend - *buffer; + + if (length < 0) + { + /* + * Disallow negative lengths! + */ + + *string = '\0'; + } + else if (length < strsize) + { + /* + * String is smaller than the buffer... + */ + + if (length > 0) + memcpy(string, *buffer, length); + + string[length] = '\0'; + } + else + { + /* + * String is larger than the buffer... + */ + + memcpy(string, *buffer, strsize - 1); + string[strsize - 1] = '\0'; + } + + if (length > 0) + (*buffer) += length; + + return (length < 0 ? NULL : string); +} + + +/* + * 'asn1_get_type()' - Get a value type. + */ + +static int /* O - Type */ +asn1_get_type(unsigned char **buffer, /* IO - Pointer in buffer */ + unsigned char *bufend) /* I - End of buffer */ +{ + int type; /* Type */ + + + type = **buffer; + (*buffer) ++; + + if ((type & 31) == 31) + type = asn1_get_packed(buffer, bufend); + + return (type); +} + + +/* + * 'asn1_set_integer()' - Set an integer value. + */ + +static void +asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */ + int integer) /* I - Integer value */ +{ + **buffer = CUPS_ASN1_INTEGER; + (*buffer) ++; + + if (integer > 0x7fffff || integer < -0x800000) + { + **buffer = 4; + (*buffer) ++; + **buffer = integer >> 24; + (*buffer) ++; + **buffer = integer >> 16; + (*buffer) ++; + **buffer = integer >> 8; + (*buffer) ++; + **buffer = integer; + (*buffer) ++; + } + else if (integer > 0x7fff || integer < -0x8000) + { + **buffer = 3; + (*buffer) ++; + **buffer = integer >> 16; + (*buffer) ++; + **buffer = integer >> 8; + (*buffer) ++; + **buffer = integer; + (*buffer) ++; + } + else if (integer > 0x7f || integer < -0x80) + { + **buffer = 2; + (*buffer) ++; + **buffer = integer >> 8; + (*buffer) ++; + **buffer = integer; + (*buffer) ++; + } + else + { + **buffer = 1; + (*buffer) ++; + **buffer = integer; + (*buffer) ++; + } +} + + +/* + * 'asn1_set_length()' - Set a value length. + */ + +static void +asn1_set_length(unsigned char **buffer, /* IO - Pointer in buffer */ + unsigned length) /* I - Length value */ +{ + if (length > 255) + { + **buffer = 0x82; /* 2-byte length */ + (*buffer) ++; + **buffer = length >> 8; + (*buffer) ++; + **buffer = length; + (*buffer) ++; + } + else if (length > 127) + { + **buffer = 0x81; /* 1-byte length */ + (*buffer) ++; + **buffer = length; + (*buffer) ++; + } + else + { + **buffer = length; /* Length */ + (*buffer) ++; + } +} + + +/* + * 'asn1_set_oid()' - Set an OID value. + */ + +static void +asn1_set_oid(unsigned char **buffer, /* IO - Pointer in buffer */ + const int *oid) /* I - OID value */ +{ + **buffer = CUPS_ASN1_OID; + (*buffer) ++; + + asn1_set_length(buffer, asn1_size_oid(oid)); + + if (oid[1] < 0) + { + asn1_set_packed(buffer, oid[0] * 40); + return; + } + + asn1_set_packed(buffer, oid[0] * 40 + oid[1]); + + for (oid += 2; *oid >= 0; oid ++) + asn1_set_packed(buffer, *oid); +} + + +/* + * 'asn1_set_packed()' - Set a packed integer value. + */ + +static void +asn1_set_packed(unsigned char **buffer, /* IO - Pointer in buffer */ + int integer) /* I - Integer value */ +{ + if (integer > 0xfffffff) + { + **buffer = ((integer >> 28) & 0x7f) | 0x80; + (*buffer) ++; + } + + if (integer > 0x1fffff) + { + **buffer = ((integer >> 21) & 0x7f) | 0x80; + (*buffer) ++; + } + + if (integer > 0x3fff) + { + **buffer = ((integer >> 14) & 0x7f) | 0x80; + (*buffer) ++; + } + + if (integer > 0x7f) + { + **buffer = ((integer >> 7) & 0x7f) | 0x80; + (*buffer) ++; + } + + **buffer = integer & 0x7f; + (*buffer) ++; +} + + +/* + * 'asn1_size_integer()' - Figure out the number of bytes needed for an + * integer value. + */ + +static int /* O - Size in bytes */ +asn1_size_integer(int integer) /* I - Integer value */ +{ + if (integer > 0x7fffff || integer < -0x800000) + return (4); + else if (integer > 0x7fff || integer < -0x8000) + return (3); + else if (integer > 0x7f || integer < -0x80) + return (2); + else + return (1); +} + + +/* + * 'asn1_size_length()' - Figure out the number of bytes needed for a + * length value. + */ + +static int /* O - Size in bytes */ +asn1_size_length(int length) /* I - Length value */ +{ + if (length > 0xff) + return (3); + else if (length > 0x7f) + return (2); + else + return (1); +} + + +/* + * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an + * OID value. + */ + +static int /* O - Size in bytes */ +asn1_size_oid(const int *oid) /* I - OID value */ +{ + int length; /* Length of value */ + + + if (oid[1] < 0) + return (asn1_size_packed(oid[0] * 40)); + + for (length = asn1_size_packed(oid[0] * 40 + oid[1]), oid += 2; + *oid >= 0; + oid ++) + length += asn1_size_packed(*oid); + + return (length); +} + + +/* + * 'asn1_size_packed()' - Figure out the number of bytes needed for a + * packed integer value. + */ + +static int /* O - Size in bytes */ +asn1_size_packed(int integer) /* I - Integer value */ +{ + if (integer > 0xfffffff) + return (5); + else if (integer > 0x1fffff) + return (4); + else if (integer > 0x3fff) + return (3); + else if (integer > 0x7f) + return (2); + else + return (1); +} + + +/* + * 'snmp_set_error()' - Set the localized error for a packet. + */ + +static void +snmp_set_error(cups_snmp_t *packet, /* I - Packet */ + const char *message) /* I - Error message */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + + + if (!cg->lang_default) + cg->lang_default = cupsLangDefault(); + + packet->error = _cupsLangString(cg->lang_default, message); +} + + +/* + * End of "$Id: snmp.c 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/snprintf.c b/cups/snprintf.c new file mode 100644 index 0000000..fcebf89 --- /dev/null +++ b/cups/snprintf.c @@ -0,0 +1,362 @@ +/* + * "$Id: snprintf.c 6649 2007-07-11 21:46:42Z mike $" + * + * snprintf functions for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * _cups_vsnprintf() - Format a string into a fixed size buffer. + * _cups_snprintf() - Format a string into a fixed size buffer. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" + + +#ifndef HAVE_VSNPRINTF +/* + * '_cups_vsnprintf()' - Format a string into a fixed size buffer. + */ + +int /* O - Number of bytes formatted */ +_cups_vsnprintf(char *buffer, /* O - Output buffer */ + size_t bufsize, /* O - Size of output buffer */ + const char *format, /* I - printf-style format string */ + va_list ap) /* I - Pointer to additional arguments */ +{ + char *bufptr, /* Pointer to position in buffer */ + *bufend, /* Pointer to end of buffer */ + sign, /* Sign of format width */ + size, /* Size character (h, l, L) */ + type; /* Format type character */ + int width, /* Width of field */ + prec; /* Number of characters of precision */ + char tformat[100], /* Temporary format string for sprintf() */ + *tptr, /* Pointer into temporary format */ + temp[1024]; /* Buffer for formatted numbers */ + char *s; /* Pointer to string */ + int slen; /* Length of string */ + int bytes; /* Total number of bytes needed */ + + + /* + * Loop through the format string, formatting as needed... + */ + + bufptr = buffer; + bufend = buffer + bufsize - 1; + bytes = 0; + + while (*format) + { + if (*format == '%') + { + tptr = tformat; + *tptr++ = *format++; + + if (*format == '%') + { + if (bufptr && bufptr < bufend) *bufptr++ = *format; + bytes ++; + format ++; + continue; + } + else if (strchr(" -+#\'", *format)) + { + *tptr++ = *format; + sign = *format++; + } + else + sign = 0; + + if (*format == '*') + { + /* + * Get width from argument... + */ + + format ++; + width = va_arg(ap, int); + + snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", width); + tptr += strlen(tptr); + } + else + { + width = 0; + + while (isdigit(*format & 255)) + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + width = width * 10 + *format++ - '0'; + } + } + + if (*format == '.') + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + format ++; + + if (*format == '*') + { + /* + * Get precision from argument... + */ + + format ++; + prec = va_arg(ap, int); + + snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", prec); + tptr += strlen(tptr); + } + else + { + prec = 0; + + while (isdigit(*format & 255)) + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + prec = prec * 10 + *format++ - '0'; + } + } + } + else + prec = -1; + + if (*format == 'l' && format[1] == 'l') + { + size = 'L'; + + if (tptr < (tformat + sizeof(tformat) - 2)) + { + *tptr++ = 'l'; + *tptr++ = 'l'; + } + + format += 2; + } + else if (*format == 'h' || *format == 'l' || *format == 'L') + { + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + size = *format++; + } + + if (!*format) + break; + + if (tptr < (tformat + sizeof(tformat) - 1)) + *tptr++ = *format; + + type = *format++; + *tptr = '\0'; + + switch (type) + { + case 'E' : /* Floating point formats */ + case 'G' : + case 'e' : + case 'f' : + case 'g' : + if ((width + 2) > sizeof(temp)) + break; + + sprintf(temp, tformat, va_arg(ap, double)); + + bytes += (int)strlen(temp); + + if (bufptr) + { + if ((bufptr + strlen(temp)) > bufend) + { + strncpy(bufptr, temp, (size_t)(bufend - bufptr)); + bufptr = bufend; + } + else + { + strcpy(bufptr, temp); + bufptr += strlen(temp); + } + } + break; + + case 'B' : /* Integer formats */ + case 'X' : + case 'b' : + case 'd' : + case 'i' : + case 'o' : + case 'u' : + case 'x' : + if ((width + 2) > sizeof(temp)) + break; + + sprintf(temp, tformat, va_arg(ap, int)); + + bytes += (int)strlen(temp); + + if (bufptr) + { + if ((bufptr + strlen(temp)) > bufend) + { + strncpy(bufptr, temp, (size_t)(bufend - bufptr)); + bufptr = bufend; + } + else + { + strcpy(bufptr, temp); + bufptr += strlen(temp); + } + } + break; + + case 'p' : /* Pointer value */ + if ((width + 2) > sizeof(temp)) + break; + + sprintf(temp, tformat, va_arg(ap, void *)); + + bytes += (int)strlen(temp); + + if (bufptr) + { + if ((bufptr + strlen(temp)) > bufend) + { + strncpy(bufptr, temp, (size_t)(bufend - bufptr)); + bufptr = bufend; + } + else + { + strcpy(bufptr, temp); + bufptr += strlen(temp); + } + } + break; + + case 'c' : /* Character or character array */ + bytes += width; + + if (bufptr) + { + if (width <= 1) + *bufptr++ = va_arg(ap, int); + else + { + if ((bufptr + width) > bufend) + width = (int)(bufend - bufptr); + + memcpy(bufptr, va_arg(ap, char *), (size_t)width); + bufptr += width; + } + } + break; + + case 's' : /* String */ + if ((s = va_arg(ap, char *)) == NULL) + s = "(null)"; + + slen = (int)strlen(s); + if (slen > width && prec != width) + width = slen; + + bytes += width; + + if (bufptr) + { + if ((bufptr + width) > bufend) + width = (int)(bufend - bufptr); + + if (slen > width) + slen = width; + + if (sign == '-') + { + strncpy(bufptr, s, (size_t)slen); + memset(bufptr + slen, ' ', (size_t)(width - slen)); + } + else + { + memset(bufptr, ' ', (size_t)(width - slen)); + strncpy(bufptr + width - slen, s, (size_t)slen); + } + + bufptr += width; + } + break; + + case 'n' : /* Output number of chars so far */ + *(va_arg(ap, int *)) = bytes; + break; + } + } + else + { + bytes ++; + + if (bufptr && bufptr < bufend) + *bufptr++ = *format; + + format ++; + } + } + + /* + * Nul-terminate the string and return the number of characters needed. + */ + + *bufptr = '\0'; + + return (bytes); +} +#endif /* !HAVE_VSNPRINT */ + + +#ifndef HAVE_SNPRINTF +/* + * '_cups_snprintf()' - Format a string into a fixed size buffer. + */ + +int /* O - Number of bytes formatted */ +_cups_snprintf(char *buffer, /* O - Output buffer */ + size_t bufsize, /* O - Size of output buffer */ + const char *format, /* I - printf-style format string */ + ...) /* I - Additional arguments as needed */ +{ + int bytes; /* Number of bytes formatted */ + va_list ap; /* Pointer to additional arguments */ + + + va_start(ap, format); + bytes = vsnprintf(buffer, bufsize, format, ap); + va_end(ap); + + return (bytes); +} +#endif /* !HAVE_SNPRINTF */ + + +/* + * End of "$Id: snprintf.c 6649 2007-07-11 21:46:42Z mike $". + */ + diff --git a/cups/sspi-private.h b/cups/sspi-private.h new file mode 100644 index 0000000..e8f36c2 --- /dev/null +++ b/cups/sspi-private.h @@ -0,0 +1,82 @@ +/* + * Private SSPI definitions for CUPS. + * + * Copyright 2010 by Apple Inc. + * + * 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/". + */ + +#ifndef _CUPS_SSPI_PRIVATE_H_ +# define _CUPS_SSPI_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include +# include +# include +# include +# include +# include +# define SECURITY_WIN32 +# include +# include + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +typedef struct /**** SSPI/SSL data structure ****/ +{ + SOCKET sock; /* TCP/IP socket */ + CredHandle creds; /* Credentials */ + CtxtHandle context; /* SSL context */ + BOOL contextInitialized; /* Is context init'd? */ + SecPkgContext_StreamSizes streamSizes; /* SSL data stream sizes */ + BYTE *decryptBuffer; /* Data pre-decryption*/ + size_t decryptBufferLength; /* Length of decrypt buffer */ + size_t decryptBufferUsed; /* Bytes used in buffer */ + BYTE *readBuffer; /* Data post-decryption */ + size_t readBufferLength; /* Length of read buffer */ + size_t readBufferUsed; /* Bytes used in buffer */ + DWORD certFlags; /* Cert verification flags */ +} _sspi_struct_t; + + +/* + * Prototypes... + */ +_sspi_struct_t *_sspiAlloc(void); +BOOL _sspiAccept(_sspi_struct_t *conn); +BOOL _sspiConnect(_sspi_struct_t *conn, + const CHAR *hostname); +void _sspiFree(_sspi_struct_t *conn); +BOOL _sspiGetCredentials(_sspi_struct_t *conn, + const LPWSTR containerName, + const TCHAR *commonName, + BOOL server); +int _sspiPending(_sspi_struct_t *conn); +int _sspiRead(_sspi_struct_t *conn, + void *buf, size_t len); +void _sspiSetAllowsAnyRoot(_sspi_struct_t *conn, + BOOL allow); +void _sspiSetAllowsExpiredCerts(_sspi_struct_t *conn, + BOOL allow); +int _sspiWrite(_sspi_struct_t *conn, + void *buf, size_t len); + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_SSPI_PRIVATE_H_ */ diff --git a/cups/sspi.c b/cups/sspi.c new file mode 100644 index 0000000..ff79e5a --- /dev/null +++ b/cups/sspi.c @@ -0,0 +1,1485 @@ +/* + * "$Id: sspi.c 3247 2011-05-12 06:22:31Z msweet $" + * + * Windows SSPI SSL implementation for CUPS. + * + * Copyright 2010-2011 by Apple Inc. + * + * 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/". + * + * Contents: + * + * sspi_alloc() - Allocate SSPI ssl object + * _sspiGetCredentials() - Retrieve an SSL/TLS certificate from the + * system store If one cannot be found, one is + * created. + * _sspiConnect() - Make an SSL connection. This function + * assumes a TCP/IP connection has already been + * successfully made + * _sspiAccept() - Accept an SSL/TLS connection + * _sspiSetAllowsAnyRoot() - Set the client cert policy for untrusted + * root certs + * _sspiSetAllowsExpiredCerts() - Set the client cert policy for expired root + * certs + * _sspiWrite() - Write a buffer to an ssl socket + * _sspiRead() - Read a buffer from an ssl socket + * _sspiPending() - Returns the number of available bytes + * _sspiFree() - Close a connection and free resources + * sspi_verify_certificate() - Verify a server certificate + */ + +/* + * Include necessary headers... + */ + +#include "sspi-private.h" +#include "debug-private.h" + + +/* required to link this library for certificate functions */ +#pragma comment(lib, "Crypt32.lib") +#pragma comment(lib, "Secur32.lib") +#pragma comment(lib, "Ws2_32.lib") + + +#if !defined(SECURITY_FLAG_IGNORE_UNKNOWN_CA) +# define SECURITY_FLAG_IGNORE_UNKNOWN_CA 0x00000100 /* Untrusted root */ +#endif + +#if !defined(SECURITY_FLAG_IGNORE_CERT_DATE_INVALID) +# define SECURITY_FLAG_IGNORE_CERT_DATE_INVALID 0x00002000 /* Expired X509 Cert. */ +#endif + +static DWORD sspi_verify_certificate(PCCERT_CONTEXT serverCert, + const CHAR *serverName, + DWORD dwCertFlags); + + +/* + * 'sspi_alloc()' - Allocate SSPI ssl object + */ +_sspi_struct_t* /* O - New SSPI/SSL object */ +_sspiAlloc(void) +{ + _sspi_struct_t *conn = calloc(sizeof(_sspi_struct_t), 1); + + if (conn) + conn->sock = INVALID_SOCKET; + + return (conn); +} + + +/* + * '_sspiGetCredentials()' - Retrieve an SSL/TLS certificate from the system store + * If one cannot be found, one is created. + */ +BOOL /* O - 1 on success, 0 on failure */ +_sspiGetCredentials(_sspi_struct_t *conn, + /* I - Client connection */ + const LPWSTR container, + /* I - Cert container name */ + const TCHAR *cn, /* I - Common name of certificate */ + BOOL isServer) + /* I - Is caller a server? */ +{ + HCERTSTORE store = NULL; /* Certificate store */ + PCCERT_CONTEXT storedContext = NULL; + /* Context created from the store */ + PCCERT_CONTEXT createdContext = NULL; + /* Context created by us */ + DWORD dwSize = 0; /* 32 bit size */ + PBYTE p = NULL; /* Temporary storage */ + HCRYPTPROV hProv = (HCRYPTPROV) NULL; + /* Handle to a CSP */ + CERT_NAME_BLOB sib; /* Arbitrary array of bytes */ + SCHANNEL_CRED SchannelCred; /* Schannel credential data */ + TimeStamp tsExpiry; /* Time stamp */ + SECURITY_STATUS Status; /* Status */ + HCRYPTKEY hKey = (HCRYPTKEY) NULL; + /* Handle to crypto key */ + CRYPT_KEY_PROV_INFO kpi; /* Key container info */ + SYSTEMTIME et; /* System time */ + CERT_EXTENSIONS exts; /* Array of cert extensions */ + CRYPT_KEY_PROV_INFO ckp; /* Handle to crypto key */ + BOOL ok = TRUE; /* Return value */ + + if (!conn) + return (FALSE); + if (!cn) + return (FALSE); + + if (!CryptAcquireContextW(&hProv, (LPWSTR) container, MS_DEF_PROV_W, + PROV_RSA_FULL, + CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET)) + { + if (GetLastError() == NTE_EXISTS) + { + if (!CryptAcquireContextW(&hProv, (LPWSTR) container, MS_DEF_PROV_W, + PROV_RSA_FULL, CRYPT_MACHINE_KEYSET)) + { + DEBUG_printf(("_sspiGetCredentials: CryptAcquireContext failed: %x\n", + GetLastError())); + ok = FALSE; + goto cleanup; + } + } + } + + store = CertOpenStore(CERT_STORE_PROV_SYSTEM, + X509_ASN_ENCODING|PKCS_7_ASN_ENCODING, + hProv, + CERT_SYSTEM_STORE_LOCAL_MACHINE | + CERT_STORE_NO_CRYPT_RELEASE_FLAG | + CERT_STORE_OPEN_EXISTING_FLAG, + L"MY"); + + if (!store) + { + DEBUG_printf(("_sspiGetCredentials: CertOpenSystemStore failed: %x\n", + GetLastError())); + ok = FALSE; + goto cleanup; + } + + dwSize = 0; + + if (!CertStrToName(X509_ASN_ENCODING, cn, CERT_OID_NAME_STR, + NULL, NULL, &dwSize, NULL)) + { + DEBUG_printf(("_sspiGetCredentials: CertStrToName failed: %x\n", + GetLastError())); + ok = FALSE; + goto cleanup; + } + + p = (PBYTE) malloc(dwSize); + + if (!p) + { + DEBUG_printf(("_sspiGetCredentials: malloc failed for %d bytes", dwSize)); + ok = FALSE; + goto cleanup; + } + + if (!CertStrToName(X509_ASN_ENCODING, cn, CERT_OID_NAME_STR, NULL, + p, &dwSize, NULL)) + { + DEBUG_printf(("_sspiGetCredentials: CertStrToName failed: %x", + GetLastError())); + ok = FALSE; + goto cleanup; + } + + sib.cbData = dwSize; + sib.pbData = p; + + storedContext = CertFindCertificateInStore(store, X509_ASN_ENCODING|PKCS_7_ASN_ENCODING, + 0, CERT_FIND_SUBJECT_NAME, &sib, NULL); + + if (!storedContext) + { + /* + * If we couldn't find the context, then we'll + * create a new one + */ + if (!CryptGenKey(hProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hKey)) + { + DEBUG_printf(("_sspiGetCredentials: CryptGenKey failed: %x", + GetLastError())); + ok = FALSE; + goto cleanup; + } + + ZeroMemory(&kpi, sizeof(kpi)); + kpi.pwszContainerName = (LPWSTR) container; + kpi.pwszProvName = MS_DEF_PROV_W; + kpi.dwProvType = PROV_RSA_FULL; + kpi.dwFlags = CERT_SET_KEY_CONTEXT_PROP_ID; + kpi.dwKeySpec = AT_KEYEXCHANGE; + + GetSystemTime(&et); + et.wYear += 10; + + ZeroMemory(&exts, sizeof(exts)); + + createdContext = CertCreateSelfSignCertificate(hProv, &sib, 0, &kpi, NULL, NULL, + &et, &exts); + + if (!createdContext) + { + DEBUG_printf(("_sspiGetCredentials: CertCreateSelfSignCertificate failed: %x", + GetLastError())); + ok = FALSE; + goto cleanup; + } + + if (!CertAddCertificateContextToStore(store, createdContext, + CERT_STORE_ADD_REPLACE_EXISTING, + &storedContext)) + { + DEBUG_printf(("_sspiGetCredentials: CertAddCertificateContextToStore failed: %x", + GetLastError())); + ok = FALSE; + goto cleanup; + } + + ZeroMemory(&ckp, sizeof(ckp)); + ckp.pwszContainerName = (LPWSTR) container; + ckp.pwszProvName = MS_DEF_PROV_W; + ckp.dwProvType = PROV_RSA_FULL; + ckp.dwFlags = CRYPT_MACHINE_KEYSET; + ckp.dwKeySpec = AT_KEYEXCHANGE; + + if (!CertSetCertificateContextProperty(storedContext, + CERT_KEY_PROV_INFO_PROP_ID, + 0, &ckp)) + { + DEBUG_printf(("_sspiGetCredentials: CertSetCertificateContextProperty failed: %x", + GetLastError())); + ok = FALSE; + goto cleanup; + } + } + + ZeroMemory(&SchannelCred, sizeof(SchannelCred)); + + SchannelCred.dwVersion = SCHANNEL_CRED_VERSION; + SchannelCred.cCreds = 1; + SchannelCred.paCred = &storedContext; + + /* + * SSPI doesn't seem to like it if grbitEnabledProtocols + * is set for a client + */ + if (isServer) + SchannelCred.grbitEnabledProtocols = SP_PROT_SSL3TLS1; + + /* + * Create an SSPI credential. + */ + Status = AcquireCredentialsHandle(NULL, UNISP_NAME, + isServer ? SECPKG_CRED_INBOUND:SECPKG_CRED_OUTBOUND, + NULL, &SchannelCred, NULL, NULL, &conn->creds, + &tsExpiry); + if (Status != SEC_E_OK) + { + DEBUG_printf(("_sspiGetCredentials: AcquireCredentialsHandle failed: %x", Status)); + ok = FALSE; + goto cleanup; + } + +cleanup: + + /* + * Cleanup + */ + if (hKey) + CryptDestroyKey(hKey); + + if (createdContext) + CertFreeCertificateContext(createdContext); + + if (storedContext) + CertFreeCertificateContext(storedContext); + + if (p) + free(p); + + if (store) + CertCloseStore(store, 0); + + if (hProv) + CryptReleaseContext(hProv, 0); + + return (ok); +} + + +/* + * '_sspiConnect()' - Make an SSL connection. This function + * assumes a TCP/IP connection has already + * been successfully made + */ +BOOL /* O - 1 on success, 0 on failure */ +_sspiConnect(_sspi_struct_t *conn, /* I - Client connection */ + const CHAR *hostname) /* I - Server hostname */ +{ + PCCERT_CONTEXT serverCert; /* Server certificate */ + DWORD dwSSPIFlags; /* SSL connection attributes we want */ + DWORD dwSSPIOutFlags; /* SSL connection attributes we got */ + TimeStamp tsExpiry; /* Time stamp */ + SECURITY_STATUS scRet; /* Status */ + DWORD cbData; /* Data count */ + SecBufferDesc inBuffer; /* Array of SecBuffer structs */ + SecBuffer inBuffers[2]; /* Security package buffer */ + SecBufferDesc outBuffer; /* Array of SecBuffer structs */ + SecBuffer outBuffers[1]; /* Security package buffer */ + BOOL ok = TRUE; /* Return value */ + + serverCert = NULL; + + dwSSPIFlags = ISC_REQ_SEQUENCE_DETECT | + ISC_REQ_REPLAY_DETECT | + ISC_REQ_CONFIDENTIALITY | + ISC_RET_EXTENDED_ERROR | + ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_STREAM; + + /* + * Initiate a ClientHello message and generate a token. + */ + outBuffers[0].pvBuffer = NULL; + outBuffers[0].BufferType = SECBUFFER_TOKEN; + outBuffers[0].cbBuffer = 0; + + outBuffer.cBuffers = 1; + outBuffer.pBuffers = outBuffers; + outBuffer.ulVersion = SECBUFFER_VERSION; + + scRet = InitializeSecurityContext(&conn->creds, NULL, TEXT(""), dwSSPIFlags, + 0, SECURITY_NATIVE_DREP, NULL, 0, &conn->context, + &outBuffer, &dwSSPIOutFlags, &tsExpiry); + + if (scRet != SEC_I_CONTINUE_NEEDED) + { + DEBUG_printf(("_sspiConnect: InitializeSecurityContext(1) failed: %x", scRet)); + ok = FALSE; + goto cleanup; + } + + /* + * Send response to server if there is one. + */ + if (outBuffers[0].cbBuffer && outBuffers[0].pvBuffer) + { + cbData = send(conn->sock, outBuffers[0].pvBuffer, outBuffers[0].cbBuffer, 0); + + if ((cbData == SOCKET_ERROR) || !cbData) + { + DEBUG_printf(("_sspiConnect: send failed: %d", WSAGetLastError())); + FreeContextBuffer(outBuffers[0].pvBuffer); + DeleteSecurityContext(&conn->context); + ok = FALSE; + goto cleanup; + } + + DEBUG_printf(("_sspiConnect: %d bytes of handshake data sent", cbData)); + + /* + * Free output buffer. + */ + FreeContextBuffer(outBuffers[0].pvBuffer); + outBuffers[0].pvBuffer = NULL; + } + + dwSSPIFlags = ISC_REQ_MANUAL_CRED_VALIDATION | + ISC_REQ_SEQUENCE_DETECT | + ISC_REQ_REPLAY_DETECT | + ISC_REQ_CONFIDENTIALITY | + ISC_RET_EXTENDED_ERROR | + ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_STREAM; + + conn->decryptBufferUsed = 0; + + /* + * Loop until the handshake is finished or an error occurs. + */ + scRet = SEC_I_CONTINUE_NEEDED; + + while(scRet == SEC_I_CONTINUE_NEEDED || + scRet == SEC_E_INCOMPLETE_MESSAGE || + scRet == SEC_I_INCOMPLETE_CREDENTIALS) + { + if ((conn->decryptBufferUsed == 0) || (scRet == SEC_E_INCOMPLETE_MESSAGE)) + { + if (conn->decryptBufferLength <= conn->decryptBufferUsed) + { + conn->decryptBufferLength += 4096; + conn->decryptBuffer = (BYTE*) realloc(conn->decryptBuffer, conn->decryptBufferLength); + + if (!conn->decryptBuffer) + { + DEBUG_printf(("_sspiConnect: unable to allocate %d byte decrypt buffer", + conn->decryptBufferLength)); + SetLastError(E_OUTOFMEMORY); + ok = FALSE; + goto cleanup; + } + } + + cbData = recv(conn->sock, conn->decryptBuffer + conn->decryptBufferUsed, + (int) (conn->decryptBufferLength - conn->decryptBufferUsed), 0); + + if (cbData == SOCKET_ERROR) + { + DEBUG_printf(("_sspiConnect: recv failed: %d", WSAGetLastError())); + ok = FALSE; + goto cleanup; + } + else if (cbData == 0) + { + DEBUG_printf(("_sspiConnect: server unexpectedly disconnected")); + ok = FALSE; + goto cleanup; + } + + DEBUG_printf(("_sspiConnect: %d bytes of handshake data received", + cbData)); + + conn->decryptBufferUsed += cbData; + } + + /* + * Set up the input buffers. Buffer 0 is used to pass in data + * received from the server. Schannel will consume some or all + * of this. Leftover data (if any) will be placed in buffer 1 and + * given a buffer type of SECBUFFER_EXTRA. + */ + inBuffers[0].pvBuffer = conn->decryptBuffer; + inBuffers[0].cbBuffer = (unsigned long) conn->decryptBufferUsed; + inBuffers[0].BufferType = SECBUFFER_TOKEN; + + inBuffers[1].pvBuffer = NULL; + inBuffers[1].cbBuffer = 0; + inBuffers[1].BufferType = SECBUFFER_EMPTY; + + inBuffer.cBuffers = 2; + inBuffer.pBuffers = inBuffers; + inBuffer.ulVersion = SECBUFFER_VERSION; + + /* + * Set up the output buffers. These are initialized to NULL + * so as to make it less likely we'll attempt to free random + * garbage later. + */ + outBuffers[0].pvBuffer = NULL; + outBuffers[0].BufferType= SECBUFFER_TOKEN; + outBuffers[0].cbBuffer = 0; + + outBuffer.cBuffers = 1; + outBuffer.pBuffers = outBuffers; + outBuffer.ulVersion = SECBUFFER_VERSION; + + /* + * Call InitializeSecurityContext. + */ + scRet = InitializeSecurityContext(&conn->creds, &conn->context, NULL, dwSSPIFlags, + 0, SECURITY_NATIVE_DREP, &inBuffer, 0, NULL, + &outBuffer, &dwSSPIOutFlags, &tsExpiry); + + /* + * If InitializeSecurityContext was successful (or if the error was + * one of the special extended ones), send the contends of the output + * buffer to the server. + */ + if (scRet == SEC_E_OK || + scRet == SEC_I_CONTINUE_NEEDED || + FAILED(scRet) && (dwSSPIOutFlags & ISC_RET_EXTENDED_ERROR)) + { + if (outBuffers[0].cbBuffer && outBuffers[0].pvBuffer) + { + cbData = send(conn->sock, outBuffers[0].pvBuffer, outBuffers[0].cbBuffer, 0); + + if ((cbData == SOCKET_ERROR) || !cbData) + { + DEBUG_printf(("_sspiConnect: send failed: %d", WSAGetLastError())); + FreeContextBuffer(outBuffers[0].pvBuffer); + DeleteSecurityContext(&conn->context); + ok = FALSE; + goto cleanup; + } + + DEBUG_printf(("_sspiConnect: %d bytes of handshake data sent", cbData)); + + /* + * Free output buffer. + */ + FreeContextBuffer(outBuffers[0].pvBuffer); + outBuffers[0].pvBuffer = NULL; + } + } + + /* + * If InitializeSecurityContext returned SEC_E_INCOMPLETE_MESSAGE, + * then we need to read more data from the server and try again. + */ + if (scRet == SEC_E_INCOMPLETE_MESSAGE) + continue; + + /* + * If InitializeSecurityContext returned SEC_E_OK, then the + * handshake completed successfully. + */ + if (scRet == SEC_E_OK) + { + /* + * If the "extra" buffer contains data, this is encrypted application + * protocol layer stuff. It needs to be saved. The application layer + * will later decrypt it with DecryptMessage. + */ + DEBUG_printf(("_sspiConnect: Handshake was successful")); + + if (inBuffers[1].BufferType == SECBUFFER_EXTRA) + { + if (conn->decryptBufferLength < inBuffers[1].cbBuffer) + { + conn->decryptBuffer = realloc(conn->decryptBuffer, inBuffers[1].cbBuffer); + + if (!conn->decryptBuffer) + { + DEBUG_printf(("_sspiConnect: unable to allocate %d bytes for decrypt buffer", + inBuffers[1].cbBuffer)); + SetLastError(E_OUTOFMEMORY); + ok = FALSE; + goto cleanup; + } + } + + memmove(conn->decryptBuffer, + conn->decryptBuffer + (conn->decryptBufferUsed - inBuffers[1].cbBuffer), + inBuffers[1].cbBuffer); + + conn->decryptBufferUsed = inBuffers[1].cbBuffer; + + DEBUG_printf(("_sspiConnect: %d bytes of app data was bundled with handshake data", + conn->decryptBufferUsed)); + } + else + conn->decryptBufferUsed = 0; + + /* + * Bail out to quit + */ + break; + } + + /* + * Check for fatal error. + */ + if (FAILED(scRet)) + { + DEBUG_printf(("_sspiConnect: InitializeSecurityContext(2) failed: %x", scRet)); + ok = FALSE; + break; + } + + /* + * If InitializeSecurityContext returned SEC_I_INCOMPLETE_CREDENTIALS, + * then the server just requested client authentication. + */ + if (scRet == SEC_I_INCOMPLETE_CREDENTIALS) + { + /* + * Unimplemented + */ + DEBUG_printf(("_sspiConnect: server requested client credentials")); + ok = FALSE; + break; + } + + /* + * Copy any leftover data from the "extra" buffer, and go around + * again. + */ + if (inBuffers[1].BufferType == SECBUFFER_EXTRA) + { + memmove(conn->decryptBuffer, + conn->decryptBuffer + (conn->decryptBufferUsed - inBuffers[1].cbBuffer), + inBuffers[1].cbBuffer); + + conn->decryptBufferUsed = inBuffers[1].cbBuffer; + } + else + { + conn->decryptBufferUsed = 0; + } + } + + if (ok) + { + conn->contextInitialized = TRUE; + + /* + * Get the server cert + */ + scRet = QueryContextAttributes(&conn->context, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (VOID*) &serverCert ); + + if (scRet != SEC_E_OK) + { + DEBUG_printf(("_sspiConnect: QueryContextAttributes failed(SECPKG_ATTR_REMOTE_CERT_CONTEXT): %x", scRet)); + ok = FALSE; + goto cleanup; + } + + scRet = sspi_verify_certificate(serverCert, hostname, conn->certFlags); + + if (scRet != SEC_E_OK) + { + DEBUG_printf(("_sspiConnect: sspi_verify_certificate failed: %x", scRet)); + ok = FALSE; + goto cleanup; + } + + /* + * Find out how big the header/trailer will be: + */ + scRet = QueryContextAttributes(&conn->context, SECPKG_ATTR_STREAM_SIZES, &conn->streamSizes); + + if (scRet != SEC_E_OK) + { + DEBUG_printf(("_sspiConnect: QueryContextAttributes failed(SECPKG_ATTR_STREAM_SIZES): %x", scRet)); + ok = FALSE; + } + } + +cleanup: + + if (serverCert) + CertFreeCertificateContext(serverCert); + + return (ok); +} + + +/* + * '_sspiAccept()' - Accept an SSL/TLS connection + */ +BOOL /* O - 1 on success, 0 on failure */ +_sspiAccept(_sspi_struct_t *conn) /* I - Client connection */ +{ + DWORD dwSSPIFlags; /* SSL connection attributes we want */ + DWORD dwSSPIOutFlags; /* SSL connection attributes we got */ + TimeStamp tsExpiry; /* Time stamp */ + SECURITY_STATUS scRet; /* SSPI Status */ + SecBufferDesc inBuffer; /* Array of SecBuffer structs */ + SecBuffer inBuffers[2]; /* Security package buffer */ + SecBufferDesc outBuffer; /* Array of SecBuffer structs */ + SecBuffer outBuffers[1]; /* Security package buffer */ + DWORD num = 0; /* 32 bit status value */ + BOOL fInitContext = TRUE; + /* Has the context been init'd? */ + BOOL ok = TRUE; /* Return value */ + + if (!conn) + return (FALSE); + + dwSSPIFlags = ASC_REQ_SEQUENCE_DETECT | + ASC_REQ_REPLAY_DETECT | + ASC_REQ_CONFIDENTIALITY | + ASC_REQ_EXTENDED_ERROR | + ASC_REQ_ALLOCATE_MEMORY | + ASC_REQ_STREAM; + + conn->decryptBufferUsed = 0; + + /* + * Set OutBuffer for AcceptSecurityContext call + */ + outBuffer.cBuffers = 1; + outBuffer.pBuffers = outBuffers; + outBuffer.ulVersion = SECBUFFER_VERSION; + + scRet = SEC_I_CONTINUE_NEEDED; + + while (scRet == SEC_I_CONTINUE_NEEDED || + scRet == SEC_E_INCOMPLETE_MESSAGE || + scRet == SEC_I_INCOMPLETE_CREDENTIALS) + { + if ((conn->decryptBufferUsed == 0) || (scRet == SEC_E_INCOMPLETE_MESSAGE)) + { + if (conn->decryptBufferLength <= conn->decryptBufferUsed) + { + conn->decryptBufferLength += 4096; + conn->decryptBuffer = (BYTE*) realloc(conn->decryptBuffer, + conn->decryptBufferLength); + + if (!conn->decryptBuffer) + { + DEBUG_printf(("_sspiAccept: unable to allocate %d byte decrypt buffer", + conn->decryptBufferLength)); + ok = FALSE; + goto cleanup; + } + } + + for (;;) + { + num = recv(conn->sock, + conn->decryptBuffer + conn->decryptBufferUsed, + (int)(conn->decryptBufferLength - conn->decryptBufferUsed), + 0); + + if ((num == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK)) + Sleep(1); + else + break; + } + + if (num == SOCKET_ERROR) + { + DEBUG_printf(("_sspiAccept: recv failed: %d", WSAGetLastError())); + ok = FALSE; + goto cleanup; + } + else if (num == 0) + { + DEBUG_printf(("_sspiAccept: client disconnected")); + ok = FALSE; + goto cleanup; + } + + DEBUG_printf(("_sspiAccept: received %d (handshake) bytes from client", + num)); + conn->decryptBufferUsed += num; + } + + /* + * InBuffers[1] is for getting extra data that + * SSPI/SCHANNEL doesn't proccess on this + * run around the loop. + */ + inBuffers[0].pvBuffer = conn->decryptBuffer; + inBuffers[0].cbBuffer = (unsigned long) conn->decryptBufferUsed; + inBuffers[0].BufferType = SECBUFFER_TOKEN; + + inBuffers[1].pvBuffer = NULL; + inBuffers[1].cbBuffer = 0; + inBuffers[1].BufferType = SECBUFFER_EMPTY; + + inBuffer.cBuffers = 2; + inBuffer.pBuffers = inBuffers; + inBuffer.ulVersion = SECBUFFER_VERSION; + + /* + * Initialize these so if we fail, pvBuffer contains NULL, + * so we don't try to free random garbage at the quit + */ + outBuffers[0].pvBuffer = NULL; + outBuffers[0].BufferType = SECBUFFER_TOKEN; + outBuffers[0].cbBuffer = 0; + + scRet = AcceptSecurityContext(&conn->creds, (fInitContext?NULL:&conn->context), + &inBuffer, dwSSPIFlags, SECURITY_NATIVE_DREP, + (fInitContext?&conn->context:NULL), &outBuffer, + &dwSSPIOutFlags, &tsExpiry); + + fInitContext = FALSE; + + if (scRet == SEC_E_OK || + scRet == SEC_I_CONTINUE_NEEDED || + (FAILED(scRet) && ((dwSSPIOutFlags & ISC_RET_EXTENDED_ERROR) != 0))) + { + if (outBuffers[0].cbBuffer && outBuffers[0].pvBuffer) + { + /* + * Send response to server if there is one + */ + num = send(conn->sock, outBuffers[0].pvBuffer, outBuffers[0].cbBuffer, 0); + + if ((num == SOCKET_ERROR) || (num == 0)) + { + DEBUG_printf(("_sspiAccept: handshake send failed: %d", WSAGetLastError())); + ok = FALSE; + goto cleanup; + } + + DEBUG_printf(("_sspiAccept: send %d handshake bytes to client", + outBuffers[0].cbBuffer)); + + FreeContextBuffer(outBuffers[0].pvBuffer); + outBuffers[0].pvBuffer = NULL; + } + } + + if (scRet == SEC_E_OK) + { + /* + * If there's extra data then save it for + * next time we go to decrypt + */ + if (inBuffers[1].BufferType == SECBUFFER_EXTRA) + { + memcpy(conn->decryptBuffer, + (LPBYTE) (conn->decryptBuffer + (conn->decryptBufferUsed - inBuffers[1].cbBuffer)), + inBuffers[1].cbBuffer); + conn->decryptBufferUsed = inBuffers[1].cbBuffer; + } + else + { + conn->decryptBufferUsed = 0; + } + + ok = TRUE; + break; + } + else if (FAILED(scRet) && (scRet != SEC_E_INCOMPLETE_MESSAGE)) + { + DEBUG_printf(("_sspiAccept: AcceptSecurityContext failed: %x", scRet)); + ok = FALSE; + break; + } + + if (scRet != SEC_E_INCOMPLETE_MESSAGE && + scRet != SEC_I_INCOMPLETE_CREDENTIALS) + { + if (inBuffers[1].BufferType == SECBUFFER_EXTRA) + { + memcpy(conn->decryptBuffer, + (LPBYTE) (conn->decryptBuffer + (conn->decryptBufferUsed - inBuffers[1].cbBuffer)), + inBuffers[1].cbBuffer); + conn->decryptBufferUsed = inBuffers[1].cbBuffer; + } + else + { + conn->decryptBufferUsed = 0; + } + } + } + + if (ok) + { + conn->contextInitialized = TRUE; + + /* + * Find out how big the header will be: + */ + scRet = QueryContextAttributes(&conn->context, SECPKG_ATTR_STREAM_SIZES, &conn->streamSizes); + + if (scRet != SEC_E_OK) + { + DEBUG_printf(("_sspiAccept: QueryContextAttributes failed: %x", scRet)); + ok = FALSE; + } + } + +cleanup: + + return (ok); +} + + +/* + * '_sspiSetAllowsAnyRoot()' - Set the client cert policy for untrusted root certs + */ +void +_sspiSetAllowsAnyRoot(_sspi_struct_t *conn, + /* I - Client connection */ + BOOL allow) + /* I - Allow any root */ +{ + conn->certFlags = (allow) ? conn->certFlags | SECURITY_FLAG_IGNORE_UNKNOWN_CA : + conn->certFlags & ~SECURITY_FLAG_IGNORE_UNKNOWN_CA; +} + + +/* + * '_sspiSetAllowsExpiredCerts()' - Set the client cert policy for expired root certs + */ +void +_sspiSetAllowsExpiredCerts(_sspi_struct_t *conn, + /* I - Client connection */ + BOOL allow) + /* I - Allow expired certs */ +{ + conn->certFlags = (allow) ? conn->certFlags | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID : + conn->certFlags & ~SECURITY_FLAG_IGNORE_CERT_DATE_INVALID; +} + + +/* + * '_sspiWrite()' - Write a buffer to an ssl socket + */ +int /* O - Bytes written or SOCKET_ERROR */ +_sspiWrite(_sspi_struct_t *conn, /* I - Client connection */ + void *buf, /* I - Buffer */ + size_t len) /* I - Buffer length */ +{ + SecBufferDesc message; /* Array of SecBuffer struct */ + SecBuffer buffers[4] = { 0 }; /* Security package buffer */ + BYTE *buffer = NULL; /* Scratch buffer */ + int bufferLen; /* Buffer length */ + size_t bytesLeft; /* Bytes left to write */ + int index = 0; /* Index into buffer */ + int num = 0; /* Return value */ + + if (!conn || !buf || !len) + { + WSASetLastError(WSAEINVAL); + num = SOCKET_ERROR; + goto cleanup; + } + + bufferLen = conn->streamSizes.cbMaximumMessage + + conn->streamSizes.cbHeader + + conn->streamSizes.cbTrailer; + + buffer = (BYTE*) malloc(bufferLen); + + if (!buffer) + { + DEBUG_printf(("_sspiWrite: buffer alloc of %d bytes failed", bufferLen)); + WSASetLastError(E_OUTOFMEMORY); + num = SOCKET_ERROR; + goto cleanup; + } + + bytesLeft = len; + + while (bytesLeft) + { + size_t chunk = min(conn->streamSizes.cbMaximumMessage, /* Size of data to write */ + bytesLeft); + SECURITY_STATUS scRet; /* SSPI status */ + + /* + * Copy user data into the buffer, starting + * just past the header + */ + memcpy(buffer + conn->streamSizes.cbHeader, + ((BYTE*) buf) + index, + chunk); + + /* + * Setup the SSPI buffers + */ + message.ulVersion = SECBUFFER_VERSION; + message.cBuffers = 4; + message.pBuffers = buffers; + buffers[0].pvBuffer = buffer; + buffers[0].cbBuffer = conn->streamSizes.cbHeader; + buffers[0].BufferType = SECBUFFER_STREAM_HEADER; + buffers[1].pvBuffer = buffer + conn->streamSizes.cbHeader; + buffers[1].cbBuffer = (unsigned long) chunk; + buffers[1].BufferType = SECBUFFER_DATA; + buffers[2].pvBuffer = buffer + conn->streamSizes.cbHeader + chunk; + buffers[2].cbBuffer = conn->streamSizes.cbTrailer; + buffers[2].BufferType = SECBUFFER_STREAM_TRAILER; + buffers[3].BufferType = SECBUFFER_EMPTY; + + /* + * Encrypt the data + */ + scRet = EncryptMessage(&conn->context, 0, &message, 0); + + if (FAILED(scRet)) + { + DEBUG_printf(("_sspiWrite: EncryptMessage failed: %x", scRet)); + WSASetLastError(WSASYSCALLFAILURE); + num = SOCKET_ERROR; + goto cleanup; + } + + /* + * Send the data. Remember the size of + * the total data to send is the size + * of the header, the size of the data + * the caller passed in and the size + * of the trailer + */ + num = send(conn->sock, + buffer, + buffers[0].cbBuffer + buffers[1].cbBuffer + buffers[2].cbBuffer, + 0); + + if ((num == SOCKET_ERROR) || (num == 0)) + { + DEBUG_printf(("_sspiWrite: send failed: %ld", WSAGetLastError())); + goto cleanup; + } + + bytesLeft -= (int) chunk; + index += (int) chunk; + } + + num = (int) len; + +cleanup: + + if (buffer) + free(buffer); + + return (num); +} + + +/* + * '_sspiRead()' - Read a buffer from an ssl socket + */ +int /* O - Bytes read or SOCKET_ERROR */ +_sspiRead(_sspi_struct_t *conn, /* I - Client connection */ + void *buf, /* I - Buffer */ + size_t len) /* I - Buffer length */ +{ + SecBufferDesc message; /* Array of SecBuffer struct */ + SecBuffer buffers[4] = { 0 }; /* Security package buffer */ + int num = 0; /* Return value */ + + if (!conn) + { + WSASetLastError(WSAEINVAL); + num = SOCKET_ERROR; + goto cleanup; + } + + /* + * If there are bytes that have already been + * decrypted and have not yet been read, return + * those + */ + if (buf && (conn->readBufferUsed > 0)) + { + int bytesToCopy = (int) min(conn->readBufferUsed, len); /* Amount of bytes to copy */ + /* from read buffer */ + + memcpy(buf, conn->readBuffer, bytesToCopy); + conn->readBufferUsed -= bytesToCopy; + + if (conn->readBufferUsed > 0) + /* + * If the caller didn't request all the bytes + * we have in the buffer, then move the unread + * bytes down + */ + memmove(conn->readBuffer, + conn->readBuffer + bytesToCopy, + conn->readBufferUsed); + + num = bytesToCopy; + } + else + { + PSecBuffer pDataBuffer; /* Data buffer */ + PSecBuffer pExtraBuffer; /* Excess data buffer */ + SECURITY_STATUS scRet; /* SSPI status */ + int i; /* Loop control variable */ + + /* + * Initialize security buffer structs + */ + message.ulVersion = SECBUFFER_VERSION; + message.cBuffers = 4; + message.pBuffers = buffers; + + do + { + /* + * If there is not enough space in the + * buffer, then increase it's size + */ + if (conn->decryptBufferLength <= conn->decryptBufferUsed) + { + conn->decryptBufferLength += 4096; + conn->decryptBuffer = (BYTE*) realloc(conn->decryptBuffer, + conn->decryptBufferLength); + + if (!conn->decryptBuffer) + { + DEBUG_printf(("_sspiRead: unable to allocate %d byte buffer", + conn->decryptBufferLength)); + WSASetLastError(E_OUTOFMEMORY); + num = SOCKET_ERROR; + goto cleanup; + } + } + + buffers[0].pvBuffer = conn->decryptBuffer; + buffers[0].cbBuffer = (unsigned long) conn->decryptBufferUsed; + buffers[0].BufferType = SECBUFFER_DATA; + buffers[1].BufferType = SECBUFFER_EMPTY; + buffers[2].BufferType = SECBUFFER_EMPTY; + buffers[3].BufferType = SECBUFFER_EMPTY; + + scRet = DecryptMessage(&conn->context, &message, 0, NULL); + + if (scRet == SEC_E_INCOMPLETE_MESSAGE) + { + if (buf) + { + num = recv(conn->sock, + conn->decryptBuffer + conn->decryptBufferUsed, + (int)(conn->decryptBufferLength - conn->decryptBufferUsed), + 0); + if (num == SOCKET_ERROR) + { + DEBUG_printf(("_sspiRead: recv failed: %d", WSAGetLastError())); + goto cleanup; + } + else if (num == 0) + { + DEBUG_printf(("_sspiRead: server disconnected")); + goto cleanup; + } + + conn->decryptBufferUsed += num; + } + else + { + num = (int) conn->readBufferUsed; + goto cleanup; + } + } + } + while (scRet == SEC_E_INCOMPLETE_MESSAGE); + + if (scRet == SEC_I_CONTEXT_EXPIRED) + { + DEBUG_printf(("_sspiRead: context expired")); + WSASetLastError(WSAECONNRESET); + num = SOCKET_ERROR; + goto cleanup; + } + else if (scRet != SEC_E_OK) + { + DEBUG_printf(("_sspiRead: DecryptMessage failed: %lx", scRet)); + WSASetLastError(WSASYSCALLFAILURE); + num = SOCKET_ERROR; + goto cleanup; + } + + /* + * The decryption worked. Now, locate data buffer. + */ + pDataBuffer = NULL; + pExtraBuffer = NULL; + for (i = 1; i < 4; i++) + { + if (buffers[i].BufferType == SECBUFFER_DATA) + pDataBuffer = &buffers[i]; + else if (!pExtraBuffer && (buffers[i].BufferType == SECBUFFER_EXTRA)) + pExtraBuffer = &buffers[i]; + } + + /* + * If a data buffer is found, then copy + * the decrypted bytes to the passed-in + * buffer + */ + if (pDataBuffer) + { + int bytesToCopy = min(pDataBuffer->cbBuffer, (int) len); + /* Number of bytes to copy into buf */ + int bytesToSave = pDataBuffer->cbBuffer - bytesToCopy; + /* Number of bytes to save in our read buffer */ + + if (bytesToCopy) + memcpy(buf, pDataBuffer->pvBuffer, bytesToCopy); + + /* + * If there are more decrypted bytes than can be + * copied to the passed in buffer, then save them + */ + if (bytesToSave) + { + if ((int)(conn->readBufferLength - conn->readBufferUsed) < bytesToSave) + { + conn->readBufferLength = conn->readBufferUsed + bytesToSave; + conn->readBuffer = realloc(conn->readBuffer, + conn->readBufferLength); + + if (!conn->readBuffer) + { + DEBUG_printf(("_sspiRead: unable to allocate %d bytes", conn->readBufferLength)); + WSASetLastError(E_OUTOFMEMORY); + num = SOCKET_ERROR; + goto cleanup; + } + } + + memcpy(((BYTE*) conn->readBuffer) + conn->readBufferUsed, + ((BYTE*) pDataBuffer->pvBuffer) + bytesToCopy, + bytesToSave); + + conn->readBufferUsed += bytesToSave; + } + + num = (buf) ? bytesToCopy : (int) conn->readBufferUsed; + } + else + { + DEBUG_printf(("_sspiRead: unable to find data buffer")); + WSASetLastError(WSASYSCALLFAILURE); + num = SOCKET_ERROR; + goto cleanup; + } + + /* + * If the decryption process left extra bytes, + * then save those back in decryptBuffer. They will + * be processed the next time through the loop. + */ + if (pExtraBuffer) + { + memmove(conn->decryptBuffer, pExtraBuffer->pvBuffer, pExtraBuffer->cbBuffer); + conn->decryptBufferUsed = pExtraBuffer->cbBuffer; + } + else + { + conn->decryptBufferUsed = 0; + } + } + +cleanup: + + return (num); +} + + +/* + * '_sspiPending()' - Returns the number of available bytes + */ +int /* O - Number of available bytes */ +_sspiPending(_sspi_struct_t *conn) /* I - Client connection */ +{ + return (_sspiRead(conn, NULL, 0)); +} + + +/* + * '_sspiFree()' - Close a connection and free resources + */ +void +_sspiFree(_sspi_struct_t *conn) /* I - Client connection */ +{ + if (!conn) + return; + + if (conn->contextInitialized) + { + SecBufferDesc message; /* Array of SecBuffer struct */ + SecBuffer buffers[1] = { 0 }; + /* Security package buffer */ + DWORD dwType; /* Type */ + DWORD status; /* Status */ + + /* + * Notify schannel that we are about to close the connection. + */ + dwType = SCHANNEL_SHUTDOWN; + + buffers[0].pvBuffer = &dwType; + buffers[0].BufferType = SECBUFFER_TOKEN; + buffers[0].cbBuffer = sizeof(dwType); + + message.cBuffers = 1; + message.pBuffers = buffers; + message.ulVersion = SECBUFFER_VERSION; + + status = ApplyControlToken(&conn->context, &message); + + if (SUCCEEDED(status)) + { + PBYTE pbMessage; /* Message buffer */ + DWORD cbMessage; /* Message buffer count */ + DWORD cbData; /* Data count */ + DWORD dwSSPIFlags; /* SSL attributes we requested */ + DWORD dwSSPIOutFlags; /* SSL attributes we received */ + TimeStamp tsExpiry; /* Time stamp */ + + dwSSPIFlags = ASC_REQ_SEQUENCE_DETECT | + ASC_REQ_REPLAY_DETECT | + ASC_REQ_CONFIDENTIALITY | + ASC_REQ_EXTENDED_ERROR | + ASC_REQ_ALLOCATE_MEMORY | + ASC_REQ_STREAM; + + buffers[0].pvBuffer = NULL; + buffers[0].BufferType = SECBUFFER_TOKEN; + buffers[0].cbBuffer = 0; + + message.cBuffers = 1; + message.pBuffers = buffers; + message.ulVersion = SECBUFFER_VERSION; + + status = AcceptSecurityContext(&conn->creds, &conn->context, NULL, + dwSSPIFlags, SECURITY_NATIVE_DREP, NULL, + &message, &dwSSPIOutFlags, &tsExpiry); + + if (SUCCEEDED(status)) + { + pbMessage = buffers[0].pvBuffer; + cbMessage = buffers[0].cbBuffer; + + /* + * Send the close notify message to the client. + */ + if (pbMessage && cbMessage) + { + cbData = send(conn->sock, pbMessage, cbMessage, 0); + if ((cbData == SOCKET_ERROR) || (cbData == 0)) + { + status = WSAGetLastError(); + DEBUG_printf(("_sspiFree: sending close notify failed: %d", status)); + } + else + { + FreeContextBuffer(pbMessage); + } + } + } + else + { + DEBUG_printf(("_sspiFree: AcceptSecurityContext failed: %x", status)); + } + } + else + { + DEBUG_printf(("_sspiFree: ApplyControlToken failed: %x", status)); + } + + DeleteSecurityContext(&conn->context); + conn->contextInitialized = FALSE; + } + + if (conn->decryptBuffer) + { + free(conn->decryptBuffer); + conn->decryptBuffer = NULL; + } + + if (conn->readBuffer) + { + free(conn->readBuffer); + conn->readBuffer = NULL; + } + + if (conn->sock != INVALID_SOCKET) + { + closesocket(conn->sock); + conn->sock = INVALID_SOCKET; + } + + free(conn); +} + + +/* + * 'sspi_verify_certificate()' - Verify a server certificate + */ +static DWORD /* 0 - Error code (0 == No error) */ +sspi_verify_certificate(PCCERT_CONTEXT serverCert, + /* I - Server certificate */ + const CHAR *serverName, + /* I - Server name */ + DWORD dwCertFlags) + /* I - Verification flags */ +{ + HTTPSPolicyCallbackData httpsPolicy; + /* HTTPS Policy Struct */ + CERT_CHAIN_POLICY_PARA policyPara; + /* Cert chain policy parameters */ + CERT_CHAIN_POLICY_STATUS policyStatus; + /* Cert chain policy status */ + CERT_CHAIN_PARA chainPara; + /* Used for searching and matching criteria */ + PCCERT_CHAIN_CONTEXT chainContext = NULL; + /* Certificate chain */ + PWSTR serverNameUnicode = NULL; + /* Unicode server name */ + LPSTR rgszUsages[] = { szOID_PKIX_KP_SERVER_AUTH, + szOID_SERVER_GATED_CRYPTO, + szOID_SGC_NETSCAPE }; + /* How are we using this certificate? */ + DWORD cUsages = sizeof(rgszUsages) / sizeof(LPSTR); + /* Number of ites in rgszUsages */ + DWORD count; /* 32 bit count variable */ + DWORD status; /* Return value */ + + if (!serverCert) + { + status = SEC_E_WRONG_PRINCIPAL; + goto cleanup; + } + + /* + * Convert server name to unicode. + */ + if (!serverName || (strlen(serverName) == 0)) + { + status = SEC_E_WRONG_PRINCIPAL; + goto cleanup; + } + + count = MultiByteToWideChar(CP_ACP, 0, serverName, -1, NULL, 0); + serverNameUnicode = LocalAlloc(LMEM_FIXED, count * sizeof(WCHAR)); + if (!serverNameUnicode) + { + status = SEC_E_INSUFFICIENT_MEMORY; + goto cleanup; + } + count = MultiByteToWideChar(CP_ACP, 0, serverName, -1, serverNameUnicode, count); + if (count == 0) + { + status = SEC_E_WRONG_PRINCIPAL; + goto cleanup; + } + + /* + * Build certificate chain. + */ + ZeroMemory(&chainPara, sizeof(chainPara)); + chainPara.cbSize = sizeof(chainPara); + chainPara.RequestedUsage.dwType = USAGE_MATCH_TYPE_OR; + chainPara.RequestedUsage.Usage.cUsageIdentifier = cUsages; + chainPara.RequestedUsage.Usage.rgpszUsageIdentifier = rgszUsages; + + if (!CertGetCertificateChain(NULL, serverCert, NULL, serverCert->hCertStore, + &chainPara, 0, NULL, &chainContext)) + { + status = GetLastError(); + DEBUG_printf(("CertGetCertificateChain returned 0x%x\n", status)); + goto cleanup; + } + + /* + * Validate certificate chain. + */ + ZeroMemory(&httpsPolicy, sizeof(HTTPSPolicyCallbackData)); + httpsPolicy.cbStruct = sizeof(HTTPSPolicyCallbackData); + httpsPolicy.dwAuthType = AUTHTYPE_SERVER; + httpsPolicy.fdwChecks = dwCertFlags; + httpsPolicy.pwszServerName = serverNameUnicode; + + memset(&policyPara, 0, sizeof(policyPara)); + policyPara.cbSize = sizeof(policyPara); + policyPara.pvExtraPolicyPara = &httpsPolicy; + + memset(&policyStatus, 0, sizeof(policyStatus)); + policyStatus.cbSize = sizeof(policyStatus); + + if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL, chainContext, + &policyPara, &policyStatus)) + { + status = GetLastError(); + DEBUG_printf(("CertVerifyCertificateChainPolicy returned %d", status)); + goto cleanup; + } + + if (policyStatus.dwError) + { + status = policyStatus.dwError; + goto cleanup; + } + + status = SEC_E_OK; + +cleanup: + + if (chainContext) + CertFreeCertificateChain(chainContext); + + if (serverNameUnicode) + LocalFree(serverNameUnicode); + + return (status); +} + + +/* + * End of "$Id: sspi.c 3247 2011-05-12 06:22:31Z msweet $". + */ diff --git a/cups/string-private.h b/cups/string-private.h new file mode 100644 index 0000000..bef1aea --- /dev/null +++ b/cups/string-private.h @@ -0,0 +1,200 @@ +/* + * "$Id$" + * + * Private string definitions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + */ + +#ifndef _CUPS_STRING_PRIVATE_H_ +# define _CUPS_STRING_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include +# include +# include +# include +# include +# include + +# include "config.h" + +# ifdef HAVE_STRING_H +# include +# endif /* HAVE_STRING_H */ + +# ifdef HAVE_STRINGS_H +# include +# endif /* HAVE_STRINGS_H */ + +# ifdef HAVE_BSTRING_H +# include +# endif /* HAVE_BSTRING_H */ + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * String pool structures... + */ + +# define _CUPS_STR_GUARD 0x12344321 + +typedef struct _cups_sp_item_s /**** String Pool Item ****/ +{ +# ifdef DEBUG_GUARDS + unsigned int guard; /* Guard word */ +# endif /* DEBUG_GUARDS */ + unsigned int ref_count; /* Reference count */ + char str[1]; /* String */ +} _cups_sp_item_t; + + +/* + * Replacements for the ctype macros that are not affected by locale, since we + * really only care about testing for ASCII characters when parsing files, etc. + * + * The _CUPS_INLINE definition controls whether we get an inline function body, + * and external function body, or an external definition. + */ + +# if defined(__GNUC__) || __STDC_VERSION__ >= 199901L +# define _CUPS_INLINE static inline +# elif defined(_MSC_VER) +# define _CUPS_INLINE static __inline +# elif defined(_CUPS_STRING_C_) +# define _CUPS_INLINE +# endif /* __GNUC__ || __STDC_VERSION__ */ + +# ifdef _CUPS_INLINE +_CUPS_INLINE int /* O - 1 on match, 0 otherwise */ +_cups_isalnum(int ch) /* I - Character to test */ +{ + return ((ch >= '0' && ch <= '9') || + (ch >= 'A' && ch <= 'Z') || + (ch >= 'a' && ch <= 'z')); +} + +_CUPS_INLINE int /* O - 1 on match, 0 otherwise */ +_cups_isalpha(int ch) /* I - Character to test */ +{ + return ((ch >= 'A' && ch <= 'Z') || + (ch >= 'a' && ch <= 'z')); +} + +_CUPS_INLINE int /* O - 1 on match, 0 otherwise */ +_cups_isspace(int ch) /* I - Character to test */ +{ + return (ch == ' ' || ch == '\f' || ch == '\n' || ch == '\r' || ch == '\t' || + ch == '\v'); +} + +_CUPS_INLINE int /* O - 1 on match, 0 otherwise */ +_cups_isupper(int ch) /* I - Character to test */ +{ + return (ch >= 'A' && ch <= 'Z'); +} + +_CUPS_INLINE int /* O - Converted character */ +_cups_tolower(int ch) /* I - Character to convert */ +{ + return (_cups_isupper(ch) ? ch - 'A' + 'a' : ch); +} +# else +extern int _cups_isalnum(int ch); +extern int _cups_isalpha(int ch); +extern int _cups_isspace(int ch); +extern int _cups_isupper(int ch); +extern int _cups_tolower(int ch); +# endif /* _CUPS_INLINE */ + + +/* + * Prototypes... + */ + +extern void _cups_strcpy(char *dst, const char *src); + +# ifndef HAVE_STRDUP +extern char *_cups_strdup(const char *); +# define strdup _cups_strdup +# endif /* !HAVE_STRDUP */ + +extern int _cups_strcasecmp(const char *, const char *); + +extern int _cups_strncasecmp(const char *, const char *, size_t n); + +# ifndef HAVE_STRLCAT +extern size_t _cups_strlcat(char *, const char *, size_t); +# define strlcat _cups_strlcat +# endif /* !HAVE_STRLCAT */ + +# ifndef HAVE_STRLCPY +extern size_t _cups_strlcpy(char *, const char *, size_t); +# define strlcpy _cups_strlcpy +# endif /* !HAVE_STRLCPY */ + +# ifndef HAVE_SNPRINTF +extern int _cups_snprintf(char *, size_t, const char *, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); +# define snprintf _cups_snprintf +# endif /* !HAVE_SNPRINTF */ + +# ifndef HAVE_VSNPRINTF +extern int _cups_vsnprintf(char *, size_t, const char *, va_list); +# define vsnprintf _cups_vsnprintf +# endif /* !HAVE_VSNPRINTF */ + +/* + * String pool functions... + */ + +extern char *_cupsStrAlloc(const char *s); +extern void _cupsStrFlush(void); +extern void _cupsStrFree(const char *s); +extern char *_cupsStrRetain(const char *s); +extern size_t _cupsStrStatistics(size_t *alloc_bytes, size_t *total_bytes); + + +/* + * Floating point number functions... + */ + +extern char *_cupsStrFormatd(char *buf, char *bufend, double number, + struct lconv *loc); +extern double _cupsStrScand(const char *buf, char **bufptr, + struct lconv *loc); + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_STRING_H_ */ + +/* + * End of "$Id$". + */ diff --git a/cups/string.c b/cups/string.c new file mode 100644 index 0000000..537af8d --- /dev/null +++ b/cups/string.c @@ -0,0 +1,766 @@ +/* + * "$Id: string.c 7460 2008-04-16 02:19:54Z mike $" + * + * String functions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * _cupsStrAlloc() - Allocate/reference a string. + * _cupsStrFlush() - Flush the string pool. + * _cupsStrFormatd() - Format a floating-point number. + * _cupsStrFree() - Free/dereference a string. + * _cupsStrRetain() - Increment the reference count of a string. + * _cupsStrScand() - Scan a string for a floating-point number. + * _cupsStrStatistics() - Return allocation statistics for string pool. + * _cups_strcpy() - Copy a string allowing for overlapping strings. + * _cups_strdup() - Duplicate a string. + * _cups_strcasecmp() - Do a case-insensitive comparison. + * _cups_strncasecmp() - Do a case-insensitive comparison on up to N chars. + * _cups_strlcat() - Safely concatenate two strings. + * _cups_strlcpy() - Safely copy two strings. + * compare_sp_items() - Compare two string pool items... + */ + +/* + * Include necessary headers... + */ + +#define _CUPS_STRING_C_ +#include "string-private.h" +#include "debug-private.h" +#include "thread-private.h" +#include "array.h" +#include +#include + + +/* + * Local globals... + */ + +static _cups_mutex_t sp_mutex = _CUPS_MUTEX_INITIALIZER; + /* Mutex to control access to pool */ +static cups_array_t *stringpool = NULL; + /* Global string pool */ + + +/* + * Local functions... + */ + +static int compare_sp_items(_cups_sp_item_t *a, _cups_sp_item_t *b); + + +/* + * '_cupsStrAlloc()' - Allocate/reference a string. + */ + +char * /* O - String pointer */ +_cupsStrAlloc(const char *s) /* I - String */ +{ + _cups_sp_item_t *item, /* String pool item */ + *key; /* Search key */ + + + /* + * Range check input... + */ + + if (!s) + return (NULL); + + /* + * Get the string pool... + */ + + _cupsMutexLock(&sp_mutex); + + if (!stringpool) + stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL); + + if (!stringpool) + { + _cupsMutexUnlock(&sp_mutex); + + return (NULL); + } + + /* + * See if the string is already in the pool... + */ + + key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); + + if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL) + { + /* + * Found it, return the cached string... + */ + + item->ref_count ++; + +#ifdef DEBUG_GUARDS + DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, " + "ref_count=%d", item, item->str, s, item->guard, + item->ref_count)); + + if (item->guard != _CUPS_STR_GUARD) + abort(); +#endif /* DEBUG_GUARDS */ + + _cupsMutexUnlock(&sp_mutex); + + return (item->str); + } + + /* + * Not found, so allocate a new one... + */ + + item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t) + strlen(s)); + if (!item) + { + _cupsMutexUnlock(&sp_mutex); + + return (NULL); + } + + item->ref_count = 1; + strcpy(item->str, s); + +#ifdef DEBUG_GUARDS + item->guard = _CUPS_STR_GUARD; + + DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, " + "ref_count=%d", item, item->str, s, item->guard, + item->ref_count)); +#endif /* DEBUG_GUARDS */ + + /* + * Add the string to the pool and return it... + */ + + cupsArrayAdd(stringpool, item); + + _cupsMutexUnlock(&sp_mutex); + + return (item->str); +} + + +/* + * '_cupsStrFlush()' - Flush the string pool. + */ + +void +_cupsStrFlush(void) +{ + _cups_sp_item_t *item; /* Current item */ + + + DEBUG_printf(("4_cupsStrFlush: %d strings in array", + cupsArrayCount(stringpool))); + + _cupsMutexLock(&sp_mutex); + + for (item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); + item; + item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) + free(item); + + cupsArrayDelete(stringpool); + stringpool = NULL; + + _cupsMutexUnlock(&sp_mutex); +} + + +/* + * '_cupsStrFormatd()' - Format a floating-point number. + */ + +char * /* O - Pointer to end of string */ +_cupsStrFormatd(char *buf, /* I - String */ + char *bufend, /* I - End of string buffer */ + double number, /* I - Number to format */ + struct lconv *loc) /* I - Locale data */ +{ + char *bufptr, /* Pointer into buffer */ + temp[1024], /* Temporary string */ + *tempdec, /* Pointer to decimal point */ + *tempptr; /* Pointer into temporary string */ + const char *dec; /* Decimal point */ + int declen; /* Length of decimal point */ + + + /* + * Format the number using the "%.12f" format and then eliminate + * unnecessary trailing 0's. + */ + + snprintf(temp, sizeof(temp), "%.12f", number); + for (tempptr = temp + strlen(temp) - 1; + tempptr > temp && *tempptr == '0'; + *tempptr-- = '\0'); + + /* + * Next, find the decimal point... + */ + +/* 07/22/2016 Mopria-notice: this causes "struct lconv has no member named 'decimal_point'" +compilation error in Android so commenting out + if (loc && loc->decimal_point) + { + dec = loc->decimal_point; + declen = (int)strlen(dec); + } + else +*/ + { + dec = "."; + declen = 1; + } + + if (declen == 1) + tempdec = strchr(temp, *dec); + else + tempdec = strstr(temp, dec); + + /* + * Copy everything up to the decimal point... + */ + + if (tempdec) + { + for (tempptr = temp, bufptr = buf; + tempptr < tempdec && bufptr < bufend; + *bufptr++ = *tempptr++); + + tempptr += declen; + + if (*tempptr && bufptr < bufend) + { + *bufptr++ = '.'; + + while (*tempptr && bufptr < bufend) + *bufptr++ = *tempptr++; + } + + *bufptr = '\0'; + } + else + { + strlcpy(buf, temp, bufend - buf + 1); + bufptr = buf + strlen(buf); + } + + return (bufptr); +} + + +/* + * '_cupsStrFree()' - Free/dereference a string. + */ + +void +_cupsStrFree(const char *s) /* I - String to free */ +{ + _cups_sp_item_t *item, /* String pool item */ + *key; /* Search key */ + + + /* + * Range check input... + */ + + if (!s) + return; + + /* + * Check the string pool... + * + * We don't need to lock the mutex yet, as we only want to know if + * the stringpool is initialized. The rest of the code will still + * work if it is initialized before we lock... + */ + + if (!stringpool) + return; + + /* + * See if the string is already in the pool... + */ + + _cupsMutexLock(&sp_mutex); + + key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); + +#ifdef DEBUG_GUARDS + if (key->guard != _CUPS_STR_GUARD) + { + DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, " + "ref_count=%d", key, key->str, key->guard, key->ref_count)); + abort(); + } +#endif /* DEBUG_GUARDS */ + + if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL && + item == key) + { + /* + * Found it, dereference... + */ + + item->ref_count --; + + if (!item->ref_count) + { + /* + * Remove and free... + */ + + cupsArrayRemove(stringpool, item); + + free(item); + } + } + + _cupsMutexUnlock(&sp_mutex); +} + + +/* + * '_cupsStrRetain()' - Increment the reference count of a string. + * + * Note: This function does not verify that the passed pointer is in the + * string pool, so any calls to it MUST know they are passing in a + * good pointer. + */ + +char * /* O - Pointer to string */ +_cupsStrRetain(const char *s) /* I - String to retain */ +{ + _cups_sp_item_t *item; /* Pointer to string pool item */ + + + if (s) + { + item = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); + +#ifdef DEBUG_GUARDS + if (item->guard != _CUPS_STR_GUARD) + { + DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, " + "ref_count=%d", item, s, item->guard, item->ref_count)); + abort(); + } +#endif /* DEBUG_GUARDS */ + + _cupsMutexLock(&sp_mutex); + + item->ref_count ++; + + _cupsMutexUnlock(&sp_mutex); + } + + return ((char *)s); +} + + +/* + * '_cupsStrScand()' - Scan a string for a floating-point number. + * + * This function handles the locale-specific BS so that a decimal + * point is always the period (".")... + */ + +double /* O - Number */ +_cupsStrScand(const char *buf, /* I - Pointer to number */ + char **bufptr, /* O - New pointer or NULL on error */ + struct lconv *loc) /* I - Locale data */ +{ + char temp[1024], /* Temporary buffer */ + *tempptr; /* Pointer into temporary buffer */ + + + /* + * Range check input... + */ + + if (!buf) + return (0.0); + + /* + * Skip leading whitespace... + */ + + while (_cups_isspace(*buf)) + buf ++; + + /* + * Copy leading sign, numbers, period, and then numbers... + */ + + tempptr = temp; + if (*buf == '-' || *buf == '+') + *tempptr++ = *buf++; + + while (isdigit(*buf & 255)) + if (tempptr < (temp + sizeof(temp) - 1)) + *tempptr++ = *buf++; + else + { + if (bufptr) + *bufptr = NULL; + + return (0.0); + } + + if (*buf == '.') + { + /* + * Read fractional portion of number... + */ + + buf ++; + +/* 07/22/2016 Mopria-notice: this causes "struct lconv has no member named 'decimal_point'" +compilation error in Android so commenting out + if (loc && loc->decimal_point) + { + strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (tempptr - temp)); + tempptr += strlen(tempptr); + } + else +*/ + if (tempptr < (temp + sizeof(temp) - 1)) + *tempptr++ = '.'; + else + { + if (bufptr) + *bufptr = NULL; + + return (0.0); + } + + while (isdigit(*buf & 255)) + if (tempptr < (temp + sizeof(temp) - 1)) + *tempptr++ = *buf++; + else + { + if (bufptr) + *bufptr = NULL; + + return (0.0); + } + } + + if (*buf == 'e' || *buf == 'E') + { + /* + * Read exponent... + */ + + if (tempptr < (temp + sizeof(temp) - 1)) + *tempptr++ = *buf++; + else + { + if (bufptr) + *bufptr = NULL; + + return (0.0); + } + + if (*buf == '+' || *buf == '-') + { + if (tempptr < (temp + sizeof(temp) - 1)) + *tempptr++ = *buf++; + else + { + if (bufptr) + *bufptr = NULL; + + return (0.0); + } + } + + while (isdigit(*buf & 255)) + if (tempptr < (temp + sizeof(temp) - 1)) + *tempptr++ = *buf++; + else + { + if (bufptr) + *bufptr = NULL; + + return (0.0); + } + } + + /* + * Nul-terminate the temporary string and return the value... + */ + + if (bufptr) + *bufptr = (char *)buf; + + *tempptr = '\0'; + + return (strtod(temp, NULL)); +} + + +/* + * '_cupsStrStatistics()' - Return allocation statistics for string pool. + */ + +size_t /* O - Number of strings */ +_cupsStrStatistics(size_t *alloc_bytes, /* O - Allocated bytes */ + size_t *total_bytes) /* O - Total string bytes */ +{ + size_t count, /* Number of strings */ + abytes, /* Allocated string bytes */ + tbytes, /* Total string bytes */ + len; /* Length of string */ + _cups_sp_item_t *item; /* Current item */ + + + /* + * Loop through strings in pool, counting everything up... + */ + + _cupsMutexLock(&sp_mutex); + + for (count = 0, abytes = 0, tbytes = 0, + item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); + item; + item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) + { + /* + * Count allocated memory, using a 64-bit aligned buffer as a basis. + */ + + count += item->ref_count; + len = (strlen(item->str) + 8) & ~7; + abytes += sizeof(_cups_sp_item_t) + len; + tbytes += item->ref_count * len; + } + + _cupsMutexUnlock(&sp_mutex); + + /* + * Return values... + */ + + if (alloc_bytes) + *alloc_bytes = abytes; + + if (total_bytes) + *total_bytes = tbytes; + + return (count); +} + + +/* + * '_cups_strcpy()' - Copy a string allowing for overlapping strings. + */ + +void +_cups_strcpy(char *dst, /* I - Destination string */ + const char *src) /* I - Source string */ +{ + while (*src) + *dst++ = *src++; + + *dst = '\0'; +} + + +/* + * '_cups_strdup()' - Duplicate a string. + */ + +#ifndef HAVE_STRDUP +char * /* O - New string pointer */ +_cups_strdup(const char *s) /* I - String to duplicate */ +{ + char *t; /* New string pointer */ + + + if (s == NULL) + return (NULL); + + if ((t = malloc(strlen(s) + 1)) == NULL) + return (NULL); + + return (strcpy(t, s)); +} +#endif /* !HAVE_STRDUP */ + + +/* + * '_cups_strcasecmp()' - Do a case-insensitive comparison. + */ + +int /* O - Result of comparison (-1, 0, or 1) */ +_cups_strcasecmp(const char *s, /* I - First string */ + const char *t) /* I - Second string */ +{ + while (*s != '\0' && *t != '\0') + { + if (_cups_tolower(*s) < _cups_tolower(*t)) + return (-1); + else if (_cups_tolower(*s) > _cups_tolower(*t)) + return (1); + + s ++; + t ++; + } + + if (*s == '\0' && *t == '\0') + return (0); + else if (*s != '\0') + return (1); + else + return (-1); +} + +/* + * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars. + */ + +int /* O - Result of comparison (-1, 0, or 1) */ +_cups_strncasecmp(const char *s, /* I - First string */ + const char *t, /* I - Second string */ + size_t n) /* I - Maximum number of characters to compare */ +{ + while (*s != '\0' && *t != '\0' && n > 0) + { + if (_cups_tolower(*s) < _cups_tolower(*t)) + return (-1); + else if (_cups_tolower(*s) > _cups_tolower(*t)) + return (1); + + s ++; + t ++; + n --; + } + + if (n == 0) + return (0); + else if (*s == '\0' && *t == '\0') + return (0); + else if (*s != '\0') + return (1); + else + return (-1); +} + + +#ifndef HAVE_STRLCAT +/* + * '_cups_strlcat()' - Safely concatenate two strings. + */ + +size_t /* O - Length of string */ +_cups_strlcat(char *dst, /* O - Destination string */ + const char *src, /* I - Source string */ + size_t size) /* I - Size of destination string buffer */ +{ + size_t srclen; /* Length of source string */ + size_t dstlen; /* Length of destination string */ + + + /* + * Figure out how much room is left... + */ + + dstlen = strlen(dst); + size -= dstlen + 1; + + if (!size) + return (dstlen); /* No room, return immediately... */ + + /* + * Figure out how much room is needed... + */ + + srclen = strlen(src); + + /* + * Copy the appropriate amount... + */ + + if (srclen > size) + srclen = size; + + memcpy(dst + dstlen, src, srclen); + dst[dstlen + srclen] = '\0'; + + return (dstlen + srclen); +} +#endif /* !HAVE_STRLCAT */ + + +#ifndef HAVE_STRLCPY +/* + * '_cups_strlcpy()' - Safely copy two strings. + */ + +size_t /* O - Length of string */ +_cups_strlcpy(char *dst, /* O - Destination string */ + const char *src, /* I - Source string */ + size_t size) /* I - Size of destination string buffer */ +{ + size_t srclen; /* Length of source string */ + + + /* + * Figure out how much room is needed... + */ + + size --; + + srclen = strlen(src); + + /* + * Copy the appropriate amount... + */ + + if (srclen > size) + srclen = size; + + memcpy(dst, src, srclen); + dst[srclen] = '\0'; + + return (srclen); +} +#endif /* !HAVE_STRLCPY */ + + +/* + * 'compare_sp_items()' - Compare two string pool items... + */ + +static int /* O - Result of comparison */ +compare_sp_items(_cups_sp_item_t *a, /* I - First item */ + _cups_sp_item_t *b) /* I - Second item */ +{ + return (strcmp(a->str, b->str)); +} + + +/* + * End of "$Id: string.c 7460 2008-04-16 02:19:54Z mike $". + */ diff --git a/cups/tempfile.c b/cups/tempfile.c new file mode 100644 index 0000000..ac59d9f --- /dev/null +++ b/cups/tempfile.c @@ -0,0 +1,233 @@ +/* + * "$Id: tempfile.c 7337 2008-02-22 04:44:04Z mike $" + * + * Temp file utilities for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * cupsTempFd() - Creates a temporary file. + * cupsTempFile() - Generates a temporary filename. + * cupsTempFile2() - Creates a temporary CUPS file. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#include +#if defined(WIN32) || defined(__EMX__) +# include +#else +# include +#endif /* WIN32 || __EMX__ */ + + +/* + * 'cupsTempFd()' - Creates a temporary file. + * + * The temporary filename is returned in the filename buffer. + * The temporary file is opened for reading and writing. + */ + +int /* O - New file descriptor or -1 on error */ +cupsTempFd(char *filename, /* I - Pointer to buffer */ + int len) /* I - Size of buffer */ +{ + int fd; /* File descriptor for temp file */ + int tries; /* Number of tries */ + const char *tmpdir; /* TMPDIR environment var */ +#ifdef WIN32 + char tmppath[1024]; /* Windows temporary directory */ + DWORD curtime; /* Current time */ +#else + struct timeval curtime; /* Current time */ +#endif /* WIN32 */ + + + /* + * See if TMPDIR is defined... + */ + +#ifdef WIN32 + if ((tmpdir = getenv("TEMP")) == NULL) + { + GetTempPath(sizeof(tmppath), tmppath); + tmpdir = tmppath; + } +#else + /* + * Previously we put root temporary files in the default CUPS temporary + * directory under /var/spool/cups. However, since the scheduler cleans + * out temporary files there and runs independently of the user apps, we + * don't want to use it unless specifically told to by cupsd. + */ + + if ((tmpdir = getenv("TMPDIR")) == NULL) +# ifdef __APPLE__ + tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */ +# else + tmpdir = "/tmp"; +# endif /* __APPLE__ */ +#endif /* WIN32 */ + + /* + * Make the temporary name using the specified directory... + */ + + tries = 0; + + do + { +#ifdef WIN32 + /* + * Get the current time of day... + */ + + curtime = GetTickCount() + tries; + + /* + * Format a string using the hex time values... + */ + + snprintf(filename, len - 1, "%s/%05lx%08lx", tmpdir, + GetCurrentProcessId(), curtime); +#else + /* + * Get the current time of day... + */ + + gettimeofday(&curtime, NULL); + + /* + * Format a string using the hex time values... + */ + + snprintf(filename, len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(), + (unsigned)(curtime.tv_sec + curtime.tv_usec + tries)); +#endif /* WIN32 */ + + /* + * Open the file in "exclusive" mode, making sure that we don't + * stomp on an existing file or someone's symlink crack... + */ + +#ifdef WIN32 + fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY, + _S_IREAD | _S_IWRITE); +#elif defined(O_NOFOLLOW) + fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600); +#else + fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600); +#endif /* WIN32 */ + + if (fd < 0 && errno != EEXIST) + break; + + tries ++; + } + while (fd < 0 && tries < 1000); + + /* + * Return the file descriptor... + */ + + return (fd); +} + + +/* + * 'cupsTempFile()' - Generates a temporary filename. + * + * The temporary filename is returned in the filename buffer. + * This function is deprecated - use @link cupsTempFd@ or + * @link cupsTempFile2@ instead. + * + * @deprecated@ + */ + +char * /* O - Filename or @code NULL@ on error */ +cupsTempFile(char *filename, /* I - Pointer to buffer */ + int len) /* I - Size of buffer */ +{ + int fd; /* File descriptor for temp file */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * See if a filename was specified... + */ + + if (filename == NULL) + { + filename = cg->tempfile; + len = sizeof(cg->tempfile); + } + + /* + * Create the temporary file... + */ + + if ((fd = cupsTempFd(filename, len)) < 0) + return (NULL); + + /* + * Close the temp file - it'll be reopened later as needed... + */ + + close(fd); + + /* + * Return the temp filename... + */ + + return (filename); +} + + +/* + * 'cupsTempFile2()' - Creates a temporary CUPS file. + * + * The temporary filename is returned in the filename buffer. + * The temporary file is opened for writing. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +cups_file_t * /* O - CUPS file or @code NULL@ on error */ +cupsTempFile2(char *filename, /* I - Pointer to buffer */ + int len) /* I - Size of buffer */ +{ + cups_file_t *file; /* CUPS file */ + int fd; /* File descriptor */ + + + if ((fd = cupsTempFd(filename, len)) < 0) + return (NULL); + else if ((file = cupsFileOpenFd(fd, "w")) == NULL) + { + close(fd); + unlink(filename); + return (NULL); + } + else + return (file); +} + + +/* + * End of "$Id: tempfile.c 7337 2008-02-22 04:44:04Z mike $". + */ diff --git a/cups/test.ppd b/cups/test.ppd new file mode 100644 index 0000000..fc453a6 --- /dev/null +++ b/cups/test.ppd @@ -0,0 +1,262 @@ +*PPD-Adobe: "4.3" +*% +*% "$Id: test.ppd 7819 2008-08-01 00:27:24Z mike $" +*% +*% Test PPD file for CUPS. +*% +*% This file is used to test the CUPS PPD API functions and cannot be +*% used with any known printers. Look on the CUPS web site for working PPD +*% files. +*% +*% If you are a PPD file developer, consider using the PPD compiler (ppdc) +*% to create your PPD files - not only will it save you time, it produces +*% consistently high-quality files. +*% +*% Copyright 2007-2010 by Apple Inc. +*% Copyright 2002-2006 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/". +*FormatVersion: "4.3" +*FileVersion: "1.3" +*LanguageVersion: English +*LanguageEncoding: ISOLatin1 +*PCFileName: "TEST.PPD" +*Manufacturer: "ESP" +*Product: "(Test)" +*cupsVersion: 1.4 +*ModelName: "Test" +*ShortNickName: "Test" +*NickName: "Test for CUPS" +*PSVersion: "(3010.000) 0" +*LanguageLevel: "3" +*ColorDevice: True +*DefaultColorSpace: RGB +*FileSystem: False +*Throughput: "1" +*LandscapeOrientation: Plus90 +*TTRasterizer: Type42 +*cupsFilter: "application/vnd.cups-raster 0 -" +*RequiresPageRegion All: True + +*% These constraints are used to test ppdConflicts() and cupsResolveConflicts() +*UIConstraints: *PageSize Letter *InputSlot Envelope +*UIConstraints: *InputSlot Envelope *PageSize Letter +*UIConstraints: *PageRegion Letter *InputSlot Envelope +*UIConstraints: *InputSlot Envelope *PageRegion Letter + +*% These constraints are used to test ppdInstallableConflict() +*UIConstraints: "*Duplex *InstalledDuplexer False" +*UIConstraints: "*InstalledDuplexer False *Duplex" + +*% These attributes test ppdFindAttr/ppdFindNext... +*cupsTest Foo/I Love Foo: "" +*cupsTest Bar/I Love Bar: "" + +*% For PageSize, we have put all of the translations in-line... +*OpenUI *PageSize/Page Size: PickOne +*fr.Translation PageSize/French Page Size: "" +*fr_CA.Translation PageSize/French Canadian Page Size: "" +*OrderDependency: 10 AnySetup *PageSize +*DefaultPageSize: Letter +*PageSize Letter/US Letter: "PageSize=Letter" +*fr.PageSize Letter/French US Letter: "" +*fr_CA.PageSize Letter/French Canadian US Letter: "" +*PageSize Letter.Banner/US Letter Banner: "PageSize=Letter.Banner" +*fr.PageSize Letter.Banner/French US Letter Banner: "" +*fr_CA.PageSize Letter.Banner/French Canadian US Letter Banner: "" +*PageSize Letter.Fullbleed/US Letter Borderless: "PageSize=Letter.Fullbleed" +*fr.PageSize Letter.Fullbleed/French US Letter Borderless: "" +*fr_CA.PageSize Letter.Fullbleed/French Canadian US Letter Borderless: "" +*PageSize A4/A4: "PageSize=A4" +*fr.PageSize A4/French A4: "" +*fr_CA.PageSize A4/French Canadian A4: "" +*PageSize Env10/#10 Envelope: "PageSize=Env10" +*fr.PageSize Env10/French #10 Envelope: "" +*fr_CA.PageSize Env10/French Canadian #10 Envelope: "" +*CloseUI: *PageSize + +*% For PageRegion, we have separated the translations... +*OpenUI *PageRegion/Page Region: PickOne +*OrderDependency: 10 AnySetup *PageRegion +*DefaultPageRegion: Letter +*PageRegion Letter/US Letter: "PageRegion=Letter" +*PageRegion Letter.Banner/US Letter Banner: "PageRegion=Letter.Fullbleed" +*PageRegion Letter.Fullbleed/US Letter Borderless: "PageRegion=Letter.Fullbleed" +*PageRegion A4/A4: "PageRegion=A4" +*PageRegion Env10/#10 Envelope: "PageRegion=Env10" +*CloseUI: *PageRegion + +*fr.Translation PageRegion/French Page Region: "" +*fr.PageRegion Letter/French US Letter: "" +*fr.PageRegion Letter.Banner/French US Letter Banner: "" +*fr.PageRegion Letter.Fullbleed/French US Letter Borderless: "" +*fr.PageRegion A4/French A4: "" +*fr.PageRegion Env10/French #10 Envelope: "" + +*fr_CA.Translation PageRegion/French Canadian Page Region: "" +*fr_CA.PageRegion Letter/French Canadian US Letter: "" +*fr_CA.PageRegion Letter.Banner/French Canadian US Letter Banner: "" +*fr_CA.PageRegion Letter.Fullbleed/French Canadian US Letter Borderless: "" +*fr_CA.PageRegion A4/French Canadian A4: "" +*fr_CA.PageRegion Env10/French Canadian #10 Envelope: "" + +*DefaultImageableArea: Letter +*ImageableArea Letter: "18 36 594 756" +*ImageableArea Letter.Banner: "18 0 594 792" +*ImageableArea Letter.Fullbleed: "0 0 612 792" +*ImageableArea A4: "18 36 577 806" +*ImageableArea Env10: "18 36 279 648" + +*DefaultPaperDimension: Letter +*PaperDimension Letter: "612 792" +*PaperDimension Letter.Banner: "612 792" +*PaperDimension Letter.Fullbleed: "612 792" +*PaperDimension A4: "595 842" +*PaperDimension Env10: "297 684" + +*% Custom page size support +*HWMargins: 0 0 0 0 +*NonUIOrderDependency: 100 AnySetup *CustomPageSize True +*CustomPageSize True/Custom Page Size: "PageSize=Custom" +*ParamCustomPageSize Width: 1 points 36 1080 +*ParamCustomPageSize Height: 2 points 36 86400 +*ParamCustomPageSize WidthOffset/Width Offset: 3 points 0 0 +*ParamCustomPageSize HeightOffset/Height Offset: 4 points 0 0 +*ParamCustomPageSize Orientation: 5 int 0 0 + +*OpenUI *InputSlot/Input Slot: PickOne +*OrderDependency: 20 AnySetup *InputSlot +*DefaultInputSlot: Tray +*InputSlot Tray/Tray: "InputSlot=Tray" +*InputSlot Manual/Manual Feed: "InputSlot=Manual" +*InputSlot Envelope/Envelope Feed: "InputSlot=Envelope" +*CloseUI: *InputSlot + +*OpenUI *MediaType/Media Type: PickOne +*OrderDependency: 25 AnySetup *MediaType +*DefaultMediaType: Plain +*MediaType Plain/Plain Paper: "MediaType=Plain" +*MediaType Matte/Matte Photo: "MediaType=Matte" +*MediaType Glossy/Glossy Photo: "MediaType=Glossy" +*MediaType Transparency/Transparency Film: "MediaType=Transparency" +*CloseUI: *MediaType + +*OpenUI *Duplex/2-Sided Printing: PickOne +*OrderDependency: 10 DocumentSetup *Duplex +*DefaultDuplex: None +*Duplex None/Off: "Duplex=None" +*Duplex DuplexNoTumble/Long Edge: "Duplex=DuplexNoTumble" +*Duplex DuplexTumble/Short Edge: "Duplex=DuplexTumble" +*CloseUI: *Duplex + +*% Installable option... +*OpenGroup: InstallableOptions/Installable Options +*OpenUI InstalledDuplexer/Duplexer Installed: Boolean +*DefaultInstalledDuplexer: False +*InstalledDuplexer False: "" +*InstalledDuplexer True: "" +*CloseUI: *InstalledDuplexer +*CloseGroup: InstallableOptions + +*% Custom options... +*OpenGroup: Extended/Extended Options + +*OpenUI IntOption/Integer: PickOne +*OrderDependency: 30 AnySetup *IntOption +*DefaultIntOption: None +*IntOption None: "" +*IntOption 1: "IntOption=1" +*IntOption 2: "IntOption=2" +*IntOption 3: "IntOption=3" +*CloseUI: *IntOption + +*CustomIntOption True/Custom Integer: "IntOption=Custom" +*ParamCustomIntOption Integer: 1 int -100 100 + +*OpenUI StringOption/String: PickOne +*OrderDependency: 40 AnySetup *StringOption +*DefaultStringOption: None +*StringOption None: "" +*StringOption foo: "StringOption=foo" +*StringOption bar: "StringOption=bar" +*CloseUI: *StringOption + +*CustomStringOption True/Custom String: "StringOption=Custom" +*ParamCustomStringOption String1: 2 string 1 10 +*ParamCustomStringOption String2: 1 string 1 10 + +*CloseGroup: Extended + +*% IPP reasons for ppdLocalizeIPPReason tests +*cupsIPPReason foo/Foo Reason: "http://foo/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/foo/bar.html" +*End +*fr.cupsIPPReason foo/La Foo Reason: "text:La%20Long%20 +text:Foo%20Reason +http://foo/fr/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/fr/foo/bar.html" +*End +*zh_TW.cupsIPPReason foo/Number 1 Foo Reason: "text:Number%201%20 +text:Foo%20Reason +http://foo/zh_TW/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/zh_TW/foo/bar.html" +*End +*zh.cupsIPPReason foo/Number 2 Foo Reason: "text:Number%202%20 +text:Foo%20Reason +http://foo/zh/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/zh/foo/bar.html" +*End + +*% Marker names for ppdLocalizeMarkerName tests +*cupsMarkerName cyan/Cyan Toner: "" +*fr.cupsMarkerName cyan/La Toner Cyan: "" +*zh_TW.cupsMarkerName cyan/Number 1 Cyan Toner: "" +*zh.cupsMarkerName cyan/Number 2 Cyan Toner: "" + +*DefaultFont: Courier +*Font AvantGarde-Book: Standard "(001.006S)" Standard ROM +*Font AvantGarde-BookOblique: Standard "(001.006S)" Standard ROM +*Font AvantGarde-Demi: Standard "(001.007S)" Standard ROM +*Font AvantGarde-DemiOblique: Standard "(001.007S)" Standard ROM +*Font Bookman-Demi: Standard "(001.004S)" Standard ROM +*Font Bookman-DemiItalic: Standard "(001.004S)" Standard ROM +*Font Bookman-Light: Standard "(001.004S)" Standard ROM +*Font Bookman-LightItalic: Standard "(001.004S)" Standard ROM +*Font Courier: Standard "(002.004S)" Standard ROM +*Font Courier-Bold: Standard "(002.004S)" Standard ROM +*Font Courier-BoldOblique: Standard "(002.004S)" Standard ROM +*Font Courier-Oblique: Standard "(002.004S)" Standard ROM +*Font Helvetica: Standard "(001.006S)" Standard ROM +*Font Helvetica-Bold: Standard "(001.007S)" Standard ROM +*Font Helvetica-BoldOblique: Standard "(001.007S)" Standard ROM +*Font Helvetica-Narrow: Standard "(001.006S)" Standard ROM +*Font Helvetica-Narrow-Bold: Standard "(001.007S)" Standard ROM +*Font Helvetica-Narrow-BoldOblique: Standard "(001.007S)" Standard ROM +*Font Helvetica-Narrow-Oblique: Standard "(001.006S)" Standard ROM +*Font Helvetica-Oblique: Standard "(001.006S)" Standard ROM +*Font NewCenturySchlbk-Bold: Standard "(001.009S)" Standard ROM +*Font NewCenturySchlbk-BoldItalic: Standard "(001.007S)" Standard ROM +*Font NewCenturySchlbk-Italic: Standard "(001.006S)" Standard ROM +*Font NewCenturySchlbk-Roman: Standard "(001.007S)" Standard ROM +*Font Palatino-Bold: Standard "(001.005S)" Standard ROM +*Font Palatino-BoldItalic: Standard "(001.005S)" Standard ROM +*Font Palatino-Italic: Standard "(001.005S)" Standard ROM +*Font Palatino-Roman: Standard "(001.005S)" Standard ROM +*Font Symbol: Special "(001.007S)" Special ROM +*Font Times-Bold: Standard "(001.007S)" Standard ROM +*Font Times-BoldItalic: Standard "(001.009S)" Standard ROM +*Font Times-Italic: Standard "(001.007S)" Standard ROM +*Font Times-Roman: Standard "(001.007S)" Standard ROM +*Font ZapfChancery-MediumItalic: Standard "(001.007S)" Standard ROM +*Font ZapfDingbats: Special "(001.004S)" Standard ROM +*% +*% End of "$Id: test.ppd 7819 2008-08-01 00:27:24Z mike $". +*% diff --git a/cups/test2.ppd b/cups/test2.ppd new file mode 100644 index 0000000..353afb6 --- /dev/null +++ b/cups/test2.ppd @@ -0,0 +1,252 @@ +*PPD-Adobe: "4.3" +*% +*% "$Id: test2.ppd 7791 2008-07-24 00:55:30Z mike $" +*% +*% Test PPD file #2 for CUPS. +*% +*% This file is used to test the CUPS PPD API functions and cannot be +*% used with any known printers. Look on the CUPS web site for working PPD +*% files. +*% +*% If you are a PPD file developer, consider using the PPD compiler (ppdc) +*% to create your PPD files - not only will it save you time, it produces +*% consistently high-quality files. +*% +*% Copyright 2007-2011 by Apple Inc. +*% Copyright 2002-2006 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/". +*FormatVersion: "4.3" +*FileVersion: "1.3" +*LanguageVersion: English +*LanguageEncoding: ISOLatin1 +*PCFileName: "TEST.PPD" +*Manufacturer: "ESP" +*Product: "(Test2)" +*cupsVersion: 1.4 +*ModelName: "Test2" +*ShortNickName: "Test2" +*NickName: "Test2 for CUPS" +*PSVersion: "(3010.000) 0" +*LanguageLevel: "3" +*ColorDevice: True +*DefaultColorSpace: RGB +*FileSystem: False +*Throughput: "1" +*LandscapeOrientation: Plus90 +*TTRasterizer: Type42 + +*% These constraints are used to test ppdConflicts() and cupsResolveConflicts() +*cupsUIConstraints envelope: "*PageSize Letter *InputSlot Envelope" +*cupsUIConstraints envelope: "*PageSize A4 *InputSlot Envelope" +*cupsUIResolver envelope: "*InputSlot Manual *PageSize Env10" + +*cupsUIConstraints envphoto: "*PageSize Env10 *InputSlot Envelope *Quality Photo" +*cupsUIResolver envphoto: "*Quality Normal" + +*% This constraint is used to test ppdInstallableConflict() +*cupsUIConstraints: "*Duplex *InstalledDuplexer False" + +*% These constraints are used to test the loop detection code in cupsResolveConflicts() +*cupsUIConstraints loop1: "*PageSize A4 *Quality Photo" +*cupsUIResolver loop1: "*Quality Normal" +*cupsUIConstraints loop2: "*PageSize A4 *Quality Normal" +*cupsUIResolver loop2: "*Quality Photo" + +*% For PageSize, we have put all of the translations in-line... +*OpenUI *PageSize/Page Size: PickOne +*fr.Translation PageSize/French Page Size: "" +*fr_CA.Translation PageSize/French Canadian Page Size: "" +*OrderDependency: 10 AnySetup *PageSize +*DefaultPageSize: Letter +*PageSize Letter/US Letter: "PageSize=Letter" +*fr.PageSize Letter/French US Letter: "" +*fr_CA.PageSize Letter/French Canadian US Letter: "" +*PageSize A4/A4: "PageSize=A4" +*fr.PageSize A4/French A4: "" +*fr_CA.PageSize A4/French Canadian A4: "" +*PageSize Env10/#10 Envelope: "PageSize=Env10" +*fr.PageSize Env10/French #10 Envelope: "" +*fr_CA.PageSize Env10/French Canadian #10 Envelope: "" +*CloseUI: *PageSize + +*% For PageRegion, we have separated the translations... +*OpenUI *PageRegion/Page Region: PickOne +*OrderDependency: 10 AnySetup *PageRegion +*DefaultPageRegion: Letter +*PageRegion Letter/US Letter: "PageRegion=Letter" +*PageRegion A4/A4: "PageRegion=A4" +*PageRegion Env10/#10 Envelope: "PageRegion=Env10" +*CloseUI: *PageRegion + +*fr.Translation PageRegion/French Page Region: "" +*fr.PageRegion Letter/French US Letter: "" +*fr.PageRegion A4/French A4: "" +*fr.PageRegion Env10/French #10 Envelope: "" + +*fr_CA.Translation PageRegion/French Canadian Page Region: "" +*fr_CA.PageRegion Letter/French Canadian US Letter: "" +*fr_CA.PageRegion A4/French Canadian A4: "" +*fr_CA.PageRegion Env10/French Canadian #10 Envelope: "" + +*DefaultImageableArea: Letter +*ImageableArea Letter: "18 36 594 756" +*ImageableArea A4: "18 36 577 806" +*ImageableArea Env10: "18 36 279 648" + +*DefaultPaperDimension: Letter +*PaperDimension Letter: "612 792" +*PaperDimension A4: "595 842" +*PaperDimension Env10: "297 684" + +*% Custom page size support +*HWMargins: 0 0 0 0 +*NonUIOrderDependency: 100 AnySetup *CustomPageSize True +*CustomPageSize True/Custom Page Size: "PageSize=Custom" +*ParamCustomPageSize Width: 1 points 36 1080 +*ParamCustomPageSize Height: 2 points 36 86400 +*ParamCustomPageSize WidthOffset/Width Offset: 3 points 0 0 +*ParamCustomPageSize HeightOffset/Height Offset: 4 points 0 0 +*ParamCustomPageSize Orientation: 5 int 0 0 + +*cupsMediaQualifier2: InputSlot +*cupsMediaQualifier3: Quality +*cupsMaxSize .Manual.: "1000 1000" +*cupsMinSize .Manual.: "100 100" +*cupsMinSize .Manual.Photo: "200 200" +*cupsMinSize ..Photo: "300 300" + +*OpenUI *InputSlot/Input Slot: PickOne +*OrderDependency: 20 AnySetup *InputSlot +*DefaultInputSlot: Tray +*InputSlot Tray/Tray: "InputSlot=Tray" +*InputSlot Manual/Manual Feed: "InputSlot=Manual" +*InputSlot Envelope/Envelope Feed: "InputSlot=Envelope" +*CloseUI: *InputSlot + +*OpenUI *Quality/Output Mode: PickOne +*OrderDependency: 20 AnySetup *Quality +*DefaultQuality: Normal +*Quality Draft: "Quality=Draft" +*Quality Normal: "Quality=Normal" +*Quality Photo: "Quality=Photo" +*CloseUI: *Quality + +*OpenUI *Duplex/2-Sided Printing: PickOne +*OrderDependency: 10 DocumentSetup *Duplex +*DefaultDuplex: None +*Duplex None/Off: "Duplex=None" +*Duplex DuplexNoTumble/Long Edge: "Duplex=DuplexNoTumble" +*Duplex DuplexTumble/Short Edge: "Duplex=DuplexTumble" +*CloseUI: *Duplex + +*% Installable option... +*OpenGroup: InstallableOptions/Installable Options +*OpenUI InstalledDuplexer/Duplexer Installed: Boolean +*DefaultInstalledDuplexer: False +*InstalledDuplexer False: "" +*InstalledDuplexer True: "" +*CloseUI: *InstalledDuplexer +*CloseGroup: InstallableOptions + +*% Custom options... +*OpenGroup: Extended/Extended Options + +*OpenUI IntOption/Integer: PickOne +*OrderDependency: 30 AnySetup *IntOption +*DefaultIntOption: None +*IntOption None: "" +*IntOption 1: "IntOption=1" +*IntOption 2: "IntOption=2" +*IntOption 3: "IntOption=3" +*CloseUI: *IntOption + +*CustomIntOption True/Custom Integer: "IntOption=Custom" +*ParamCustomIntOption Integer: 1 int -100 100 + +*OpenUI StringOption/String: PickOne +*OrderDependency: 40 AnySetup *StringOption +*DefaultStringOption: None +*StringOption None: "" +*StringOption foo: "StringOption=foo" +*StringOption bar: "StringOption=bar" +*CloseUI: *StringOption + +*CustomStringOption True/Custom String: "StringOption=Custom" +*ParamCustomStringOption String: 1 string 1 10 + +*CloseGroup: Extended + +*% IPP reasons for ppdLocalizeIPPReason tests +*cupsIPPReason foo/Foo Reason: "http://foo/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/foo/bar.html" +*End +*fr.cupsIPPReason foo/La Foo Reason: "text:La%20Long%20 +text:Foo%20Reason +http://foo/fr/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/fr/foo/bar.html" +*End +*zh_TW.cupsIPPReason foo/Number 1 Foo Reason: "text:Number%201%20 +text:Foo%20Reason +http://foo/zh_TW/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/zh_TW/foo/bar.html" +*End +*zh.cupsIPPReason foo/Number 2 Foo Reason: "text:Number%202%20 +text:Foo%20Reason +http://foo/zh/bar.html +help:anchor='foo'%20bookID=Vendor%20Help +/help/zh/foo/bar.html" +*End + +*% Marker names for ppdLocalizeMarkerName tests +*cupsMarkerName cyan/Cyan Toner: "" +*fr.cupsMarkerName cyan/La Toner Cyan: "" +*zh_TW.cupsMarkerName cyan/Number 1 Cyan Toner: "" +*zh.cupsMarkerName cyan/Number 2 Cyan Toner: "" + +*DefaultFont: Courier +*Font AvantGarde-Book: Standard "(001.006S)" Standard ROM +*Font AvantGarde-BookOblique: Standard "(001.006S)" Standard ROM +*Font AvantGarde-Demi: Standard "(001.007S)" Standard ROM +*Font AvantGarde-DemiOblique: Standard "(001.007S)" Standard ROM +*Font Bookman-Demi: Standard "(001.004S)" Standard ROM +*Font Bookman-DemiItalic: Standard "(001.004S)" Standard ROM +*Font Bookman-Light: Standard "(001.004S)" Standard ROM +*Font Bookman-LightItalic: Standard "(001.004S)" Standard ROM +*Font Courier: Standard "(002.004S)" Standard ROM +*Font Courier-Bold: Standard "(002.004S)" Standard ROM +*Font Courier-BoldOblique: Standard "(002.004S)" Standard ROM +*Font Courier-Oblique: Standard "(002.004S)" Standard ROM +*Font Helvetica: Standard "(001.006S)" Standard ROM +*Font Helvetica-Bold: Standard "(001.007S)" Standard ROM +*Font Helvetica-BoldOblique: Standard "(001.007S)" Standard ROM +*Font Helvetica-Narrow: Standard "(001.006S)" Standard ROM +*Font Helvetica-Narrow-Bold: Standard "(001.007S)" Standard ROM +*Font Helvetica-Narrow-BoldOblique: Standard "(001.007S)" Standard ROM +*Font Helvetica-Narrow-Oblique: Standard "(001.006S)" Standard ROM +*Font Helvetica-Oblique: Standard "(001.006S)" Standard ROM +*Font NewCenturySchlbk-Bold: Standard "(001.009S)" Standard ROM +*Font NewCenturySchlbk-BoldItalic: Standard "(001.007S)" Standard ROM +*Font NewCenturySchlbk-Italic: Standard "(001.006S)" Standard ROM +*Font NewCenturySchlbk-Roman: Standard "(001.007S)" Standard ROM +*Font Palatino-Bold: Standard "(001.005S)" Standard ROM +*Font Palatino-BoldItalic: Standard "(001.005S)" Standard ROM +*Font Palatino-Italic: Standard "(001.005S)" Standard ROM +*Font Palatino-Roman: Standard "(001.005S)" Standard ROM +*Font Symbol: Special "(001.007S)" Special ROM +*Font Times-Bold: Standard "(001.007S)" Standard ROM +*Font Times-BoldItalic: Standard "(001.009S)" Standard ROM +*Font Times-Italic: Standard "(001.007S)" Standard ROM +*Font Times-Roman: Standard "(001.007S)" Standard ROM +*Font ZapfChancery-MediumItalic: Standard "(001.007S)" Standard ROM +*Font ZapfDingbats: Special "(001.004S)" Standard ROM +*% +*% End of "$Id: test2.ppd 7791 2008-07-24 00:55:30Z mike $". +*% diff --git a/cups/testadmin.c b/cups/testadmin.c new file mode 100644 index 0000000..a16025c --- /dev/null +++ b/cups/testadmin.c @@ -0,0 +1,120 @@ +/* + * "$Id: testadmin.c 7720 2008-07-11 22:46:21Z mike $" + * + * Admin function test program for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 2006 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. + * + * Contents: + * + * main() - Main entry. + * show_settings() - Show settings in the array... + */ + +/* + * Include necessary headers... + */ + +#include "adminutil.h" +#include "string-private.h" + + +/* + * Local functions... + */ + +static void show_settings(int num_settings, cups_option_t *settings); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line args */ + char *argv[]) /* I - Command-line arguments */ +{ + int i, /* Looping var */ + num_settings; /* Number of settings */ + cups_option_t *settings; /* Settings */ + http_t *http; /* Connection to server */ + + + /* + * Connect to the server using the defaults... + */ + + http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); + + /* + * Set the current configuration if we have anything on the command-line... + */ + + if (argc > 1) + { + for (i = 1, num_settings = 0, settings = NULL; i < argc; i ++) + num_settings = cupsParseOptions(argv[i], num_settings, &settings); + + if (cupsAdminSetServerSettings(http, num_settings, settings)) + { + puts("New server settings:"); + cupsFreeOptions(num_settings, settings); + } + else + { + printf("Server settings not changed: %s\n", cupsLastErrorString()); + return (1); + } + } + else + puts("Current server settings:"); + + /* + * Get the current configuration... + */ + + if (cupsAdminGetServerSettings(http, &num_settings, &settings)) + { + show_settings(num_settings, settings); + cupsFreeOptions(num_settings, settings); + return (0); + } + else + { + printf(" %s\n", cupsLastErrorString()); + return (1); + } +} + + +/* + * 'show_settings()' - Show settings in the array... + */ + +static void +show_settings( + int num_settings, /* I - Number of settings */ + cups_option_t *settings) /* I - Settings */ +{ + while (num_settings > 0) + { + printf(" %s=%s\n", settings->name, settings->value); + + settings ++; + num_settings --; + } +} + + +/* + * End of "$Id: testadmin.c 7720 2008-07-11 22:46:21Z mike $". + */ diff --git a/cups/testarray.c b/cups/testarray.c new file mode 100644 index 0000000..2e254c2 --- /dev/null +++ b/cups/testarray.c @@ -0,0 +1,480 @@ +/* + * "$Id: testarray.c 6649 2007-07-11 21:46:42Z mike $" + * + * Array test program for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * main() - Main entry. + * get_seconds() - Get the current time in seconds... + * load_words() - Load words from a file. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "debug-private.h" +#include "array.h" +#include "dir.h" + + +/* + * Local functions... + */ + +static double get_seconds(void); +static int load_words(const char *filename, cups_array_t *array); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int i; /* Looping var */ + cups_array_t *array, /* Test array */ + *dup_array; /* Duplicate array */ + int status; /* Exit status */ + char *text; /* Text from array */ + char word[256]; /* Word from file */ + double start, /* Start time */ + end; /* End time */ + cups_dir_t *dir; /* Current directory */ + cups_dentry_t *dent; /* Directory entry */ + char *saved[32]; /* Saved entries */ + void *data; /* User data for arrays */ + + + /* + * No errors so far... + */ + + status = 0; + + /* + * cupsArrayNew() + */ + + fputs("cupsArrayNew: ", stdout); + + data = (void *)"testarray"; + array = cupsArrayNew((cups_array_func_t)strcmp, data); + + if (array) + puts("PASS"); + else + { + puts("FAIL (returned NULL, expected pointer)"); + status ++; + } + + /* + * cupsArrayUserData() + */ + + fputs("cupsArrayUserData: ", stdout); + if (cupsArrayUserData(array) == data) + puts("PASS"); + else + { + printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array), + data); + status ++; + } + + /* + * cupsArrayAdd() + */ + + fputs("cupsArrayAdd: ", stdout); + + if (!cupsArrayAdd(array, strdup("One Fish"))) + { + puts("FAIL (\"One Fish\")"); + status ++; + } + else + { + if (!cupsArrayAdd(array, strdup("Two Fish"))) + { + puts("FAIL (\"Two Fish\")"); + status ++; + } + else + { + if (!cupsArrayAdd(array, strdup("Red Fish"))) + { + puts("FAIL (\"Red Fish\")"); + status ++; + } + else + { + if (!cupsArrayAdd(array, strdup("Blue Fish"))) + { + puts("FAIL (\"Blue Fish\")"); + status ++; + } + else + puts("PASS"); + } + } + } + + /* + * cupsArrayCount() + */ + + fputs("cupsArrayCount: ", stdout); + if (cupsArrayCount(array) == 4) + puts("PASS"); + else + { + printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array)); + status ++; + } + + /* + * cupsArrayFirst() + */ + + fputs("cupsArrayFirst: ", stdout); + if ((text = (char *)cupsArrayFirst(array)) != NULL && + !strcmp(text, "Blue Fish")) + puts("PASS"); + else + { + printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text); + status ++; + } + + /* + * cupsArrayNext() + */ + + fputs("cupsArrayNext: ", stdout); + if ((text = (char *)cupsArrayNext(array)) != NULL && + !strcmp(text, "One Fish")) + puts("PASS"); + else + { + printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text); + status ++; + } + + /* + * cupsArrayLast() + */ + + fputs("cupsArrayLast: ", stdout); + if ((text = (char *)cupsArrayLast(array)) != NULL && + !strcmp(text, "Two Fish")) + puts("PASS"); + else + { + printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text); + status ++; + } + + /* + * cupsArrayPrev() + */ + + fputs("cupsArrayPrev: ", stdout); + if ((text = (char *)cupsArrayPrev(array)) != NULL && + !strcmp(text, "Red Fish")) + puts("PASS"); + else + { + printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text); + status ++; + } + + /* + * cupsArrayFind() + */ + + fputs("cupsArrayFind: ", stdout); + if ((text = (char *)cupsArrayFind(array, (void *)"One Fish")) != NULL && + !strcmp(text, "One Fish")) + puts("PASS"); + else + { + printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text); + status ++; + } + + /* + * cupsArrayCurrent() + */ + + fputs("cupsArrayCurrent: ", stdout); + if ((text = (char *)cupsArrayCurrent(array)) != NULL && + !strcmp(text, "One Fish")) + puts("PASS"); + else + { + printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text); + status ++; + } + + /* + * cupsArrayDup() + */ + + fputs("cupsArrayDup: ", stdout); + if ((dup_array = cupsArrayDup(array)) != NULL && + cupsArrayCount(dup_array) == 4) + puts("PASS"); + else + { + printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n", + dup_array, cupsArrayCount(dup_array)); + status ++; + } + + /* + * cupsArrayRemove() + */ + + fputs("cupsArrayRemove: ", stdout); + if (cupsArrayRemove(array, (void *)"One Fish") && + cupsArrayCount(array) == 3) + puts("PASS"); + else + { + printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n", + cupsArrayCount(array)); + status ++; + } + + /* + * cupsArrayClear() + */ + + fputs("cupsArrayClear: ", stdout); + cupsArrayClear(array); + if (cupsArrayCount(array) == 0) + puts("PASS"); + else + { + printf("FAIL (%d elements, expected 0 elements)\n", + cupsArrayCount(array)); + status ++; + } + + /* + * Now load this source file and grab all of the unique words... + */ + + fputs("Load unique words: ", stdout); + fflush(stdout); + + start = get_seconds(); + + if ((dir = cupsDirOpen(".")) == NULL) + { + puts("FAIL (cupsDirOpen failed)"); + status ++; + } + else + { + while ((dent = cupsDirRead(dir)) != NULL) + { + i = strlen(dent->filename) - 2; + + if (i > 0 && dent->filename[i] == '.' && + (dent->filename[i + 1] == 'c' || + dent->filename[i + 1] == 'h')) + load_words(dent->filename, array); + } + + cupsDirClose(dir); + + end = get_seconds(); + + printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array), + end - start, cupsArrayCount(array) / (end - start)); + fflush(stdout); + + for (text = (char *)cupsArrayFirst(array); text;) + { + /* + * Copy this word to the word buffer (safe because we strdup'd from + * the same buffer in the first place... :) + */ + + strcpy(word, text); + + /* + * Grab the next word and compare... + */ + + if ((text = (char *)cupsArrayNext(array)) == NULL) + break; + + if (strcmp(word, text) >= 0) + break; + } + + if (text) + { + printf("FAIL (\"%s\" >= \"%s\"!)\n", word, text); + status ++; + } + else + puts("PASS"); + } + + /* + * Test deleting with iteration... + */ + + fputs("Delete While Iterating: ", stdout); + + text = (char *)cupsArrayFirst(array); + cupsArrayRemove(array, text); + free(text); + + text = (char *)cupsArrayNext(array); + if (!text) + { + puts("FAIL (cupsArrayNext returned NULL!)"); + status ++; + } + else + puts("PASS"); + + /* + * Test save/restore... + */ + + fputs("cupsArraySave: ", stdout); + + for (i = 0, text = (char *)cupsArrayFirst(array); + i < 32; + i ++, text = (char *)cupsArrayNext(array)) + { + saved[i] = text; + + if (!cupsArraySave(array)) + break; + } + + if (i < 32) + printf("FAIL (depth = %d)\n", i); + else + puts("PASS"); + + fputs("cupsArrayRestore: ", stdout); + + while (i > 0) + { + i --; + + text = cupsArrayRestore(array); + if (text != saved[i]) + break; + } + + if (i) + printf("FAIL (depth = %d)\n", i); + else + puts("PASS"); + + /* + * Delete the arrays... + */ + + cupsArrayDelete(array); + cupsArrayDelete(dup_array); + + /* + * Summarize the results and return... + */ + + if (!status) + puts("\nALL TESTS PASSED!"); + else + printf("\n%d TEST(S) FAILED!\n", status); + + return (status); +} + + +/* + * 'get_seconds()' - Get the current time in seconds... + */ + +#ifdef WIN32 +# include + + +static double +get_seconds(void) +{ +} +#else +# include + + +static double +get_seconds(void) +{ + struct timeval curtime; /* Current time */ + + + gettimeofday(&curtime, NULL); + return (curtime.tv_sec + 0.000001 * curtime.tv_usec); +} +#endif /* WIN32 */ + + +/* + * 'load_words()' - Load words from a file. + */ + +static int /* O - 1 on success, 0 on failure */ +load_words(const char *filename, /* I - File to load */ + cups_array_t *array) /* I - Array to add to */ +{ + FILE *fp; /* Test file */ + char word[256]; /* Word from file */ + + + if ((fp = fopen(filename, "r")) == NULL) + { + perror(filename); + return (0); + } + + while (fscanf(fp, "%255s", word) == 1) + { + if (!cupsArrayFind(array, word)) + cupsArrayAdd(array, strdup(word)); + } + + fclose(fp); + + return (1); +} + + +/* + * End of "$Id: testarray.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/cups/testconflicts.c b/cups/testconflicts.c new file mode 100644 index 0000000..0ccca82 --- /dev/null +++ b/cups/testconflicts.c @@ -0,0 +1,138 @@ +/* + * "$Id: testconflicts.c 3755 2012-03-30 05:59:14Z msweet $" + * + * PPD constraint test program for CUPS. + * + * Copyright 2008-2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * main() - Main entry. + */ + +/* + * Include necessary headers... + */ + +#include "cups.h" +#include "ppd.h" +#include "string-private.h" + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int i; /* Looping var */ + ppd_file_t *ppd; /* PPD file loaded from disk */ + char line[256], /* Input buffer */ + *ptr, /* Pointer into buffer */ + *optr, /* Pointer to first option name */ + *cptr; /* Pointer to first choice */ + int num_options; /* Number of options */ + cups_option_t *options; /* Options */ + char *option, /* Current option */ + *choice; /* Current choice */ + + + if (argc != 2) + { + puts("Usage: testconflicts filename.ppd"); + return (1); + } + + if ((ppd = ppdOpenFile(argv[1])) == NULL) + { + ppd_status_t err; /* Last error in file */ + int linenum; /* Line number in file */ + + err = ppdLastError(&linenum); + + printf("Unable to open PPD file \"%s\": %s on line %d\n", argv[1], + ppdErrorString(err), linenum); + return (1); + } + + ppdMarkDefaults(ppd); + + option = NULL; + choice = NULL; + + for (;;) + { + num_options = 0; + options = NULL; + + if (!cupsResolveConflicts(ppd, option, choice, &num_options, &options)) + puts("Unable to resolve conflicts!"); + else if ((!option && num_options > 0) || (option && num_options > 1)) + { + fputs("Resolved conflicts with the following options:\n ", stdout); + for (i = 0; i < num_options; i ++) + if (!option || _cups_strcasecmp(option, options[i].name)) + printf(" %s=%s", options[i].name, options[i].value); + putchar('\n'); + + cupsFreeOptions(num_options, options); + } + + if (option) + { + free(option); + option = NULL; + } + + if (choice) + { + free(choice); + choice = NULL; + } + + printf("\nNew Option(s): "); + fflush(stdout); + if (!fgets(line, sizeof(line), stdin) || line[0] == '\n') + break; + + for (ptr = line; isspace(*ptr & 255); ptr ++); + for (optr = ptr; *ptr && *ptr != '='; ptr ++); + if (!*ptr) + break; + for (*ptr++ = '\0', cptr = ptr; *ptr && !isspace(*ptr & 255); ptr ++); + if (!*ptr) + break; + *ptr++ = '\0'; + + option = strdup(optr); + choice = strdup(cptr); + num_options = cupsParseOptions(ptr, 0, &options); + + ppdMarkOption(ppd, option, choice); + if (cupsMarkOptions(ppd, num_options, options)) + puts("Options Conflict!"); + cupsFreeOptions(num_options, options); + } + + if (option) + free(option); + if (choice) + free(choice); + + return (0); +} + + +/* + * End of "$Id: testconflicts.c 3755 2012-03-30 05:59:14Z msweet $". + */ diff --git a/cups/testcups.c b/cups/testcups.c new file mode 100644 index 0000000..18bb39c --- /dev/null +++ b/cups/testcups.c @@ -0,0 +1,570 @@ +/* + * "$Id$" + * + * CUPS API test program for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2007 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. + * + * Contents: + * + * main() - Main entry. + * dests_equal() - Determine whether two destinations are equal. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "cups.h" +#include "ppd.h" +#include + + +/* + * Local functions... + */ + +static int dests_equal(cups_dest_t *a, cups_dest_t *b); +static int enum_cb(void *user_data, unsigned flags, cups_dest_t *dest); +static void show_diffs(cups_dest_t *a, cups_dest_t *b); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int status = 0, /* Exit status */ + i, /* Looping var */ + num_dests; /* Number of destinations */ + cups_dest_t *dests, /* Destinations */ + *dest, /* Current destination */ + *named_dest; /* Current named destination */ + const char *ppdfile; /* PPD file */ + ppd_file_t *ppd; /* PPD file data */ + int num_jobs; /* Number of jobs for queue */ + cups_job_t *jobs; /* Jobs for queue */ + + + if (argc > 1) + { + if (!strcmp(argv[1], "enum")) + { + cups_ptype_t mask = CUPS_PRINTER_LOCAL, + /* Printer type mask */ + type = CUPS_PRINTER_LOCAL; + /* Printer type */ + int msec = 0; /* Timeout in milliseconds */ + + + for (i = 2; i < argc; i ++) + if (isdigit(argv[i][0] & 255) || argv[i][0] == '.') + msec = (int)(atof(argv[i]) * 1000); + else if (!_cups_strcasecmp(argv[i], "bw")) + { + mask |= CUPS_PRINTER_BW; + type |= CUPS_PRINTER_BW; + } + else if (!_cups_strcasecmp(argv[i], "color")) + { + mask |= CUPS_PRINTER_COLOR; + type |= CUPS_PRINTER_COLOR; + } + else if (!_cups_strcasecmp(argv[i], "mono")) + { + mask |= CUPS_PRINTER_COLOR; + } + else if (!_cups_strcasecmp(argv[i], "duplex")) + { + mask |= CUPS_PRINTER_DUPLEX; + type |= CUPS_PRINTER_DUPLEX; + } + else if (!_cups_strcasecmp(argv[i], "simplex")) + { + mask |= CUPS_PRINTER_DUPLEX; + } + else if (!_cups_strcasecmp(argv[i], "staple")) + { + mask |= CUPS_PRINTER_STAPLE; + type |= CUPS_PRINTER_STAPLE; + } + else if (!_cups_strcasecmp(argv[i], "copies")) + { + mask |= CUPS_PRINTER_COPIES; + type |= CUPS_PRINTER_COPIES; + } + else if (!_cups_strcasecmp(argv[i], "collate")) + { + mask |= CUPS_PRINTER_COLLATE; + type |= CUPS_PRINTER_COLLATE; + } + else if (!_cups_strcasecmp(argv[i], "punch")) + { + mask |= CUPS_PRINTER_PUNCH; + type |= CUPS_PRINTER_PUNCH; + } + else if (!_cups_strcasecmp(argv[i], "cover")) + { + mask |= CUPS_PRINTER_COVER; + type |= CUPS_PRINTER_COVER; + } + else if (!_cups_strcasecmp(argv[i], "bind")) + { + mask |= CUPS_PRINTER_BIND; + type |= CUPS_PRINTER_BIND; + } + else if (!_cups_strcasecmp(argv[i], "sort")) + { + mask |= CUPS_PRINTER_SORT; + type |= CUPS_PRINTER_SORT; + } + else if (!_cups_strcasecmp(argv[i], "mfp")) + { + mask |= CUPS_PRINTER_MFP; + type |= CUPS_PRINTER_MFP; + } + else if (!_cups_strcasecmp(argv[i], "printer")) + { + mask |= CUPS_PRINTER_MFP; + } + else if (!_cups_strcasecmp(argv[i], "large")) + { + mask |= CUPS_PRINTER_LARGE; + type |= CUPS_PRINTER_LARGE; + } + else if (!_cups_strcasecmp(argv[i], "medium")) + { + mask |= CUPS_PRINTER_MEDIUM; + type |= CUPS_PRINTER_MEDIUM; + } + else if (!_cups_strcasecmp(argv[i], "small")) + { + mask |= CUPS_PRINTER_SMALL; + type |= CUPS_PRINTER_SMALL; + } + else + fprintf(stderr, "Unknown argument \"%s\" ignored...\n", argv[i]); + + cupsEnumDests(CUPS_DEST_FLAGS_NONE, msec, NULL, type, mask, enum_cb, NULL); + } + else if (!strcmp(argv[1], "password")) + { + const char *pass = cupsGetPassword("Password:"); + /* Password string */ + + if (pass) + printf("Password entered: %s\n", pass); + else + puts("No password entered."); + } + else if (!strcmp(argv[1], "print") && argc == 5) + { + /* + * ./testcups printer file interval + */ + + int interval, /* Interval between writes */ + job_id; /* Job ID */ + cups_file_t *fp; /* Print file */ + char buffer[16384]; /* Read/write buffer */ + ssize_t bytes; /* Bytes read/written */ + + if ((fp = cupsFileOpen(argv[3], "r")) == NULL) + { + printf("Unable to open \"%s\": %s\n", argv[2], strerror(errno)); + return (1); + } + + if ((job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, argv[2], "testcups", 0, + NULL)) <= 0) + { + printf("Unable to create print job on %s: %s\n", argv[1], + cupsLastErrorString()); + return (1); + } + + interval = atoi(argv[4]); + + if (cupsStartDocument(CUPS_HTTP_DEFAULT, argv[1], job_id, argv[2], + CUPS_FORMAT_AUTO, 1) != HTTP_CONTINUE) + { + puts("Unable to start document!"); + return (1); + } + + while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) + { + printf("Writing %d bytes...\n", (int)bytes); + + if (cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, + bytes) != HTTP_CONTINUE) + { + puts("Unable to write bytes!"); + return (1); + } + + if (interval > 0) + sleep(interval); + } + + cupsFileClose(fp); + + if (cupsFinishDocument(CUPS_HTTP_DEFAULT, argv[1]) > IPP_OK_SUBST) + { + puts("Unable to finish document!"); + return (1); + } + } + else + { + puts("Usage:"); + puts(""); + puts("Run basic unit tests:"); + puts(""); + puts(" ./testcups"); + puts(""); + puts("Enumerate printers (for N seconds, -1 for indefinitely):"); + puts(""); + puts(" ./testcups enum [seconds]"); + puts(""); + puts("Ask for a password:"); + puts(""); + puts(" ./testcups password"); + puts(""); + puts("Print a file (interval controls delay between buffers in seconds):"); + puts(""); + puts(" ./testcups print printer file interval"); + return (1); + } + + return (0); + } + + /* + * cupsGetDests() + */ + + fputs("cupsGetDests: ", stdout); + fflush(stdout); + + num_dests = cupsGetDests(&dests); + + if (num_dests == 0) + { + puts("FAIL"); + return (1); + } + else + { + printf("PASS (%d dests)\n", num_dests); + + for (i = num_dests, dest = dests; i > 0; i --, dest ++) + { + printf(" %s", dest->name); + + if (dest->instance) + printf(" /%s", dest->instance); + + if (dest->is_default) + puts(" ***DEFAULT***"); + else + putchar('\n'); + } + } + + /* + * cupsGetDest(NULL) + */ + + fputs("cupsGetDest(NULL): ", stdout); + fflush(stdout); + + if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) == NULL) + { + for (i = num_dests, dest = dests; i > 0; i --, dest ++) + if (dest->is_default) + break; + + if (i) + { + status = 1; + puts("FAIL"); + } + else + puts("PASS (no default)"); + + dest = NULL; + } + else + printf("PASS (%s)\n", dest->name); + + /* + * cupsGetNamedDest(NULL, NULL, NULL) + */ + + fputs("cupsGetNamedDest(NULL, NULL, NULL): ", stdout); + fflush(stdout); + + if ((named_dest = cupsGetNamedDest(NULL, NULL, NULL)) == NULL || + !dests_equal(dest, named_dest)) + { + if (!dest) + puts("PASS (no default)"); + else if (named_dest) + { + puts("FAIL (different values)"); + show_diffs(dest, named_dest); + status = 1; + } + else + { + puts("FAIL (no default)"); + status = 1; + } + } + else + printf("PASS (%s)\n", named_dest->name); + + if (named_dest) + cupsFreeDests(1, named_dest); + + /* + * cupsGetDest(printer) + */ + + printf("cupsGetDest(\"%s\"): ", dests[num_dests / 2].name); + fflush(stdout); + + if ((dest = cupsGetDest(dests[num_dests / 2].name, NULL, num_dests, + dests)) == NULL) + { + puts("FAIL"); + return (1); + } + else + puts("PASS"); + + /* + * cupsGetNamedDest(NULL, printer, instance) + */ + + printf("cupsGetNamedDest(NULL, \"%s\", \"%s\"): ", dest->name, + dest->instance ? dest->instance : "(null)"); + fflush(stdout); + + if ((named_dest = cupsGetNamedDest(NULL, dest->name, + dest->instance)) == NULL || + !dests_equal(dest, named_dest)) + { + if (named_dest) + { + puts("FAIL (different values)"); + show_diffs(dest, named_dest); + } + else + puts("FAIL (no destination)"); + + + status = 1; + } + else + puts("PASS"); + + if (named_dest) + cupsFreeDests(1, named_dest); + + /* + * cupsPrintFile() + */ + + fputs("cupsPrintFile: ", stdout); + fflush(stdout); + + if (cupsPrintFile(dest->name, "../data/testprint", "Test Page", + dest->num_options, dest->options) <= 0) + { + printf("FAIL (%s)\n", cupsLastErrorString()); + return (1); + } + else + puts("PASS"); + + /* + * cupsGetPPD(printer) + */ + + fputs("cupsGetPPD(): ", stdout); + fflush(stdout); + + if ((ppdfile = cupsGetPPD(dest->name)) == NULL) + { + puts("FAIL"); + } + else + { + puts("PASS"); + + /* + * ppdOpenFile() + */ + + fputs("ppdOpenFile(): ", stdout); + fflush(stdout); + + if ((ppd = ppdOpenFile(ppdfile)) == NULL) + { + puts("FAIL"); + return (1); + } + else + puts("PASS"); + + ppdClose(ppd); + unlink(ppdfile); + } + + /* + * cupsGetJobs() + */ + + fputs("cupsGetJobs: ", stdout); + fflush(stdout); + + num_jobs = cupsGetJobs(&jobs, NULL, 0, -1); + + if (num_jobs == 0) + { + puts("FAIL"); + return (1); + } + else + puts("PASS"); + + cupsFreeJobs(num_jobs, jobs); + cupsFreeDests(num_dests, dests); + + return (status); +} + + +/* + * 'dests_equal()' - Determine whether two destinations are equal. + */ + +static int /* O - 1 if equal, 0 if not equal */ +dests_equal(cups_dest_t *a, /* I - First destination */ + cups_dest_t *b) /* I - Second destination */ +{ + int i; /* Looping var */ + cups_option_t *aoption; /* Current option */ + const char *bval; /* Option value */ + + + if (a == b) + return (1); + + if (!a || !b) + return (0); + + if (_cups_strcasecmp(a->name, b->name) || + (a->instance && !b->instance) || + (!a->instance && b->instance) || + (a->instance && _cups_strcasecmp(a->instance, b->instance)) || + a->num_options != b->num_options) + return (0); + + for (i = a->num_options, aoption = a->options; i > 0; i --, aoption ++) + if ((bval = cupsGetOption(aoption->name, b->num_options, + b->options)) == NULL || + strcmp(aoption->value, bval)) + return (0); + + return (1); +} + + +/* + * 'enum_cb()' - Report additions and removals. + */ + +static int /* O - 1 to continue, 0 to stop */ +enum_cb(void *user_data, /* I - User data (unused) */ + unsigned flags, /* I - Destination flags */ + cups_dest_t *dest) /* I - Destination */ +{ + int i; /* Looping var */ + cups_option_t *option; /* Current option */ + + + if (flags & CUPS_DEST_FLAGS_REMOVED) + printf("Removed '%s':\n", dest->name); + else + printf("Added '%s':\n", dest->name); + + for (i = dest->num_options, option = dest->options; i > 0; i --, option ++) + printf(" %s=\"%s\"\n", option->name, option->value); + + putchar('\n'); + + return (1); +} + + +/* + * 'show_diffs()' - Show differences between two destinations. + */ + +static void +show_diffs(cups_dest_t *a, /* I - First destination */ + cups_dest_t *b) /* I - Second destination */ +{ + int i; /* Looping var */ + cups_option_t *aoption; /* Current option */ + const char *bval; /* Option value */ + + + if (!a || !b) + return; + + puts(" Item cupsGetDest cupsGetNamedDest"); + puts(" -------------------- -------------------- --------------------"); + + if (_cups_strcasecmp(a->name, b->name)) + printf(" name %-20.20s %-20.20s\n", a->name, b->name); + + if ((a->instance && !b->instance) || + (!a->instance && b->instance) || + (a->instance && _cups_strcasecmp(a->instance, b->instance))) + printf(" instance %-20.20s %-20.20s\n", + a->instance ? a->instance : "(null)", + b->instance ? b->instance : "(null)"); + + if (a->num_options != b->num_options) + printf(" num_options %-20d %-20d\n", a->num_options, + b->num_options); + + for (i = a->num_options, aoption = a->options; i > 0; i --, aoption ++) + if ((bval = cupsGetOption(aoption->name, b->num_options, + b->options)) == NULL || + strcmp(aoption->value, bval)) + printf(" %-20.20s %-20.20s %-20.20s\n", aoption->name, + aoption->value, bval ? bval : "(null)"); +} + + +/* + * End of "$Id$". + */ diff --git a/cups/testfile.c b/cups/testfile.c new file mode 100644 index 0000000..2aa3fb9 --- /dev/null +++ b/cups/testfile.c @@ -0,0 +1,821 @@ +/* + * "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $" + * + * File test program for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * main() - Main entry. + * count_lines() - Count the number of lines in a file. + * random_tests() - Do random access tests. + * read_write_tests() - Perform read/write tests. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "debug-private.h" +#include "file.h" +#include +#include +#ifdef HAVE_LIBZ +# include +#endif /* HAVE_LIBZ */ +#ifdef WIN32 +# include +#else +# include +#endif /* WIN32 */ +#include + + +/* + * Local functions... + */ + +static int count_lines(cups_file_t *fp); +static int random_tests(void); +static int read_write_tests(int compression); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int status; /* Exit status */ + char filename[1024]; /* Filename buffer */ + cups_file_t *fp; /* File pointer */ +#ifndef WIN32 + int fds[2]; /* Open file descriptors */ + cups_file_t *fdfile; /* File opened with cupsFileOpenFd() */ +#endif /* !WIN32 */ + int count; /* Number of lines in file */ + + + if (argc == 1) + { + /* + * Do uncompressed file tests... + */ + + status = read_write_tests(0); + +#ifdef HAVE_LIBZ + /* + * Do compressed file tests... + */ + + putchar('\n'); + + status += read_write_tests(1); +#endif /* HAVE_LIBZ */ + + /* + * Do uncompressed random I/O tests... + */ + + status += random_tests(); + +#ifndef WIN32 + /* + * Test fdopen and close without reading... + */ + + pipe(fds); + close(fds[1]); + + fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout); + fflush(stdout); + + if ((fdfile = cupsFileOpenFd(fds[0], "r")) == NULL) + { + puts("FAIL"); + status ++; + } + else + { + /* + * Able to open file, now close without reading. If we don't return + * before the alarm fires, that is a failure and we will crash on the + * alarm signal... + */ + + puts("PASS"); + fputs("cupsFileClose(no read): ", stdout); + fflush(stdout); + + alarm(5); + cupsFileClose(fdfile); + alarm(0); + + puts("PASS"); + } +#endif /* !WIN32 */ + + /* + * Count lines in psglyphs, rewind, then count again. + */ + + fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout); + + if ((fp = cupsFileOpen("../data/media.defs", "r")) == NULL) + { + puts("FAIL"); + status ++; + } + else + { + puts("PASS"); + fputs("cupsFileGets: ", stdout); + + if ((count = count_lines(fp)) != 208) + { + printf("FAIL (got %d lines, expected 208)\n", count); + status ++; + } + else + { + puts("PASS"); + fputs("cupsFileRewind: ", stdout); + + if (cupsFileRewind(fp) != 0) + { + puts("FAIL"); + status ++; + } + else + { + puts("PASS"); + fputs("cupsFileGets: ", stdout); + + if ((count = count_lines(fp)) != 208) + { + printf("FAIL (got %d lines, expected 208)\n", count); + status ++; + } + else + puts("PASS"); + } + } + + cupsFileClose(fp); + } + + /* + * Test path functions... + */ + + fputs("\ncupsFileFind: ", stdout); +#ifdef WIN32 + if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) && + cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename))) +#else + if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) && + cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename))) +#endif /* WIN32 */ + printf("PASS (%s)\n", filename); + else + { + puts("FAIL"); + status ++; + } + + /* + * Summarize the results and return... + */ + + if (!status) + puts("\nALL TESTS PASSED!"); + else + printf("\n%d TEST(S) FAILED!\n", status); + } + else + { + /* + * Cat the filename on the command-line... + */ + + char line[1024]; /* Line from file */ + + if ((fp = cupsFileOpen(argv[1], "r")) == NULL) + { + perror(argv[1]); + status = 1; + } + else + { + status = 0; + + while (cupsFileGets(fp, line, sizeof(line))) + puts(line); + + if (!cupsFileEOF(fp)) + perror(argv[1]); + + cupsFileClose(fp); + } + } + + return (status); +} + + +/* + * 'count_lines()' - Count the number of lines in a file. + */ + +static int /* O - Number of lines */ +count_lines(cups_file_t *fp) /* I - File to read from */ +{ + int count; /* Number of lines */ + char line[1024]; /* Line buffer */ + + + for (count = 0; cupsFileGets(fp, line, sizeof(line)); count ++); + + return (count); +} + + +/* + * 'random_tests()' - Do random access tests. + */ + +static int /* O - Status */ +random_tests(void) +{ + int status, /* Status of tests */ + pass, /* Current pass */ + count, /* Number of records read */ + record, /* Current record */ + num_records; /* Number of records */ + ssize_t pos, /* Position in file */ + expected; /* Expected position in file */ + cups_file_t *fp; /* File */ + char buffer[512]; /* Data buffer */ + + + /* + * Run 4 passes, each time appending to a data file and then reopening the + * file for reading to validate random records in the file. + */ + + for (status = 0, pass = 0; pass < 4; pass ++) + { + /* + * cupsFileOpen(append) + */ + + printf("\ncupsFileOpen(append %d): ", pass); + + if ((fp = cupsFileOpen("testfile.dat", "a")) == NULL) + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + break; + } + else + puts("PASS"); + + /* + * cupsFileTell() + */ + + expected = 256 * sizeof(buffer) * pass; + + fputs("cupsFileTell(): ", stdout); + if ((pos = cupsFileTell(fp)) != expected) + { + printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n", + CUPS_LLCAST pos, CUPS_LLCAST expected); + status ++; + break; + } + else + puts("PASS"); + + /* + * cupsFileWrite() + */ + + fputs("cupsFileWrite(256 512-byte records): ", stdout); + for (record = 0; record < 256; record ++) + { + memset(buffer, record, sizeof(buffer)); + if (cupsFileWrite(fp, buffer, sizeof(buffer)) < sizeof(buffer)) + break; + } + + if (record < 256) + { + printf("FAIL (%d: %s)\n", record, strerror(errno)); + status ++; + break; + } + else + puts("PASS"); + + /* + * cupsFileTell() + */ + + expected += 256 * sizeof(buffer); + + fputs("cupsFileTell(): ", stdout); + if ((pos = cupsFileTell(fp)) != expected) + { + printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n", + CUPS_LLCAST pos, CUPS_LLCAST expected); + status ++; + break; + } + else + puts("PASS"); + + cupsFileClose(fp); + + /* + * cupsFileOpen(read) + */ + + printf("\ncupsFileOpen(read %d): ", pass); + + if ((fp = cupsFileOpen("testfile.dat", "r")) == NULL) + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + break; + } + else + puts("PASS"); + + /* + * cupsFileSeek, cupsFileRead + */ + + fputs("cupsFileSeek(), cupsFileRead(): ", stdout); + + for (num_records = (pass + 1) * 256, count = (pass + 1) * 256, + record = CUPS_RAND() % num_records; + count > 0; + count --, record = (record + (CUPS_RAND() & 31) - 16 + num_records) % + num_records) + { + /* + * The last record is always the first... + */ + + if (count == 1) + record = 0; + + /* + * Try reading the data for the specified record, and validate the + * contents... + */ + + expected = sizeof(buffer) * record; + + if ((pos = cupsFileSeek(fp, expected)) != expected) + { + printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n", + CUPS_LLCAST pos, CUPS_LLCAST expected); + status ++; + break; + } + else + { + if (cupsFileRead(fp, buffer, sizeof(buffer)) != sizeof(buffer)) + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + break; + } + else if ((buffer[0] & 255) != (record & 255) || + memcmp(buffer, buffer + 1, sizeof(buffer) - 1)) + { + printf("FAIL (Bad Data - %d instead of %d)\n", buffer[0] & 255, + record & 255); + status ++; + break; + } + } + } + + if (count == 0) + puts("PASS"); + + cupsFileClose(fp); + } + + /* + * Remove the test file... + */ + + unlink("testfile.dat"); + + /* + * Return the test status... + */ + + return (status); +} + + +/* + * 'read_write_tests()' - Perform read/write tests. + */ + +static int /* O - Status */ +read_write_tests(int compression) /* I - Use compression? */ +{ + int i; /* Looping var */ + cups_file_t *fp; /* File */ + int status; /* Exit status */ + char line[1024], /* Line from file */ + *value; /* Directive value from line */ + int linenum; /* Line number */ + unsigned char readbuf[8192], /* Read buffer */ + writebuf[8192]; /* Write buffer */ + int byte; /* Byte from file */ + off_t length; /* Length of file */ + static const char *partial_line = "partial line"; + /* Partial line */ + + + /* + * No errors so far... + */ + + status = 0; + + /* + * Initialize the write buffer with random data... + */ + + CUPS_SRAND((unsigned)time(NULL)); + + for (i = 0; i < (int)sizeof(writebuf); i ++) + writebuf[i] = CUPS_RAND(); + + /* + * cupsFileOpen(write) + */ + + printf("cupsFileOpen(write%s): ", compression ? " compressed" : ""); + + fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", + compression ? "w9" : "w"); + if (fp) + { + puts("PASS"); + + /* + * cupsFileCompression() + */ + + fputs("cupsFileCompression(): ", stdout); + + if (cupsFileCompression(fp) == compression) + puts("PASS"); + else + { + printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp), + compression); + status ++; + } + + /* + * cupsFilePuts() + */ + + fputs("cupsFilePuts(): ", stdout); + + if (cupsFilePuts(fp, "# Hello, World\n") > 0) + puts("PASS"); + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFilePrintf() + */ + + fputs("cupsFilePrintf(): ", stdout); + + for (i = 0; i < 1000; i ++) + if (cupsFilePrintf(fp, "TestLine %03d\n", i) < 0) + break; + + if (i >= 1000) + puts("PASS"); + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFilePutChar() + */ + + fputs("cupsFilePutChar(): ", stdout); + + for (i = 0; i < 256; i ++) + if (cupsFilePutChar(fp, i) < 0) + break; + + if (i >= 256) + puts("PASS"); + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFileWrite() + */ + + fputs("cupsFileWrite(): ", stdout); + + for (i = 0; i < 10000; i ++) + if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0) + break; + + if (i >= 10000) + puts("PASS"); + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFilePuts() with partial line... + */ + + fputs("cupsFilePuts(\"partial line\"): ", stdout); + + if (cupsFilePuts(fp, partial_line) > 0) + puts("PASS"); + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFileTell() + */ + + fputs("cupsFileTell(): ", stdout); + + if ((length = cupsFileTell(fp)) == 81933283) + puts("PASS"); + else + { + printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length); + status ++; + } + + /* + * cupsFileClose() + */ + + fputs("cupsFileClose(): ", stdout); + + if (!cupsFileClose(fp)) + puts("PASS"); + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + } + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFileOpen(read) + */ + + fputs("\ncupsFileOpen(read): ", stdout); + + fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r"); + if (fp) + { + puts("PASS"); + + /* + * cupsFileGets() + */ + + fputs("cupsFileGets(): ", stdout); + + if (cupsFileGets(fp, line, sizeof(line))) + { + if (line[0] == '#') + puts("PASS"); + else + { + printf("FAIL (Got line \"%s\", expected comment line)\n", line); + status ++; + } + } + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFileCompression() + */ + + fputs("cupsFileCompression(): ", stdout); + + if (cupsFileCompression(fp) == compression) + puts("PASS"); + else + { + printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp), + compression); + status ++; + } + + /* + * cupsFileGetConf() + */ + + linenum = 1; + + fputs("cupsFileGetConf(): ", stdout); + + for (i = 0; i < 1000; i ++) + if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) + break; + else if (_cups_strcasecmp(line, "TestLine") || !value || atoi(value) != i || + linenum != (i + 2)) + break; + + if (i >= 1000) + puts("PASS"); + else if (line[0]) + { + printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum, + line, value ? value : "(null)"); + status ++; + } + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFileGetChar() + */ + + fputs("cupsFileGetChar(): ", stdout); + + for (i = 0; i < 256; i ++) + if ((byte = cupsFileGetChar(fp)) != i) + break; + + if (i >= 256) + puts("PASS"); + else if (byte >= 0) + { + printf("FAIL (Got %d, expected %d)\n", byte, i); + status ++; + } + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFileRead() + */ + + fputs("cupsFileRead(): ", stdout); + + for (i = 0; i < 10000; i ++) + if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0) + break; + else if (memcmp(readbuf, writebuf, sizeof(readbuf))) + break; + + if (i >= 10000) + puts("PASS"); + else if (byte > 0) + { + printf("FAIL (Pass %d, ", i); + + for (i = 0; i < (int)sizeof(readbuf); i ++) + if (readbuf[i] != writebuf[i]) + break; + + printf("match failed at offset %d - got %02X, expected %02X)\n", + i, readbuf[i], writebuf[i]); + } + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * cupsFileGetChar() with partial line... + */ + + fputs("cupsFileGetChar(partial line): ", stdout); + + for (i = 0; i < (int)strlen(partial_line); i ++) + if ((byte = cupsFileGetChar(fp)) < 0) + break; + else if (byte != partial_line[i]) + break; + + if (!partial_line[i]) + puts("PASS"); + else + { + printf("FAIL (got '%c', expected '%c')\n", byte, partial_line[i]); + status ++; + } + + /* + * cupsFileTell() + */ + + fputs("cupsFileTell(): ", stdout); + + if ((length = cupsFileTell(fp)) == 81933283) + puts("PASS"); + else + { + printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length); + status ++; + } + + /* + * cupsFileClose() + */ + + fputs("cupsFileClose(): ", stdout); + + if (!cupsFileClose(fp)) + puts("PASS"); + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + } + else + { + printf("FAIL (%s)\n", strerror(errno)); + status ++; + } + + /* + * Remove the test file... + */ + + unlink(compression ? "testfile.dat.gz" : "testfile.dat"); + + /* + * Return the test status... + */ + + return (status); +} + + +/* + * End of "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $". + */ diff --git a/cups/testhttp.c b/cups/testhttp.c new file mode 100644 index 0000000..10fb624 --- /dev/null +++ b/cups/testhttp.c @@ -0,0 +1,604 @@ +/* + * "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $" + * + * HTTP test program for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * main() - Main entry. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "http-private.h" + + +/* + * Types and structures... + */ + +typedef struct uri_test_s /**** URI test cases ****/ +{ + http_uri_status_t result; /* Expected return value */ + const char *uri, /* URI */ + *scheme, /* Scheme string */ + *username, /* Username:password string */ + *hostname, /* Hostname string */ + *resource; /* Resource string */ + int port, /* Port number */ + assemble_port; /* Port number for httpAssembleURI() */ +} uri_test_t; + + +/* + * Local globals... + */ + +static uri_test_t uri_tests[] = /* URI test data */ + { + /* Start with valid URIs */ + { HTTP_URI_OK, "file:/filename", + "file", "", "", "/filename", 0, 0 }, + { HTTP_URI_OK, "file:/filename%20with%20spaces", + "file", "", "", "/filename with spaces", 0, 0 }, + { HTTP_URI_OK, "file:///filename", + "file", "", "", "/filename", 0, 0 }, + { HTTP_URI_OK, "file:///filename%20with%20spaces", + "file", "", "", "/filename with spaces", 0, 0 }, + { HTTP_URI_OK, "file://localhost/filename", + "file", "", "localhost", "/filename", 0, 0 }, + { HTTP_URI_OK, "file://localhost/filename%20with%20spaces", + "file", "", "localhost", "/filename with spaces", 0, 0 }, + { HTTP_URI_OK, "http://server/", + "http", "", "server", "/", 80, 0 }, + { HTTP_URI_OK, "http://username@server/", + "http", "username", "server", "/", 80, 0 }, + { HTTP_URI_OK, "http://username:passwor%64@server/", + "http", "username:password", "server", "/", 80, 0 }, + { HTTP_URI_OK, "http://username:passwor%64@server:8080/", + "http", "username:password", "server", "/", 8080, 8080 }, + { HTTP_URI_OK, "http://username:passwor%64@server:8080/directory/filename", + "http", "username:password", "server", "/directory/filename", 8080, 8080 }, + { HTTP_URI_OK, "http://[2000::10:100]:631/ipp", + "http", "", "2000::10:100", "/ipp", 631, 631 }, + { HTTP_URI_OK, "https://username:passwor%64@server/directory/filename", + "https", "username:password", "server", "/directory/filename", 443, 0 }, + { HTTP_URI_OK, "ipp://username:passwor%64@[::1]/ipp", + "ipp", "username:password", "::1", "/ipp", 631, 0 }, + { HTTP_URI_OK, "lpd://server/queue?reserve=yes", + "lpd", "", "server", "/queue?reserve=yes", 515, 0 }, + { HTTP_URI_OK, "mailto:user@domain.com", + "mailto", "", "", "user@domain.com", 0, 0 }, + { HTTP_URI_OK, "socket://server/", + "socket", "", "server", "/", 9100, 0 }, + { HTTP_URI_OK, "socket://192.168.1.1:9101/", + "socket", "", "192.168.1.1", "/", 9101, 9101 }, + { HTTP_URI_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp", + "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 }, + { HTTP_URI_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", + "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 }, + { HTTP_URI_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/", + "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0 }, + { HTTP_URI_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./", + "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0 }, + + /* Missing scheme */ + { HTTP_URI_MISSING_SCHEME, "/path/to/file/index.html", + "file", "", "", "/path/to/file/index.html", 0, 0 }, + { HTTP_URI_MISSING_SCHEME, "//server/ipp", + "ipp", "", "server", "/ipp", 631, 0 }, + + /* Unknown scheme */ + { HTTP_URI_UNKNOWN_SCHEME, "vendor://server/resource", + "vendor", "", "server", "/resource", 0, 0 }, + + /* Missing resource */ + { HTTP_URI_MISSING_RESOURCE, "socket://[::192.168.2.1]", + "socket", "", "::192.168.2.1", "/", 9100, 0 }, + { HTTP_URI_MISSING_RESOURCE, "socket://192.168.1.1:9101", + "socket", "", "192.168.1.1", "/", 9101 }, + + /* Bad URI */ + { HTTP_URI_BAD_URI, "", + "", "", "", "", 0, 0 }, + + /* Bad scheme */ + { HTTP_URI_BAD_SCHEME, "bad_scheme://server/resource", + "", "", "", "", 0, 0 }, + + /* Bad username */ + { HTTP_URI_BAD_USERNAME, "http://username:passwor%6@server/resource", + "http", "", "", "", 80, 0 }, + + /* Bad hostname */ + { HTTP_URI_BAD_HOSTNAME, "http://[/::1]/index.html", + "http", "", "", "", 80, 0 }, + { HTTP_URI_BAD_HOSTNAME, "http://[", + "http", "", "", "", 80, 0 }, + { HTTP_URI_BAD_HOSTNAME, "http://serve%7/index.html", + "http", "", "", "", 80, 0 }, + { HTTP_URI_BAD_HOSTNAME, "http://server with spaces/index.html", + "http", "", "", "", 80, 0 }, + + /* Bad port number */ + { HTTP_URI_BAD_PORT, "http://127.0.0.1:9999a/index.html", + "http", "", "127.0.0.1", "", 0, 0 }, + + /* Bad resource */ + { HTTP_URI_BAD_RESOURCE, "http://server/index.html%", + "http", "", "server", "", 80, 0 }, + { HTTP_URI_BAD_RESOURCE, "http://server/index with spaces.html", + "http", "", "server", "", 80, 0 } + }; +static const char * const base64_tests[][2] = + { + { "A", "QQ==" }, + /* 010000 01 */ + { "AB", "QUI=" }, + /* 010000 010100 0010 */ + { "ABC", "QUJD" }, + /* 010000 010100 001001 000011 */ + { "ABCD", "QUJDRA==" }, + /* 010000 010100 001001 000011 010001 00 */ + { "ABCDE", "QUJDREU=" }, + /* 010000 010100 001001 000011 010001 000100 0101 */ + { "ABCDEF", "QUJDREVG" }, + /* 010000 010100 001001 000011 010001 000100 010101 000110 */ + }; + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int i, j, k; /* Looping vars */ + http_t *http; /* HTTP connection */ + http_encryption_t encryption; /* Encryption type */ + http_status_t status; /* Status of GET command */ + int failures; /* Number of test failures */ + char buffer[8192]; /* Input buffer */ + long bytes; /* Number of bytes read */ + FILE *out; /* Output file */ + char encode[256], /* Base64-encoded string */ + decode[256]; /* Base64-decoded string */ + int decodelen; /* Length of decoded string */ + char scheme[HTTP_MAX_URI], /* Scheme from URI */ + hostname[HTTP_MAX_URI], /* Hostname from URI */ + username[HTTP_MAX_URI], /* Username:password from URI */ + resource[HTTP_MAX_URI]; /* Resource from URI */ + int port; /* Port number from URI */ + http_uri_status_t uri_status; /* Status of URI separation */ + http_addrlist_t *addrlist, /* Address list */ + *addr; /* Current address */ + off_t length, total; /* Length and total bytes */ + time_t start, current; /* Start and end time */ + static const char * const uri_status_strings[] = + { + "HTTP_URI_OVERFLOW", + "HTTP_URI_BAD_ARGUMENTS", + "HTTP_URI_BAD_RESOURCE", + "HTTP_URI_BAD_PORT", + "HTTP_URI_BAD_HOSTNAME", + "HTTP_URI_BAD_USERNAME", + "HTTP_URI_BAD_SCHEME", + "HTTP_URI_BAD_URI", + "HTTP_URI_OK", + "HTTP_URI_MISSING_SCHEME", + "HTTP_URI_UNKNOWN_SCHEME", + "HTTP_URI_MISSING_RESOURCE" + }; + + + /* + * Do API tests if we don't have a URL on the command-line... + */ + + if (argc == 1) + { + failures = 0; + + /* + * httpGetDateString()/httpGetDateTime() + */ + + fputs("httpGetDateString()/httpGetDateTime(): ", stdout); + + start = time(NULL); + strcpy(buffer, httpGetDateString(start)); + current = httpGetDateTime(buffer); + + i = (int)(current - start); + if (i < 0) + i = -i; + + if (!i) + puts("PASS"); + else + { + failures ++; + puts("FAIL"); + printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600, + (i / 60) % 60, i % 60); + printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer); + printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current); + printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current, + httpGetDateString(current)); + } + + /* + * httpDecode64_2()/httpEncode64_2() + */ + + fputs("httpDecode64_2()/httpEncode64_2(): ", stdout); + + for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++) + { + httpEncode64_2(encode, sizeof(encode), base64_tests[i][0], + (int)strlen(base64_tests[i][0])); + decodelen = (int)sizeof(decode); + httpDecode64_2(decode, &decodelen, base64_tests[i][1]); + + if (strcmp(decode, base64_tests[i][0])) + { + failures ++; + + if (j) + { + puts("FAIL"); + j = 1; + } + + printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n", + decode, base64_tests[i][0]); + } + + if (strcmp(encode, base64_tests[i][1])) + { + failures ++; + + if (j) + { + puts("FAIL"); + j = 1; + } + + printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n", + encode, base64_tests[i][1]); + } + } + + if (!j) + puts("PASS"); + + /* + * httpGetHostname() + */ + + fputs("httpGetHostname(): ", stdout); + + if (httpGetHostname(NULL, hostname, sizeof(hostname))) + printf("PASS (%s)\n", hostname); + else + { + failures ++; + puts("FAIL"); + } + + /* + * httpAddrGetList() + */ + + printf("httpAddrGetList(%s): ", hostname); + + addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL); + if (addrlist) + { + for (i = 0, addr = addrlist; addr; i ++, addr = addr->next) + { + char numeric[1024]; /* Numeric IP address */ + + + httpAddrString(&(addr->addr), numeric, sizeof(numeric)); + if (!strcmp(numeric, "UNKNOWN")) + break; + } + + if (addr) + printf("FAIL (bad address for %s)\n", hostname); + else + printf("PASS (%d address(es) for %s)\n", i, hostname); + + httpAddrFreeList(addrlist); + } + else if (isdigit(hostname[0] & 255)) + { + puts("FAIL (ignored because hostname is numeric)"); + } + else + { + failures ++; + puts("FAIL"); + } + + /* + * Test httpSeparateURI()... + */ + + fputs("httpSeparateURI(): ", stdout); + for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++) + { + uri_status = httpSeparateURI(HTTP_URI_CODING_MOST, + uri_tests[i].uri, scheme, sizeof(scheme), + username, sizeof(username), + hostname, sizeof(hostname), &port, + resource, sizeof(resource)); + if (uri_status != uri_tests[i].result || + strcmp(scheme, uri_tests[i].scheme) || + strcmp(username, uri_tests[i].username) || + strcmp(hostname, uri_tests[i].hostname) || + port != uri_tests[i].port || + strcmp(resource, uri_tests[i].resource)) + { + failures ++; + + if (!j) + { + puts("FAIL"); + j = 1; + } + + printf(" \"%s\":\n", uri_tests[i].uri); + + if (uri_status != uri_tests[i].result) + printf(" Returned %s instead of %s\n", + uri_status_strings[uri_status + 8], + uri_status_strings[uri_tests[i].result + 8]); + + if (strcmp(scheme, uri_tests[i].scheme)) + printf(" Scheme \"%s\" instead of \"%s\"\n", + scheme, uri_tests[i].scheme); + + if (strcmp(username, uri_tests[i].username)) + printf(" Username \"%s\" instead of \"%s\"\n", + username, uri_tests[i].username); + + if (strcmp(hostname, uri_tests[i].hostname)) + printf(" Hostname \"%s\" instead of \"%s\"\n", + hostname, uri_tests[i].hostname); + + if (port != uri_tests[i].port) + printf(" Port %d instead of %d\n", + port, uri_tests[i].port); + + if (strcmp(resource, uri_tests[i].resource)) + printf(" Resource \"%s\" instead of \"%s\"\n", + resource, uri_tests[i].resource); + } + } + + if (!j) + printf("PASS (%d URIs tested)\n", + (int)(sizeof(uri_tests) / sizeof(uri_tests[0]))); + + /* + * Test httpAssembleURI()... + */ + + fputs("httpAssembleURI(): ", stdout); + for (i = 0, j = 0, k = 0; + i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); + i ++) + if (uri_tests[i].result == HTTP_URI_OK && + !strstr(uri_tests[i].uri, "%64") && + strstr(uri_tests[i].uri, "//")) + { + k ++; + uri_status = httpAssembleURI(HTTP_URI_CODING_MOST, + buffer, sizeof(buffer), + uri_tests[i].scheme, + uri_tests[i].username, + uri_tests[i].hostname, + uri_tests[i].assemble_port, + uri_tests[i].resource); + + if (uri_status != HTTP_URI_OK) + { + failures ++; + + if (!j) + { + puts("FAIL"); + j = 1; + } + + printf(" \"%s\": %s\n", uri_tests[i].uri, + uri_status_strings[uri_status + 8]); + } + else if (strcmp(buffer, uri_tests[i].uri)) + { + failures ++; + + if (!j) + { + puts("FAIL"); + j = 1; + } + + printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri, + buffer); + } + } + + if (!j) + printf("PASS (%d URIs tested)\n", k); + + /* + * Show a summary and return... + */ + + if (failures) + printf("\n%d TESTS FAILED!\n", failures); + else + puts("\nALL TESTS PASSED!"); + + return (failures); + } + else if (strstr(argv[1], "._tcp")) + { + /* + * Test resolving an mDNS name. + */ + + char resolved[1024]; /* Resolved URI */ + + + printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]); + fflush(stdout); + + if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), + _HTTP_RESOLVE_DEFAULT, NULL, NULL)) + { + puts("FAIL"); + return (1); + } + else + printf("PASS (%s)\n", resolved); + + printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]); + fflush(stdout); + + if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), + _HTTP_RESOLVE_FQDN, NULL, NULL)) + { + puts("FAIL"); + return (1); + } + else if (strstr(resolved, ".local:")) + { + printf("FAIL (%s)\n", resolved); + return (1); + } + else + { + printf("PASS (%s)\n", resolved); + return (0); + } + } + else if (!strcmp(argv[1], "-u") && argc == 3) + { + /* + * Test URI separation... + */ + + uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme, + sizeof(scheme), username, sizeof(username), + hostname, sizeof(hostname), &port, + resource, sizeof(resource)); + printf("uri_status = %s\n", uri_status_strings[uri_status + 8]); + printf("scheme = \"%s\"\n", scheme); + printf("username = \"%s\"\n", username); + printf("hostname = \"%s\"\n", hostname); + printf("port = %d\n", port); + printf("resource = \"%s\"\n", resource); + + return (0); + } + + /* + * Test HTTP GET requests... + */ + + http = NULL; + out = stdout; + + for (i = 1; i < argc; i ++) + { + if (!strcmp(argv[i], "-o")) + { + i ++; + if (i >= argc) + break; + + out = fopen(argv[i], "wb"); + continue; + } + + httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme), + username, sizeof(username), + hostname, sizeof(hostname), &port, + resource, sizeof(resource)); + + if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") || + port == 443) + encryption = HTTP_ENCRYPT_ALWAYS; + else + encryption = HTTP_ENCRYPT_IF_REQUESTED; + + http = httpConnectEncrypt(hostname, port, encryption); + if (http == NULL) + { + perror(hostname); + continue; + } + printf("Requesting file \"%s\"...\n", resource); + httpClearFields(http); + httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); + httpGet(http, resource); + while ((status = httpUpdate(http)) == HTTP_CONTINUE); + + if (status == HTTP_OK) + puts("GET OK:"); + else + printf("GET failed with status %d...\n", status); + + start = time(NULL); + length = httpGetLength2(http); + total = 0; + + while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) + { + total += bytes; + fwrite(buffer, bytes, 1, out); + if (out != stdout) + { + current = time(NULL); + if (current == start) current ++; + printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes (" + CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total, + CUPS_LLCAST length, CUPS_LLCAST (total / (current - start))); + fflush(stdout); + } + } + } + + puts("Closing connection to server..."); + httpClose(http); + + if (out != stdout) + fclose(out); + + return (0); +} + + +/* + * End of "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $". + */ diff --git a/cups/testi18n.c b/cups/testi18n.c new file mode 100644 index 0000000..725c01b --- /dev/null +++ b/cups/testi18n.c @@ -0,0 +1,619 @@ +/* + * "$Id: testi18n.c 7560 2008-05-13 06:34:04Z mike $" + * + * Internationalization test for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * main() - Main entry for internationalization test module. + * print_utf8() - Print UTF-8 string with (optional) message. + */ + +/* + * Include necessary headers... + */ + +#include "string-private.h" +#include "language-private.h" +#include +#include +#include + + +/* + * Local globals... + */ + +static const char * const lang_encodings[] = + { /* Encoding strings */ + "us-ascii", "iso-8859-1", + "iso-8859-2", "iso-8859-3", + "iso-8859-4", "iso-8859-5", + "iso-8859-6", "iso-8859-7", + "iso-8859-8", "iso-8859-9", + "iso-8859-10", "utf-8", + "iso-8859-13", "iso-8859-14", + "iso-8859-15", "windows-874", + "windows-1250", "windows-1251", + "windows-1252", "windows-1253", + "windows-1254", "windows-1255", + "windows-1256", "windows-1257", + "windows-1258", "koi8-r", + "koi8-u", "iso-8859-11", + "iso-8859-16", "mac-roman", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "windows-932", "windows-936", + "windows-949", "windows-950", + "windows-1361", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "unknown", "unknown", + "euc-cn", "euc-jp", + "euc-kr", "euc-tw", + "jis-x0213" + }; + + +/* + * Local functions... + */ + +static void print_utf8(const char *msg, const cups_utf8_t *src); + + +/* + * 'main()' - Main entry for internationalization test module. + */ + +int /* O - Exit code */ +main(int argc, /* I - Argument Count */ + char *argv[]) /* I - Arguments */ +{ + FILE *fp; /* File pointer */ + int count; /* File line counter */ + int status, /* Status of current test */ + errors; /* Error count */ + char line[1024]; /* File line source string */ + int len; /* Length (count) of string */ + char legsrc[1024], /* Legacy source string */ + legdest[1024], /* Legacy destination string */ + *legptr; /* Pointer into legacy string */ + cups_utf8_t utf8latin[] = /* UTF-8 Latin-1 source */ + { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xC3, 0x84, 0x2E, 0x00 }; + /* "A != ." - use ISO 8859-1 */ + cups_utf8_t utf8repla[] = /* UTF-8 Latin-1 replacement */ + { 0x41, 0x20, 0xE2, 0x89, 0xA2, 0x20, 0xC3, 0x84, 0x2E, 0x00 }; + /* "A ." */ + cups_utf8_t utf8greek[] = /* UTF-8 Greek source string */ + { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xCE, 0x91, 0x2E, 0x00 }; + /* "A != ." - use ISO 8859-7 */ + cups_utf8_t utf8japan[] = /* UTF-8 Japanese source */ + { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xEE, 0x9C, 0x80, 0x2E, 0x00 }; + /* "A != ." - use Windows 932 or EUC-JP */ + cups_utf8_t utf8taiwan[] = /* UTF-8 Chinese source */ + { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xE4, 0xB9, 0x82, 0x2E, 0x00 }; + /* "A != ." - use Windows 950 (Big5) or EUC-TW */ + cups_utf8_t utf8dest[1024]; /* UTF-8 destination string */ + cups_utf32_t utf32dest[1024]; /* UTF-32 destination string */ + + + if (argc > 1) + { + int i; /* Looping var */ + cups_encoding_t encoding; /* Source encoding */ + + + if (argc != 3) + { + puts("Usage: ./testi18n [filename charset]"); + return (1); + } + + if ((fp = fopen(argv[1], "rb")) == NULL) + { + perror(argv[1]); + return (1); + } + + for (i = 0, encoding = CUPS_AUTO_ENCODING; + i < (int)(sizeof(lang_encodings) / sizeof(lang_encodings[0])); + i ++) + if (!_cups_strcasecmp(lang_encodings[i], argv[2])) + { + encoding = (cups_encoding_t)i; + break; + } + + if (encoding == CUPS_AUTO_ENCODING) + { + fprintf(stderr, "%s: Unknown character set!\n", argv[2]); + return (1); + } + + while (fgets(line, sizeof(line), fp)) + { + if (cupsCharsetToUTF8(utf8dest, line, sizeof(utf8dest), encoding) < 0) + { + fprintf(stderr, "%s: Unable to convert line: %s", argv[1], line); + return (1); + } + + fputs((char *)utf8dest, stdout); + } + + fclose(fp); + return (0); + } + + /* + * Start with some conversion tests from a UTF-8 test file. + */ + + errors = 0; + + if ((fp = fopen("utf8demo.txt", "rb")) == NULL) + { + perror("utf8demo.txt"); + return (1); + } + + /* + * cupsUTF8ToUTF32 + */ + + fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout); + + for (count = 0, status = 0; fgets(line, sizeof(line), fp);) + { + count ++; + + if (cupsUTF8ToUTF32(utf32dest, (cups_utf8_t *)line, 1024) < 0) + { + printf("FAIL (UTF-8 to UTF-32 on line %d)\n", count); + errors ++; + status = 1; + break; + } + } + + if (!status) + puts("PASS"); + + /* + * cupsUTF8ToCharset(CUPS_EUC_JP) + */ + + fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout); + + rewind(fp); + + for (count = 0, status = 0; fgets(line, sizeof(line), fp);) + { + count ++; + + len = cupsUTF8ToCharset(legdest, (cups_utf8_t *)line, 1024, CUPS_EUC_JP); + if (len < 0) + { + printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count); + errors ++; + status = 1; + break; + } + } + + if (!status) + puts("PASS"); + + fclose(fp); + + /* + * Test UTF-8 to legacy charset (ISO 8859-1)... + */ + + fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout); + + legdest[0] = 0; + + len = cupsUTF8ToCharset(legdest, utf8latin, 1024, CUPS_ISO8859_1); + if (len < 0) + { + printf("FAIL (len=%d)\n", len); + errors ++; + } + else + puts("PASS"); + + /* + * cupsCharsetToUTF8 + */ + + fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout); + + strcpy(legsrc, legdest); + + len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_ISO8859_1); + if (len != strlen((char *)utf8latin)) + { + printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8latin)); + print_utf8(" utf8latin", utf8latin); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else if (memcmp(utf8latin, utf8dest, len)) + { + puts("FAIL (results do not match)"); + print_utf8(" utf8latin", utf8latin); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else if (cupsUTF8ToCharset(legdest, utf8repla, 1024, CUPS_ISO8859_1) < 0) + { + puts("FAIL (replacement characters do not work!)"); + errors ++; + } + else + puts("PASS"); + + /* + * Test UTF-8 to/from legacy charset (ISO 8859-7)... + */ + + fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout); + + if (cupsUTF8ToCharset(legdest, utf8greek, 1024, CUPS_ISO8859_7) < 0) + { + puts("FAIL"); + errors ++; + } + else + { + for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); + + if (*legptr) + { + puts("FAIL (unknown character)"); + errors ++; + } + else + puts("PASS"); + } + + fputs("cupsCharsetToUTF8(CUPS_ISO8859_7): ", stdout); + + strcpy(legsrc, legdest); + + len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_ISO8859_7); + if (len != strlen((char *)utf8greek)) + { + printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8greek)); + print_utf8(" utf8greek", utf8greek); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else if (memcmp(utf8greek, utf8dest, len)) + { + puts("FAIL (results do not match)"); + print_utf8(" utf8greek", utf8greek); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else + puts("PASS"); + + /* + * Test UTF-8 to/from legacy charset (Windows 932)... + */ + + fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout); + + if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_WINDOWS_932) < 0) + { + puts("FAIL"); + errors ++; + } + else + { + for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); + + if (*legptr) + { + puts("FAIL (unknown character)"); + errors ++; + } + else + puts("PASS"); + } + + fputs("cupsCharsetToUTF8(CUPS_WINDOWS_932): ", stdout); + + strcpy(legsrc, legdest); + + len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_932); + if (len != strlen((char *)utf8japan)) + { + printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8japan)); + print_utf8(" utf8japan", utf8japan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else if (memcmp(utf8japan, utf8dest, len)) + { + puts("FAIL (results do not match)"); + print_utf8(" utf8japan", utf8japan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else + puts("PASS"); + + /* + * Test UTF-8 to/from legacy charset (EUC-JP)... + */ + + fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout); + + if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_EUC_JP) < 0) + { + puts("FAIL"); + errors ++; + } + else + { + for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); + + if (*legptr) + { + puts("FAIL (unknown character)"); + errors ++; + } + else + puts("PASS"); + } + +#ifndef __linux + fputs("cupsCharsetToUTF8(CUPS_EUC_JP): ", stdout); + + strcpy(legsrc, legdest); + + len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_JP); + if (len != strlen((char *)utf8japan)) + { + printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8japan)); + print_utf8(" utf8japan", utf8japan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else if (memcmp(utf8japan, utf8dest, len)) + { + puts("FAIL (results do not match)"); + print_utf8(" utf8japan", utf8japan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else + puts("PASS"); +#endif /* !__linux */ + + /* + * Test UTF-8 to/from legacy charset (Windows 950)... + */ + + fputs("cupsUTF8ToCharset(CUPS_WINDOWS_950): ", stdout); + + if (cupsUTF8ToCharset(legdest, utf8taiwan, 1024, CUPS_WINDOWS_950) < 0) + { + puts("FAIL"); + errors ++; + } + else + { + for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); + + if (*legptr) + { + puts("FAIL (unknown character)"); + errors ++; + } + else + puts("PASS"); + } + + fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout); + + strcpy(legsrc, legdest); + + len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_950); + if (len != strlen((char *)utf8taiwan)) + { + printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8taiwan)); + print_utf8(" utf8taiwan", utf8taiwan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else if (memcmp(utf8taiwan, utf8dest, len)) + { + puts("FAIL (results do not match)"); + print_utf8(" utf8taiwan", utf8taiwan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else + puts("PASS"); + + /* + * Test UTF-8 to/from legacy charset (EUC-TW)... + */ + + fputs("cupsUTF8ToCharset(CUPS_EUC_TW): ", stdout); + + if (cupsUTF8ToCharset(legdest, utf8taiwan, 1024, CUPS_EUC_TW) < 0) + { + puts("FAIL"); + errors ++; + } + else + { + for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); + + if (*legptr) + { + puts("FAIL (unknown character)"); + errors ++; + } + else + puts("PASS"); + } + + fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout); + + strcpy(legsrc, legdest); + + len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_TW); + if (len != strlen((char *)utf8taiwan)) + { + printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8taiwan)); + print_utf8(" utf8taiwan", utf8taiwan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else if (memcmp(utf8taiwan, utf8dest, len)) + { + puts("FAIL (results do not match)"); + print_utf8(" utf8taiwan", utf8taiwan); + print_utf8(" utf8dest", utf8dest); + errors ++; + } + else + puts("PASS"); + +#if 0 + /* + * Test UTF-8 (16-bit) to UTF-32 (w/ BOM)... + */ + if (verbose) + printf("\ntesti18n: Testing UTF-8 to UTF-32 (w/ BOM)...\n"); + len = cupsUTF8ToUTF32(utf32dest, utf8good, 1024); + if (len < 0) + return (1); + if (verbose) + { + print_utf8(" utf8good ", utf8good); + print_utf32(" utf32dest", utf32dest); + } + memcpy (utf32src, utf32dest, (len + 1) * sizeof(cups_utf32_t)); + len = cupsUTF32ToUTF8(utf8dest, utf32src, 1024); + if (len < 0) + return (1); + if (len != strlen ((char *) utf8good)) + return (1); + if (memcmp(utf8good, utf8dest, len) != 0) + return (1); + + /* + * Test invalid UTF-8 (16-bit) to UTF-32 (w/ BOM)... + */ + if (verbose) + printf("\ntesti18n: Testing UTF-8 bad 16-bit source string...\n"); + len = cupsUTF8ToUTF32(utf32dest, utf8bad, 1024); + if (len >= 0) + return (1); + if (verbose) + print_utf8(" utf8bad ", utf8bad); + + /* + * Test _cupsCharmapFlush()... + */ + if (verbose) + printf("\ntesti18n: Testing _cupsCharmapFlush()...\n"); + _cupsCharmapFlush(); + return (0); +#endif /* 0 */ + + return (errors > 0); +} + + +/* + * 'print_utf8()' - Print UTF-8 string with (optional) message. + */ + +static void +print_utf8(const char *msg, /* I - Message String */ + const cups_utf8_t *src) /* I - UTF-8 Source String */ +{ + const char *prefix; /* Prefix string */ + + + if (msg) + printf("%s:", msg); + + for (prefix = " "; *src; src ++) + { + printf("%s%02x", prefix, *src); + + if ((src[0] & 0x80) && (src[1] & 0x80)) + prefix = ""; + else + prefix = " "; + } + + putchar('\n'); +} + + +/* + * End of "$Id: testi18n.c 7560 2008-05-13 06:34:04Z mike $" + */ diff --git a/cups/testipp.c b/cups/testipp.c new file mode 100644 index 0000000..5655b6b --- /dev/null +++ b/cups/testipp.c @@ -0,0 +1,1005 @@ +/* + * "$Id: testipp.c 6649 2007-07-11 21:46:42Z mike $" + * + * IPP test program for CUPS. + * + * Copyright 2007-2012 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. + * + * Contents: + * + * main() - Main entry. + * hex_dump() - Produce a hex dump of a buffer. + * print_attributes() - Print the attributes in a request... + * read_cb() - Read data from a buffer. + * write_cb() - Write data into a buffer. + */ + +/* + * Include necessary headers... + */ + +#include "file.h" +#include "string-private.h" +#include "ipp-private.h" +#ifdef WIN32 +# include +#else +# include +# include +#endif /* WIN32 */ + + +/* + * Local types... + */ + +typedef struct _ippdata_t +{ + size_t rpos, /* Read position */ + wused, /* Bytes used */ + wsize; /* Max size of buffer */ + ipp_uchar_t *wbuffer; /* Buffer */ +} _ippdata_t; + + +/* + * Local globals... + */ + +ipp_uchar_t collection[] = /* Collection buffer */ + { + 0x01, 0x01, /* IPP version */ + 0x00, 0x02, /* Print-Job operation */ + 0x00, 0x00, 0x00, 0x01, + /* Request ID */ + + IPP_TAG_OPERATION, + + IPP_TAG_CHARSET, + 0x00, 0x12, /* Name length + name */ + 'a','t','t','r','i','b','u','t','e','s','-', + 'c','h','a','r','s','e','t', + 0x00, 0x05, /* Value length + value */ + 'u','t','f','-','8', + + IPP_TAG_LANGUAGE, + 0x00, 0x1b, /* Name length + name */ + 'a','t','t','r','i','b','u','t','e','s','-', + 'n','a','t','u','r','a','l','-','l','a','n', + 'g','u','a','g','e', + 0x00, 0x02, /* Value length + value */ + 'e','n', + + IPP_TAG_URI, + 0x00, 0x0b, /* Name length + name */ + 'p','r','i','n','t','e','r','-','u','r','i', + 0x00, 0x1c, /* Value length + value */ + 'i','p','p',':','/','/','l','o','c','a','l', + 'h','o','s','t','/','p','r','i','n','t','e', + 'r','s','/','f','o','o', + + IPP_TAG_JOB, /* job group tag */ + + IPP_TAG_BEGIN_COLLECTION, + /* begCollection tag */ + 0x00, 0x09, /* Name length + name */ + 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', + 0x00, 0x00, /* No value */ + IPP_TAG_MEMBERNAME, /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0a, /* Value length + value */ + 'm', 'e', 'd', 'i', 'a', '-', 's', 'i', 'z', 'e', + IPP_TAG_BEGIN_COLLECTION, + /* begCollection tag */ + 0x00, 0x00, /* Name length + name */ + 0x00, 0x00, /* No value */ + IPP_TAG_MEMBERNAME, + /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0b, /* Value length + value */ + 'x', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n', + IPP_TAG_INTEGER, /* integer tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x04, /* Value length + value */ + 0x00, 0x00, 0x54, 0x56, + IPP_TAG_MEMBERNAME, + /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0b, /* Value length + value */ + 'y', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n', + IPP_TAG_INTEGER, /* integer tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x04, /* Value length + value */ + 0x00, 0x00, 0x6d, 0x24, + IPP_TAG_END_COLLECTION, + /* endCollection tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x00, /* No value */ + IPP_TAG_MEMBERNAME, /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0b, /* Value length + value */ + 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r', + IPP_TAG_KEYWORD, /* keyword tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x04, /* Value length + value */ + 'b', 'l', 'u', 'e', + + IPP_TAG_MEMBERNAME, /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0a, /* Value length + value */ + 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e', + IPP_TAG_KEYWORD, /* keyword tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x05, /* Value length + value */ + 'p', 'l', 'a', 'i', 'n', + IPP_TAG_END_COLLECTION, + /* endCollection tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x00, /* No value */ + + IPP_TAG_BEGIN_COLLECTION, + /* begCollection tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x00, /* No value */ + IPP_TAG_MEMBERNAME, /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0a, /* Value length + value */ + 'm', 'e', 'd', 'i', 'a', '-', 's', 'i', 'z', 'e', + IPP_TAG_BEGIN_COLLECTION, + /* begCollection tag */ + 0x00, 0x00, /* Name length + name */ + 0x00, 0x00, /* No value */ + IPP_TAG_MEMBERNAME, + /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0b, /* Value length + value */ + 'x', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n', + IPP_TAG_INTEGER, /* integer tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x04, /* Value length + value */ + 0x00, 0x00, 0x52, 0x08, + IPP_TAG_MEMBERNAME, + /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0b, /* Value length + value */ + 'y', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n', + IPP_TAG_INTEGER, /* integer tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x04, /* Value length + value */ + 0x00, 0x00, 0x74, 0x04, + IPP_TAG_END_COLLECTION, + /* endCollection tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x00, /* No value */ + IPP_TAG_MEMBERNAME, /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0b, /* Value length + value */ + 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r', + IPP_TAG_KEYWORD, /* keyword tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x05, /* Value length + value */ + 'p', 'l', 'a', 'i', 'd', + + IPP_TAG_MEMBERNAME, /* memberAttrName tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x0a, /* Value length + value */ + 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e', + IPP_TAG_KEYWORD, /* keyword tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x06, /* Value length + value */ + 'g', 'l', 'o', 's', 's', 'y', + IPP_TAG_END_COLLECTION, + /* endCollection tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x00, /* No value */ + + IPP_TAG_END /* end tag */ + }; + +ipp_uchar_t mixed[] = /* Mixed value buffer */ + { + 0x01, 0x01, /* IPP version */ + 0x00, 0x02, /* Print-Job operation */ + 0x00, 0x00, 0x00, 0x01, + /* Request ID */ + + IPP_TAG_OPERATION, + + IPP_TAG_INTEGER, /* integer tag */ + 0x00, 0x1f, /* Name length + name */ + 'n', 'o', 't', 'i', 'f', 'y', '-', 'l', 'e', 'a', 's', 'e', + '-', 'd', 'u', 'r', 'a', 't', 'i', 'o', 'n', '-', 's', 'u', + 'p', 'p', 'o', 'r', 't', 'e', 'd', + 0x00, 0x04, /* Value length + value */ + 0x00, 0x00, 0x00, 0x01, + + IPP_TAG_RANGE, /* rangeOfInteger tag */ + 0x00, 0x00, /* No name */ + 0x00, 0x08, /* Value length + value */ + 0x00, 0x00, 0x00, 0x10, + 0x00, 0x00, 0x00, 0x20, + + IPP_TAG_END /* end tag */ + }; + + +/* + * Local functions... + */ + +void hex_dump(const char *title, ipp_uchar_t *buffer, int bytes); +void print_attributes(ipp_t *ipp, int indent); +ssize_t read_cb(_ippdata_t *data, ipp_uchar_t *buffer, size_t bytes); +ssize_t write_cb(_ippdata_t *data, ipp_uchar_t *buffer, size_t bytes); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + _ippdata_t data; /* IPP buffer */ + ipp_uchar_t buffer[8192]; /* Write buffer data */ + ipp_t *cols[2], /* Collections */ + *size; /* media-size collection */ + ipp_t *request; /* Request */ + ipp_attribute_t *media_col, /* media-col attribute */ + *media_size, /* media-size attribute */ + *attr; /* Other attribute */ + ipp_state_t state; /* State */ + int length; /* Length of data */ + cups_file_t *fp; /* File pointer */ + int i; /* Looping var */ + int status; /* Status of tests (0 = success, 1 = fail) */ + + + status = 0; + + if (argc == 1) + { + /* + * Test request generation code... + */ + + printf("Create Sample Request: "); + + request = ippNew(); + request->request.op.version[0] = 0x01; + request->request.op.version[1] = 0x01; + request->request.op.operation_id = IPP_PRINT_JOB; + request->request.op.request_id = 1; + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, + "attributes-charset", NULL, "utf-8"); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, + "attributes-natural-language", NULL, "en"); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, + "printer-uri", NULL, "ipp://localhost/printers/foo"); + + cols[0] = ippNew(); + size = ippNew(); + ippAddInteger(size, IPP_TAG_ZERO, IPP_TAG_INTEGER, "x-dimension", 21590); + ippAddInteger(size, IPP_TAG_ZERO, IPP_TAG_INTEGER, "y-dimension", 27940); + ippAddCollection(cols[0], IPP_TAG_JOB, "media-size", size); + ippDelete(size); + ippAddString(cols[0], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-color", NULL, + "blue"); + ippAddString(cols[0], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-type", NULL, + "plain"); + + cols[1] = ippNew(); + size = ippNew(); + ippAddInteger(size, IPP_TAG_ZERO, IPP_TAG_INTEGER, "x-dimension", 21000); + ippAddInteger(size, IPP_TAG_ZERO, IPP_TAG_INTEGER, "y-dimension", 29700); + ippAddCollection(cols[1], IPP_TAG_JOB, "media-size", size); + ippDelete(size); + ippAddString(cols[1], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-color", NULL, + "plaid"); + ippAddString(cols[1], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-type", NULL, + "glossy"); + + ippAddCollections(request, IPP_TAG_JOB, "media-col", 2, + (const ipp_t **)cols); + ippDelete(cols[0]); + ippDelete(cols[1]); + + length = ippLength(request); + if (length != sizeof(collection)) + { + printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n", + length, (int)sizeof(collection)); + status = 1; + } + else + puts("PASS"); + + /* + * Write test #1... + */ + + printf("Write Sample to Memory: "); + + data.wused = 0; + data.wsize = sizeof(buffer); + data.wbuffer = buffer; + + while ((state = ippWriteIO(&data, (ipp_iocb_t)write_cb, 1, NULL, + request)) != IPP_DATA) + if (state == IPP_ERROR) + break; + + if (state != IPP_DATA) + { + printf("FAIL - %d bytes written.\n", (int)data.wused); + status = 1; + } + else if (data.wused != sizeof(collection)) + { + printf("FAIL - wrote %d bytes, expected %d bytes!\n", (int)data.wused, + (int)sizeof(collection)); + hex_dump("Bytes Written", data.wbuffer, data.wused); + hex_dump("Baseline", collection, sizeof(collection)); + status = 1; + } + else if (memcmp(data.wbuffer, collection, data.wused)) + { + for (i = 0; i < data.wused; i ++) + if (data.wbuffer[i] != collection[i]) + break; + + printf("FAIL - output does not match baseline at 0x%04x!\n", i); + hex_dump("Bytes Written", data.wbuffer, data.wused); + hex_dump("Baseline", collection, sizeof(collection)); + status = 1; + } + else + puts("PASS"); + + ippDelete(request); + + /* + * Read the data back in and confirm... + */ + + printf("Read Sample from Memory: "); + + request = ippNew(); + data.rpos = 0; + + while ((state = ippReadIO(&data, (ipp_iocb_t)read_cb, 1, NULL, + request)) != IPP_DATA) + if (state == IPP_ERROR) + break; + + length = ippLength(request); + + if (state != IPP_DATA) + { + printf("FAIL - %d bytes read.\n", (int)data.rpos); + status = 1; + } + else if (data.rpos != data.wused) + { + printf("FAIL - read %d bytes, expected %d bytes!\n", (int)data.rpos, + (int)data.wused); + print_attributes(request, 8); + status = 1; + } + else if (length != sizeof(collection)) + { + printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n", + length, (int)sizeof(collection)); + print_attributes(request, 8); + status = 1; + } + else + puts("PASS"); + + fputs("ippFindAttribute(media-col): ", stdout); + if ((media_col = ippFindAttribute(request, "media-col", + IPP_TAG_BEGIN_COLLECTION)) == NULL) + { + if ((media_col = ippFindAttribute(request, "media-col", + IPP_TAG_ZERO)) == NULL) + puts("FAIL (not found)"); + else + printf("FAIL (wrong type - %s)\n", ippTagString(media_col->value_tag)); + + status = 1; + } + else if (media_col->num_values != 2) + { + printf("FAIL (wrong count - %d)\n", media_col->num_values); + status = 1; + } + else + puts("PASS"); + + if (media_col) + { + fputs("ippFindAttribute(media-size 1): ", stdout); + if ((media_size = ippFindAttribute(media_col->values[0].collection, + "media-size", + IPP_TAG_BEGIN_COLLECTION)) == NULL) + { + if ((media_size = ippFindAttribute(media_col->values[0].collection, + "media-col", + IPP_TAG_ZERO)) == NULL) + puts("FAIL (not found)"); + else + printf("FAIL (wrong type - %s)\n", + ippTagString(media_size->value_tag)); + + status = 1; + } + else + { + if ((attr = ippFindAttribute(media_size->values[0].collection, + "x-dimension", IPP_TAG_INTEGER)) == NULL) + { + if ((attr = ippFindAttribute(media_size->values[0].collection, + "x-dimension", IPP_TAG_ZERO)) == NULL) + puts("FAIL (missing x-dimension)"); + else + printf("FAIL (wrong type for x-dimension - %s)\n", + ippTagString(attr->value_tag)); + + status = 1; + } + else if (attr->values[0].integer != 21590) + { + printf("FAIL (wrong value for x-dimension - %d)\n", + attr->values[0].integer); + status = 1; + } + else if ((attr = ippFindAttribute(media_size->values[0].collection, + "y-dimension", + IPP_TAG_INTEGER)) == NULL) + { + if ((attr = ippFindAttribute(media_size->values[0].collection, + "y-dimension", IPP_TAG_ZERO)) == NULL) + puts("FAIL (missing y-dimension)"); + else + printf("FAIL (wrong type for y-dimension - %s)\n", + ippTagString(attr->value_tag)); + + status = 1; + } + else if (attr->values[0].integer != 27940) + { + printf("FAIL (wrong value for y-dimension - %d)\n", + attr->values[0].integer); + status = 1; + } + else + puts("PASS"); + } + + fputs("ippFindAttribute(media-size 2): ", stdout); + if ((media_size = ippFindAttribute(media_col->values[1].collection, + "media-size", + IPP_TAG_BEGIN_COLLECTION)) == NULL) + { + if ((media_size = ippFindAttribute(media_col->values[1].collection, + "media-col", + IPP_TAG_ZERO)) == NULL) + puts("FAIL (not found)"); + else + printf("FAIL (wrong type - %s)\n", + ippTagString(media_size->value_tag)); + + status = 1; + } + else + { + if ((attr = ippFindAttribute(media_size->values[0].collection, + "x-dimension", + IPP_TAG_INTEGER)) == NULL) + { + if ((attr = ippFindAttribute(media_size->values[0].collection, + "x-dimension", IPP_TAG_ZERO)) == NULL) + puts("FAIL (missing x-dimension)"); + else + printf("FAIL (wrong type for x-dimension - %s)\n", + ippTagString(attr->value_tag)); + + status = 1; + } + else if (attr->values[0].integer != 21000) + { + printf("FAIL (wrong value for x-dimension - %d)\n", + attr->values[0].integer); + status = 1; + } + else if ((attr = ippFindAttribute(media_size->values[0].collection, + "y-dimension", + IPP_TAG_INTEGER)) == NULL) + { + if ((attr = ippFindAttribute(media_size->values[0].collection, + "y-dimension", IPP_TAG_ZERO)) == NULL) + puts("FAIL (missing y-dimension)"); + else + printf("FAIL (wrong type for y-dimension - %s)\n", + ippTagString(attr->value_tag)); + + status = 1; + } + else if (attr->values[0].integer != 29700) + { + printf("FAIL (wrong value for y-dimension - %d)\n", + attr->values[0].integer); + status = 1; + } + else + puts("PASS"); + } + } + + ippDelete(request); + + /* + * Read the mixed data and confirm we converted everything to rangeOfInteger + * values... + */ + + printf("Read Mixed integer/rangeOfInteger from Memory: "); + + request = ippNew(); + data.rpos = 0; + data.wused = sizeof(mixed); + data.wsize = sizeof(mixed); + data.wbuffer = mixed; + + while ((state = ippReadIO(&data, (ipp_iocb_t)read_cb, 1, NULL, + request)) != IPP_DATA) + if (state == IPP_ERROR) + break; + + length = ippLength(request); + + if (state != IPP_DATA) + { + printf("FAIL - %d bytes read.\n", (int)data.rpos); + status = 1; + } + else if (data.rpos != sizeof(mixed)) + { + printf("FAIL - read %d bytes, expected %d bytes!\n", (int)data.rpos, + (int)sizeof(mixed)); + print_attributes(request, 8); + status = 1; + } + else if (length != (sizeof(mixed) + 4)) + { + printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n", + length, (int)sizeof(mixed) + 4); + print_attributes(request, 8); + status = 1; + } + else + puts("PASS"); + + fputs("ippFindAttribute(notify-lease-duration-supported): ", stdout); + if ((attr = ippFindAttribute(request, "notify-lease-duration-supported", + IPP_TAG_ZERO)) == NULL) + { + puts("FAIL (not found)"); + status = 1; + } + else if (attr->value_tag != IPP_TAG_RANGE) + { + printf("FAIL (wrong type - %s)\n", ippTagString(attr->value_tag)); + status = 1; + } + else if (attr->num_values != 2) + { + printf("FAIL (wrong count - %d)\n", attr->num_values); + status = 1; + } + else if (attr->values[0].range.lower != 1 || + attr->values[0].range.upper != 1 || + attr->values[1].range.lower != 16 || + attr->values[1].range.upper != 32) + { + printf("FAIL (wrong values - %d,%d and %d,%d)\n", + attr->values[0].range.lower, + attr->values[0].range.upper, + attr->values[1].range.lower, + attr->values[1].range.upper); + status = 1; + } + else + puts("PASS"); + + ippDelete(request); + + /* + * Test _ippFindOption() private API... + */ + + fputs("_ippFindOption(printer-type): ", stdout); + if (_ippFindOption("printer-type")) + puts("PASS"); + else + { + puts("FAIL"); + status = 1; + } + + /* + * Summarize... + */ + + putchar('\n'); + + if (status) + puts("Core IPP tests failed."); + else + puts("Core IPP tests passed."); + } + else + { + /* + * Read IPP files... + */ + + for (i = 1; i < argc; i ++) + { + 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_ATTRIBUTE); + + if (state != IPP_DATA) + { + printf("Error reading IPP message from \"%s\"!\n", argv[i]); + status = 1; + } + else + { + printf("\n%s:\n", argv[i]); + print_attributes(request, 4); + } + + ippDelete(request); + cupsFileClose(fp); + } + } + + return (status); +} + + +/* + * 'hex_dump()' - Produce a hex dump of a buffer. + */ + +void +hex_dump(const char *title, /* I - Title */ + ipp_uchar_t *buffer, /* I - Buffer to dump */ + int bytes) /* I - Number of bytes */ +{ + int i, j; /* Looping vars */ + int ch; /* Current ASCII char */ + + + /* + * Show lines of 16 bytes at a time... + */ + + printf(" %s:\n", title); + + for (i = 0; i < bytes; i += 16) + { + /* + * Show the offset... + */ + + printf(" %04x ", i); + + /* + * Then up to 16 bytes in hex... + */ + + for (j = 0; j < 16; j ++) + if ((i + j) < bytes) + printf(" %02x", buffer[i + j]); + else + printf(" "); + + /* + * Then the ASCII representation of the bytes... + */ + + putchar(' '); + putchar(' '); + + for (j = 0; j < 16 && (i + j) < bytes; j ++) + { + ch = buffer[i + j] & 127; + + if (ch < ' ' || ch == 127) + putchar('.'); + else + putchar(ch); + } + + putchar('\n'); + } +} + + +/* + * 'print_attributes()' - Print the attributes in a request... + */ + +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" + }; + + + for (group = IPP_TAG_ZERO, attr = ipp->attrs; attr; attr = attr->next) + { + if (!attr->name && indent == 4) + { + group = IPP_TAG_ZERO; + putchar('\n'); + continue; + } + + if (group != attr->group_tag) + { + group = attr->group_tag; + + printf("\n%*s%s:\n\n", indent - 4, "", tags[group]); + } + + printf("%*s%s (", indent, "", attr->name ? attr->name : "(null)"); + if (attr->num_values > 1) + printf("1setOf "); + printf("%s):", tags[attr->value_tag]); + + 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 : + { + time_t vtime; /* Date/Time value */ + struct tm *vdate; /* Date info */ + char vstring[256]; /* Formatted time */ + + for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++) + { + vtime = ippDateToTime(val->date); + vdate = localtime(&vtime); + strftime(vstring, sizeof(vstring), "%c", vdate); + printf(" (%s)", vstring); + } + } + 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; + } + } +} + + +/* + * 'read_cb()' - Read data from a buffer. + */ + +ssize_t /* O - Number of bytes read */ +read_cb(_ippdata_t *data, /* I - Data */ + ipp_uchar_t *buffer, /* O - Buffer to read */ + size_t bytes) /* I - Number of bytes to read */ +{ + size_t count; /* Number of bytes */ + + + /* + * Copy bytes from the data buffer to the read buffer... + */ + + if ((count = data->wsize - data->rpos) > bytes) + count = bytes; + + memcpy(buffer, data->wbuffer + data->rpos, count); + data->rpos += count; + + /* + * Return the number of bytes read... + */ + + return (count); +} + + +/* + * 'write_cb()' - Write data into a buffer. + */ + +ssize_t /* O - Number of bytes written */ +write_cb(_ippdata_t *data, /* I - Data */ + ipp_uchar_t *buffer, /* I - Buffer to write */ + size_t bytes) /* I - Number of bytes to write */ +{ + size_t count; /* Number of bytes */ + + + /* + * Loop until all bytes are written... + */ + + if ((count = data->wsize - data->wused) > bytes) + count = bytes; + + memcpy(data->wbuffer + data->wused, buffer, count); + data->wused += count; + + /* + * Return the number of bytes written... + */ + + return (count); +} + + +/* + * End of "$Id: testipp.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/cups/testlang.c b/cups/testlang.c new file mode 100644 index 0000000..2cec8b6 --- /dev/null +++ b/cups/testlang.c @@ -0,0 +1,114 @@ +/* + * "$Id: testlang.c 6649 2007-07-11 21:46:42Z mike $" + * + * Localization test program for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * main() - Load the specified language and show the strings for yes and no. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * 'main()' - Load the specified language and show the strings for yes and no. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int i; /* Looping var */ + int errors = 0; /* Number of errors */ + cups_lang_t *language; /* Message catalog */ + cups_lang_t *language2; /* Message catalog */ + struct lconv *loc; /* Locale data */ + char buffer[1024]; /* String buffer */ + double number; /* Number */ + static const char * const tests[] = /* Test strings */ + { + "1", + "-1", + "3", + "5.125" + }; + + + _cupsSetLocale(argv); + + if (argc == 1) + { + language = cupsLangDefault(); + language2 = cupsLangDefault(); + } + else + { + language = cupsLangGet(argv[1]); + language2 = cupsLangGet(argv[1]); + } + + if (language != language2) + { + errors ++; + + puts("**** ERROR: Language cache did not work! ****"); + puts("First result from cupsLangGet:"); + } + + printf("Language = \"%s\"\n", language->language); + printf("Encoding = \"%s\"\n", _cupsEncodingName(language->encoding)); + printf("No = \"%s\"\n", _cupsLangString(language, "No")); + printf("Yes = \"%s\"\n", _cupsLangString(language, "Yes")); + + if (language != language2) + { + puts("Second result from cupsLangGet:"); + + printf("Language = \"%s\"\n", language2->language); + printf("Encoding = \"%s\"\n", _cupsEncodingName(language2->encoding)); + printf("No = \"%s\"\n", _cupsLangString(language2, "No")); + printf("Yes = \"%s\"\n", _cupsLangString(language2, "Yes")); + } + + loc = localeconv(); + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i ++) + { + number = _cupsStrScand(tests[i], NULL, loc); + + printf("_cupsStrScand(\"%s\") number=%f\n", tests[i], number); + + _cupsStrFormatd(buffer, buffer + sizeof(buffer), number, loc); + + printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number, buffer); + + if (strcmp(buffer, tests[i])) + { + errors ++; + puts("**** ERROR: Bad formatted number! ****"); + } + } + + return (errors > 0); +} + + +/* + * End of "$Id: testlang.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/cups/testoptions.c b/cups/testoptions.c new file mode 100644 index 0000000..d14b64f --- /dev/null +++ b/cups/testoptions.c @@ -0,0 +1,116 @@ +/* + * "$Id: testoptions.c 1992 2010-03-24 14:32:08Z msweet $" + * + * Option test program for CUPS. + * + * Copyright 2008-2010 by Apple Inc. + * + * 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. + * + * Contents: + * + * main() - Test option processing functions. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" + + +/* + * 'main()' - Test option processing functions. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int status = 0, /* Exit status */ + num_options; /* Number of options */ + cups_option_t *options; /* Options */ + const char *value; /* Value of an option */ + + + if (argc == 1) + { + /* + * cupsParseOptions() + */ + + fputs("cupsParseOptions: ", stdout); + + num_options = cupsParseOptions("foo=1234 " + "bar=\"One Fish\",\"Two Fish\",\"Red Fish\"," + "\"Blue Fish\" " + "baz={param1=1 param2=2} " + "foobar=FOO\\ BAR " + "barfoo=barfoo " + "barfoo=\"\'BAR FOO\'\"", 0, &options); + + if (num_options != 5) + { + printf("FAIL (num_options=%d, expected 5)\n", num_options); + status ++; + } + else if ((value = cupsGetOption("foo", num_options, options)) == NULL || + strcmp(value, "1234")) + { + printf("FAIL (foo=\"%s\", expected \"1234\")\n", value); + status ++; + } + else if ((value = cupsGetOption("bar", num_options, options)) == NULL || + strcmp(value, "One Fish,Two Fish,Red Fish,Blue Fish")) + { + printf("FAIL (bar=\"%s\", expected \"One Fish,Two Fish,Red Fish,Blue " + "Fish\")\n", value); + status ++; + } + else if ((value = cupsGetOption("baz", num_options, options)) == NULL || + strcmp(value, "{param1=1 param2=2}")) + { + printf("FAIL (baz=\"%s\", expected \"{param1=1 param2=2}\")\n", value); + status ++; + } + else if ((value = cupsGetOption("foobar", num_options, options)) == NULL || + strcmp(value, "FOO BAR")) + { + printf("FAIL (foobar=\"%s\", expected \"FOO BAR\")\n", value); + status ++; + } + else if ((value = cupsGetOption("barfoo", num_options, options)) == NULL || + strcmp(value, "\'BAR FOO\'")) + { + printf("FAIL (barfoo=\"%s\", expected \"\'BAR FOO\'\")\n", value); + status ++; + } + else + puts("PASS"); + } + else + { + int i; /* Looping var */ + cups_option_t *option; /* Current option */ + + + num_options = cupsParseOptions(argv[1], 0, &options); + + for (i = 0, option = options; i < num_options; i ++, option ++) + printf("options[%d].name=\"%s\", value=\"%s\"\n", i, option->name, + option->value); + } + + exit (status); +} + + +/* + * End of "$Id: testoptions.c 1992 2010-03-24 14:32:08Z msweet $". + */ diff --git a/cups/testppd.c b/cups/testppd.c new file mode 100644 index 0000000..5e291ce --- /dev/null +++ b/cups/testppd.c @@ -0,0 +1,1102 @@ +/* + * "$Id: testppd.c 7897 2008-09-02 19:33:19Z mike $" + * + * PPD test program for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * main() - Main entry. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#ifdef WIN32 +# include +#else +# include +# include +#endif /* WIN32 */ + + +/* + * Test data... + */ + +static const char *default_code = + "[{\n" + "%%BeginFeature: *InstalledDuplexer False\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *PageRegion Letter\n" + "PageRegion=Letter\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *InputSlot Tray\n" + "InputSlot=Tray\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *MediaType Plain\n" + "MediaType=Plain\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *IntOption None\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *StringOption None\n" + "%%EndFeature\n" + "} stopped cleartomark\n"; + +static const char *custom_code = + "[{\n" + "%%BeginFeature: *InstalledDuplexer False\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *InputSlot Tray\n" + "InputSlot=Tray\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *MediaType Plain\n" + "MediaType=Plain\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *IntOption None\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *CustomStringOption True\n" + "(value\\0502\\051)\n" + "(value 1)\n" + "StringOption=Custom\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *CustomPageSize True\n" + "400\n" + "500\n" + "0\n" + "0\n" + "0\n" + "PageSize=Custom\n" + "%%EndFeature\n" + "} stopped cleartomark\n"; + +static const char *default2_code = + "[{\n" + "%%BeginFeature: *InstalledDuplexer False\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *InputSlot Tray\n" + "InputSlot=Tray\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *Quality Normal\n" + "Quality=Normal\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *IntOption None\n" + "%%EndFeature\n" + "} stopped cleartomark\n" + "[{\n" + "%%BeginFeature: *StringOption None\n" + "%%EndFeature\n" + "} stopped cleartomark\n"; + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int i; /* Looping var */ + ppd_file_t *ppd; /* PPD file loaded from disk */ + int status; /* Status of tests (0 = success, 1 = fail) */ + int conflicts; /* Number of conflicts */ + char *s; /* String */ + char buffer[8192]; /* String buffer */ + const char *text, /* Localized text */ + *val; /* Option value */ + int num_options; /* Number of options */ + cups_option_t *options; /* Options */ + ppd_size_t minsize, /* Minimum size */ + maxsize, /* Maximum size */ + *size; /* Current size */ + ppd_attr_t *attr; /* Current attribute */ + + + status = 0; + + if (argc == 1) + { + /* + * Setup directories for locale stuff... + */ + + if (access("locale", 0)) + { + mkdir("locale", 0777); + mkdir("locale/fr", 0777); + symlink("../../../locale/cups_fr.po", "locale/fr/cups_fr.po"); + mkdir("locale/zh_TW", 0777); + symlink("../../../locale/cups_zh_TW.po", "locale/zh_TW/cups_zh_TW.po"); + } + + putenv("LOCALEDIR=locale"); + putenv("SOFTWARE=CUPS"); + + /* + * Do tests with test.ppd... + */ + + fputs("ppdOpenFile(test.ppd): ", stdout); + + if ((ppd = _ppdOpenFile("test.ppd", _PPD_LOCALIZATION_ALL)) != NULL) + puts("PASS"); + else + { + ppd_status_t err; /* Last error in file */ + int line; /* Line number in file */ + + + status ++; + err = ppdLastError(&line); + + printf("FAIL (%s on line %d)\n", ppdErrorString(err), line); + } + + fputs("ppdFindAttr(wildcard): ", stdout); + if ((attr = ppdFindAttr(ppd, "cupsTest", NULL)) == NULL) + { + status ++; + puts("FAIL (not found)"); + } + else if (strcmp(attr->name, "cupsTest") || strcmp(attr->spec, "Foo")) + { + status ++; + printf("FAIL (got \"%s %s\")\n", attr->name, attr->spec); + } + else + puts("PASS"); + + fputs("ppdFindNextAttr(wildcard): ", stdout); + if ((attr = ppdFindNextAttr(ppd, "cupsTest", NULL)) == NULL) + { + status ++; + puts("FAIL (not found)"); + } + else if (strcmp(attr->name, "cupsTest") || strcmp(attr->spec, "Bar")) + { + status ++; + printf("FAIL (got \"%s %s\")\n", attr->name, attr->spec); + } + else + puts("PASS"); + + fputs("ppdFindAttr(Foo): ", stdout); + if ((attr = ppdFindAttr(ppd, "cupsTest", "Foo")) == NULL) + { + status ++; + puts("FAIL (not found)"); + } + else if (strcmp(attr->name, "cupsTest") || strcmp(attr->spec, "Foo")) + { + status ++; + printf("FAIL (got \"%s %s\")\n", attr->name, attr->spec); + } + else + puts("PASS"); + + fputs("ppdFindNextAttr(Foo): ", stdout); + if ((attr = ppdFindNextAttr(ppd, "cupsTest", "Foo")) != NULL) + { + status ++; + printf("FAIL (got \"%s %s\")\n", attr->name, attr->spec); + } + else + puts("PASS"); + + fputs("ppdMarkDefaults: ", stdout); + ppdMarkDefaults(ppd); + + if ((conflicts = ppdConflicts(ppd)) == 0) + puts("PASS"); + else + { + status ++; + printf("FAIL (%d conflicts)\n", conflicts); + } + + fputs("ppdEmitString (defaults): ", stdout); + if ((s = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL && + !strcmp(s, default_code)) + puts("PASS"); + else + { + status ++; + printf("FAIL (%d bytes instead of %d)\n", s ? (int)strlen(s) : 0, + (int)strlen(default_code)); + + if (s) + puts(s); + } + + if (s) + free(s); + + fputs("ppdEmitString (custom size and string): ", stdout); + ppdMarkOption(ppd, "PageSize", "Custom.400x500"); + ppdMarkOption(ppd, "StringOption", "{String1=\"value 1\" String2=value(2)}"); + + if ((s = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL && + !strcmp(s, custom_code)) + puts("PASS"); + else + { + status ++; + printf("FAIL (%d bytes instead of %d)\n", s ? (int)strlen(s) : 0, + (int)strlen(custom_code)); + + if (s) + puts(s); + } + + if (s) + free(s); + + /* + * Test constraints... + */ + + fputs("cupsGetConflicts(InputSlot=Envelope): ", stdout); + ppdMarkOption(ppd, "PageSize", "Letter"); + + num_options = cupsGetConflicts(ppd, "InputSlot", "Envelope", &options); + if (num_options != 2 || + (val = cupsGetOption("PageRegion", num_options, options)) == NULL || + _cups_strcasecmp(val, "Letter") || + (val = cupsGetOption("PageSize", num_options, options)) == NULL || + _cups_strcasecmp(val, "Letter")) + { + printf("FAIL (%d options:", num_options); + for (i = 0; i < num_options; i ++) + printf(" %s=%s", options[i].name, options[i].value); + puts(")"); + status ++; + } + else + puts("PASS"); + + fputs("ppdConflicts(): ", stdout); + ppdMarkOption(ppd, "InputSlot", "Envelope"); + + if ((conflicts = ppdConflicts(ppd)) == 2) + puts("PASS (2)"); + else + { + printf("FAIL (%d)\n", conflicts); + status ++; + } + + fputs("cupsResolveConflicts(InputSlot=Envelope): ", stdout); + num_options = 0; + options = NULL; + if (!cupsResolveConflicts(ppd, "InputSlot", "Envelope", &num_options, + &options)) + { + puts("FAIL (Unable to resolve)"); + status ++; + } + else if (num_options != 2 || + !cupsGetOption("PageSize", num_options, options)) + { + printf("FAIL (%d options:", num_options); + for (i = 0; i < num_options; i ++) + printf(" %s=%s", options[i].name, options[i].value); + puts(")"); + status ++; + } + else + puts("PASS (Resolved by changing PageSize)"); + + cupsFreeOptions(num_options, options); + + fputs("cupsResolveConflicts(No option/choice): ", stdout); + num_options = 0; + options = NULL; + if (cupsResolveConflicts(ppd, NULL, NULL, &num_options, &options) && + num_options == 1 && !_cups_strcasecmp(options[0].name, "InputSlot") && + !_cups_strcasecmp(options[0].value, "Tray")) + puts("PASS (Resolved by changing InputSlot)"); + else if (num_options > 0) + { + printf("FAIL (%d options:", num_options); + for (i = 0; i < num_options; i ++) + printf(" %s=%s", options[i].name, options[i].value); + puts(")"); + status ++; + } + else + { + puts("FAIL (Unable to resolve)"); + status ++; + } + cupsFreeOptions(num_options, options); + + fputs("ppdInstallableConflict(): ", stdout); + if (ppdInstallableConflict(ppd, "Duplex", "DuplexNoTumble") && + !ppdInstallableConflict(ppd, "Duplex", "None")) + puts("PASS"); + else if (!ppdInstallableConflict(ppd, "Duplex", "DuplexNoTumble")) + { + puts("FAIL (Duplex=DuplexNoTumble did not conflict)"); + status ++; + } + else + { + puts("FAIL (Duplex=None conflicted)"); + status ++; + } + + /* + * ppdPageSizeLimits + */ + + fputs("ppdPageSizeLimits: ", stdout); + if (ppdPageSizeLimits(ppd, &minsize, &maxsize)) + { + if (minsize.width != 36 || minsize.length != 36 || + maxsize.width != 1080 || maxsize.length != 86400) + { + printf("FAIL (got min=%.0fx%.0f, max=%.0fx%.0f, " + "expected min=36x36, max=1080x86400)\n", minsize.width, + minsize.length, maxsize.width, maxsize.length); + status ++; + } + else + puts("PASS"); + } + else + { + puts("FAIL (returned 0)"); + status ++; + } + + /* + * cupsMarkOptions with PWG and IPP size names. + */ + + fputs("cupsMarkOptions(media=iso-a4): ", stdout); + num_options = cupsAddOption("media", "iso-a4", 0, &options); + cupsMarkOptions(ppd, num_options, options); + cupsFreeOptions(num_options, options); + + size = ppdPageSize(ppd, NULL); + if (!size || strcmp(size->name, "A4")) + { + printf("FAIL (%s)\n", size ? size->name : "unknown"); + status ++; + } + else + puts("PASS"); + + fputs("cupsMarkOptions(media=na_letter_8.5x11in): ", stdout); + num_options = cupsAddOption("media", "na_letter_8.5x11in", 0, &options); + cupsMarkOptions(ppd, num_options, options); + cupsFreeOptions(num_options, options); + + size = ppdPageSize(ppd, NULL); + if (!size || strcmp(size->name, "Letter")) + { + printf("FAIL (%s)\n", size ? size->name : "unknown"); + status ++; + } + else + puts("PASS"); + + fputs("cupsMarkOptions(media=oe_letter-fullbleed_8.5x11in): ", stdout); + num_options = cupsAddOption("media", "oe_letter-fullbleed_8.5x11in", 0, + &options); + cupsMarkOptions(ppd, num_options, options); + cupsFreeOptions(num_options, options); + + size = ppdPageSize(ppd, NULL); + if (!size || strcmp(size->name, "Letter.Fullbleed")) + { + printf("FAIL (%s)\n", size ? size->name : "unknown"); + status ++; + } + else + puts("PASS"); + + fputs("cupsMarkOptions(media=A4): ", stdout); + num_options = cupsAddOption("media", "A4", 0, &options); + cupsMarkOptions(ppd, num_options, options); + cupsFreeOptions(num_options, options); + + size = ppdPageSize(ppd, NULL); + if (!size || strcmp(size->name, "A4")) + { + printf("FAIL (%s)\n", size ? size->name : "unknown"); + status ++; + } + else + puts("PASS"); + + /* + * Custom sizes... + */ + + fputs("cupsMarkOptions(media=Custom.8x10in): ", stdout); + num_options = cupsAddOption("media", "Custom.8x10in", 0, &options); + cupsMarkOptions(ppd, num_options, options); + cupsFreeOptions(num_options, options); + + size = ppdPageSize(ppd, NULL); + if (!size || strcmp(size->name, "Custom") || + size->width != 576 || size->length != 720) + { + printf("FAIL (%s - %gx%g)\n", size ? size->name : "unknown", + size ? size->width : 0.0, size ? size->length : 0.0); + status ++; + } + else + puts("PASS"); + + /* + * Test localization... + */ + + fputs("ppdLocalizeIPPReason(text): ", stdout); + if (ppdLocalizeIPPReason(ppd, "foo", NULL, buffer, sizeof(buffer)) && + !strcmp(buffer, "Foo Reason")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"Foo Reason\")\n", buffer); + } + + fputs("ppdLocalizeIPPReason(http): ", stdout); + if (ppdLocalizeIPPReason(ppd, "foo", "http", buffer, sizeof(buffer)) && + !strcmp(buffer, "http://foo/bar.html")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"http://foo/bar.html\")\n", buffer); + } + + fputs("ppdLocalizeIPPReason(help): ", stdout); + if (ppdLocalizeIPPReason(ppd, "foo", "help", buffer, sizeof(buffer)) && + !strcmp(buffer, "help:anchor='foo'%20bookID=Vendor%20Help")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"help:anchor='foo'%%20bookID=Vendor%%20Help\")\n", buffer); + } + + fputs("ppdLocalizeIPPReason(file): ", stdout); + if (ppdLocalizeIPPReason(ppd, "foo", "file", buffer, sizeof(buffer)) && + !strcmp(buffer, "/help/foo/bar.html")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"/help/foo/bar.html\")\n", buffer); + } + + putenv("LANG=fr"); + putenv("LC_ALL=fr"); + putenv("LC_CTYPE=fr"); + putenv("LC_MESSAGES=fr"); + + fputs("ppdLocalizeIPPReason(fr text): ", stdout); + if (ppdLocalizeIPPReason(ppd, "foo", NULL, buffer, sizeof(buffer)) && + !strcmp(buffer, "La Long Foo Reason")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"La Long Foo Reason\")\n", buffer); + } + + putenv("LANG=zh_TW"); + putenv("LC_ALL=zh_TW"); + putenv("LC_CTYPE=zh_TW"); + putenv("LC_MESSAGES=zh_TW"); + + fputs("ppdLocalizeIPPReason(zh_TW text): ", stdout); + if (ppdLocalizeIPPReason(ppd, "foo", NULL, buffer, sizeof(buffer)) && + !strcmp(buffer, "Number 1 Foo Reason")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"Number 1 Foo Reason\")\n", buffer); + } + + /* + * cupsMarkerName localization... + */ + + putenv("LANG=en"); + putenv("LC_ALL=en"); + putenv("LC_CTYPE=en"); + putenv("LC_MESSAGES=en"); + + fputs("ppdLocalizeMarkerName(bogus): ", stdout); + + if ((text = ppdLocalizeMarkerName(ppd, "bogus")) != NULL) + { + status ++; + printf("FAIL (\"%s\" instead of NULL)\n", text); + } + else + puts("PASS"); + + fputs("ppdLocalizeMarkerName(cyan): ", stdout); + + if ((text = ppdLocalizeMarkerName(ppd, "cyan")) != NULL && + !strcmp(text, "Cyan Toner")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"Cyan Toner\")\n", + text ? text : "(null)"); + } + + putenv("LANG=fr"); + putenv("LC_ALL=fr"); + putenv("LC_CTYPE=fr"); + putenv("LC_MESSAGES=fr"); + + fputs("ppdLocalizeMarkerName(fr cyan): ", stdout); + if ((text = ppdLocalizeMarkerName(ppd, "cyan")) != NULL && + !strcmp(text, "La Toner Cyan")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"La Toner Cyan\")\n", + text ? text : "(null)"); + } + + putenv("LANG=zh_TW"); + putenv("LC_ALL=zh_TW"); + putenv("LC_CTYPE=zh_TW"); + putenv("LC_MESSAGES=zh_TW"); + + fputs("ppdLocalizeMarkerName(zh_TW cyan): ", stdout); + if ((text = ppdLocalizeMarkerName(ppd, "cyan")) != NULL && + !strcmp(text, "Number 1 Cyan Toner")) + puts("PASS"); + else + { + status ++; + printf("FAIL (\"%s\" instead of \"Number 1 Cyan Toner\")\n", + text ? text : "(null)"); + } + + ppdClose(ppd); + + /* + * Test new constraints... + */ + + fputs("ppdOpenFile(test2.ppd): ", stdout); + + if ((ppd = ppdOpenFile("test2.ppd")) != NULL) + puts("PASS"); + else + { + ppd_status_t err; /* Last error in file */ + int line; /* Line number in file */ + + + status ++; + err = ppdLastError(&line); + + printf("FAIL (%s on line %d)\n", ppdErrorString(err), line); + } + + fputs("ppdMarkDefaults: ", stdout); + ppdMarkDefaults(ppd); + + if ((conflicts = ppdConflicts(ppd)) == 0) + puts("PASS"); + else + { + status ++; + printf("FAIL (%d conflicts)\n", conflicts); + } + + fputs("ppdEmitString (defaults): ", stdout); + if ((s = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL && + !strcmp(s, default2_code)) + puts("PASS"); + else + { + status ++; + printf("FAIL (%d bytes instead of %d)\n", s ? (int)strlen(s) : 0, + (int)strlen(default2_code)); + + if (s) + puts(s); + } + + if (s) + free(s); + + fputs("ppdConflicts(): ", stdout); + ppdMarkOption(ppd, "PageSize", "Env10"); + ppdMarkOption(ppd, "InputSlot", "Envelope"); + ppdMarkOption(ppd, "Quality", "Photo"); + + if ((conflicts = ppdConflicts(ppd)) == 1) + puts("PASS (1)"); + else + { + printf("FAIL (%d)\n", conflicts); + status ++; + } + + fputs("cupsResolveConflicts(Quality=Photo): ", stdout); + num_options = 0; + options = NULL; + if (cupsResolveConflicts(ppd, "Quality", "Photo", &num_options, + &options)) + { + printf("FAIL (%d options:", num_options); + for (i = 0; i < num_options; i ++) + printf(" %s=%s", options[i].name, options[i].value); + puts(")"); + status ++; + } + else + puts("PASS (Unable to resolve)"); + cupsFreeOptions(num_options, options); + + fputs("cupsResolveConflicts(No option/choice): ", stdout); + num_options = 0; + options = NULL; + if (cupsResolveConflicts(ppd, NULL, NULL, &num_options, &options) && + num_options == 1 && !_cups_strcasecmp(options->name, "Quality") && + !_cups_strcasecmp(options->value, "Normal")) + puts("PASS"); + else if (num_options > 0) + { + printf("FAIL (%d options:", num_options); + for (i = 0; i < num_options; i ++) + printf(" %s=%s", options[i].name, options[i].value); + puts(")"); + status ++; + } + else + { + puts("FAIL (Unable to resolve!)"); + status ++; + } + cupsFreeOptions(num_options, options); + + fputs("cupsResolveConflicts(loop test): ", stdout); + ppdMarkOption(ppd, "PageSize", "A4"); + ppdMarkOption(ppd, "InputSlot", "Tray"); + ppdMarkOption(ppd, "Quality", "Photo"); + num_options = 0; + options = NULL; + if (!cupsResolveConflicts(ppd, NULL, NULL, &num_options, &options)) + puts("PASS"); + else if (num_options > 0) + { + printf("FAIL (%d options:", num_options); + for (i = 0; i < num_options; i ++) + printf(" %s=%s", options[i].name, options[i].value); + puts(")"); + } + else + puts("FAIL (No conflicts!)"); + + fputs("ppdInstallableConflict(): ", stdout); + if (ppdInstallableConflict(ppd, "Duplex", "DuplexNoTumble") && + !ppdInstallableConflict(ppd, "Duplex", "None")) + puts("PASS"); + else if (!ppdInstallableConflict(ppd, "Duplex", "DuplexNoTumble")) + { + puts("FAIL (Duplex=DuplexNoTumble did not conflict)"); + status ++; + } + else + { + puts("FAIL (Duplex=None conflicted)"); + status ++; + } + + /* + * ppdPageSizeLimits + */ + + ppdMarkDefaults(ppd); + + fputs("ppdPageSizeLimits(default): ", stdout); + if (ppdPageSizeLimits(ppd, &minsize, &maxsize)) + { + if (minsize.width != 36 || minsize.length != 36 || + maxsize.width != 1080 || maxsize.length != 86400) + { + printf("FAIL (got min=%.0fx%.0f, max=%.0fx%.0f, " + "expected min=36x36, max=1080x86400)\n", minsize.width, + minsize.length, maxsize.width, maxsize.length); + status ++; + } + else + puts("PASS"); + } + else + { + puts("FAIL (returned 0)"); + status ++; + } + + ppdMarkOption(ppd, "InputSlot", "Manual"); + + fputs("ppdPageSizeLimits(InputSlot=Manual): ", stdout); + if (ppdPageSizeLimits(ppd, &minsize, &maxsize)) + { + if (minsize.width != 100 || minsize.length != 100 || + maxsize.width != 1000 || maxsize.length != 1000) + { + printf("FAIL (got min=%.0fx%.0f, max=%.0fx%.0f, " + "expected min=100x100, max=1000x1000)\n", minsize.width, + minsize.length, maxsize.width, maxsize.length); + status ++; + } + else + puts("PASS"); + } + else + { + puts("FAIL (returned 0)"); + status ++; + } + + ppdMarkOption(ppd, "Quality", "Photo"); + + fputs("ppdPageSizeLimits(Quality=Photo): ", stdout); + if (ppdPageSizeLimits(ppd, &minsize, &maxsize)) + { + if (minsize.width != 200 || minsize.length != 200 || + maxsize.width != 1000 || maxsize.length != 1000) + { + printf("FAIL (got min=%.0fx%.0f, max=%.0fx%.0f, " + "expected min=200x200, max=1000x1000)\n", minsize.width, + minsize.length, maxsize.width, maxsize.length); + status ++; + } + else + puts("PASS"); + } + else + { + puts("FAIL (returned 0)"); + status ++; + } + + ppdMarkOption(ppd, "InputSlot", "Tray"); + + fputs("ppdPageSizeLimits(Quality=Photo): ", stdout); + if (ppdPageSizeLimits(ppd, &minsize, &maxsize)) + { + if (minsize.width != 300 || minsize.length != 300 || + maxsize.width != 1080 || maxsize.length != 86400) + { + printf("FAIL (got min=%.0fx%.0f, max=%.0fx%.0f, " + "expected min=300x300, max=1080x86400)\n", minsize.width, + minsize.length, maxsize.width, maxsize.length); + status ++; + } + else + puts("PASS"); + } + else + { + puts("FAIL (returned 0)"); + status ++; + } + } + else + { + const char *filename; /* PPD filename */ + struct stat fileinfo; /* File information */ + + + if (!strncmp(argv[1], "-d", 2)) + { + const char *printer; /* Printer name */ + + if (argv[1][2]) + printer = argv[1] + 2; + else if (argv[2]) + printer = argv[2]; + else + { + puts("Usage: ./testppd -d printer"); + return (1); + } + + filename = cupsGetPPD(printer); + + if (!filename) + { + printf("%s: %s\n", printer, cupsLastErrorString()); + return (1); + } + } + else + filename = argv[1]; + + if (lstat(filename, &fileinfo)) + { + printf("%s: %s\n", filename, strerror(errno)); + return (1); + } + + if (S_ISLNK(fileinfo.st_mode)) + { + char realfile[1024]; /* Real file path */ + ssize_t realsize; /* Size of real file path */ + + + if ((realsize = readlink(filename, realfile, sizeof(realfile) - 1)) < 0) + strcpy(realfile, "Unknown"); + else + realfile[realsize] = '\0'; + + if (stat(realfile, &fileinfo)) + printf("%s: symlink to \"%s\", %s\n", filename, realfile, + strerror(errno)); + else + printf("%s: symlink to \"%s\", %ld bytes\n", filename, realfile, + (long)fileinfo.st_size); + } + else + printf("%s: regular file, %ld bytes\n", filename, (long)fileinfo.st_size); + + if ((ppd = ppdOpenFile(filename)) == NULL) + { + ppd_status_t err; /* Last error in file */ + int line; /* Line number in file */ + + + status ++; + err = ppdLastError(&line); + + printf("%s: %s on line %d\n", argv[1], ppdErrorString(err), line); + } + else + { + int j, k; /* Looping vars */ + ppd_group_t *group; /* Option group */ + ppd_option_t *option; /* Option */ + ppd_coption_t *coption; /* Custom option */ + ppd_cparam_t *cparam; /* Custom parameter */ + ppd_const_t *c; /* UIConstraints */ + char lang[255], /* LANG environment variable */ + lc_all[255], /* LC_ALL environment variable */ + lc_ctype[255], /* LC_CTYPE environment variable */ + lc_messages[255];/* LC_MESSAGES environment variable */ + + + if (argc > 2) + { + snprintf(lang, sizeof(lang), "LANG=%s", argv[2]); + putenv(lang); + snprintf(lc_all, sizeof(lc_all), "LC_ALL=%s", argv[2]); + putenv(lc_all); + snprintf(lc_ctype, sizeof(lc_ctype), "LC_CTYPE=%s", argv[2]); + putenv(lc_ctype); + snprintf(lc_messages, sizeof(lc_messages), "LC_MESSAGES=%s", argv[2]); + putenv(lc_messages); + } + + ppdLocalize(ppd); + ppdMarkDefaults(ppd); + + if (argc > 3) + { + text = ppdLocalizeIPPReason(ppd, argv[3], NULL, buffer, sizeof(buffer)); + printf("ppdLocalizeIPPReason(%s)=%s\n", argv[3], + text ? text : "(null)"); + return (text == NULL); + } + + for (i = ppd->num_groups, group = ppd->groups; + i > 0; + i --, group ++) + { + printf("%s (%s):\n", group->name, group->text); + + for (j = group->num_options, option = group->options; + j > 0; + j --, option ++) + { + printf(" %s (%s):\n", option->keyword, option->text); + + for (k = 0; k < option->num_choices; k ++) + printf(" - %s%s (%s)\n", + option->choices[k].marked ? "*" : "", + option->choices[k].choice, option->choices[k].text); + + if ((coption = ppdFindCustomOption(ppd, option->keyword)) != NULL) + { + for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); + cparam; + cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) + { + switch (cparam->type) + { + case PPD_CUSTOM_CURVE : + printf(" %s(%s): PPD_CUSTOM_CURVE (%g to %g)\n", + cparam->name, cparam->text, + cparam->minimum.custom_curve, + cparam->maximum.custom_curve); + break; + + case PPD_CUSTOM_INT : + printf(" %s(%s): PPD_CUSTOM_INT (%d to %d)\n", + cparam->name, cparam->text, + cparam->minimum.custom_int, + cparam->maximum.custom_int); + break; + + case PPD_CUSTOM_INVCURVE : + printf(" %s(%s): PPD_CUSTOM_INVCURVE (%g to %g)\n", + cparam->name, cparam->text, + cparam->minimum.custom_invcurve, + cparam->maximum.custom_invcurve); + break; + + case PPD_CUSTOM_PASSCODE : + printf(" %s(%s): PPD_CUSTOM_PASSCODE (%d to %d)\n", + cparam->name, cparam->text, + cparam->minimum.custom_passcode, + cparam->maximum.custom_passcode); + break; + + case PPD_CUSTOM_PASSWORD : + printf(" %s(%s): PPD_CUSTOM_PASSWORD (%d to %d)\n", + cparam->name, cparam->text, + cparam->minimum.custom_password, + cparam->maximum.custom_password); + break; + + case PPD_CUSTOM_POINTS : + printf(" %s(%s): PPD_CUSTOM_POINTS (%g to %g)\n", + cparam->name, cparam->text, + cparam->minimum.custom_points, + cparam->maximum.custom_points); + break; + + case PPD_CUSTOM_REAL : + printf(" %s(%s): PPD_CUSTOM_REAL (%g to %g)\n", + cparam->name, cparam->text, + cparam->minimum.custom_real, + cparam->maximum.custom_real); + break; + + case PPD_CUSTOM_STRING : + printf(" %s(%s): PPD_CUSTOM_STRING (%d to %d)\n", + cparam->name, cparam->text, + cparam->minimum.custom_string, + cparam->maximum.custom_string); + break; + } + } + } + } + } + + puts("\nSizes:"); + for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++) + printf(" %s = %gx%g, [%g %g %g %g]\n", size->name, size->width, + size->length, size->left, size->bottom, size->right, size->top); + + puts("\nConstraints:"); + + for (i = ppd->num_consts, c = ppd->consts; i > 0; i --, c ++) + printf(" *UIConstraints: *%s %s *%s %s\n", c->option1, c->choice1, + c->option2, c->choice2); + if (ppd->num_consts == 0) + puts(" NO CONSTRAINTS"); + + puts("\nFilters:"); + + for (i = 0; i < ppd->num_filters; i ++) + printf(" %s\n", ppd->filters[i]); + + if (ppd->num_filters == 0) + puts(" NO FILTERS"); + + puts("\nAttributes:"); + + for (attr = (ppd_attr_t *)cupsArrayFirst(ppd->sorted_attrs); + attr; + attr = (ppd_attr_t *)cupsArrayNext(ppd->sorted_attrs)) + printf(" *%s %s/%s: \"%s\"\n", attr->name, attr->spec, + attr->text, attr->value ? attr->value : ""); + } + + if (!strncmp(argv[1], "-d", 2)) + unlink(filename); + } + +#ifdef __APPLE__ + if (getenv("MallocStackLogging") && getenv("MallocStackLoggingNoCompact")) + { + char command[1024]; /* malloc_history command */ + + snprintf(command, sizeof(command), "malloc_history %d -all_by_size", + getpid()); + fflush(stdout); + system(command); + } +#endif /* __APPLE__ */ + + ppdClose(ppd); + + return (status); +} + + +/* + * End of "$Id: testppd.c 7897 2008-09-02 19:33:19Z mike $". + */ diff --git a/cups/testpwg.c b/cups/testpwg.c new file mode 100644 index 0000000..ef3279f --- /dev/null +++ b/cups/testpwg.c @@ -0,0 +1,525 @@ +/* + * "$Id: testpwg.c 3794 2012-04-23 22:44:16Z msweet $" + * + * PWG test program for CUPS. + * + * Copyright 2009-2012 by Apple Inc. + * + * 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. + * + * Contents: + * + * main() - Main entry. + * test_pagesize() - Test the PWG mapping functions. + * test_ppd_cache() - Test the PPD cache functions. + */ + +/* + * Include necessary headers... + */ + +#include "ppd-private.h" +#include "file-private.h" + + +/* + * Local functions... + */ + +static int test_pagesize(_ppd_cache_t *pc, ppd_file_t *ppd, + const char *ppdsize); +static int test_ppd_cache(_ppd_cache_t *pc, ppd_file_t *ppd); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line args */ + char *argv[]) /* I - Command-line arguments */ +{ + int status; /* Status of tests (0 = success, 1 = fail) */ + const char *ppdfile; /* PPD filename */ + ppd_file_t *ppd; /* PPD file */ + _ppd_cache_t *pc; /* PPD cache and PWG mapping data */ + _pwg_media_t *pwgmedia; /* PWG media size */ + + + status = 0; + + if (argc < 2 || argc > 3) + { + puts("Usage: ./testpwg filename.ppd [jobfile]"); + return (1); + } + + ppdfile = argv[1]; + + printf("ppdOpenFile(%s): ", ppdfile); + if ((ppd = ppdOpenFile(ppdfile)) == NULL) + { + ppd_status_t err; /* Last error in file */ + int line; /* Line number in file */ + + + err = ppdLastError(&line); + + printf("FAIL (%s on line %d)\n", ppdErrorString(err), line); + + return (1); + } + else + puts("PASS"); + + fputs("_ppdCacheCreateWithPPD(ppd): ", stdout); + if ((pc = _ppdCacheCreateWithPPD(ppd)) == NULL) + { + puts("FAIL"); + status ++; + } + else + { + puts("PASS"); + status += test_ppd_cache(pc, ppd); + + if (argc == 3) + { + /* + * Test PageSize mapping code. + */ + + int fd; /* Job file descriptor */ + const char *pagesize; /* PageSize value */ + ipp_t *job; /* Job attributes */ + ipp_attribute_t *media; /* Media attribute */ + + if ((fd = open(argv[2], O_RDONLY)) >= 0) + { + job = ippNew(); + ippReadFile(fd, job); + close(fd); + + if ((media = ippFindAttribute(job, "media", IPP_TAG_ZERO)) != NULL && + media->value_tag != IPP_TAG_NAME && + media->value_tag != IPP_TAG_KEYWORD) + media = NULL; + + if (media) + printf("_ppdCacheGetPageSize(media=%s): ", + media->values[0].string.text); + else + fputs("_ppdCacheGetPageSize(media-col): ", stdout); + + fflush(stdout); + + if ((pagesize = _ppdCacheGetPageSize(pc, job, NULL, NULL)) == NULL) + { + puts("FAIL (Not Found)"); + status = 1; + } + else if (media && _cups_strcasecmp(pagesize, media->values[0].string.text)) + { + printf("FAIL (Got \"%s\", Expected \"%s\")\n", pagesize, + media->values[0].string.text); + status = 1; + } + else + printf("PASS (%s)\n", pagesize); + + ippDelete(job); + } + else + { + perror(argv[2]); + status = 1; + } + } + + /* + * _ppdCacheDestroy should never fail... + */ + + fputs("_ppdCacheDestroy(pc): ", stdout); + _ppdCacheDestroy(pc); + puts("PASS"); + } + + fputs("_pwgMediaForPWG(\"iso_a4_210x297mm\"): ", stdout); + if ((pwgmedia = _pwgMediaForPWG("iso_a4_210x297mm")) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "iso_a4_210x297mm")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else if (pwgmedia->width != 21000 || pwgmedia->length != 29700) + { + printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length); + status ++; + } + else + puts("PASS"); + + fputs("_pwgMediaForLegacy(\"na-letter\"): ", stdout); + if ((pwgmedia = _pwgMediaForLegacy("na-letter")) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "na_letter_8.5x11in")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else if (pwgmedia->width != 21590 || pwgmedia->length != 27940) + { + printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length); + status ++; + } + else + puts("PASS"); + + fputs("_pwgMediaForPPD(\"4x6\"): ", stdout); + if ((pwgmedia = _pwgMediaForPPD("4x6")) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "na_index-4x6_4x6in")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else if (pwgmedia->width != 10160 || pwgmedia->length != 15240) + { + printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length); + status ++; + } + else + puts("PASS"); + + fputs("_pwgMediaForPPD(\"10x15cm\"): ", stdout); + if ((pwgmedia = _pwgMediaForPPD("10x15cm")) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "om_100x150mm_100x150mm")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else if (pwgmedia->width != 10000 || pwgmedia->length != 15000) + { + printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length); + status ++; + } + else + puts("PASS"); + + fputs("_pwgMediaForPPD(\"Custom.10x15cm\"): ", stdout); + if ((pwgmedia = _pwgMediaForPPD("Custom.10x15cm")) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "custom_10x15cm_100x150mm")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else if (pwgmedia->width != 10000 || pwgmedia->length != 15000) + { + printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length); + status ++; + } + else + puts("PASS"); + + fputs("_pwgMediaForSize(29700, 42000): ", stdout); + if ((pwgmedia = _pwgMediaForSize(29700, 42000)) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "iso_a3_297x420mm")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else + puts("PASS"); + + fputs("_pwgMediaForSize(9842, 19050): ", stdout); + if ((pwgmedia = _pwgMediaForSize(9842, 19050)) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "na_monarch_3.875x7.5in")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else + printf("PASS (%s)\n", pwgmedia->pwg); + + fputs("_pwgMediaForSize(9800, 19000): ", stdout); + if ((pwgmedia = _pwgMediaForSize(9800, 19000)) == NULL) + { + puts("FAIL (not found)"); + status ++; + } + else if (strcmp(pwgmedia->pwg, "jpn_you6_98x190mm")) + { + printf("FAIL (%s)\n", pwgmedia->pwg); + status ++; + } + else + printf("PASS (%s)\n", pwgmedia->pwg); + + return (status); +} + + +/* + * 'test_pagesize()' - Test the PWG mapping functions. + */ + +static int /* O - 1 on failure, 0 on success */ +test_pagesize(_ppd_cache_t *pc, /* I - PWG mapping data */ + ppd_file_t *ppd, /* I - PPD file */ + const char *ppdsize) /* I - PPD page size */ +{ + int status = 0; /* Return status */ + ipp_t *job; /* Job attributes */ + const char *pagesize; /* PageSize value */ + + + if (ppdPageSize(ppd, ppdsize)) + { + printf("_ppdCacheGetPageSize(keyword=%s): ", ppdsize); + fflush(stdout); + + if ((pagesize = _ppdCacheGetPageSize(pc, NULL, ppdsize, NULL)) == NULL) + { + puts("FAIL (Not Found)"); + status = 1; + } + else if (_cups_strcasecmp(pagesize, ppdsize)) + { + printf("FAIL (Got \"%s\", Expected \"%s\")\n", pagesize, ppdsize); + status = 1; + } + else + puts("PASS"); + + job = ippNew(); + ippAddString(job, IPP_TAG_JOB, IPP_TAG_KEYWORD, "media", NULL, ppdsize); + + printf("_ppdCacheGetPageSize(media=%s): ", ppdsize); + fflush(stdout); + + if ((pagesize = _ppdCacheGetPageSize(pc, job, NULL, NULL)) == NULL) + { + puts("FAIL (Not Found)"); + status = 1; + } + else if (_cups_strcasecmp(pagesize, ppdsize)) + { + printf("FAIL (Got \"%s\", Expected \"%s\")\n", pagesize, ppdsize); + status = 1; + } + else + puts("PASS"); + + ippDelete(job); + } + + return (status); +} + + +/* + * 'test_ppd_cache()' - Test the PPD cache functions. + */ + +static int /* O - 1 on failure, 0 on success */ +test_ppd_cache(_ppd_cache_t *pc, /* I - PWG mapping data */ + ppd_file_t *ppd) /* I - PPD file */ +{ + int i, /* Looping var */ + status = 0; /* Return status */ + _ppd_cache_t *pc2; /* Loaded data */ + _pwg_size_t *size, /* Size from original */ + *size2; /* Size from saved */ + _pwg_map_t *map, /* Map from original */ + *map2; /* Map from saved */ + + + /* + * Verify that we can write and read back the same data... + */ + + fputs("_ppdCacheWriteFile(test.pwg): ", stdout); + if (!_ppdCacheWriteFile(pc, "test.pwg", NULL)) + { + puts("FAIL"); + status ++; + } + else + puts("PASS"); + + fputs("_ppdCacheCreateWithFile(test.pwg): ", stdout); + if ((pc2 = _ppdCacheCreateWithFile("test.pwg", NULL)) == NULL) + { + puts("FAIL"); + status ++; + } + else + { + // TODO: FINISH ADDING ALL VALUES IN STRUCTURE + if (pc2->num_sizes != pc->num_sizes) + { + if (!status) + puts("FAIL"); + + printf(" SAVED num_sizes=%d, ORIG num_sizes=%d\n", pc2->num_sizes, + pc->num_sizes); + + status ++; + } + else + { + for (i = pc->num_sizes, size = pc->sizes, size2 = pc2->sizes; + i > 0; + i --, size ++, size2 ++) + { + if (strcmp(size2->map.pwg, size->map.pwg) || + strcmp(size2->map.ppd, size->map.ppd) || + size2->width != size->width || + size2->length != size->length || + size2->left != size->left || + size2->bottom != size->bottom || + size2->right != size->right || + size2->top != size->top) + { + if (!status) + puts("FAIL"); + + if (strcmp(size->map.pwg, size2->map.pwg)) + printf(" SAVED size->map.pwg=\"%s\", ORIG " + "size->map.pwg=\"%s\"\n", size2->map.pwg, size->map.pwg); + + if (strcmp(size2->map.ppd, size->map.ppd)) + printf(" SAVED size->map.ppd=\"%s\", ORIG " + "size->map.ppd=\"%s\"\n", size2->map.ppd, size->map.ppd); + + if (size2->width != size->width) + printf(" SAVED size->width=%d, ORIG size->width=%d\n", + size2->width, size->width); + + if (size2->length != size->length) + printf(" SAVED size->length=%d, ORIG size->length=%d\n", + size2->length, size->length); + + if (size2->left != size->left) + printf(" SAVED size->left=%d, ORIG size->left=%d\n", + size2->left, size->left); + + if (size2->bottom != size->bottom) + printf(" SAVED size->bottom=%d, ORIG size->bottom=%d\n", + size2->bottom, size->bottom); + + if (size2->right != size->right) + printf(" SAVED size->right=%d, ORIG size->right=%d\n", + size2->right, size->right); + + if (size2->top != size->top) + printf(" SAVED size->top=%d, ORIG size->top=%d\n", + size2->top, size->top); + + status ++; + break; + } + } + + for (i = pc->num_sources, map = pc->sources, map2 = pc2->sources; + i > 0; + i --, map ++, map2 ++) + { + if (strcmp(map2->pwg, map->pwg) || + strcmp(map2->ppd, map->ppd)) + { + if (!status) + puts("FAIL"); + + if (strcmp(map->pwg, map2->pwg)) + printf(" SAVED source->pwg=\"%s\", ORIG source->pwg=\"%s\"\n", + map2->pwg, map->pwg); + + if (strcmp(map2->ppd, map->ppd)) + printf(" SAVED source->ppd=\"%s\", ORIG source->ppd=\"%s\"\n", + map2->ppd, map->ppd); + + status ++; + break; + } + } + + for (i = pc->num_types, map = pc->types, map2 = pc2->types; + i > 0; + i --, map ++, map2 ++) + { + if (strcmp(map2->pwg, map->pwg) || + strcmp(map2->ppd, map->ppd)) + { + if (!status) + puts("FAIL"); + + if (strcmp(map->pwg, map2->pwg)) + printf(" SAVED type->pwg=\"%s\", ORIG type->pwg=\"%s\"\n", + map2->pwg, map->pwg); + + if (strcmp(map2->ppd, map->ppd)) + printf(" SAVED type->ppd=\"%s\", ORIG type->ppd=\"%s\"\n", + map2->ppd, map->ppd); + + status ++; + break; + } + } + } + + if (!status) + puts("PASS"); + + _ppdCacheDestroy(pc2); + } + + /* + * Test PageSize mapping code... + */ + + status += test_pagesize(pc, ppd, "Letter"); + status += test_pagesize(pc, ppd, "na-letter"); + status += test_pagesize(pc, ppd, "A4"); + status += test_pagesize(pc, ppd, "iso-a4"); + + return (status); +} + + +/* + * End of "$Id: testpwg.c 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/testsnmp.c b/cups/testsnmp.c new file mode 100644 index 0000000..b60c2b7 --- /dev/null +++ b/cups/testsnmp.c @@ -0,0 +1,304 @@ +/* + * "$Id: testsnmp.c 3411 2011-09-07 22:31:27Z msweet $" + * + * SNMP test program for CUPS. + * + * Copyright 2008-2010 by Apple Inc. + * + * 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. + * + * Contents: + * + * main() - Main entry. + * scan_oid() - Scan an OID value. + * show_oid() - Show the specified OID. + * usage() - Show program usage and exit. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include "snmp-private.h" + + +/* + * Local functions... + */ + +static void print_packet(cups_snmp_t *packet, void *data); +static int show_oid(int fd, const char *community, + http_addr_t *addr, const char *s, int walk); +static void usage(void) __attribute__((noreturn)); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line args */ + char *argv[]) /* I - Command-line arguments */ +{ + int i; /* Looping var */ + int fd = -1; /* SNMP socket */ + http_addrlist_t *host = NULL; /* Address of host */ + int walk = 0; /* Walk OIDs? */ + char *oid = NULL; /* Last OID shown */ + const char *community; /* Community name */ + + + fputs("_cupsSNMPDefaultCommunity: ", stdout); + + if ((community = _cupsSNMPDefaultCommunity()) == NULL) + { + puts("FAIL (NULL community name)"); + return (1); + } + + printf("PASS (%s)\n", community); + + /* + * Query OIDs from the command-line... + */ + + for (i = 1; i < argc; i ++) + if (!strcmp(argv[i], "-c")) + { + i ++; + + if (i >= argc) + usage(); + else + community = argv[i]; + } + else if (!strcmp(argv[i], "-d")) + _cupsSNMPSetDebug(10); + else if (!strcmp(argv[i], "-w")) + walk = 1; + else if (!host) + { + if ((host = httpAddrGetList(argv[i], AF_UNSPEC, "161")) == NULL) + { + printf("testsnmp: Unable to find \"%s\"!\n", argv[1]); + return (1); + } + + if (fd < 0) + { + fputs("_cupsSNMPOpen: ", stdout); + + if ((fd = _cupsSNMPOpen(host->addr.addr.sa_family)) < 0) + { + printf("FAIL (%s)\n", strerror(errno)); + return (1); + } + + puts("PASS"); + } + } + else if (!show_oid(fd, community, &(host->addr), argv[i], walk)) + return (1); + else + oid = argv[i]; + + if (!host) + usage(); + + if (!oid) + { + if (!show_oid(fd, community, &(host->addr), + walk ? ".1.3.6.1.2.1.43" : + ".1.3.6.1.2.1.43.10.2.1.4.1.1", walk)) + return (1); + } + + return (0); +} + + +/* + * 'print_packet()' - Print the contents of the response packet. + */ + +static void +print_packet(cups_snmp_t *packet, /* I - SNMP response packet */ + void *data) /* I - User data pointer (not used) */ +{ + int i; /* Looping var */ + char temp[1024]; /* Temporary OID string */ + + + (void)data; + + printf("%s = ", _cupsSNMPOIDToString(packet->object_name, temp, sizeof(temp))); + + switch (packet->object_type) + { + case CUPS_ASN1_BOOLEAN : + printf("BOOLEAN %s\n", + packet->object_value.boolean ? "TRUE" : "FALSE"); + break; + + case CUPS_ASN1_INTEGER : + printf("INTEGER %d\n", packet->object_value.integer); + break; + + case CUPS_ASN1_BIT_STRING : + printf("BIT-STRING \"%s\"\n", + (char *)packet->object_value.string.bytes); + break; + + case CUPS_ASN1_OCTET_STRING : + printf("OCTET-STRING \"%s\"\n", + (char *)packet->object_value.string.bytes); + break; + + case CUPS_ASN1_NULL_VALUE : + puts("NULL-VALUE"); + break; + + case CUPS_ASN1_OID : + printf("OID %s\n", _cupsSNMPOIDToString(packet->object_value.oid, + temp, sizeof(temp))); + break; + + case CUPS_ASN1_HEX_STRING : + fputs("Hex-STRING", stdout); + for (i = 0; i < packet->object_value.string.num_bytes; i ++) + printf(" %02X", packet->object_value.string.bytes[i]); + putchar('\n'); + break; + + case CUPS_ASN1_COUNTER : + printf("Counter %d\n", packet->object_value.counter); + break; + + case CUPS_ASN1_GAUGE : + printf("Gauge %u\n", packet->object_value.gauge); + break; + + case CUPS_ASN1_TIMETICKS : + printf("Timeticks %u days, %u:%02u:%02u.%02u\n", + packet->object_value.timeticks / 8640000, + (packet->object_value.timeticks / 360000) % 24, + (packet->object_value.timeticks / 6000) % 60, + (packet->object_value.timeticks / 100) % 60, + packet->object_value.timeticks % 100); + break; + + default : + printf("Unknown-%X\n", packet->object_type); + break; + } +} + + +/* + * 'show_oid()' - Show the specified OID. + */ + +static int /* O - 1 on success, 0 on error */ +show_oid(int fd, /* I - SNMP socket */ + const char *community, /* I - Community name */ + http_addr_t *addr, /* I - Address to query */ + const char *s, /* I - OID to query */ + int walk) /* I - Walk OIDs? */ +{ + int i; /* Looping var */ + int oid[CUPS_SNMP_MAX_OID]; /* OID */ + cups_snmp_t packet; /* SNMP packet */ + char temp[1024]; /* Temporary OID string */ + + + if (!_cupsSNMPStringToOID(s, oid, sizeof(oid) / sizeof(oid[0]))) + { + puts("testsnmp: Bad OID"); + return (0); + } + + if (walk) + { + printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp))); + + if (_cupsSNMPWalk(fd, addr, CUPS_SNMP_VERSION_1, community, oid, 5.0, + print_packet, NULL) < 0) + { + printf("FAIL (%s)\n", strerror(errno)); + return (0); + } + } + else + { + printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp))); + + if (!_cupsSNMPWrite(fd, addr, CUPS_SNMP_VERSION_1, community, + CUPS_ASN1_GET_REQUEST, 1, oid)) + { + printf("FAIL (%s)\n", strerror(errno)); + return (0); + } + + puts("PASS"); + + fputs("_cupsSNMPRead(5.0): ", stdout); + + if (!_cupsSNMPRead(fd, &packet, 5.0)) + { + puts("FAIL (timeout)"); + return (0); + } + + if (!_cupsSNMPIsOID(&packet, oid)) + { + printf("FAIL (bad OID %d", packet.object_name[0]); + for (i = 1; packet.object_name[i] >= 0; i ++) + printf(".%d", packet.object_name[i]); + puts(")"); + return (0); + } + + if (packet.error) + { + printf("FAIL (%s)\n", packet.error); + return (0); + } + + puts("PASS"); + + print_packet(&packet, NULL); + } + + return (1); +} + + +/* + * 'usage()' - Show program usage and exit. + */ + +static void +usage(void) +{ + puts("Usage: testsnmp [options] host-or-ip [oid ...]"); + puts(""); + puts("Options:"); + puts(""); + puts(" -c community Set community name"); + puts(" -d Enable debugging"); + puts(" -w Walk all OIDs under the specified one"); + + exit (1); +} + + +/* + * End of "$Id: testsnmp.c 3411 2011-09-07 22:31:27Z msweet $". + */ diff --git a/cups/thread-private.h b/cups/thread-private.h new file mode 100644 index 0000000..3409be4 --- /dev/null +++ b/cups/thread-private.h @@ -0,0 +1,98 @@ +/* + * "$Id: thread-private.h 3794 2012-04-23 22:44:16Z msweet $" + * + * Private threading definitions for CUPS. + * + * Copyright 2009-2012 by Apple Inc. + * + * 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/". + */ + +#ifndef _CUPS_THREAD_PRIVATE_H_ +# define _CUPS_THREAD_PRIVATE_H_ + +/* + * Include necessary headers... + */ + +# include "config.h" + + +/* + * C++ magic... + */ + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +# ifdef HAVE_PTHREAD_H +# include +typedef void *(*_cups_thread_func_t)(void *arg); +typedef pthread_mutex_t _cups_mutex_t; +typedef pthread_rwlock_t _cups_rwlock_t; +typedef pthread_key_t _cups_threadkey_t; +# define _CUPS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER +# define _CUPS_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER +# define _CUPS_THREADKEY_INITIALIZER -1 +# define _cupsThreadGetData(k) pthread_getspecific(k) +# define _cupsThreadSetData(k,p) pthread_setspecific(k,p) + +# elif defined(WIN32) +# include +# include +typedef void *(__stdcall *_cups_thread_func_t)(void *arg); +typedef struct _cups_mutex_s +{ + int m_init; /* Flag for on-demand initialization */ + CRITICAL_SECTION m_criticalSection; + /* Win32 Critical Section */ +} _cups_mutex_t; +typedef _cups_mutex_t _cups_rwlock_t; /* TODO: Implement Win32 reader/writer lock */ +typedef DWORD _cups_threadkey_t; +# define _CUPS_MUTEX_INITIALIZER { 0, 0 } +# define _CUPS_RWLOCK_INITIALIZER { 0, 0 } +# define _CUPS_THREADKEY_INITIALIZER 0 +# define _cupsThreadGetData(k) TlsGetValue(k) +# define _cupsThreadSetData(k,p) TlsSetValue(k,p) + +# else +typedef void *(*_cups_thread_func_t)(void *arg); +typedef char _cups_mutex_t; +typedef char _cups_rwlock_t; +typedef void *_cups_threadkey_t; +# define _CUPS_MUTEX_INITIALIZER 0 +# define _CUPS_RWLOCK_INITIALIZER 0 +# define _CUPS_THREADKEY_INITIALIZER (void *)0 +# define _cupsThreadGetData(k) k +# define _cupsThreadSetData(k,p) k=p +# endif /* HAVE_PTHREAD_H */ + + +/* + * Functions... + */ + +extern void _cupsMutexInit(_cups_mutex_t *mutex); +extern void _cupsMutexLock(_cups_mutex_t *mutex); +extern void _cupsMutexUnlock(_cups_mutex_t *mutex); +extern void _cupsRWInit(_cups_rwlock_t *rwlock); +extern void _cupsRWLockRead(_cups_rwlock_t *rwlock); +extern void _cupsRWLockWrite(_cups_rwlock_t *rwlock); +extern void _cupsRWUnlock(_cups_rwlock_t *rwlock); +extern int _cupsThreadCreate(_cups_thread_func_t func, void *arg); + + +# ifdef __cplusplus +} +# endif /* __cplusplus */ +#endif /* !_CUPS_THREAD_PRIVATE_H_ */ + +/* + * End of "$Id: thread-private.h 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/thread.c b/cups/thread.c new file mode 100644 index 0000000..0f62329 --- /dev/null +++ b/cups/thread.c @@ -0,0 +1,336 @@ +/* + * "$Id: thread.c 3794 2012-04-23 22:44:16Z msweet $" + * + * Threading primitives for CUPS. + * + * Copyright 2009-2012 by Apple Inc. + * + * 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/". + * + * Contents: + * + * _cupsMutexInit() - Initialize a mutex. + * _cupsMutexLock() - Lock a mutex. + * _cupsMutexUnlock() - Unlock a mutex. + * _cupsRWInit() - Initialize a reader/writer lock. + * _cupsRWLockRead() - Acquire a reader/writer lock for reading. + * _cupsRWLockWrite() - Acquire a reader/writer lock for writing. + * _cupsRWUnlock() - Release a reader/writer lock. + * _cupsThreadCreate() - Create a thread. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include "thread-private.h" + + +#if defined(HAVE_PTHREAD_H) +/* + * '_cupsMutexInit()' - Initialize a mutex. + */ + +void +_cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */ +{ + pthread_mutex_init(mutex, NULL); +} + + +/* + * '_cupsMutexLock()' - Lock a mutex. + */ + +void +_cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */ +{ + pthread_mutex_lock(mutex); +} + + +/* + * '_cupsMutexUnlock()' - Unlock a mutex. + */ + +void +_cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */ +{ + pthread_mutex_unlock(mutex); +} + + +/* + * '_cupsRWInit()' - Initialize a reader/writer lock. + */ + +void +_cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + pthread_rwlock_init(rwlock, NULL); +} + + +/* + * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading. + */ + +void +_cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + pthread_rwlock_rdlock(rwlock); +} + + +/* + * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing. + */ + +void +_cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */ +{ + pthread_rwlock_wrlock(rwlock); +} + + +/* + * '_cupsRWUnlock()' - Release a reader/writer lock. + */ + +void +_cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + pthread_rwlock_unlock(rwlock); +} + + +/* + * '_cupsThreadCreate()' - Create a thread. + */ + +int /* O - 0 on failure, 1 on success */ +_cupsThreadCreate( + _cups_thread_func_t func, /* I - Entry point */ + void *arg) /* I - Entry point context */ +{ + pthread_t thread; + + return (pthread_create(&thread, NULL, (void *(*)(void *))func, arg) == 0); +} + + +#elif defined(WIN32) +# include + + +/* + * '_cupsMutexInit()' - Initialize a mutex. + */ + +void +_cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */ +{ + InitializeCriticalSection(&mutex->m_criticalSection); + mutex->m_init = 1; +} + + +/* + * '_cupsMutexLock()' - Lock a mutex. + */ + +void +_cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */ +{ + if (!mutex->m_init) + { + _cupsGlobalLock(); + + if (!mutex->m_init) + { + InitializeCriticalSection(&mutex->m_criticalSection); + mutex->m_init = 1; + } + + _cupsGlobalUnlock(); + } + + EnterCriticalSection(&mutex->m_criticalSection); +} + + +/* + * '_cupsMutexUnlock()' - Unlock a mutex. + */ + +void +_cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */ +{ + LeaveCriticalSection(&mutex->m_criticalSection); +} + + +/* + * '_cupsRWInit()' - Initialize a reader/writer lock. + */ + +void +_cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + _cupsMutexInit((_cups_mutex_t *)rwlock); +} + + +/* + * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading. + */ + +void +_cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + _cupsMutexLock((_cups_mutex_t *)rwlock); +} + + +/* + * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing. + */ + +void +_cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */ +{ + _cupsMutexLock((_cups_mutex_t *)rwlock); +} + + +/* + * '_cupsRWUnlock()' - Release a reader/writer lock. + */ + +void +_cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + _cupsMutexUnlock((_cups_mutex_t *)rwlock); +} + + +/* + * '_cupsThreadCreate()' - Create a thread. + */ + +int /* O - 0 on failure, 1 on success */ +_cupsThreadCreate( + _cups_thread_func_t func, /* I - Entry point */ + void *arg) /* I - Entry point context */ +{ + return (_beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL) + != 0); +} + + +#else +/* + * '_cupsMutexInit()' - Initialize a mutex. + */ + +void +_cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */ +{ + (void)mutex; +} + + +/* + * '_cupsMutexLock()' - Lock a mutex. + */ + +void +_cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */ +{ + (void)mutex; +} + + +/* + * '_cupsMutexUnlock()' - Unlock a mutex. + */ + +void +_cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */ +{ + (void)mutex; +} + + +/* + * '_cupsRWInit()' - Initialize a reader/writer lock. + */ + +void +_cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + (void)rwlock; +} + + +/* + * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading. + */ + +void +_cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + (void)rwlock; +} + + +/* + * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing. + */ + +void +_cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */ +{ + (void)rwlock; +} + + +/* + * '_cupsRWUnlock()' - Release a reader/writer lock. + */ + +void +_cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ +{ + (void)rwlock; +} + + +/* + * '_cupsThreadCreate()' - Create a thread. + */ + +int /* O - 0 on failure, 1 on success */ +_cupsThreadCreate( + _cups_thread_func_t func, /* I - Entry point */ + void *arg) /* I - Entry point context */ +{ + fputs("DEBUG: CUPS was compiled without threading support, no thread " + "created.\n", stderr); + + (void)func; + (void)arg; + + return (0); +} +#endif /* HAVE_PTHREAD_H */ + + +/* + * End of "$Id: thread.c 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/cups/transcode.c b/cups/transcode.c new file mode 100644 index 0000000..8c58dab --- /dev/null +++ b/cups/transcode.c @@ -0,0 +1,720 @@ +/* + * "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $" + * + * Transcoding support for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * _cupsCharmapFlush() - Flush all character set maps out of cache. + * cupsCharsetToUTF8() - Convert legacy character set to UTF-8. + * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set. + * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32. + * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#ifdef HAVE_ICONV_H +# include +#endif /* HAVE_ICONV_H */ + + +/* + * Local globals... + */ + +#ifdef HAVE_ICONV_H +static _cups_mutex_t map_mutex = _CUPS_MUTEX_INITIALIZER; + /* Mutex to control access to maps */ +static iconv_t map_from_utf8 = (iconv_t)-1; + /* Convert from UTF-8 to charset */ +static iconv_t map_to_utf8 = (iconv_t)-1; + /* Convert from charset to UTF-8 */ +static cups_encoding_t map_encoding = CUPS_AUTO_ENCODING; + /* Which charset is cached */ +#endif /* HAVE_ICONV_H */ + + +/* + * '_cupsCharmapFlush()' - Flush all character set maps out of cache. + */ + +void +_cupsCharmapFlush(void) +{ +#ifdef HAVE_ICONV_H + if (map_from_utf8 != (iconv_t)-1) + { + iconv_close(map_from_utf8); + map_from_utf8 = (iconv_t)-1; + } + + if (map_to_utf8 != (iconv_t)-1) + { + iconv_close(map_to_utf8); + map_to_utf8 = (iconv_t)-1; + } + + map_encoding = CUPS_AUTO_ENCODING; +#endif /* HAVE_ICONV_H */ +} + + +/* + * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8. + */ + +int /* O - Count or -1 on error */ +cupsCharsetToUTF8( + cups_utf8_t *dest, /* O - Target string */ + const char *src, /* I - Source string */ + const int maxout, /* I - Max output */ + const cups_encoding_t encoding) /* I - Encoding */ +{ + cups_utf8_t *destptr; /* Pointer into UTF-8 buffer */ +#ifdef HAVE_ICONV_H + size_t srclen, /* Length of source string */ + outBytesLeft; /* Bytes remaining in output buffer */ +#endif /* HAVE_ICONV_H */ + + + /* + * Check for valid arguments... + */ + + DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)", + dest, src, maxout, encoding)); + + if (!dest || !src || maxout < 1) + { + if (dest) + *dest = '\0'; + + DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1"); + return (-1); + } + + /* + * Handle identity conversions... + */ + + if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII || + encoding >= CUPS_ENCODING_VBCS_END) + { + strlcpy((char *)dest, src, maxout); + return ((int)strlen((char *)dest)); + } + + /* + * Handle ISO-8859-1 to UTF-8 directly... + */ + + destptr = dest; + + if (encoding == CUPS_ISO8859_1) + { + int ch; /* Character from string */ + cups_utf8_t *destend; /* End of UTF-8 buffer */ + + + destend = dest + maxout - 2; + + while (*src && destptr < destend) + { + ch = *src++ & 255; + + if (ch & 128) + { + *destptr++ = 0xc0 | (ch >> 6); + *destptr++ = 0x80 | (ch & 0x3f); + } + else + *destptr++ = ch; + } + + *destptr = '\0'; + + return ((int)(destptr - dest)); + } + + /* + * Convert input legacy charset to UTF-8... + */ + +#ifdef HAVE_ICONV_H + _cupsMutexLock(&map_mutex); + + if (map_encoding != encoding) + { + _cupsCharmapFlush(); + + map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8"); + map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding)); + map_encoding = encoding; + } + + if (map_to_utf8 != (iconv_t)-1) + { + char *altdestptr = (char *)dest; /* Silence bogus GCC type-punned */ + + srclen = strlen(src); + outBytesLeft = maxout - 1; + + iconv(map_to_utf8, (char **)&src, &srclen, &altdestptr, &outBytesLeft); + *altdestptr = '\0'; + + _cupsMutexUnlock(&map_mutex); + + return ((int)(altdestptr - (char *)dest)); + } + + _cupsMutexUnlock(&map_mutex); +#endif /* HAVE_ICONV_H */ + + /* + * No iconv() support, so error out... + */ + + *destptr = '\0'; + + return (-1); +} + + +/* + * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set. + */ + +int /* O - Count or -1 on error */ +cupsUTF8ToCharset( + char *dest, /* O - Target string */ + const cups_utf8_t *src, /* I - Source string */ + const int maxout, /* I - Max output */ + const cups_encoding_t encoding) /* I - Encoding */ +{ + char *destptr; /* Pointer into destination */ +#ifdef HAVE_ICONV_H + size_t srclen, /* Length of source string */ + outBytesLeft; /* Bytes remaining in output buffer */ +#endif /* HAVE_ICONV_H */ + + + /* + * Check for valid arguments... + */ + + if (!dest || !src || maxout < 1) + { + if (dest) + *dest = '\0'; + + return (-1); + } + + /* + * Handle identity conversions... + */ + + if (encoding == CUPS_UTF8 || + encoding >= CUPS_ENCODING_VBCS_END) + { + strlcpy(dest, (char *)src, maxout); + return ((int)strlen(dest)); + } + + /* + * Handle UTF-8 to ISO-8859-1 directly... + */ + + destptr = dest; + + if (encoding == CUPS_ISO8859_1 || encoding <= CUPS_US_ASCII) + { + int ch, /* Character from string */ + maxch; /* Maximum character for charset */ + char *destend; /* End of ISO-8859-1 buffer */ + + maxch = encoding == CUPS_ISO8859_1 ? 256 : 128; + destend = dest + maxout - 1; + + while (*src && destptr < destend) + { + ch = *src++; + + if ((ch & 0xe0) == 0xc0) + { + ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f); + + if (ch < maxch) + *destptr++ = ch; + else + *destptr++ = '?'; + } + else if ((ch & 0xf0) == 0xe0 || + (ch & 0xf8) == 0xf0) + *destptr++ = '?'; + else if (!(ch & 0x80)) + *destptr++ = ch; + } + + *destptr = '\0'; + + return ((int)(destptr - dest)); + } + +#ifdef HAVE_ICONV_H + /* + * Convert input UTF-8 to legacy charset... + */ + + _cupsMutexLock(&map_mutex); + + if (map_encoding != encoding) + { + _cupsCharmapFlush(); + + map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8"); + map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding)); + map_encoding = encoding; + } + + if (map_from_utf8 != (iconv_t)-1) + { + char *altsrc = (char *)src; /* Silence bogus GCC type-punned */ + + srclen = strlen((char *)src); + outBytesLeft = maxout - 1; + + iconv(map_from_utf8, &altsrc, &srclen, &destptr, &outBytesLeft); + *destptr = '\0'; + + _cupsMutexUnlock(&map_mutex); + + return ((int)(destptr - dest)); + } + + _cupsMutexUnlock(&map_mutex); +#endif /* HAVE_ICONV_H */ + + /* + * No iconv() support, so error out... + */ + + *destptr = '\0'; + + return (-1); +} + + +/* + * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32. + * + * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows... + * + * UTF-32 char UTF-8 char(s) + * -------------------------------------------------- + * 0 to 127 = 0xxxxxxx (US-ASCII) + * 128 to 2047 = 110xxxxx 10yyyyyy + * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz + * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx + * + * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4, + * which would convert to five- or six-octet UTF-8 sequences... + */ + +int /* O - Count or -1 on error */ +cupsUTF8ToUTF32( + cups_utf32_t *dest, /* O - Target string */ + const cups_utf8_t *src, /* I - Source string */ + const int maxout) /* I - Max output */ +{ + int i; /* Looping variable */ + cups_utf8_t ch; /* Character value */ + cups_utf8_t next; /* Next character value */ + cups_utf32_t ch32; /* UTF-32 character value */ + + + /* + * Check for valid arguments and clear output... + */ + + DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest, + src, maxout)); + + if (dest) + *dest = 0; + + if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)"); + + return (-1); + } + + /* + * Convert input UTF-8 to output UTF-32... + */ + + for (i = maxout - 1; *src && i > 0; i --) + { + ch = *src++; + + /* + * Convert UTF-8 character(s) to UTF-32 character... + */ + + if (!(ch & 0x80)) + { + /* + * One-octet UTF-8 <= 127 (US-ASCII)... + */ + + *dest++ = ch; + + DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src[-1], ch)); + continue; + } + else if ((ch & 0xe0) == 0xc0) + { + /* + * Two-octet UTF-8 <= 2047 (Latin-x)... + */ + + next = *src++; + if ((next & 0xc0) != 0x80) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + ch32 = ((ch & 0x1f) << 6) | (next & 0x3f); + + /* + * Check for non-shortest form (invalid UTF-8)... + */ + + if (ch32 < 0x80) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + *dest++ = ch32; + + DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X", + src[-2], src[-1], (unsigned)ch32)); + } + else if ((ch & 0xf0) == 0xe0) + { + /* + * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)... + */ + + next = *src++; + if ((next & 0xc0) != 0x80) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + ch32 = ((ch & 0x0f) << 6) | (next & 0x3f); + + next = *src++; + if ((next & 0xc0) != 0x80) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + ch32 = (ch32 << 6) | (next & 0x3f); + + /* + * Check for non-shortest form (invalid UTF-8)... + */ + + if (ch32 < 0x800) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + *dest++ = ch32; + + DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X", + src[-3], src[-2], src[-1], (unsigned)ch32)); + } + else if ((ch & 0xf8) == 0xf0) + { + /* + * Four-octet UTF-8... + */ + + next = *src++; + if ((next & 0xc0) != 0x80) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + ch32 = ((ch & 0x07) << 6) | (next & 0x3f); + + next = *src++; + if ((next & 0xc0) != 0x80) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + ch32 = (ch32 << 6) | (next & 0x3f); + + next = *src++; + if ((next & 0xc0) != 0x80) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + ch32 = (ch32 << 6) | (next & 0x3f); + + /* + * Check for non-shortest form (invalid UTF-8)... + */ + + if (ch32 < 0x10000) + { + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + *dest++ = ch32; + + DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X", + src[-4], src[-3], src[-2], src[-1], (unsigned)ch32)); + } + else + { + /* + * More than 4-octet (invalid UTF-8 sequence)... + */ + + DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)"); + + return (-1); + } + + /* + * Check for UTF-16 surrogate (illegal UTF-8)... + */ + + if (ch32 >= 0xd800 && ch32 <= 0xdfff) + return (-1); + } + + *dest = 0; + + DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout - 1 - i)); + + return (maxout - 1 - i); +} + + +/* + * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8. + * + * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows... + * + * UTF-32 char UTF-8 char(s) + * -------------------------------------------------- + * 0 to 127 = 0xxxxxxx (US-ASCII) + * 128 to 2047 = 110xxxxx 10yyyyyy + * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz + * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx + * + * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4, + * which would convert to five- or six-octet UTF-8 sequences... + */ + +int /* O - Count or -1 on error */ +cupsUTF32ToUTF8( + cups_utf8_t *dest, /* O - Target string */ + const cups_utf32_t *src, /* I - Source string */ + const int maxout) /* I - Max output */ +{ + cups_utf8_t *start; /* Start of destination string */ + int i; /* Looping variable */ + int swap; /* Byte-swap input to output */ + cups_utf32_t ch; /* Character value */ + + + /* + * Check for valid arguments and clear output... + */ + + DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest, src, + maxout)); + + if (dest) + *dest = '\0'; + + if (!dest || !src || maxout < 1) + { + DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)"); + + return (-1); + } + + /* + * Check for leading BOM in UTF-32 and inverted BOM... + */ + + start = dest; + swap = *src == 0xfffe0000; + + DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap)); + + if (*src == 0xfffe0000 || *src == 0xfeff) + src ++; + + /* + * Convert input UTF-32 to output UTF-8... + */ + + for (i = maxout - 1; *src && i > 0;) + { + ch = *src++; + + /* + * Byte swap input UTF-32, if necessary... + * (only byte-swapping 24 of 32 bits) + */ + + if (swap) + ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000)); + + /* + * Check for beyond Plane 16 (invalid UTF-32)... + */ + + if (ch > 0x10ffff) + { + DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)"); + + return (-1); + } + + /* + * Convert UTF-32 character to UTF-8 character(s)... + */ + + if (ch < 0x80) + { + /* + * One-octet UTF-8 <= 127 (US-ASCII)... + */ + + *dest++ = (cups_utf8_t)ch; + i --; + + DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch, dest[-1])); + } + else if (ch < 0x800) + { + /* + * Two-octet UTF-8 <= 2047 (Latin-x)... + */ + + if (i < 2) + { + DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)"); + + return (-1); + } + + *dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f)); + *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f)); + i -= 2; + + DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch, + dest[-2], dest[-1])); + } + else if (ch < 0x10000) + { + /* + * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)... + */ + + if (i < 3) + { + DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)"); + + return (-1); + } + + *dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f)); + *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f)); + *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f)); + i -= 3; + + DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch, + dest[-3], dest[-2], dest[-1])); + } + else + { + /* + * Four-octet UTF-8... + */ + + if (i < 4) + { + DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)"); + + return (-1); + } + + *dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07)); + *dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f)); + *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f)); + *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f)); + i -= 4; + + DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x", + (unsigned)ch, dest[-4], dest[-3], dest[-2], dest[-1])); + } + } + + *dest = '\0'; + + DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest - start))); + + return ((int)(dest - start)); +} + + +/* + * End of "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $" + */ diff --git a/cups/transcode.h b/cups/transcode.h new file mode 100644 index 0000000..e2db951 --- /dev/null +++ b/cups/transcode.h @@ -0,0 +1,81 @@ +/* + * "$Id: transcode.h 7026 2007-10-19 00:57:45Z mike $" + * + * Transcoding definitions for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + */ + +#ifndef _CUPS_TRANSCODE_H_ +# define _CUPS_TRANSCODE_H_ + +/* + * Include necessary headers... + */ + +# include "language.h" + +# ifdef __cplusplus +extern "C" { +# endif /* __cplusplus */ + + +/* + * Constants... + */ + +# define CUPS_MAX_USTRING 8192 /* Max size of Unicode string */ + + +/* + * Types... + */ + +typedef unsigned char cups_utf8_t; /* UTF-8 Unicode/ISO-10646 unit */ +typedef unsigned long cups_utf32_t; /* UTF-32 Unicode/ISO-10646 unit */ +typedef unsigned short cups_ucs2_t; /* UCS-2 Unicode/ISO-10646 unit */ +typedef unsigned long cups_ucs4_t; /* UCS-4 Unicode/ISO-10646 unit */ +typedef unsigned char cups_sbcs_t; /* SBCS Legacy 8-bit unit */ +typedef unsigned short cups_dbcs_t; /* DBCS Legacy 16-bit unit */ +typedef unsigned long cups_vbcs_t; /* VBCS Legacy 32-bit unit */ + /* EUC uses 8, 16, 24, 32-bit */ + + +/* + * Prototypes... + */ + +extern int cupsCharsetToUTF8(cups_utf8_t *dest, + const char *src, + const int maxout, + const cups_encoding_t encoding) _CUPS_API_1_2; +extern int cupsUTF8ToCharset(char *dest, + const cups_utf8_t *src, + const int maxout, + const cups_encoding_t encoding) _CUPS_API_1_2; +extern int cupsUTF8ToUTF32(cups_utf32_t *dest, + const cups_utf8_t *src, + const int maxout) _CUPS_API_1_2; +extern int cupsUTF32ToUTF8(cups_utf8_t *dest, + const cups_utf32_t *src, + const int maxout) _CUPS_API_1_2; + +# ifdef __cplusplus +} +# endif /* __cplusplus */ + +#endif /* !_CUPS_TRANSCODE_H_ */ + + +/* + * End of "$Id: transcode.h 7026 2007-10-19 00:57:45Z mike $" + */ diff --git a/cups/usersys.c b/cups/usersys.c new file mode 100644 index 0000000..1a14461 --- /dev/null +++ b/cups/usersys.c @@ -0,0 +1,1068 @@ +/* + * "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $" + * + * User, system, and password routines for CUPS. + * + * Copyright 2007-2013 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * cupsEncryption() - Get the current encryption settings. + * cupsGetPassword() - Get a password from the user. + * cupsGetPassword2() - Get a password from the user using the advanced + * password callback. + * cupsServer() - Return the hostname/address of the current + * server. + * cupsSetClientCertCB() - Set the client certificate callback. + * cupsSetEncryption() - Set the encryption preference. + * cupsSetPasswordCB() - Set the password callback for CUPS. + * cupsSetPasswordCB2() - Set the advanced password callback for CUPS. + * cupsSetServer() - Set the default server name and port. + * cupsSetServerCertCB() - Set the server certificate callback. + * cupsSetUser() - Set the default user name. + * cupsUser() - Return the current user's name. + * _cupsGetPassword() - Get a password from the user. + * _cupsGSSServiceName() - Get the GSS (Kerberos) service name. + * _cupsSetDefaults() - Set the default server, port, and encryption. + * cups_read_client_conf() - Read a client.conf file. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#ifdef WIN32 +# include +#else +# include +# include +#endif /* WIN32 */ + + +/* + * Local constants... + */ + +#define _CUPS_PASSCHAR '*' /* Character that is echoed for password */ + + +/* + * Local functions... + */ + +static void cups_read_client_conf(cups_file_t *fp, + _cups_globals_t *cg, + const char *cups_encryption, + const char *cups_server, + const char *cups_user, +#ifdef HAVE_GSSAPI + const char *cups_gssservicename, +#endif /* HAVE_GSSAPI */ + const char *cups_anyroot, + const char *cups_expiredroot, + const char *cups_expiredcerts); + + +/* + * 'cupsEncryption()' - Get the current encryption settings. + * + * The default encryption setting comes from the CUPS_ENCRYPTION + * environment variable, then the ~/.cups/client.conf file, and finally the + * /etc/cups/client.conf file. If not set, the default is + * @code HTTP_ENCRYPT_IF_REQUESTED@. + * + * Note: The current encryption setting is tracked separately for each thread + * in a program. Multi-threaded programs that override the setting via the + * @link cupsSetEncryption@ function need to do so in each thread for the same + * setting to be used. + */ + +http_encryption_t /* O - Encryption settings */ +cupsEncryption(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (cg->encryption == (http_encryption_t)-1) + _cupsSetDefaults(); + + return (cg->encryption); +} + + +/* + * 'cupsGetPassword()' - Get a password from the user. + * + * Uses the current password callback function. Returns @code NULL@ if the + * user does not provide a password. + * + * Note: The current password callback function is tracked separately for each + * thread in a program. Multi-threaded programs that override the setting via + * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to + * do so in each thread for the same function to be used. + */ + +const char * /* O - Password */ +cupsGetPassword(const char *prompt) /* I - Prompt string */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + return ((cg->password_cb)(prompt, NULL, NULL, NULL, cg->password_data)); +} + + +/* + * 'cupsGetPassword2()' - Get a password from the user using the advanced + * password callback. + * + * Uses the current password callback function. Returns @code NULL@ if the + * user does not provide a password. + * + * Note: The current password callback function is tracked separately for each + * thread in a program. Multi-threaded programs that override the setting via + * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to + * do so in each thread for the same function to be used. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +const char * /* O - Password */ +cupsGetPassword2(const char *prompt, /* I - Prompt string */ + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *method, /* I - Request method ("GET", "POST", "PUT") */ + const char *resource) /* I - Resource path */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (!http) + http = _cupsConnect(); + + return ((cg->password_cb)(prompt, http, method, resource, cg->password_data)); +} + + +/* + * 'cupsServer()' - Return the hostname/address of the current server. + * + * The default server comes from the CUPS_SERVER environment variable, then the + * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not + * set, the default is the local system - either "localhost" or a domain socket + * path. + * + * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6 + * address, or a domain socket pathname. + * + * Note: The current server is tracked separately for each thread in a program. + * Multi-threaded programs that override the server via the + * @link cupsSetServer@ function need to do so in each thread for the same + * server to be used. + */ + +const char * /* O - Server name */ +cupsServer(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (!cg->server[0]) + _cupsSetDefaults(); + + return (cg->server); +} + + +/* + * 'cupsSetClientCertCB()' - Set the client certificate callback. + * + * Pass @code NULL@ to restore the default callback. + * + * Note: The current certificate callback is tracked separately for each thread + * in a program. Multi-threaded programs that override the callback need to do + * so in each thread for the same callback to be used. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +void +cupsSetClientCertCB( + cups_client_cert_cb_t cb, /* I - Callback function */ + void *user_data) /* I - User data pointer */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + cg->client_cert_cb = cb; + cg->client_cert_data = user_data; +} + + +/* + * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS + * connections. + * + * Note: The default credentials are tracked separately for each thread in a + * program. Multi-threaded programs that override the setting need to do so in + * each thread for the same setting to be used. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +int /* O - Status of call (0 = success) */ +cupsSetCredentials( + cups_array_t *credentials) /* I - Array of credentials */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (cupsArrayCount(credentials) < 1) + return (-1); + + _httpFreeCredentials(cg->tls_credentials); + cg->tls_credentials = _httpCreateCredentials(credentials); + + return (cg->tls_credentials ? 0 : -1); +} + + +/* + * 'cupsSetEncryption()' - Set the encryption preference. + * + * The default encryption setting comes from the CUPS_ENCRYPTION + * environment variable, then the ~/.cups/client.conf file, and finally the + * /etc/cups/client.conf file. If not set, the default is + * @code HTTP_ENCRYPT_IF_REQUESTED@. + * + * Note: The current encryption setting is tracked separately for each thread + * in a program. Multi-threaded programs that override the setting need to do + * so in each thread for the same setting to be used. + */ + +void +cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + cg->encryption = e; + + if (cg->http) + httpEncryption(cg->http, e); +} + + +/* + * 'cupsSetPasswordCB()' - Set the password callback for CUPS. + * + * Pass @code NULL@ to restore the default (console) password callback, which + * reads the password from the console. Programs should call either this + * function or @link cupsSetPasswordCB2@, as only one callback can be registered + * by a program per thread. + * + * Note: The current password callback is tracked separately for each thread + * in a program. Multi-threaded programs that override the callback need to do + * so in each thread for the same callback to be used. + */ + +void +cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (cb == (cups_password_cb_t)0) + cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; + else + cg->password_cb = (cups_password_cb2_t)cb; + + cg->password_data = NULL; +} + + +/* + * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS. + * + * Pass @code NULL@ to restore the default (console) password callback, which + * reads the password from the console. Programs should call either this + * function or @link cupsSetPasswordCB2@, as only one callback can be registered + * by a program per thread. + * + * Note: The current password callback is tracked separately for each thread + * in a program. Multi-threaded programs that override the callback need to do + * so in each thread for the same callback to be used. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +void +cupsSetPasswordCB2( + cups_password_cb2_t cb, /* I - Callback function */ + void *user_data) /* I - User data pointer */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (cb == (cups_password_cb2_t)0) + cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; + else + cg->password_cb = cb; + + cg->password_data = user_data; +} + + +/* + * 'cupsSetServer()' - Set the default server name and port. + * + * The "server" string can be a fully-qualified hostname, a numeric + * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP + * addresses can be optionally followed by a colon and port number to override + * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the + * default server name and port. + * + * Note: The current server is tracked separately for each thread in a program. + * Multi-threaded programs that override the server need to do so in each + * thread for the same server to be used. + */ + +void +cupsSetServer(const char *server) /* I - Server name */ +{ + char *options, /* Options */ + *port; /* Pointer to port */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (server) + { + strlcpy(cg->server, server, sizeof(cg->server)); + + if (cg->server[0] != '/' && (options = strrchr(cg->server, '/')) != NULL) + { + *options++ = '\0'; + + if (!strcmp(options, "version=1.0")) + cg->server_version = 10; + else if (!strcmp(options, "version=1.1")) + cg->server_version = 11; + else if (!strcmp(options, "version=2.0")) + cg->server_version = 20; + else if (!strcmp(options, "version=2.1")) + cg->server_version = 21; + else if (!strcmp(options, "version=2.2")) + cg->server_version = 22; + } + else + cg->server_version = 20; + + if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL && + !strchr(port, ']') && isdigit(port[1] & 255)) + { + *port++ = '\0'; + + cg->ipp_port = atoi(port); + } + + if (cg->server[0] == '/') + strcpy(cg->servername, "localhost"); + else + strlcpy(cg->servername, cg->server, sizeof(cg->servername)); + } + else + { + cg->server[0] = '\0'; + cg->servername[0] = '\0'; + cg->server_version = 20; + } + + if (cg->http) + { + httpClose(cg->http); + cg->http = NULL; + } +} + + +/* + * 'cupsSetServerCertCB()' - Set the server certificate callback. + * + * Pass @code NULL@ to restore the default callback. + * + * Note: The current credentials callback is tracked separately for each thread + * in a program. Multi-threaded programs that override the callback need to do + * so in each thread for the same callback to be used. + * + * @since CUPS 1.5/OS X 10.7@ + */ + +void +cupsSetServerCertCB( + cups_server_cert_cb_t cb, /* I - Callback function */ + void *user_data) /* I - User data pointer */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + cg->server_cert_cb = cb; + cg->server_cert_data = user_data; +} + + +/* + * 'cupsSetUser()' - Set the default user name. + * + * Pass @code NULL@ to restore the default user name. + * + * Note: The current user name is tracked separately for each thread in a + * program. Multi-threaded programs that override the user name need to do so + * in each thread for the same user name to be used. + */ + +void +cupsSetUser(const char *user) /* I - User name */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (user) + strlcpy(cg->user, user, sizeof(cg->user)); + else + cg->user[0] = '\0'; +} + + +/* + * 'cupsUser()' - Return the current user's name. + * + * Note: The current user name is tracked separately for each thread in a + * program. Multi-threaded programs that override the user name with the + * @link cupsSetUser@ function need to do so in each thread for the same user + * name to be used. + */ + +const char * /* O - User name */ +cupsUser(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + if (!cg->user[0]) + _cupsSetDefaults(); + + return (cg->user); +} + + +/* + * '_cupsGetPassword()' - Get a password from the user. + */ + +const char * /* O - Password or @code NULL@ if none */ +_cupsGetPassword(const char *prompt) /* I - Prompt string */ +{ +#ifdef WIN32 + HANDLE tty; /* Console handle */ + DWORD mode; /* Console mode */ + char passch, /* Current key press */ + *passptr, /* Pointer into password string */ + *passend; /* End of password string */ + DWORD passbytes; /* Bytes read */ + _cups_globals_t *cg = _cupsGlobals(); + /* Thread globals */ + + + /* + * Disable input echo and set raw input... + */ + + if ((tty = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) + return (NULL); + + if (!GetConsoleMode(tty, &mode)) + return (NULL); + + if (!SetConsoleMode(tty, 0)) + return (NULL); + + /* + * Display the prompt... + */ + + printf("%s ", prompt); + fflush(stdout); + + /* + * Read the password string from /dev/tty until we get interrupted or get a + * carriage return or newline... + */ + + passptr = cg->password; + passend = cg->password + sizeof(cg->password) - 1; + + while (ReadFile(tty, &passch, 1, &passbytes, NULL)) + { + if (passch == 0x0A || passch == 0x0D) + { + /* + * Enter/return... + */ + + break; + } + else if (passch == 0x08 || passch == 0x7F) + { + /* + * Backspace/delete (erase character)... + */ + + if (passptr > cg->password) + { + passptr --; + fputs("\010 \010", stdout); + } + else + putchar(0x07); + } + else if (passch == 0x15) + { + /* + * CTRL+U (erase line) + */ + + if (passptr > cg->password) + { + while (passptr > cg->password) + { + passptr --; + fputs("\010 \010", stdout); + } + } + else + putchar(0x07); + } + else if (passch == 0x03) + { + /* + * CTRL+C... + */ + + passptr = cg->password; + break; + } + else if ((passch & 255) < 0x20 || passptr >= passend) + putchar(0x07); + else + { + *passptr++ = passch; + putchar(_CUPS_PASSCHAR); + } + + fflush(stdout); + } + + putchar('\n'); + fflush(stdout); + + /* + * Cleanup... + */ + + SetConsoleMode(tty, mode); + + /* + * Return the proper value... + */ + + if (passbytes == 1 && passptr > cg->password) + { + *passptr = '\0'; + return (cg->password); + } + else + { + memset(cg->password, 0, sizeof(cg->password)); + return (NULL); + } + +#else + int tty; /* /dev/tty - never read from stdin */ + struct termios original, /* Original input mode */ + noecho; /* No echo input mode */ + char passch, /* Current key press */ + *passptr, /* Pointer into password string */ + *passend; /* End of password string */ + ssize_t passbytes; /* Bytes read */ + _cups_globals_t *cg = _cupsGlobals(); + /* Thread globals */ + + + /* + * Disable input echo and set raw input... + */ + + if ((tty = open("/dev/tty", O_RDONLY)) < 0) + return (NULL); + + if (tcgetattr(tty, &original)) + { + close(tty); + return (NULL); + } + + noecho = original; + noecho.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + + if (tcsetattr(tty, TCSAFLUSH, &noecho)) + { + close(tty); + return (NULL); + } + + /* + * Display the prompt... + */ + + printf("%s ", prompt); + fflush(stdout); + + /* + * Read the password string from /dev/tty until we get interrupted or get a + * carriage return or newline... + */ + + passptr = cg->password; + passend = cg->password + sizeof(cg->password) - 1; + + while ((passbytes = read(tty, &passch, 1)) == 1) + { + if (passch == noecho.c_cc[VEOL] || passch == noecho.c_cc[VEOL2] || + passch == 0x0A || passch == 0x0D) + { + /* + * Enter/return... + */ + + break; + } + else if (passch == noecho.c_cc[VERASE] || + passch == 0x08 || passch == 0x7F) + { + /* + * Backspace/delete (erase character)... + */ + + if (passptr > cg->password) + { + passptr --; + fputs("\010 \010", stdout); + } + else + putchar(0x07); + } + else if (passch == noecho.c_cc[VKILL]) + { + /* + * CTRL+U (erase line) + */ + + if (passptr > cg->password) + { + while (passptr > cg->password) + { + passptr --; + fputs("\010 \010", stdout); + } + } + else + putchar(0x07); + } + else if (passch == noecho.c_cc[VINTR] || passch == noecho.c_cc[VQUIT] || + passch == noecho.c_cc[VEOF]) + { + /* + * CTRL+C, CTRL+D, or CTRL+Z... + */ + + passptr = cg->password; + break; + } + else if ((passch & 255) < 0x20 || passptr >= passend) + putchar(0x07); + else + { + *passptr++ = passch; + putchar(_CUPS_PASSCHAR); + } + + fflush(stdout); + } + + putchar('\n'); + fflush(stdout); + + /* + * Cleanup... + */ + + tcsetattr(tty, TCSAFLUSH, &original); + close(tty); + + /* + * Return the proper value... + */ + + if (passbytes == 1 && passptr > cg->password) + { + *passptr = '\0'; + return (cg->password); + } + else + { + memset(cg->password, 0, sizeof(cg->password)); + return (NULL); + } +#endif /* WIN32 */ +} + + +#ifdef HAVE_GSSAPI +/* + * '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name. + */ + +const char * +_cupsGSSServiceName(void) +{ + _cups_globals_t *cg = _cupsGlobals(); /* Thread globals */ + + + if (!cg->gss_service_name[0]) + _cupsSetDefaults(); + + return (cg->gss_service_name); +} +#endif /* HAVE_GSSAPI */ + + +/* + * '_cupsSetDefaults()' - Set the default server, port, and encryption. + */ + +void +_cupsSetDefaults(void) +{ + cups_file_t *fp; /* File */ + const char *home, /* Home directory of user */ + *cups_encryption, /* CUPS_ENCRYPTION env var */ + *cups_server, /* CUPS_SERVER env var */ + *cups_user, /* CUPS_USER/USER env var */ +#ifdef HAVE_GSSAPI + *cups_gssservicename, /* CUPS_GSSSERVICENAME env var */ +#endif /* HAVE_GSSAPI */ + *cups_anyroot, /* CUPS_ANYROOT env var */ + *cups_expiredroot, /* CUPS_EXPIREDROOT env var */ + *cups_expiredcerts; /* CUPS_EXPIREDCERTS env var */ + char filename[1024]; /* Filename */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + DEBUG_puts("_cupsSetDefaults()"); + + /* + * First collect environment variables... + */ + + cups_encryption = getenv("CUPS_ENCRYPTION"); + cups_server = getenv("CUPS_SERVER"); +#ifdef HAVE_GSSAPI + cups_gssservicename = getenv("CUPS_GSSSERVICENAME"); +#endif /* HAVE_GSSAPI */ + cups_anyroot = getenv("CUPS_ANYROOT"); + cups_expiredroot = getenv("CUPS_EXPIREDROOT"); + cups_expiredcerts = getenv("CUPS_EXPIREDCERTS"); + + if ((cups_user = getenv("CUPS_USER")) == NULL) + cups_user = getenv("USER"); + + /* + * Then, if needed, read the ~/.cups/client.conf or /etc/cups/client.conf + * files to get the default values... + */ + + if (cg->encryption == (http_encryption_t)-1 || !cg->server[0] || + !cg->user[0] || !cg->ipp_port) + { + if ((home = getenv("HOME")) != NULL) + { + /* + * Look for ~/.cups/client.conf... + */ + + snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home); + fp = cupsFileOpen(filename, "r"); + } + else + fp = NULL; + + if (!fp) + { + /* + * Look for CUPS_SERVERROOT/client.conf... + */ + + snprintf(filename, sizeof(filename), "%s/client.conf", + cg->cups_serverroot); + fp = cupsFileOpen(filename, "r"); + } + + /* + * Read the configuration file and apply any environment variables; both + * functions handle NULL cups_file_t pointers... + */ + + cups_read_client_conf(fp, cg, cups_encryption, cups_server, cups_user, +#ifdef HAVE_GSSAPI + cups_gssservicename, +#endif /* HAVE_GSSAPI */ + cups_anyroot, cups_expiredroot, + cups_expiredcerts); + cupsFileClose(fp); + } +} + + +/* + * 'cups_read_client_conf()' - Read a client.conf file. + */ + +static void +cups_read_client_conf( + cups_file_t *fp, /* I - File to read */ + _cups_globals_t *cg, /* I - Global data */ + const char *cups_encryption, /* I - CUPS_ENCRYPTION env var */ + const char *cups_server, /* I - CUPS_SERVER env var */ + const char *cups_user, /* I - CUPS_USER env var */ +#ifdef HAVE_GSSAPI + const char *cups_gssservicename, + /* I - CUPS_GSSSERVICENAME env var */ +#endif /* HAVE_GSSAPI */ + const char *cups_anyroot, /* I - CUPS_ANYROOT env var */ + const char *cups_expiredroot, /* I - CUPS_EXPIREDROOT env var */ + const char *cups_expiredcerts) /* I - CUPS_EXPIREDCERTS env var */ +{ + int linenum; /* Current line number */ + char line[1024], /* Line from file */ + *value, /* Pointer into line */ + encryption[1024], /* Encryption value */ +#ifndef __APPLE__ + server_name[1024], /* ServerName value */ +#endif /* !__APPLE__ */ + user[256], /* User value */ + any_root[1024], /* AllowAnyRoot value */ + expired_root[1024], /* AllowExpiredRoot value */ + expired_certs[1024]; /* AllowExpiredCerts value */ +#ifdef HAVE_GSSAPI + char gss_service_name[32]; /* GSSServiceName value */ +#endif /* HAVE_GSSAPI */ + + + /* + * Read from the file... + */ + + linenum = 0; + while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) + { + if (!cups_encryption && cg->encryption == (http_encryption_t)-1 && + !_cups_strcasecmp(line, "Encryption") && value) + { + strlcpy(encryption, value, sizeof(encryption)); + cups_encryption = encryption; + } +#ifndef __APPLE__ + /* + * The Server directive is not supported on OS X due to app sandboxing + * restrictions, i.e. not all apps request network access. + */ + else if (!cups_server && (!cg->server[0] || !cg->ipp_port) && + !_cups_strcasecmp(line, "ServerName") && value) + { + strlcpy(server_name, value, sizeof(server_name)); + cups_server = server_name; + } +#endif /* !__APPLE__ */ + else if (!cups_user && !_cups_strcasecmp(line, "User") && value) + { + strlcpy(user, value, sizeof(user)); + cups_user = user; + } + else if (!cups_anyroot && !_cups_strcasecmp(line, "AllowAnyRoot") && value) + { + strlcpy(any_root, value, sizeof(any_root)); + cups_anyroot = any_root; + } + else if (!cups_expiredroot && !_cups_strcasecmp(line, "AllowExpiredRoot") && + value) + { + strlcpy(expired_root, value, sizeof(expired_root)); + cups_expiredroot = expired_root; + } + else if (!cups_expiredcerts && !_cups_strcasecmp(line, "AllowExpiredCerts") && + value) + { + strlcpy(expired_certs, value, sizeof(expired_certs)); + cups_expiredcerts = expired_certs; + } +#ifdef HAVE_GSSAPI + else if (!cups_gssservicename && !_cups_strcasecmp(line, "GSSServiceName") && + value) + { + strlcpy(gss_service_name, value, sizeof(gss_service_name)); + cups_gssservicename = gss_service_name; + } +#endif /* HAVE_GSSAPI */ + } + + /* + * Set values... + */ + + if (cg->encryption == (http_encryption_t)-1 && cups_encryption) + { + if (!_cups_strcasecmp(cups_encryption, "never")) + cg->encryption = HTTP_ENCRYPT_NEVER; + else if (!_cups_strcasecmp(cups_encryption, "always")) + cg->encryption = HTTP_ENCRYPT_ALWAYS; + else if (!_cups_strcasecmp(cups_encryption, "required")) + cg->encryption = HTTP_ENCRYPT_REQUIRED; + else + cg->encryption = HTTP_ENCRYPT_IF_REQUESTED; + } + + if ((!cg->server[0] || !cg->ipp_port) && cups_server) + cupsSetServer(cups_server); + + if (!cg->server[0]) + { +#ifdef CUPS_DEFAULT_DOMAINSOCKET + /* + * If we are compiled with domain socket support, only use the + * domain socket if it exists and has the right permissions... + */ + + struct stat sockinfo; /* Domain socket information */ + + if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) && + (sockinfo.st_mode & S_IRWXO) == S_IRWXO) + cups_server = CUPS_DEFAULT_DOMAINSOCKET; + else +#endif /* CUPS_DEFAULT_DOMAINSOCKET */ + cups_server = "localhost"; + + cupsSetServer(cups_server); + } + + if (!cg->ipp_port) + { + const char *ipp_port; /* IPP_PORT environment variable */ + + if ((ipp_port = getenv("IPP_PORT")) != NULL) + { + if ((cg->ipp_port = atoi(ipp_port)) <= 0) + cg->ipp_port = CUPS_DEFAULT_IPP_PORT; + } + else + cg->ipp_port = CUPS_DEFAULT_IPP_PORT; + } + + if (!cg->user[0]) + { + if (cups_user) + strlcpy(cg->user, cups_user, sizeof(cg->user)); + else + { +#ifdef WIN32 + /* + * Get the current user name from the OS... + */ + + DWORD size; /* Size of string */ + + size = sizeof(cg->user); + if (!GetUserName(cg->user, &size)) +#else + /* + * Get the user name corresponding to the current UID... + */ + + struct passwd *pwd; /* User/password entry */ + + // 07/22/2016 Mopria-notice: setpwent is not defined in Android so commenting out + //setpwent(); + if ((pwd = getpwuid(getuid())) != NULL) + { + /* + * Found a match! + */ + + strlcpy(cg->user, pwd->pw_name, sizeof(cg->user)); + } + else +#endif /* WIN32 */ + { + /* + * Use the default "unknown" user name... + */ + + strcpy(cg->user, "unknown"); + } + } + } + +#ifdef HAVE_GSSAPI + if (!cups_gssservicename) + cups_gssservicename = CUPS_DEFAULT_GSSSERVICENAME; + + strlcpy(cg->gss_service_name, cups_gssservicename, + sizeof(cg->gss_service_name)); +#endif /* HAVE_GSSAPI */ + + if (cups_anyroot) + cg->any_root = !_cups_strcasecmp(cups_anyroot, "yes") || + !_cups_strcasecmp(cups_anyroot, "on") || + !_cups_strcasecmp(cups_anyroot, "true"); + + if (cups_expiredroot) + cg->expired_root = !_cups_strcasecmp(cups_expiredroot, "yes") || + !_cups_strcasecmp(cups_expiredroot, "on") || + !_cups_strcasecmp(cups_expiredroot, "true"); + + if (cups_expiredcerts) + cg->expired_certs = !_cups_strcasecmp(cups_expiredcerts, "yes") || + !_cups_strcasecmp(cups_expiredcerts, "on") || + !_cups_strcasecmp(cups_expiredcerts, "true"); +} + +// 07/22/2016 Mopria-notice: localeconv is not defined in Android so need to define for cross compilation +struct lconv *localeconv(void) { + return NULL; +} + +/* + * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $". + */ + + diff --git a/cups/utf8demo.txt b/cups/utf8demo.txt new file mode 100644 index 0000000..03802e4 --- /dev/null +++ b/cups/utf8demo.txt @@ -0,0 +1,213 @@ +UTF-8 encoded sample plain-text file +‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +Markus Kuhn [ˈmaʳkʊs kuːn] — 2002-07-25 + + +The ASCII compatible UTF-8 encoding used in this plain-text file +is defined in Unicode, ISO 10646-1, and RFC 2279. + + +Using Unicode/UTF-8, you can write in emails and source code things such as + +Mathematics and sciences: + + ∮ E⋅da = Q, n → ∞, ∑ f(i) = ∏ g(i), ⎧⎡⎛┌─────┐⎞⎤⎫ + ⎪⎢⎜│a²+b³ ⎟⎥⎪ + ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β), ⎪⎢⎜│───── ⎟⎥⎪ + ⎪⎢⎜⎷ c₈ ⎟⎥⎪ + ℕ ⊆ ℕ₀ ⊂ ℤ ⊂ ℚ ⊂ ℝ ⊂ ℂ, ⎨⎢⎜ ⎟⎥⎬ + ⎪⎢⎜ ∞ ⎟⎥⎪ + ⊥ < a ≠ b ≡ c ≤ d ≪ ⊤ ⇒ (⟦A⟧ ⇔ ⟪B⟫), ⎪⎢⎜ ⎲ ⎟⎥⎪ + ⎪⎢⎜ ⎳aⁱ-bⁱ⎟⎥⎪ + 2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm ⎩⎣⎝i=1 ⎠⎦⎭ + +Linguistics and dictionaries: + + ði ıntəˈnæʃənəl fəˈnɛtık əsoʊsiˈeıʃn + Y [ˈʏpsilɔn], Yen [jɛn], Yoga [ˈjoːgɑ] + +APL: + + ((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈ + +Nicer typography in plain text files: + + ╔══════════════════════════════════════════╗ + ║ ║ + ║ • ‘single’ and “double” quotes ║ + ║ ║ + ║ • Curly apostrophes: “We’ve been here” ║ + ║ ║ + ║ • Latin-1 apostrophe and accents: '´` ║ + ║ ║ + ║ • ‚deutsche‘ „Anführungszeichen“ ║ + ║ ║ + ║ • †, ‡, ‰, •, 3–4, —, −5/+5, ™, … ║ + ║ ║ + ║ • ASCII safety test: 1lI|, 0OD, 8B ║ + ║ ╭─────────╮ ║ + ║ • the euro symbol: │ 14.95 € │ ║ + ║ ╰─────────╯ ║ + ╚══════════════════════════════════════════╝ + +Combining characters: + + STARGΛ̊TE SG-1, a = v̇ = r̈, a⃑ ⊥ b⃑ + +Greek (in Polytonic): + + The Greek anthem: + + Σὲ γνωρίζω ἀπὸ τὴν κόψη + τοῦ σπαθιοῦ τὴν τρομερή, + σὲ γνωρίζω ἀπὸ τὴν ὄψη + ποὺ μὲ βία μετράει τὴ γῆ. + + ᾿Απ᾿ τὰ κόκκαλα βγαλμένη + τῶν ῾Ελλήνων τὰ ἱερά + καὶ σὰν πρῶτα ἀνδρειωμένη + χαῖρε, ὦ χαῖρε, ᾿Ελευθεριά! + + From a speech of Demosthenes in the 4th century BC: + + Οὐχὶ ταὐτὰ παρίσταταί μοι γιγνώσκειν, ὦ ἄνδρες ᾿Αθηναῖοι, + ὅταν τ᾿ εἰς τὰ πράγματα ἀποβλέψω καὶ ὅταν πρὸς τοὺς + λόγους οὓς ἀκούω· τοὺς μὲν γὰρ λόγους περὶ τοῦ + τιμωρήσασθαι Φίλιππον ὁρῶ γιγνομένους, τὰ δὲ πράγματ᾿ + εἰς τοῦτο προήκοντα, ὥσθ᾿ ὅπως μὴ πεισόμεθ᾿ αὐτοὶ + πρότερον κακῶς σκέψασθαι δέον. οὐδέν οὖν ἄλλο μοι δοκοῦσιν + οἱ τὰ τοιαῦτα λέγοντες ἢ τὴν ὑπόθεσιν, περὶ ἧς βουλεύεσθαι, + οὐχὶ τὴν οὖσαν παριστάντες ὑμῖν ἁμαρτάνειν. ἐγὼ δέ, ὅτι μέν + ποτ᾿ ἐξῆν τῇ πόλει καὶ τὰ αὑτῆς ἔχειν ἀσφαλῶς καὶ Φίλιππον + τιμωρήσασθαι, καὶ μάλ᾿ ἀκριβῶς οἶδα· ἐπ᾿ ἐμοῦ γάρ, οὐ πάλαι + γέγονεν ταῦτ᾿ ἀμφότερα· νῦν μέντοι πέπεισμαι τοῦθ᾿ ἱκανὸν + προλαβεῖν ἡμῖν εἶναι τὴν πρώτην, ὅπως τοὺς συμμάχους + σώσομεν. ἐὰν γὰρ τοῦτο βεβαίως ὑπάρξῃ, τότε καὶ περὶ τοῦ + τίνα τιμωρήσεταί τις καὶ ὃν τρόπον ἐξέσται σκοπεῖν· πρὶν δὲ + τὴν ἀρχὴν ὀρθῶς ὑποθέσθαι, μάταιον ἡγοῦμαι περὶ τῆς + τελευτῆς ὁντινοῦν ποιεῖσθαι λόγον. + + Δημοσθένους, Γ´ ᾿Ολυνθιακὸς + +Georgian: + + From a Unicode conference invitation: + + გთხოვთ ახლავე გაიაროთ რეგისტრაცია Unicode-ის მეათე საერთაშორისო + კონფერენციაზე დასასწრებად, რომელიც გაიმართება 10-12 მარტს, + ქ. მაინცში, გერმანიაში. კონფერენცია შეჰკრებს ერთად მსოფლიოს + ექსპერტებს ისეთ დარგებში როგორიცაა ინტერნეტი და Unicode-ი, + ინტერნაციონალიზაცია და ლოკალიზაცია, Unicode-ის გამოყენება + ოპერაციულ სისტემებსა, და გამოყენებით პროგრამებში, შრიფტებში, + ტექსტების დამუშავებასა და მრავალენოვან კომპიუტერულ სისტემებში. + +Russian: + + From a Unicode conference invitation: + + Зарегистрируйтесь сейчас на Десятую Международную Конференцию по + Unicode, которая состоится 10-12 марта 1997 года в Майнце в Германии. + Конференция соберет широкий круг экспертов по вопросам глобального + Интернета и Unicode, локализации и интернационализации, воплощению и + применению Unicode в различных операционных системах и программных + приложениях, шрифтах, верстке и многоязычных компьютерных системах. + +Thai (UCS Level 2): + + Excerpt from a poetry on The Romance of The Three Kingdoms (a Chinese + classic 'San Gua'): + + [----------------------------|------------------------] + ๏ แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช พระปกเกศกองบู๊กู้ขึ้นใหม่ + สิบสองกษัตริย์ก่อนหน้าแลถัดไป สององค์ไซร้โง่เขลาเบาปัญญา + ทรงนับถือขันทีเป็นที่พึ่ง บ้านเมืองจึงวิปริตเป็นนักหนา + โฮจิ๋นเรียกทัพทั่วหัวเมืองมา หมายจะฆ่ามดชั่วตัวสำคัญ + เหมือนขับไสไล่เสือจากเคหา รับหมาป่าเข้ามาเลยอาสัญ + ฝ่ายอ้องอุ้นยุแยกให้แตกกัน ใช้สาวนั้นเป็นชนวนชื่นชวนใจ + พลันลิฉุยกุยกีกลับก่อเหตุ ช่างอาเพศจริงหนาฟ้าร้องไห้ + ต้องรบราฆ่าฟันจนบรรลัย ฤๅหาใครค้ำชูกู้บรรลังก์ ฯ + + (The above is a two-column text. If combining characters are handled + correctly, the lines of the second column should be aligned with the + | character above.) + +Ethiopian: + + Proverbs in the Amharic language: + + ሰማይ አይታረስ ንጉሥ አይከሰስ። + ብላ ካለኝ እንደአባቴ በቆመጠኝ። + ጌጥ ያለቤቱ ቁምጥና ነው። + ደሀ በሕልሙ ቅቤ ባይጠጣ ንጣት በገደለው። + የአፍ ወለምታ በቅቤ አይታሽም። + አይጥ በበላ ዳዋ ተመታ። + ሲተረጉሙ ይደረግሙ። + ቀስ በቀስ፥ ዕንቁላል በእግሩ ይሄዳል። + ድር ቢያብር አንበሳ ያስር። + ሰው እንደቤቱ እንጅ እንደ ጉረቤቱ አይተዳደርም። + እግዜር የከፈተውን ጉሮሮ ሳይዘጋው አይድርም። + የጎረቤት ሌባ፥ ቢያዩት ይስቅ ባያዩት ያጠልቅ። + ሥራ ከመፍታት ልጄን ላፋታት። + ዓባይ ማደሪያ የለው፥ ግንድ ይዞ ይዞራል። + የእስላም አገሩ መካ የአሞራ አገሩ ዋርካ። + ተንጋሎ ቢተፉ ተመልሶ ባፉ። + ወዳጅህ ማር ቢሆን ጨርስህ አትላሰው። + እግርህን በፍራሽህ ልክ ዘርጋ። + +Runes: + + ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ + + (Old English, which transcribed into Latin reads 'He cwaeth that he + bude thaem lande northweardum with tha Westsae.' and means 'He said + that he lived in the northern land near the Western Sea.') + +Braille: + + ⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌ + + ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠙⠑⠁⠙⠒ ⠞⠕ ⠃⠑⠛⠔ ⠺⠊⠹⠲ ⡹⠻⠑ ⠊⠎ ⠝⠕ ⠙⠳⠃⠞ + ⠱⠁⠞⠑⠧⠻ ⠁⠃⠳⠞ ⠹⠁⠞⠲ ⡹⠑ ⠗⠑⠛⠊⠌⠻ ⠕⠋ ⠙⠊⠎ ⠃⠥⠗⠊⠁⠇ ⠺⠁⠎ + ⠎⠊⠛⠝⠫ ⠃⠹ ⠹⠑ ⠊⠇⠻⠛⠹⠍⠁⠝⠂ ⠹⠑ ⠊⠇⠻⠅⠂ ⠹⠑ ⠥⠝⠙⠻⠞⠁⠅⠻⠂ + ⠁⠝⠙ ⠹⠑ ⠡⠊⠑⠋ ⠍⠳⠗⠝⠻⠲ ⡎⠊⠗⠕⠕⠛⠑ ⠎⠊⠛⠝⠫ ⠊⠞⠲ ⡁⠝⠙ + ⡎⠊⠗⠕⠕⠛⠑⠰⠎ ⠝⠁⠍⠑ ⠺⠁⠎ ⠛⠕⠕⠙ ⠥⠏⠕⠝ ⠰⡡⠁⠝⠛⠑⠂ ⠋⠕⠗ ⠁⠝⠹⠹⠔⠛ ⠙⠑ + ⠡⠕⠎⠑ ⠞⠕ ⠏⠥⠞ ⠙⠊⠎ ⠙⠁⠝⠙ ⠞⠕⠲ + + ⡕⠇⠙ ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ + + ⡍⠔⠙⠖ ⡊ ⠙⠕⠝⠰⠞ ⠍⠑⠁⠝ ⠞⠕ ⠎⠁⠹ ⠹⠁⠞ ⡊ ⠅⠝⠪⠂ ⠕⠋ ⠍⠹ + ⠪⠝ ⠅⠝⠪⠇⠫⠛⠑⠂ ⠱⠁⠞ ⠹⠻⠑ ⠊⠎ ⠏⠜⠞⠊⠊⠥⠇⠜⠇⠹ ⠙⠑⠁⠙ ⠁⠃⠳⠞ + ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ ⡊ ⠍⠊⠣⠞ ⠙⠁⠧⠑ ⠃⠑⠲ ⠔⠊⠇⠔⠫⠂ ⠍⠹⠎⠑⠇⠋⠂ ⠞⠕ + ⠗⠑⠛⠜⠙ ⠁ ⠊⠕⠋⠋⠔⠤⠝⠁⠊⠇ ⠁⠎ ⠹⠑ ⠙⠑⠁⠙⠑⠌ ⠏⠊⠑⠊⠑ ⠕⠋ ⠊⠗⠕⠝⠍⠕⠝⠛⠻⠹ + ⠔ ⠹⠑ ⠞⠗⠁⠙⠑⠲ ⡃⠥⠞ ⠹⠑ ⠺⠊⠎⠙⠕⠍ ⠕⠋ ⠳⠗ ⠁⠝⠊⠑⠌⠕⠗⠎ + ⠊⠎ ⠔ ⠹⠑ ⠎⠊⠍⠊⠇⠑⠆ ⠁⠝⠙ ⠍⠹ ⠥⠝⠙⠁⠇⠇⠪⠫ ⠙⠁⠝⠙⠎ + ⠩⠁⠇⠇ ⠝⠕⠞ ⠙⠊⠌⠥⠗⠃ ⠊⠞⠂ ⠕⠗ ⠹⠑ ⡊⠳⠝⠞⠗⠹⠰⠎ ⠙⠕⠝⠑ ⠋⠕⠗⠲ ⡹⠳ + ⠺⠊⠇⠇ ⠹⠻⠑⠋⠕⠗⠑ ⠏⠻⠍⠊⠞ ⠍⠑ ⠞⠕ ⠗⠑⠏⠑⠁⠞⠂ ⠑⠍⠏⠙⠁⠞⠊⠊⠁⠇⠇⠹⠂ ⠹⠁⠞ + ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ + + (The first couple of paragraphs of "A Christmas Carol" by Dickens) + +Compact font selection example text: + + ABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789 + abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ + –—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвгд + ∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi?⑀₂ἠḂӥẄɐː⍎אԱა + +Greetings in various languages: + + Hello world, Καλημέρα κόσμε, コンニチハ + +Box drawing alignment tests: █ + ▉ + ╔══╦══╗ ┌──┬──┐ ╭──┬──╮ ╭──┬──╮ ┏━━┳━━┓ ┎┒┏┑ ╷ ╻ ┏┯┓ ┌┰┐ ▊ ╱╲╱╲╳╳╳ + ║┌─╨─┐║ │╔═╧═╗│ │╒═╪═╕│ │╓─╁─╖│ ┃┌─╂─┐┃ ┗╃╄┙ ╶┼╴╺╋╸┠┼┨ ┝╋┥ ▋ ╲╱╲╱╳╳╳ + ║│╲ ╱│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╿ │┃ ┍╅╆┓ ╵ ╹ ┗┷┛ └┸┘ ▌ ╱╲╱╲╳╳╳ + ╠╡ ╳ ╞╣ ├╢ ╟┤ ├┼─┼─┼┤ ├╫─╂─╫┤ ┣┿╾┼╼┿┫ ┕┛┖┚ ┌┄┄┐ ╎ ┏┅┅┓ ┋ ▍ ╲╱╲╱╳╳╳ + ║│╱ ╲│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╽ │┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▎ + ║└─╥─┘║ │╚═╤═╝│ │╘═╪═╛│ │╙─╀─╜│ ┃└─╂─┘┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▏ + ╚══╩══╝ └──┴──┘ ╰──┴──╯ ╰──┴──╯ ┗━━┻━━┛ ▗▄▖▛▀▜ └╌╌┘ ╎ ┗╍╍┛ ┋ ▁▂▃▄▅▆▇█ + ▝▀▘▙▄▟ + + diff --git a/cups/util.c b/cups/util.c new file mode 100644 index 0000000..07a660d --- /dev/null +++ b/cups/util.c @@ -0,0 +1,1848 @@ +/* + * "$Id: util.c 7850 2008-08-20 00:07:25Z mike $" + * + * Printing utilities for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * cupsCancelJob() - Cancel a print job on the default server. + * cupsCancelJob2() - Cancel or purge a print job. + * cupsCreateJob() - Create an empty job for streaming. + * cupsFinishDocument() - Finish sending a document. + * cupsFreeJobs() - Free memory used by job data. + * cupsGetClasses() - Get a list of printer classes from the default + * server. + * cupsGetDefault() - Get the default printer or class for the default + * server. + * cupsGetDefault2() - Get the default printer or class for the specified + * server. + * cupsGetJobs() - Get the jobs from the default server. + * cupsGetJobs2() - Get the jobs from the specified server. + * cupsGetPPD() - Get the PPD file for a printer on the default + * server. + * cupsGetPPD2() - Get the PPD file for a printer from the specified + * server. + * cupsGetPPD3() - Get the PPD file for a printer on the specified + * server if it has changed. + * cupsGetPrinters() - Get a list of printers from the default server. + * cupsGetServerPPD() - Get an available PPD file from the server. + * cupsPrintFile() - Print a file to a printer or class on the default + * server. + * cupsPrintFile2() - Print a file to a printer or class on the + * specified server. + * cupsPrintFiles() - Print one or more files to a printer or class on + * the default server. + * cupsPrintFiles2() - Print one or more files to a printer or class on + * the specified server. + * cupsStartDocument() - Add a document to a job created with + * cupsCreateJob(). + * cups_get_printer_uri() - Get the printer-uri-supported attribute for the + * first printer in a class. + */ + +/* + * Include necessary headers... + */ + +#include "cups-private.h" +#include +#include +#if defined(WIN32) || defined(__EMX__) +# include +#else +# include +#endif /* WIN32 || __EMX__ */ + + +/* + * Local functions... + */ + +static int cups_get_printer_uri(http_t *http, const char *name, + char *host, int hostsize, int *port, + char *resource, int resourcesize, + int depth); + + +/* + * 'cupsCancelJob()' - Cancel a print job on the default server. + * + * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ + * to cancel the current job on the named destination. + * + * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get + * the cause of any failure. + */ + +int /* O - 1 on success, 0 on failure */ +cupsCancelJob(const char *name, /* I - Name of printer or class */ + int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ +{ + return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) + < IPP_REDIRECTION_OTHER_SITE); +} + + +/* + * 'cupsCancelJob2()' - Cancel or purge a print job. + * + * Canceled jobs remain in the job history while purged jobs are removed + * from the job history. + * + * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ + * to cancel the current job on the named destination. + * + * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get + * the cause of any failure. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +ipp_status_t /* O - IPP status */ +cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name, /* I - Name of printer or class */ + int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ + int purge) /* I - 1 to purge, 0 to cancel */ +{ + char uri[HTTP_MAX_URI]; /* Job/printer URI */ + ipp_t *request; /* IPP request */ + + + /* + * Range check input... + */ + + if (job_id < -1 || (!name && job_id == 0)) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (0); + } + + /* + * Connect to the default server as needed... + */ + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (IPP_SERVICE_UNAVAILABLE); + + /* + * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following + * attributes: + * + * attributes-charset + * attributes-natural-language + * job-uri or printer-uri + job-id + * requesting-user-name + * [purge-job] or [purge-jobs] + */ + + request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB); + + if (name) + { + httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, + "localhost", ippPort(), "/printers/%s", name); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, + uri); + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", + job_id); + } + else if (job_id > 0) + { + snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); + } + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", + NULL, cupsUser()); + + if (purge && job_id >= 0) + ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1); + else if (!purge && job_id < 0) + ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0); + + /* + * Do the request... + */ + + ippDelete(cupsDoRequest(http, request, "/jobs/")); + + return (cupsLastError()); +} + + +/* + * 'cupsCreateJob()' - Create an empty job for streaming. + * + * Use this function when you want to stream print data using the + * @link cupsStartDocument@, @link cupsWriteRequestData@, and + * @link cupsFinishDocument@ functions. If you have one or more files to + * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function + * instead. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +int /* O - Job ID or 0 on error */ +cupsCreateJob( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name, /* I - Destination name */ + const char *title, /* I - Title of job */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + char printer_uri[1024], /* Printer URI */ + resource[1024]; /* Printer resource */ + ipp_t *request, /* Create-Job request */ + *response; /* Create-Job response */ + ipp_attribute_t *attr; /* job-id attribute */ + int job_id = 0; /* job-id value */ + + + DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", " + "num_options=%d, options=%p)", + http, name, title, num_options, options)); + + /* + * Range check input... + */ + + if (!name) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + return (0); + } + + /* + * Build a Create-Job request... + */ + + if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); + return (0); + } + + httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", + NULL, "localhost", ippPort(), "/printers/%s", name); + snprintf(resource, sizeof(resource), "/printers/%s", name); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", + NULL, printer_uri); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", + NULL, cupsUser()); + if (title) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, + title); + cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB); + cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION); + + /* + * Send the request and get the job-id... + */ + + response = cupsDoRequest(http, request, resource); + + if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL) + job_id = attr->values[0].integer; + + ippDelete(response); + + /* + * Return it... + */ + + return (job_id); +} + + +/* + * 'cupsFinishDocument()' - Finish sending a document. + * + * The document must have been started using @link cupsStartDocument@. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +ipp_status_t /* O - Status of document submission */ +cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name) /* I - Destination name */ +{ + char resource[1024]; /* Printer resource */ + + + snprintf(resource, sizeof(resource), "/printers/%s", name); + + ippDelete(cupsGetResponse(http, resource)); + + return (cupsLastError()); +} + + +/* + * 'cupsFreeJobs()' - Free memory used by job data. + */ + +void +cupsFreeJobs(int num_jobs, /* I - Number of jobs */ + cups_job_t *jobs) /* I - Jobs */ +{ + int i; /* Looping var */ + cups_job_t *job; /* Current job */ + + + if (num_jobs <= 0 || !jobs) + return; + + for (i = num_jobs, job = jobs; i > 0; i --, job ++) + { + _cupsStrFree(job->dest); + _cupsStrFree(job->user); + _cupsStrFree(job->format); + _cupsStrFree(job->title); + } + + free(jobs); +} + + +/* + * 'cupsGetClasses()' - Get a list of printer classes from the default server. + * + * This function is deprecated - use @link cupsGetDests@ instead. + * + * @deprecated@ + */ + +int /* O - Number of classes */ +cupsGetClasses(char ***classes) /* O - Classes */ +{ + int n; /* Number of classes */ + ipp_t *request, /* IPP Request */ + *response; /* IPP Response */ + ipp_attribute_t *attr; /* Current attribute */ + char **temp; /* Temporary pointer */ + http_t *http; /* Connection to server */ + + + if (!classes) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + return (0); + } + + *classes = NULL; + + if ((http = _cupsConnect()) == NULL) + return (0); + + /* + * Build a CUPS_GET_CLASSES request, which requires the following + * attributes: + * + * attributes-charset + * attributes-natural-language + * requested-attributes + */ + + request = ippNewRequest(CUPS_GET_CLASSES); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "requested-attributes", NULL, "printer-name"); + + /* + * Do the request and get back a response... + */ + + n = 0; + + if ((response = cupsDoRequest(http, request, "/")) != NULL) + { + for (attr = response->attrs; attr != NULL; attr = attr->next) + if (attr->name != NULL && + _cups_strcasecmp(attr->name, "printer-name") == 0 && + attr->value_tag == IPP_TAG_NAME) + { + if (n == 0) + temp = malloc(sizeof(char *)); + else + temp = realloc(*classes, sizeof(char *) * (n + 1)); + + if (temp == NULL) + { + /* + * Ran out of memory! + */ + + while (n > 0) + { + n --; + free((*classes)[n]); + } + + free(*classes); + ippDelete(response); + return (0); + } + + *classes = temp; + temp[n] = strdup(attr->values[0].string.text); + n ++; + } + + ippDelete(response); + } + + return (n); +} + + +/* + * 'cupsGetDefault()' - Get the default printer or class for the default server. + * + * This function returns the default printer or class as defined by + * the LPDEST or PRINTER environment variables. If these environment + * variables are not set, the server default destination is returned. + * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ + * functions to get the user-defined default printer, as this function does + * not support the lpoptions-defined default printer. + */ + +const char * /* O - Default printer or @code NULL@ */ +cupsGetDefault(void) +{ + /* + * Return the default printer... + */ + + return (cupsGetDefault2(CUPS_HTTP_DEFAULT)); +} + + +/* + * 'cupsGetDefault2()' - Get the default printer or class for the specified server. + * + * This function returns the default printer or class as defined by + * the LPDEST or PRINTER environment variables. If these environment + * variables are not set, the server default destination is returned. + * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ + * functions to get the user-defined default printer, as this function does + * not support the lpoptions-defined default printer. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +const char * /* O - Default printer or @code NULL@ */ +cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ +{ + ipp_t *request, /* IPP Request */ + *response; /* IPP Response */ + ipp_attribute_t *attr; /* Current attribute */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * See if we have a user default printer set... + */ + + if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer))) + return (cg->def_printer); + + /* + * Connect to the server as needed... + */ + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (NULL); + + /* + * Build a CUPS_GET_DEFAULT request, which requires the following + * attributes: + * + * attributes-charset + * attributes-natural-language + */ + + request = ippNewRequest(CUPS_GET_DEFAULT); + + /* + * Do the request and get back a response... + */ + + if ((response = cupsDoRequest(http, request, "/")) != NULL) + { + if ((attr = ippFindAttribute(response, "printer-name", + IPP_TAG_NAME)) != NULL) + { + strlcpy(cg->def_printer, attr->values[0].string.text, + sizeof(cg->def_printer)); + ippDelete(response); + return (cg->def_printer); + } + + ippDelete(response); + } + + return (NULL); +} + + +/* + * 'cupsGetJobs()' - Get the jobs from the default server. + * + * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless + * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are + * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns + * jobs that are stopped, canceled, aborted, or completed. + */ + +int /* O - Number of jobs */ +cupsGetJobs(cups_job_t **jobs, /* O - Job data */ + const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ + int myjobs, /* I - 0 = all users, 1 = mine */ + int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ +{ + /* + * Return the jobs... + */ + + return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs)); +} + + + +/* + * 'cupsGetJobs2()' - Get the jobs from the specified server. + * + * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless + * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are + * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns + * jobs that are stopped, canceled, aborted, or completed. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +int /* O - Number of jobs */ +cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + cups_job_t **jobs, /* O - Job data */ + const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ + int myjobs, /* I - 0 = all users, 1 = mine */ + int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ +{ + int n; /* Number of jobs */ + ipp_t *request, /* IPP Request */ + *response; /* IPP Response */ + ipp_attribute_t *attr; /* Current attribute */ + cups_job_t *temp; /* Temporary pointer */ + int id, /* job-id */ + priority, /* job-priority */ + size; /* job-k-octets */ + ipp_jstate_t state; /* job-state */ + time_t completed_time, /* time-at-completed */ + creation_time, /* time-at-creation */ + processing_time; /* time-at-processing */ + const char *dest, /* job-printer-uri */ + *format, /* document-format */ + *title, /* job-name */ + *user; /* job-originating-user-name */ + char uri[HTTP_MAX_URI]; /* URI for jobs */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + static const char * const attrs[] = /* Requested attributes */ + { + "document-format", + "job-id", + "job-k-octets", + "job-name", + "job-originating-user-name", + "job-printer-uri", + "job-priority", + "job-state", + "time-at-completed", + "time-at-creation", + "time-at-processing" + }; + + + /* + * Range check input... + */ + + if (!jobs) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + return (-1); + } + + /* + * Get the right URI... + */ + + if (name) + { + if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, + "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1); + + return (-1); + } + } + else + strcpy(uri, "ipp://localhost/"); + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (-1); + + /* + * Build an IPP_GET_JOBS request, which requires the following + * attributes: + * + * attributes-charset + * attributes-natural-language + * printer-uri + * requesting-user-name + * which-jobs + * my-jobs + * requested-attributes + */ + + request = ippNewRequest(IPP_GET_JOBS); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, + "printer-uri", NULL, uri); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, + "requesting-user-name", NULL, cupsUser()); + + if (myjobs) + ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); + + if (whichjobs == CUPS_WHICHJOBS_COMPLETED) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "which-jobs", NULL, "completed"); + else if (whichjobs == CUPS_WHICHJOBS_ALL) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "which-jobs", NULL, "all"); + + ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "requested-attributes", sizeof(attrs) / sizeof(attrs[0]), + NULL, attrs); + + /* + * Do the request and get back a response... + */ + + n = 0; + *jobs = NULL; + + if ((response = cupsDoRequest(http, request, "/")) != NULL) + { + for (attr = response->attrs; attr; attr = attr->next) + { + /* + * Skip leading attributes until we hit a job... + */ + + while (attr && attr->group_tag != IPP_TAG_JOB) + attr = attr->next; + + if (!attr) + break; + + /* + * Pull the needed attributes from this job... + */ + + id = 0; + size = 0; + priority = 50; + state = IPP_JOB_PENDING; + user = "unknown"; + dest = NULL; + format = "application/octet-stream"; + title = "untitled"; + creation_time = 0; + completed_time = 0; + processing_time = 0; + + while (attr && attr->group_tag == IPP_TAG_JOB) + { + if (!strcmp(attr->name, "job-id") && + attr->value_tag == IPP_TAG_INTEGER) + id = attr->values[0].integer; + else if (!strcmp(attr->name, "job-state") && + attr->value_tag == IPP_TAG_ENUM) + state = (ipp_jstate_t)attr->values[0].integer; + else if (!strcmp(attr->name, "job-priority") && + attr->value_tag == IPP_TAG_INTEGER) + priority = attr->values[0].integer; + else if (!strcmp(attr->name, "job-k-octets") && + attr->value_tag == IPP_TAG_INTEGER) + size = attr->values[0].integer; + else if (!strcmp(attr->name, "time-at-completed") && + attr->value_tag == IPP_TAG_INTEGER) + completed_time = attr->values[0].integer; + else if (!strcmp(attr->name, "time-at-creation") && + attr->value_tag == IPP_TAG_INTEGER) + creation_time = attr->values[0].integer; + else if (!strcmp(attr->name, "time-at-processing") && + attr->value_tag == IPP_TAG_INTEGER) + processing_time = attr->values[0].integer; + else if (!strcmp(attr->name, "job-printer-uri") && + attr->value_tag == IPP_TAG_URI) + { + if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL) + dest ++; + } + else if (!strcmp(attr->name, "job-originating-user-name") && + attr->value_tag == IPP_TAG_NAME) + user = attr->values[0].string.text; + else if (!strcmp(attr->name, "document-format") && + attr->value_tag == IPP_TAG_MIMETYPE) + format = attr->values[0].string.text; + else if (!strcmp(attr->name, "job-name") && + (attr->value_tag == IPP_TAG_TEXT || + attr->value_tag == IPP_TAG_NAME)) + title = attr->values[0].string.text; + + attr = attr->next; + } + + /* + * See if we have everything needed... + */ + + if (!dest || !id) + { + if (!attr) + break; + else + continue; + } + + /* + * Allocate memory for the job... + */ + + if (n == 0) + temp = malloc(sizeof(cups_job_t)); + else + temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1)); + + if (!temp) + { + /* + * Ran out of memory! + */ + + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + + cupsFreeJobs(n, *jobs); + *jobs = NULL; + + ippDelete(response); + + return (-1); + } + + *jobs = temp; + temp += n; + n ++; + + /* + * Copy the data over... + */ + + temp->dest = _cupsStrAlloc(dest); + temp->user = _cupsStrAlloc(user); + temp->format = _cupsStrAlloc(format); + temp->title = _cupsStrAlloc(title); + temp->id = id; + temp->priority = priority; + temp->state = state; + temp->size = size; + temp->completed_time = completed_time; + temp->creation_time = creation_time; + temp->processing_time = processing_time; + + if (!attr) + break; + } + + ippDelete(response); + } + + if (n == 0 && cg->last_error >= IPP_BAD_REQUEST) + return (-1); + else + return (n); +} + + +/* + * 'cupsGetPPD()' - Get the PPD file for a printer on the default server. + * + * For classes, @code cupsGetPPD@ returns the PPD file for the first printer + * in the class. + * + * The returned filename is stored in a static buffer and is overwritten with + * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the + * file that is created and must @code unlink@ the returned filename. + */ + +const char * /* O - Filename for PPD file */ +cupsGetPPD(const char *name) /* I - Destination name */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + time_t modtime = 0; /* Modification time */ + + + /* + * Return the PPD file... + */ + + cg->ppd_filename[0] = '\0'; + + if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename, + sizeof(cg->ppd_filename)) == HTTP_OK) + return (cg->ppd_filename); + else + return (NULL); +} + + +/* + * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server. + * + * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer + * in the class. + * + * The returned filename is stored in a static buffer and is overwritten with + * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the + * file that is created and must @code unlink@ the returned filename. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +const char * /* O - Filename for PPD file */ +cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name) /* I - Destination name */ +{ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + time_t modtime = 0; /* Modification time */ + + + cg->ppd_filename[0] = '\0'; + + if (cupsGetPPD3(http, name, &modtime, cg->ppd_filename, + sizeof(cg->ppd_filename)) == HTTP_OK) + return (cg->ppd_filename); + else + return (NULL); +} + + +/* + * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified + * server if it has changed. + * + * The "modtime" parameter contains the modification time of any + * locally-cached content and is updated with the time from the PPD file on + * the server. + * + * The "buffer" parameter contains the local PPD filename. If it contains + * the empty string, a new temporary file is created, otherwise the existing + * file will be overwritten as needed. The caller "owns" the file that is + * created and must @code unlink@ the returned filename. + * + * On success, @code HTTP_OK@ is returned for a new PPD file and + * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other + * status is an error. + * + * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer + * in the class. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +http_status_t /* O - HTTP status */ +cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ + const char *name, /* I - Destination name */ + time_t *modtime, /* IO - Modification time */ + char *buffer, /* I - Filename buffer */ + size_t bufsize) /* I - Size of filename buffer */ +{ + int http_port; /* Port number */ + char http_hostname[HTTP_MAX_HOST]; + /* Hostname associated with connection */ + http_t *http2; /* Alternate HTTP connection */ + int fd; /* PPD file */ + char localhost[HTTP_MAX_URI],/* Local hostname */ + hostname[HTTP_MAX_URI], /* Hostname */ + resource[HTTP_MAX_URI]; /* Resource name */ + int port; /* Port number */ + http_status_t status; /* HTTP status from server */ + char tempfile[1024] = ""; /* Temporary filename */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + /* + * Range check input... + */ + + DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, " + "bufsize=%d)", http, name, modtime, + modtime ? (int)*modtime : 0, buffer, (int)bufsize)); + + if (!name) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name"), 1); + return (HTTP_NOT_ACCEPTABLE); + } + + if (!modtime) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time"), 1); + return (HTTP_NOT_ACCEPTABLE); + } + + if (!buffer || bufsize <= 1) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer"), 1); + return (HTTP_NOT_ACCEPTABLE); + } + +#ifndef WIN32 + /* + * See if the PPD file is available locally... + */ + + if (http) + httpGetHostname(http, hostname, sizeof(hostname)); + else + { + strlcpy(hostname, cupsServer(), sizeof(hostname)); + if (hostname[0] == '/') + strlcpy(hostname, "localhost", sizeof(hostname)); + } + + if (!_cups_strcasecmp(hostname, "localhost")) + { + char ppdname[1024]; /* PPD filename */ + struct stat ppdinfo; /* PPD file information */ + + + snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot, + name); + if (!stat(ppdname, &ppdinfo)) + { + /* + * OK, the file exists, use it! + */ + + if (buffer[0]) + { + unlink(buffer); + + if (symlink(ppdname, buffer) && errno != EEXIST) + { + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + + return (HTTP_SERVER_ERROR); + } + } + else + { + int tries; /* Number of tries */ + const char *tmpdir; /* TMPDIR environment variable */ + struct timeval curtime; /* Current time */ + + /* + * Previously we put root temporary files in the default CUPS temporary + * directory under /var/spool/cups. However, since the scheduler cleans + * out temporary files there and runs independently of the user apps, we + * don't want to use it unless specifically told to by cupsd. + */ + + if ((tmpdir = getenv("TMPDIR")) == NULL) +# ifdef __APPLE__ + tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */ +# else + tmpdir = "/tmp"; +# endif /* __APPLE__ */ + + /* + * Make the temporary name using the specified directory... + */ + + tries = 0; + + do + { + /* + * Get the current time of day... + */ + + gettimeofday(&curtime, NULL); + + /* + * Format a string using the hex time values... + */ + + snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir, + (unsigned long)curtime.tv_sec, + (unsigned long)curtime.tv_usec); + + /* + * Try to make a symlink... + */ + + if (!symlink(ppdname, buffer)) + break; + + tries ++; + } + while (tries < 1000); + + if (tries >= 1000) + { + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + + return (HTTP_SERVER_ERROR); + } + } + + if (*modtime >= ppdinfo.st_mtime) + return (HTTP_NOT_MODIFIED); + else + { + *modtime = ppdinfo.st_mtime; + return (HTTP_OK); + } + } + } +#endif /* !WIN32 */ + + /* + * Try finding a printer URI for this printer... + */ + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (HTTP_SERVICE_UNAVAILABLE); + + if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, + resource, sizeof(resource), 0)) + return (HTTP_NOT_FOUND); + + DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname, + port)); + + /* + * Remap local hostname to localhost... + */ + + httpGetHostname(NULL, localhost, sizeof(localhost)); + + DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost)); + + if (!_cups_strcasecmp(localhost, hostname)) + strcpy(hostname, "localhost"); + + /* + * Get the hostname and port number we are connected to... + */ + + httpGetHostname(http, http_hostname, sizeof(http_hostname)); + http_port = _httpAddrPort(http->hostaddr); + + DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d", + http_hostname, http_port)); + + /* + * Reconnect to the correct server as needed... + */ + + if (!_cups_strcasecmp(http_hostname, hostname) && port == http_port) + http2 = http; + else if ((http2 = httpConnectEncrypt(hostname, port, + cupsEncryption())) == NULL) + { + DEBUG_puts("1cupsGetPPD3: Unable to connect to server"); + + return (HTTP_SERVICE_UNAVAILABLE); + } + + /* + * Get a temp file... + */ + + if (buffer[0]) + fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600); + else + fd = cupsTempFd(tempfile, sizeof(tempfile)); + + if (fd < 0) + { + /* + * Can't open file; close the server connection and return NULL... + */ + + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + + if (http2 != http) + httpClose(http2); + + return (HTTP_SERVER_ERROR); + } + + /* + * And send a request to the HTTP server... + */ + + strlcat(resource, ".ppd", sizeof(resource)); + + if (*modtime > 0) + httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE, + httpGetDateString(*modtime)); + + status = cupsGetFd(http2, resource, fd); + + close(fd); + + /* + * See if we actually got the file or an error... + */ + + if (status == HTTP_OK) + { + *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE)); + + if (tempfile[0]) + strlcpy(buffer, tempfile, bufsize); + } + else if (status != HTTP_NOT_MODIFIED) + { + _cupsSetHTTPError(status); + + if (buffer[0]) + unlink(buffer); + else if (tempfile[0]) + unlink(tempfile); + } + else if (tempfile[0]) + unlink(tempfile); + + if (http2 != http) + httpClose(http2); + + /* + * Return the PPD file... + */ + + DEBUG_printf(("1cupsGetPPD3: Returning status %d", status)); + + return (status); +} + + +/* + * 'cupsGetPrinters()' - Get a list of printers from the default server. + * + * This function is deprecated - use @link cupsGetDests@ instead. + * + * @deprecated@ + */ + +int /* O - Number of printers */ +cupsGetPrinters(char ***printers) /* O - Printers */ +{ + int n; /* Number of printers */ + ipp_t *request, /* IPP Request */ + *response; /* IPP Response */ + ipp_attribute_t *attr; /* Current attribute */ + char **temp; /* Temporary pointer */ + http_t *http; /* Connection to server */ + + + /* + * Range check input... + */ + + if (!printers) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + return (0); + } + + *printers = NULL; + + /* + * Try to connect to the server... + */ + + if ((http = _cupsConnect()) == NULL) + return (0); + + /* + * Build a CUPS_GET_PRINTERS request, which requires the following + * attributes: + * + * attributes-charset + * attributes-natural-language + * requested-attributes + */ + + request = ippNewRequest(CUPS_GET_PRINTERS); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, + "requested-attributes", NULL, "printer-name"); + + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, + "printer-type", 0); + + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, + "printer-type-mask", CUPS_PRINTER_CLASS); + + /* + * Do the request and get back a response... + */ + + n = 0; + + if ((response = cupsDoRequest(http, request, "/")) != NULL) + { + for (attr = response->attrs; attr != NULL; attr = attr->next) + if (attr->name != NULL && + _cups_strcasecmp(attr->name, "printer-name") == 0 && + attr->value_tag == IPP_TAG_NAME) + { + if (n == 0) + temp = malloc(sizeof(char *)); + else + temp = realloc(*printers, sizeof(char *) * (n + 1)); + + if (temp == NULL) + { + /* + * Ran out of memory! + */ + + while (n > 0) + { + n --; + free((*printers)[n]); + } + + free(*printers); + ippDelete(response); + return (0); + } + + *printers = temp; + temp[n] = strdup(attr->values[0].string.text); + n ++; + } + + ippDelete(response); + } + + return (n); +} + + +/* + * 'cupsGetServerPPD()' - Get an available PPD file from the server. + * + * This function returns the named PPD file from the server. The + * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@ + * operation. + * + * You must remove (unlink) the PPD file when you are finished with + * it. The PPD filename is stored in a static location that will be + * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@, + * or @link cupsGetServerPPD@. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +char * /* O - Name of PPD file or @code NULL@ on error */ +cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name) /* I - Name of PPD file ("ppd-name") */ +{ + int fd; /* PPD file descriptor */ + ipp_t *request; /* IPP request */ + _cups_globals_t *cg = _cupsGlobals(); + /* Pointer to library globals */ + + + /* + * Range check input... + */ + + if (!name) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name"), 1); + + return (NULL); + } + + if (!http) + if ((http = _cupsConnect()) == NULL) + return (NULL); + + /* + * Get a temp file... + */ + + if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0) + { + /* + * Can't open file; close the server connection and return NULL... + */ + + _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); + + return (NULL); + } + + /* + * Get the PPD file... + */ + + request = ippNewRequest(CUPS_GET_PPD); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL, + name); + + ippDelete(cupsDoIORequest(http, request, "/", -1, fd)); + + close(fd); + + if (cupsLastError() != IPP_OK) + { + unlink(cg->ppd_filename); + return (NULL); + } + else + return (cg->ppd_filename); +} + + +/* + * 'cupsPrintFile()' - Print a file to a printer or class on the default server. + */ + +int /* O - Job ID or 0 on error */ +cupsPrintFile(const char *name, /* I - Destination name */ + const char *filename, /* I - File to print */ + const char *title, /* I - Title of job */ + int num_options,/* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", " + "title=\"%s\", num_options=%d, options=%p)", + name, filename, title, num_options, options)); + + return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, + num_options, options)); +} + + +/* + * 'cupsPrintFile2()' - Print a file to a printer or class on the specified + * server. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +int /* O - Job ID or 0 on error */ +cupsPrintFile2( + http_t *http, /* I - Connection to server */ + const char *name, /* I - Destination name */ + const char *filename, /* I - File to print */ + const char *title, /* I - Title of job */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", " + "title=\"%s\", num_options=%d, options=%p)", + http, name, filename, title, num_options, options)); + + return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, + options)); +} + + +/* + * 'cupsPrintFiles()' - Print one or more files to a printer or class on the + * default server. + */ + +int /* O - Job ID or 0 on error */ +cupsPrintFiles( + const char *name, /* I - Destination name */ + int num_files, /* I - Number of files */ + const char **files, /* I - File(s) to print */ + const char *title, /* I - Title of job */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, " + "files=%p, title=\"%s\", num_options=%d, options=%p)", + name, num_files, files, title, num_options, options)); + + /* + * Print the file(s)... + */ + + return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title, + num_options, options)); +} + + +/* + * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the + * specified server. + * + * @since CUPS 1.1.21/OS X 10.4@ + */ + +int /* O - Job ID or 0 on error */ +cupsPrintFiles2( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name, /* I - Destination name */ + int num_files, /* I - Number of files */ + const char **files, /* I - File(s) to print */ + const char *title, /* I - Title of job */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + int i; /* Looping var */ + int job_id; /* New job ID */ + const char *docname; /* Basename of current filename */ + const char *format; /* Document format */ + cups_file_t *fp; /* Current file */ + char buffer[8192]; /* Copy buffer */ + ssize_t bytes; /* Bytes in buffer */ + http_status_t status; /* Status of write */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + ipp_status_t cancel_status; /* Status code to preserve */ + char *cancel_message; /* Error message to preserve */ + + + DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, " + "files=%p, title=\"%s\", num_options=%d, options=%p)", + http, name, num_files, files, title, num_options, options)); + + /* + * Range check input... + */ + + if (!name || num_files < 1 || !files) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); + + return (0); + } + + /* + * Create the print job... + */ + + if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0) + return (0); + + /* + * Send each of the files... + */ + + if (cupsGetOption("raw", num_options, options)) + format = CUPS_FORMAT_RAW; + else if ((format = cupsGetOption("document-format", num_options, + options)) == NULL) + format = CUPS_FORMAT_AUTO; + + for (i = 0; i < num_files; i ++) + { + /* + * Start the next file... + */ + + if ((docname = strrchr(files[i], '/')) != NULL) + docname ++; + else + docname = files[i]; + + if ((fp = cupsFileOpen(files[i], "rb")) == NULL) + { + /* + * Unable to open print file, cancel the job and return... + */ + + _cupsSetError(IPP_DOCUMENT_ACCESS_ERROR, NULL, 0); + goto cancel_job; + } + + status = cupsStartDocument(http, name, job_id, docname, format, + i == (num_files - 1)); + + while (status == HTTP_CONTINUE && + (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) + status = cupsWriteRequestData(http, buffer, bytes); + + cupsFileClose(fp); + + if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK) + { + /* + * Unable to queue, cancel the job and return... + */ + + goto cancel_job; + } + } + + return (job_id); + + /* + * If we get here, something happened while sending the print job so we need + * to cancel the job without setting the last error (since we need to preserve + * the current error... + */ + + cancel_job: + + cancel_status = cg->last_error; + cancel_message = cg->last_status_message ? + _cupsStrRetain(cg->last_status_message) : NULL; + + cupsCancelJob2(http, name, job_id, 0); + + cg->last_error = cancel_status; + cg->last_status_message = cancel_message; + + return (0); +} + + +/* + * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob(). + * + * Use @link cupsWriteRequestData@ to write data for the document and + * @link cupsFinishDocument@ to finish the document and get the submission status. + * + * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@, + * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and + * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although + * any supported MIME type string can be supplied. + * + * @since CUPS 1.4/OS X 10.6@ + */ + +http_status_t /* O - HTTP status of request */ +cupsStartDocument( + http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ + const char *name, /* I - Destination name */ + int job_id, /* I - Job ID from @link cupsCreateJob@ */ + const char *docname, /* I - Name of document */ + const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */ + int last_document) /* I - 1 for last document in job, 0 otherwise */ +{ + char resource[1024], /* Resource for destinatio */ + printer_uri[1024]; /* Printer URI */ + ipp_t *request; /* Send-Document request */ + http_status_t status; /* HTTP status */ + + + /* + * Create a Send-Document request... + */ + + if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL) + { + _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); + return (HTTP_ERROR); + } + + httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", + NULL, "localhost", ippPort(), "/printers/%s", name); + snprintf(resource, sizeof(resource), "/printers/%s", name); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", + NULL, printer_uri); + ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id); + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", + NULL, cupsUser()); + if (docname) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name", + NULL, docname); + if (format) + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, + "document-format", NULL, format); + ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document); + + /* + * Send and delete the request, then return the status... + */ + + status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE); + + ippDelete(request); + + return (status); +} + + +/* + * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the + * first printer in a class. + */ + +static int /* O - 1 on success, 0 on failure */ +cups_get_printer_uri( + http_t *http, /* I - Connection to server */ + const char *name, /* I - Name of printer or class */ + char *host, /* I - Hostname buffer */ + int hostsize, /* I - Size of hostname buffer */ + int *port, /* O - Port number */ + char *resource, /* I - Resource buffer */ + int resourcesize, /* I - Size of resource buffer */ + int depth) /* I - Depth of query */ +{ + int i; /* Looping var */ + int http_port; /* Port number */ + http_t *http2; /* Alternate HTTP connection */ + ipp_t *request, /* IPP request */ + *response; /* IPP response */ + ipp_attribute_t *attr; /* Current attribute */ + char uri[HTTP_MAX_URI], /* printer-uri attribute */ + scheme[HTTP_MAX_URI], /* Scheme name */ + username[HTTP_MAX_URI], /* Username:password */ + classname[255], /* Temporary class name */ + http_hostname[HTTP_MAX_HOST]; + /* Hostname associated with connection */ + static const char * const requested_attrs[] = + { /* Requested attributes */ + "device-uri", + "member-uris", + "printer-uri-supported", + "printer-type" + }; + + + DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, " + "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)", + http, name, host, hostsize, resource, resourcesize, depth)); + + /* + * Setup the printer URI... + */ + + if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, + "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) + { + _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1); + + *host = '\0'; + *resource = '\0'; + + return (0); + } + + DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri)); + + /* + * Get the hostname and port number we are connected to... + */ + + httpGetHostname(http, http_hostname, sizeof(http_hostname)); + http_port = _httpAddrPort(http->hostaddr); + + /* + * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following + * attributes: + * + * attributes-charset + * attributes-natural-language + * printer-uri + * requested-attributes + */ + + request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); + + ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", + NULL, uri); + + ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME, + "requested-attributes", + sizeof(requested_attrs) / sizeof(requested_attrs[0]), + NULL, requested_attrs); + + /* + * Do the request and get back a response... + */ + + snprintf(resource, resourcesize, "/printers/%s", name); + + if ((response = cupsDoRequest(http, request, resource)) != NULL) + { + const char *device_uri = NULL; /* device-uri value */ + + if ((attr = ippFindAttribute(response, "device-uri", + IPP_TAG_URI)) != NULL) + device_uri = attr->values[0].string.text; + + if (device_uri && + (!strncmp(device_uri, "ipp://", 6) || + !strncmp(device_uri, "ipps://", 7) || + ((strstr(device_uri, "._ipp.") != NULL || + strstr(device_uri, "._ipps.") != NULL) && + !strcmp(device_uri + strlen(device_uri) - 5, "/cups")))) + { + /* + * Statically-configured shared printer. + */ + + httpSeparateURI(HTTP_URI_CODING_ALL, + _httpResolveURI(device_uri, uri, sizeof(uri), + _HTTP_RESOLVE_DEFAULT, NULL, NULL), + scheme, sizeof(scheme), username, sizeof(username), + host, hostsize, port, resource, resourcesize); + ippDelete(response); + + return (1); + } + else if ((attr = ippFindAttribute(response, "member-uris", + IPP_TAG_URI)) != NULL) + { + /* + * Get the first actual printer name in the class... + */ + + for (i = 0; i < attr->num_values; i ++) + { + httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, + scheme, sizeof(scheme), username, sizeof(username), + host, hostsize, port, resource, resourcesize); + if (!strncmp(resource, "/printers/", 10)) + { + /* + * Found a printer! + */ + + ippDelete(response); + + return (1); + } + } + + /* + * No printers in this class - try recursively looking for a printer, + * but not more than 3 levels deep... + */ + + if (depth < 3) + { + for (i = 0; i < attr->num_values; i ++) + { + httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, + scheme, sizeof(scheme), username, sizeof(username), + host, hostsize, port, resource, resourcesize); + if (!strncmp(resource, "/classes/", 9)) + { + /* + * Found a class! Connect to the right server... + */ + + if (!_cups_strcasecmp(http_hostname, host) && *port == http_port) + http2 = http; + else if ((http2 = httpConnectEncrypt(host, *port, + cupsEncryption())) == NULL) + { + DEBUG_puts("8cups_get_printer_uri: Unable to connect to server"); + + continue; + } + + /* + * Look up printers on that server... + */ + + strlcpy(classname, resource + 9, sizeof(classname)); + + cups_get_printer_uri(http2, classname, host, hostsize, port, + resource, resourcesize, depth + 1); + + /* + * Close the connection as needed... + */ + + if (http2 != http) + httpClose(http2); + + if (*host) + return (1); + } + } + } + } + else if ((attr = ippFindAttribute(response, "printer-uri-supported", + IPP_TAG_URI)) != NULL) + { + httpSeparateURI(HTTP_URI_CODING_ALL, + _httpResolveURI(attr->values[0].string.text, uri, + sizeof(uri), _HTTP_RESOLVE_DEFAULT, + NULL, NULL), + scheme, sizeof(scheme), username, sizeof(username), + host, hostsize, port, resource, resourcesize); + ippDelete(response); + + if (!strncmp(resource, "/classes/", 9)) + { + _cupsSetError(IPP_INTERNAL_ERROR, + _("No printer-uri found for class"), 1); + + *host = '\0'; + *resource = '\0'; + + return (0); + } + + return (1); + } + + ippDelete(response); + } + + if (cupsLastError() != IPP_NOT_FOUND) + _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found"), 1); + + *host = '\0'; + *resource = '\0'; + + return (0); +} + + +/* + * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $". + */ diff --git a/cups/versioning.h b/cups/versioning.h new file mode 100644 index 0000000..ad2b5d2 --- /dev/null +++ b/cups/versioning.h @@ -0,0 +1,90 @@ +/* + * "$Id: versioning.h 3794 2012-04-23 22:44:16Z msweet $" + * + * API versioning definitions for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * + * 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. + */ + +#ifndef _CUPS_VERSIONING_H_ +# define _CUPS_VERSIONING_H_ + +/* + * This header defines several constants - _CUPS_DEPRECATED, + * _CUPS_API_1_1, _CUPS_API_1_1_19, _CUPS_API_1_1_20, _CUPS_API_1_1_21, + * _CUPS_API_1_2, _CUPS_API_1_3, _CUPS_API_1_4, _CUPS_API_1_5, _CUPS_API_1_6 - + * which add compiler-specific attributes that flag functions that are + * deprecated or added in particular releases. + * + * On OS X, the _CUPS_API_* constants are defined based on the values of + * the MAC_OS_X_VERSION_MIN_ALLOWED and MAC_OS_X_VERSION_MAX_ALLOWED constants + * provided by the compiler. + */ + +# if defined(__APPLE__) && !defined(_CUPS_SOURCE) +# include +# ifndef AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER +# define AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER __attribute__((unavailable)) +# endif /* !AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER */ +# ifndef AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER +# define AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER __attribute__((unavailable)) +# endif /* !AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER */ +# ifndef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER +# define AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER __attribute__((unavailable)) +# endif /* !AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER */ +# ifndef AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER +# define AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER __attribute__((unavailable)) +# endif /* !AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER */ +# ifndef AVAILABLE_MAC_OS_X_VERSION_10_7_AND_LATER +# define AVAILABLE_MAC_OS_X_VERSION_10_7_AND_LATER __attribute__((unavailable)) +# endif /* !AVAILABLE_MAC_OS_X_VERSION_10_7_AND_LATER */ +# ifndef AVAILABLE_MAC_OS_X_VERSION_10_8_AND_LATER +# define AVAILABLE_MAC_OS_X_VERSION_10_8_AND_LATER __attribute__((unavailable)) +# endif /* !AVAILABLE_MAC_OS_X_VERSION_10_8_AND_LATER */ +# define _CUPS_API_1_1_19 AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER +# define _CUPS_API_1_1_20 AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER +# define _CUPS_API_1_1_21 AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER +# define _CUPS_API_1_2 AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER +# define _CUPS_API_1_3 AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER +# define _CUPS_API_1_4 AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER +# define _CUPS_API_1_5 AVAILABLE_MAC_OS_X_VERSION_10_7_AND_LATER +# define _CUPS_API_1_6 +# else +# define _CUPS_API_1_1_19 +# define _CUPS_API_1_1_20 +# define _CUPS_API_1_1_21 +# define _CUPS_API_1_2 +# define _CUPS_API_1_3 +# define _CUPS_API_1_4 +# define _CUPS_API_1_5 +# define _CUPS_API_1_6 +# endif /* __APPLE__ && !_CUPS_SOURCE */ + +/* + * With GCC 3.0 and higher, we can mark old APIs "deprecated" so you get + * a warning at compile-time. + */ + +# if defined(__GNUC__) && __GNUC__ > 2 && !defined(_CUPS_SOURCE) +# define _CUPS_DEPRECATED __attribute__ ((__deprecated__)) +# else +# define _CUPS_DEPRECATED +# endif /* __GNUC__ && __GNUC__ > 2 && !_CUPS_SOURCE */ + +# ifndef __GNUC__ +# define __attribute__(x) +# endif /* !__GNUC__ */ + +#endif /* !_CUPS_VERSIONING_H_ */ + +/* + * End of "$Id: versioning.h 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/filter/Dependencies b/filter/Dependencies new file mode 100644 index 0000000..159135b --- /dev/null +++ b/filter/Dependencies @@ -0,0 +1,61 @@ +error.o: error.c ../cups/raster-private.h ../cups/raster.h ../cups/cups.h \ + ../cups/file.h ../cups/versioning.h ../cups/ipp.h ../cups/http.h \ + ../cups/array.h ../cups/language.h ../cups/ppd.h \ + ../cups/debug-private.h ../cups/string-private.h ../config.h +interpret.o: interpret.c ../cups/raster-private.h ../cups/raster.h \ + ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ + ../cups/http.h ../cups/array.h ../cups/language.h ../cups/ppd.h \ + ../cups/debug-private.h ../cups/string-private.h ../config.h +raster.o: raster.c ../cups/raster-private.h ../cups/raster.h \ + ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ + ../cups/http.h ../cups/array.h ../cups/language.h ../cups/ppd.h \ + ../cups/debug-private.h ../cups/string-private.h ../config.h +commandtops.o: commandtops.c ../cups/cups-private.h \ + ../cups/string-private.h ../config.h ../cups/debug-private.h \ + ../cups/versioning.h ../cups/ipp-private.h ../cups/ipp.h \ + ../cups/http.h ../cups/array.h ../cups/http-private.h \ + ../cups/md5-private.h ../cups/language-private.h ../cups/transcode.h \ + ../cups/language.h ../cups/pwg-private.h ../cups/cups.h ../cups/file.h \ + ../cups/ppd-private.h ../cups/ppd.h ../cups/thread-private.h \ + ../cups/sidechannel.h +gziptoany.o: gziptoany.c ../cups/cups-private.h ../cups/string-private.h \ + ../config.h ../cups/debug-private.h ../cups/versioning.h \ + ../cups/ipp-private.h ../cups/ipp.h ../cups/http.h ../cups/array.h \ + ../cups/http-private.h ../cups/md5-private.h \ + ../cups/language-private.h ../cups/transcode.h ../cups/language.h \ + ../cups/pwg-private.h ../cups/cups.h ../cups/file.h \ + ../cups/ppd-private.h ../cups/ppd.h ../cups/thread-private.h +common.o: common.c common.h ../cups/string-private.h ../config.h \ + ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ + ../cups/http.h ../cups/array.h ../cups/language.h ../cups/ppd.h +pstops.o: pstops.c common.h ../cups/string-private.h ../config.h \ + ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ + ../cups/http.h ../cups/array.h ../cups/language.h ../cups/ppd.h \ + ../cups/language-private.h ../cups/transcode.h +rasterbench.o: rasterbench.c ../config.h ../cups/raster.h ../cups/cups.h \ + ../cups/file.h ../cups/versioning.h ../cups/ipp.h ../cups/http.h \ + ../cups/array.h ../cups/language.h ../cups/ppd.h +rastertoepson.o: rastertoepson.c ../cups/cups.h ../cups/file.h \ + ../cups/versioning.h ../cups/ipp.h ../cups/http.h ../cups/array.h \ + ../cups/language.h ../cups/ppd.h ../cups/string-private.h ../config.h \ + ../cups/language-private.h ../cups/transcode.h ../cups/raster.h +rastertohp.o: rastertohp.c ../cups/cups.h ../cups/file.h \ + ../cups/versioning.h ../cups/ipp.h ../cups/http.h ../cups/array.h \ + ../cups/language.h ../cups/ppd.h ../cups/string-private.h ../config.h \ + ../cups/language-private.h ../cups/transcode.h ../cups/raster.h +rastertolabel.o: rastertolabel.c ../cups/cups.h ../cups/file.h \ + ../cups/versioning.h ../cups/ipp.h ../cups/http.h ../cups/array.h \ + ../cups/language.h ../cups/ppd.h ../cups/string-private.h ../config.h \ + ../cups/language-private.h ../cups/transcode.h ../cups/raster.h +rastertopwg.o: rastertopwg.c ../cups/cups-private.h \ + ../cups/string-private.h ../config.h ../cups/debug-private.h \ + ../cups/versioning.h ../cups/ipp-private.h ../cups/ipp.h \ + ../cups/http.h ../cups/array.h ../cups/http-private.h \ + ../cups/md5-private.h ../cups/language-private.h ../cups/transcode.h \ + ../cups/language.h ../cups/pwg-private.h ../cups/cups.h ../cups/file.h \ + ../cups/ppd-private.h ../cups/ppd.h ../cups/thread-private.h \ + ../cups/raster.h +testraster.o: testraster.c ../cups/raster-private.h ../cups/raster.h \ + ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ + ../cups/http.h ../cups/array.h ../cups/language.h ../cups/ppd.h \ + ../cups/debug-private.h ../cups/string-private.h ../config.h diff --git a/filter/Makefile b/filter/Makefile new file mode 100644 index 0000000..85f9d8a --- /dev/null +++ b/filter/Makefile @@ -0,0 +1,400 @@ +# +# "$Id: Makefile 7871 2008-08-27 21:12:43Z mike $" +# +# Filter makefile for CUPS. +# +# Copyright 2007-2012 by Apple Inc. +# Copyright 1997-2006 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. +# + +include ../Makedefs + + +FILTERS = \ + commandtops \ + gziptoany \ + pstops \ + rastertoepson \ + rastertohp \ + rastertolabel \ + rastertopwg +LIBTARGETS = \ + $(LIBCUPSIMAGE) \ + libcupsimage.a +UNITTARGETS = \ + rasterbench \ + testraster +TARGETS = \ + $(LIBTARGETS) \ + $(FILTERS) + +IMAGEOBJS = error.o interpret.o raster.o +OBJS = $(IMAGEOBJS) \ + commandtops.o gziptoany.o common.o pstops.o \ + rasterbench.o rastertoepson.o rastertohp.o rastertolabel.o \ + rastertopwg.o testraster.o + + +# +# Make all targets... +# + +all: $(TARGETS) + + +# +# Make library targets... +# + +libs: $(LIBTARGETS) + + +# +# Make unit tests... +# + +unittests: $(UNITTARGETS) + + +# +# Clean all object files... +# + +clean: + $(RM) $(OBJS) $(TARGETS) $(UNITTARGETS) + $(RM) libcupsimage.so libcupsimage.sl libcupsimage.dylib + + +# +# Update dependencies (without system header dependencies...) +# + +depend: + $(CC) -MM $(ALL_CFLAGS) $(OBJS:.o=.c) >Dependencies + + +# +# Install all targets... +# + +install: all install-data install-headers install-libs install-exec + + +# +# Install data files... +# + +install-data: + + +# +# Install programs... +# + +install-exec: + $(INSTALL_DIR) -m 755 $(SERVERBIN)/filter + for file in $(FILTERS); do \ + $(INSTALL_BIN) $$file $(SERVERBIN)/filter; \ + done + $(RM) $(SERVERBIN)/filter/rastertodymo + $(LN) rastertolabel $(SERVERBIN)/filter/rastertodymo + if test "x$(SYMROOT)" != "x"; then \ + $(INSTALL_DIR) $(SYMROOT); \ + for file in $(FILTERS); do \ + cp $$file $(SYMROOT); \ + done \ + fi + + +# +# Install headers... +# + +install-headers: + + +# +# Install libraries... +# + +install-libs: $(INSTALLSTATIC) + $(INSTALL_DIR) -m 755 $(LIBDIR) + $(INSTALL_LIB) $(LIBCUPSIMAGE) $(LIBDIR) + -if test $(LIBCUPSIMAGE) = "libcupsimage.so.2" -o $(LIBCUPSIMAGE) = "libcupsimage.sl.2"; then \ + $(RM) $(LIBDIR)/`basename $(LIBCUPSIMAGE) .2`; \ + $(LN) $(LIBCUPSIMAGE) $(LIBDIR)/`basename $(LIBCUPSIMAGE) .2`; \ + fi + -if test $(LIBCUPSIMAGE) = "libcupsimage.2.dylib"; then \ + $(RM) $(LIBDIR)/libcupsimage.dylib; \ + $(LN) $(LIBCUPSIMAGE) $(LIBDIR)/libcupsimage.dylib; \ + fi + if test "x$(SYMROOT)" != "x"; then \ + $(INSTALL_DIR) $(SYMROOT); \ + cp $(LIBCUPSIMAGE) $(SYMROOT); \ + fi + +installstatic: + $(INSTALL_DIR) -m 755 $(LIBDIR) + $(INSTALL_LIB) -m 755 libcupsimage.a $(LIBDIR) + $(RANLIB) $(LIBDIR)/libcupsimage.a + $(CHMOD) 555 $(LIBDIR)/libcupsimage.a + + +# +# Uninstall all targets... +# + +uninstall: + for file in $(FILTERS); do \ + $(RM) $(SERVERBIN)/filter/$$file; \ + done + $(RM) $(SERVERBIN)/filter/rastertodymo + -$(RMDIR) $(SERVERBIN)/filter + -$(RMDIR) $(SERVERBIN) + $(RM) $(LIBDIR)/libcupsimage.2.dylib + $(RM) $(LIBDIR)/libcupsimage.a + $(RM) $(LIBDIR)/libcupsimage.dylib + $(RM) $(LIBDIR)/libcupsimage_s.a + $(RM) $(LIBDIR)/libcupsimage.sl + $(RM) $(LIBDIR)/libcupsimage.sl.2 + $(RM) $(LIBDIR)/libcupsimage.so + $(RM) $(LIBDIR)/libcupsimage.so.2 + -$(RMDIR) $(LIBDIR) + + +# +# Automatic API help files... +# + +apihelp: + echo Generating CUPS API help files... + mxmldoc --section "Programming" --title "Raster API" \ + --css ../doc/cups-printable.css \ + --header api-raster.header --intro api-raster.shtml \ + api-raster.xml \ + ../cups/raster.h interpret.c raster.c \ + >../doc/help/api-raster.html + mxmldoc --tokens help/api-raster.html api-raster.xml >../doc/help/api-raster.tokens + $(RM) api-raster.xml + mxmldoc --section "Programming" \ + --title "Developing PostScript Printer Drivers" \ + --css ../doc/cups-printable.css \ + --header postscript-driver.header \ + --intro postscript-driver.shtml \ + >../doc/help/postscript-driver.html + mxmldoc --section "Programming" \ + --title "Introduction to the PPD Compiler" \ + --css ../doc/cups-printable.css \ + --header ppd-compiler.header \ + --intro ppd-compiler.shtml \ + >../doc/help/ppd-compiler.html + mxmldoc --section "Programming" \ + --title "Developing Raster Printer Drivers" \ + --css ../doc/cups-printable.css \ + --header raster-driver.header \ + --intro raster-driver.shtml \ + >../doc/help/raster-driver.html + mxmldoc --section "Specifications" \ + --title "CUPS PPD Extensions" \ + --css ../doc/cups-printable.css \ + --header spec-ppd.header \ + --intro spec-ppd.shtml \ + >../doc/help/spec-ppd.html + +framedhelp: + echo Generating CUPS API help files... + mxmldoc --section "Programming" --title "Raster API" \ + --framed ../cups/api-raster \ + --css ../doc/cups-printable.css \ + --header api-raster.header --intro api-raster.shtml \ + ../cups/raster.h interpret.c raster.c + mxmldoc --section "Programming" \ + --title "Developing PostScript Printer Drivers" \ + --framed ../cups/postscript-driver \ + --css ../doc/cups-printable.css \ + --header postscript-driver.header \ + --intro postscript-driver.shtml + mxmldoc --section "Programming" \ + --title "Introduction to the PPD Compiler" \ + --framed ../cups/ppd-compiler \ + --css ../doc/cups-printable.css \ + --header ppd-compiler.header \ + --intro ppd-compiler.shtml + mxmldoc --section "Programming" \ + --title "Developing Raster Printer Drivers" \ + --framed ../cups/raster-driver \ + --css ../doc/cups-printable.css \ + --header raster-driver.header \ + --intro raster-driver.shtml + mxmldoc --section "Specifications" \ + --title "CUPS PPD Extensions" \ + --framed ../cups/spec-ppd \ + --css ../doc/cups-printable.css \ + --header spec-ppd.header \ + --intro spec-ppd.shtml \ + + +# +# commandtops +# + +commandtops: commandtops.o ../cups/$(LIBCUPS) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ commandtops.o $(LIBS) + + +# +# gziptoany +# + +gziptoany: gziptoany.o ../Makedefs ../cups/$(LIBCUPS) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ gziptoany.o $(LIBZ) $(LIBS) + + +# +# libcupsimage.so.2, libcupsimage.sl.2 +# + +libcupsimage.so.2 libcupsimage.sl.2: $(IMAGEOBJS) + echo Linking $@... + $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ $(IMAGEOBJS) $(DSOLIBS) \ + -L../cups $(LINKCUPS) + $(RM) `basename $@ .2` + $(LN) $@ `basename $@ .2` + + +# +# libcupsimage.2.dylib +# + +libcupsimage.2.dylib: $(IMAGEOBJS) $(LIBCUPSIMAGEORDER) + echo Linking $@... + $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ \ + -install_name $(libdir)/$@ \ + -current_version 2.3.0 \ + -compatibility_version 2.0.0 \ + $(IMAGEOBJS) $(DSOLIBS) -L../cups $(LINKCUPS) + $(RM) libcupsimage.dylib + $(LN) $@ libcupsimage.dylib + + +# +# libcupsimage_s.a +# + +libcupsimage_s.a: $(IMAGEOBJS) libcupsimage_s.exp + echo Linking $@... + $(DSO) $(DSOFLAGS) -Wl,-berok,-bexport:libcupsimage_s.exp \ + -o libcupsimage_s.o $(IMAGEOBJS) $(DSOLIBS) + $(RM) $@ + $(AR) $(ARFLAGS) $@ libcupsimage_s.o + + +# +# libcupsimage.la +# + +libcupsimage.la: $(IMAGEOBJS) + echo Linking $@... + $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ $(IMAGEOBJS:.o=.lo) $(DSOLIBS) \ + -L../cups $(LINKCUPS) \ + -rpath $(LIBDIR) -version-info 2:3 + + +# +# libcupsimage.a +# + +libcupsimage.a: $(IMAGEOBJS) + echo Archiving $@... + $(RM) $@ + $(AR) $(ARFLAGS) $@ $(IMAGEOBJS) + $(RANLIB) $@ + + +# +# pstops +# + +pstops: pstops.o common.o ../cups/$(LIBCUPS) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ pstops.o common.o $(LIBS) + + +# +# rastertoepson +# + +rastertoepson: rastertoepson.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ rastertoepson.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) + + +# +# rastertohp +# + +rastertohp: rastertohp.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ rastertohp.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) + + +# +# rastertolabel +# + +rastertolabel: rastertolabel.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ rastertolabel.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) + + +# +# rastertopwg +# + +rastertopwg: rastertopwg.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ rastertopwg.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) + + +# +# testraster +# + +testraster: testraster.o ../cups/$(LIBCUPSSTATIC) libcupsimage.a + echo Linking $@... + $(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testraster.o libcupsimage.a \ + ../cups/$(LIBCUPSSTATIC) $(IMGLIBS) $(DSOLIBS) $(COMMONLIBS) \ + $(SSLLIBS) $(DNSSDLIBS) $(LIBGSSAPI) + echo Running raster API tests... + ./testraster + + +# +# rasterbench +# + +rasterbench: rasterbench.o libcupsimage.a + echo Linking $@... + $(CC) $(LDFLAGS) -o $@ rasterbench.o libcupsimage.a $(LIBS) + + +# +# Dependencies... +# + +include Dependencies + + +# +# End of "$Id: Makefile 7871 2008-08-27 21:12:43Z mike $". +# diff --git a/filter/api-raster.header b/filter/api-raster.header new file mode 100644 index 0000000..42c11d1 --- /dev/null +++ b/filter/api-raster.header @@ -0,0 +1,37 @@ + + +

Raster API

+ +
+ + + + + + + + + + + + + + + + +
Headercups/raster.h
Library-lcupsimage
See AlsoProgramming: Introduction to CUPS Programming
+ Programming: CUPS API
+ Programming: PPD API
+ References: CUPS PPD Specification
diff --git a/filter/api-raster.shtml b/filter/api-raster.shtml new file mode 100644 index 0000000..cb137d5 --- /dev/null +++ b/filter/api-raster.shtml @@ -0,0 +1,160 @@ + + +

Overview

+ +

The CUPS raster API provides a standard interface for reading and writing +CUPS raster streams which are used for printing to raster printers. Because the +raster format is updated from time to time, it is important to use this API to +avoid incompatibilities with newer versions of CUPS.

+ +

Two kinds of CUPS filters use the CUPS raster API - raster image processor +(RIP) filters such as pstoraster and cgpdftoraster +(OS X) that produce CUPS raster files and printer driver filters that +convert CUPS raster files into a format usable by the printer. Printer +driver filters are by far the most common.

+ +

CUPS raster files (application/vnd.cups-raster) consists of +a stream of raster page descriptions produced by one of the RIP filters such as +pstoraster, imagetoraster, or +cgpdftoraster. CUPS raster files are referred to using the +cups_raster_t type and are +opened using the cupsRasterOpen +function. For example, to read raster data from the standard input, open +file descriptor 0:

+ +
+#include <cups/raster.h>>
+
+cups_raster_t *ras = cupsRasterOpen(0, CUPS_RASTER_READ);
+
+ +

Each page of data begins with a page dictionary structure called +cups_page_header2_t. This +structure contains the colorspace, bits per color, media size, media type, +hardware resolution, and so forth used for the page.

+ +
Note: + +

Do not confuse the colorspace in the page header with the PPD + ColorModel keyword. ColorModel refers to the general type of + color used for a device (Gray, RGB, CMYK, DeviceN) and is often used to + select a particular colorspace for the page header along with the associate + color profile. The page header colorspace (cupsColorSpace) describes + both the type and organization of the color data, for example KCMY (black + first) instead of CMYK and RGBA (RGB + alpha) instead of RGB.

+ +
+ +

You read the page header using the +cupsRasterReadHeader2 +function:

+ +
+#include <cups/raster.h>>
+
+cups_raster_t *ras = cupsRasterOpen(0, CUPS_RASTER_READ);
+cups_page_header2_t header;
+
+while (cupsRasterReadHeader2(ras, &header))
+{
+  /* setup this page */
+
+  /* read raster data */
+
+  /* finish this page */
+}
+
+ +

After the page dictionary comes the page data which is a full-resolution, +possibly compressed bitmap representing the page in the printer's output +colorspace. You read uncompressed raster data using the +cupsRasterReadPixels +function. A for loop is normally used to read the page one line +at a time:

+ +
+#include <cups/raster.h>>
+
+cups_raster_t *ras = cupsRasterOpen(0, CUPS_RASTER_READ);
+cups_page_header2_t header;
+int page = 0;
+int y;
+char *buffer;
+
+while (cupsRasterReadHeader2(ras, &header))
+{
+  /* setup this page */
+  page ++;
+  fprintf(stderr, "PAGE: %d %d\n", page, header.NumCopies);
+
+  /* allocate memory for 1 line */
+  buffer = malloc(header.cupsBytesPerLine);
+
+  /* read raster data */
+  for (y = 0; y < header.cupsHeight; y ++)
+  {
+    if (cupsRasterReadPixels(ras, buffer, header.cupsBytesPerLine) == 0)
+      break;
+
+    /* write raster data to printer on stdout */
+  }
+
+  /* finish this page */
+}
+
+ +

When you are done reading the raster data, call the +cupsRasterClose function to free +the memory used to read the raster file:

+ +
+cups_raster_t *ras;
+
+cupsRasterClose(ras);
+
+ + +

Functions by Task

+ +

Opening and Closing Raster Streams

+ + + +

Reading Raster Streams

+ + + +

Writing Raster Streams

+ + diff --git a/filter/commandtops.c b/filter/commandtops.c new file mode 100644 index 0000000..a75809c --- /dev/null +++ b/filter/commandtops.c @@ -0,0 +1,538 @@ +/* + * "$Id: commandtops.c 3794 2012-04-23 22:44:16Z msweet $" + * + * PostScript command filter for CUPS. + * + * Copyright 2008-2012 by Apple Inc. + * + * 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/". + * + * + * Contents: + * + * main() - Process a CUPS command file. + * auto_configure() - Automatically configure the printer using + * PostScript query commands and/or SNMP lookups. + * begin_ps() - Send the standard PostScript prolog. + * end_ps() - Send the standard PostScript trailer. + * print_self_test_page() - Print a self-test page. + * report_levels() - Report supply levels. + */ + +/* + * Include necessary headers... + */ + +#include +#include +#include + + +/* + * Local functions... + */ + +static int auto_configure(ppd_file_t *ppd, const char *user); +static void begin_ps(ppd_file_t *ppd, const char *user); +static void end_ps(ppd_file_t *ppd); +static void print_self_test_page(ppd_file_t *ppd, const char *user); +static void report_levels(ppd_file_t *ppd, const char *user); + + +/* + * 'main()' - Process a CUPS command file. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int status = 0; /* Exit status */ + cups_file_t *fp; /* Command file */ + char line[1024], /* Line from file */ + *value; /* Value on line */ + int linenum; /* Line number in file */ + ppd_file_t *ppd; /* PPD file */ + + + /* + * Check for valid arguments... + */ + + if (argc < 6 || argc > 7) + { + /* + * We don't have the correct number of arguments; write an error message + * and return. + */ + + _cupsLangPrintf(stderr, + _("Usage: %s job-id user title copies options [file]"), + argv[0]); + return (1); + } + + /* + * Open the PPD file... + */ + + if ((ppd = ppdOpenFile(getenv("PPD"))) == NULL) + { + fputs("ERROR: Unable to open PPD file!\n", stderr); + return (1); + } + + /* + * Open the command file as needed... + */ + + if (argc == 7) + { + if ((fp = cupsFileOpen(argv[6], "r")) == NULL) + { + perror("ERROR: Unable to open command file - "); + return (1); + } + } + else + fp = cupsFileStdin(); + + /* + * Read the commands from the file and send the appropriate commands... + */ + + linenum = 0; + + while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) + { + /* + * Parse the command... + */ + + if (!_cups_strcasecmp(line, "AutoConfigure")) + status |= auto_configure(ppd, argv[2]); + else if (!_cups_strcasecmp(line, "PrintSelfTestPage")) + print_self_test_page(ppd, argv[2]); + else if (!_cups_strcasecmp(line, "ReportLevels")) + report_levels(ppd, argv[2]); + else + { + _cupsLangPrintFilter(stderr, "ERROR", + _("Invalid printer command \"%s\"."), line); + status = 1; + } + } + + return (status); +} + + +/* + * 'auto_configure()' - Automatically configure the printer using PostScript + * query commands and/or SNMP lookups. + */ + +static int /* O - Exit status */ +auto_configure(ppd_file_t *ppd, /* I - PPD file */ + const char *user) /* I - Printing user */ +{ + int status = 0; /* Exit status */ + ppd_option_t *option; /* Current option in PPD */ + ppd_attr_t *attr; /* Query command attribute */ + const char *valptr; /* Pointer into attribute value */ + char buffer[1024], /* String buffer */ + *bufptr; /* Pointer into buffer */ + ssize_t bytes; /* Number of bytes read */ + int datalen; /* Side-channel data length */ + + + /* + * See if the backend supports bidirectional I/O... + */ + + datalen = 1; + if (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI, buffer, &datalen, + 30.0) != CUPS_SC_STATUS_OK || + buffer[0] != CUPS_SC_BIDI_SUPPORTED) + { + fputs("DEBUG: Unable to auto-configure PostScript Printer - no " + "bidirectional I/O available!\n", stderr); + return (1); + } + + /* + * Put the printer in PostScript mode... + */ + + begin_ps(ppd, user); + + /* + * (STR #4028) + * + * As a lot of PPDs contain bad PostScript query code, we need to prevent one + * bad query sequence from affecting all auto-configuration. The following + * error handler allows us to log PostScript errors to cupsd. + */ + + puts("/cups_handleerror {\n" + " $error /newerror false put\n" + " (:PostScript error in \") print cups_query_keyword print (\": ) " + "print\n" + " $error /errorname get 128 string cvs print\n" + " (; offending command:) print $error /command get 128 string cvs " + "print (\n) print flush\n" + "} bind def\n" + "errordict /timeout {} put\n" + "/cups_query_keyword (?Unknown) def\n"); + fflush(stdout); + + /* + * Wait for the printer to become connected... + */ + + do + { + sleep(1); + datalen = 1; + } + while (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_CONNECTED, buffer, &datalen, + 5.0) == CUPS_SC_STATUS_OK && !buffer[0]); + + /* + * Then loop through every option in the PPD file and ask for the current + * value... + */ + + fputs("DEBUG: Auto-configuring PostScript printer...\n", stderr); + + for (option = ppdFirstOption(ppd); option; option = ppdNextOption(ppd)) + { + /* + * See if we have a query command for this option... + */ + + snprintf(buffer, sizeof(buffer), "?%s", option->keyword); + + if ((attr = ppdFindAttr(ppd, buffer, NULL)) == NULL || !attr->value) + { + fprintf(stderr, "DEBUG: Skipping %s option...\n", option->keyword); + continue; + } + + /* + * Send the query code to the printer... + */ + + fprintf(stderr, "DEBUG: Querying %s...\n", option->keyword); + + for (bufptr = buffer, valptr = attr->value; *valptr; valptr ++) + { + /* + * Log the query code, breaking at newlines... + */ + + if (*valptr == '\n') + { + *bufptr = '\0'; + fprintf(stderr, "DEBUG: %s\\n\n", buffer); + bufptr = buffer; + } + else if (*valptr < ' ') + { + if (bufptr >= (buffer + sizeof(buffer) - 4)) + { + *bufptr = '\0'; + fprintf(stderr, "DEBUG: %s\n", buffer); + bufptr = buffer; + } + + if (*valptr == '\r') + { + *bufptr++ = '\\'; + *bufptr++ = 'r'; + } + else if (*valptr == '\t') + { + *bufptr++ = '\\'; + *bufptr++ = 't'; + } + else + { + *bufptr++ = '\\'; + *bufptr++ = '0' + ((*valptr / 64) & 7); + *bufptr++ = '0' + ((*valptr / 8) & 7); + *bufptr++ = '0' + (*valptr & 7); + } + } + else + { + if (bufptr >= (buffer + sizeof(buffer) - 1)) + { + *bufptr = '\0'; + fprintf(stderr, "DEBUG: %s\n", buffer); + bufptr = buffer; + } + + *bufptr++ = *valptr; + } + } + + if (bufptr > buffer) + { + *bufptr = '\0'; + fprintf(stderr, "DEBUG: %s\n", buffer); + } + + printf("/cups_query_keyword (?%s) def\n", option->keyword); + /* Set keyword for error reporting */ + fputs("{ (", stdout); + for (valptr = attr->value; *valptr; valptr ++) + { + if (*valptr == '(' || *valptr == ')' || *valptr == '\\') + putchar('\\'); + putchar(*valptr); + } + fputs(") cvx exec } stopped { cups_handleerror } if clear\n", stdout); + /* Send query code */ + fflush(stdout); + + datalen = 0; + cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT, buffer, &datalen, 5.0); + + /* + * Read the response data... + */ + + bufptr = buffer; + buffer[0] = '\0'; + while ((bytes = cupsBackChannelRead(bufptr, + sizeof(buffer) - (bufptr - buffer) - 1, + 10.0)) > 0) + { + /* + * No newline at the end? Go on reading ... + */ + + bufptr += bytes; + *bufptr = '\0'; + + if (bytes == 0 || + (bufptr > buffer && bufptr[-1] != '\r' && bufptr[-1] != '\n')) + continue; + + /* + * Trim whitespace and control characters from both ends... + */ + + bytes = bufptr - buffer; + + for (bufptr --; bufptr >= buffer; bufptr --) + if (isspace(*bufptr & 255) || iscntrl(*bufptr & 255)) + *bufptr = '\0'; + else + break; + + for (bufptr = buffer; isspace(*bufptr & 255) || iscntrl(*bufptr & 255); + bufptr ++); + + if (bufptr > buffer) + { + _cups_strcpy(buffer, bufptr); + bufptr = buffer; + } + + fprintf(stderr, "DEBUG: Got %d bytes.\n", (int)bytes); + + /* + * Skip blank lines... + */ + + if (!buffer[0]) + continue; + + /* + * Check the response... + */ + + if ((bufptr = strchr(buffer, ':')) != NULL) + { + /* + * PostScript code for this option in the PPD is broken; show the + * interpreter's error message that came back... + */ + + fprintf(stderr, "DEBUG%s\n", bufptr); + break; + } + + /* + * Verify the result is a valid option choice... + */ + + if (!ppdFindChoice(option, buffer)) + { + if (!strcasecmp(buffer, "Unknown")) + break; + + bufptr = buffer; + buffer[0] = '\0'; + continue; + } + + /* + * Write out the result and move on to the next option... + */ + + fprintf(stderr, "PPD: Default%s=%s\n", option->keyword, buffer); + break; + } + + /* + * Printer did not answer this option's query + */ + + if (bytes <= 0) + { + fprintf(stderr, + "DEBUG: No answer to query for option %s within 10 seconds.\n", + option->keyword); + status = 1; + } + } + + /* + * Finish the job... + */ + + fflush(stdout); + end_ps(ppd); + + /* + * Return... + */ + + if (status) + _cupsLangPrintFilter(stderr, "WARNING", + _("Unable to configure printer options.")); + + return (0); +} + + +/* + * 'begin_ps()' - Send the standard PostScript prolog. + */ + +static void +begin_ps(ppd_file_t *ppd, /* I - PPD file */ + const char *user) /* I - Username */ +{ + (void)user; + + if (ppd->jcl_begin) + { + fputs(ppd->jcl_begin, stdout); + fputs(ppd->jcl_ps, stdout); + } + + puts("%!"); + puts("userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n"); + + fflush(stdout); +} + + +/* + * 'end_ps()' - Send the standard PostScript trailer. + */ + +static void +end_ps(ppd_file_t *ppd) /* I - PPD file */ +{ + if (ppd->jcl_end) + fputs(ppd->jcl_end, stdout); + else + putchar(0x04); + + fflush(stdout); +} + + +/* + * 'print_self_test_page()' - Print a self-test page. + */ + +static void +print_self_test_page(ppd_file_t *ppd, /* I - PPD file */ + const char *user) /* I - Printing user */ +{ + /* + * Put the printer in PostScript mode... + */ + + begin_ps(ppd, user); + + /* + * Send a simple file the draws a box around the imageable area and shows + * the product/interpreter information... + */ + + puts("\r%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%\n" + "\r%%%% If you can read this, you are using the wrong driver for your " + "printer. %%%%\n" + "\r%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%\n" + "0 setgray\n" + "2 setlinewidth\n" + "initclip newpath clippath gsave stroke grestore pathbbox\n" + "exch pop exch pop exch 9 add exch 9 sub moveto\n" + "/Courier findfont 12 scalefont setfont\n" + "0 -12 rmoveto gsave product show grestore\n" + "0 -12 rmoveto gsave version show ( ) show revision 20 string cvs show " + "grestore\n" + "0 -12 rmoveto gsave serialnumber 20 string cvs show grestore\n" + "showpage"); + + /* + * Finish the job... + */ + + end_ps(ppd); +} + + +/* + * 'report_levels()' - Report supply levels. + */ + +static void +report_levels(ppd_file_t *ppd, /* I - PPD file */ + const char *user) /* I - Printing user */ +{ + /* + * Put the printer in PostScript mode... + */ + + begin_ps(ppd, user); + + /* + * Don't bother sending any additional PostScript commands, since we just + * want the backend to have enough time to collect the supply info. + */ + + /* + * Finish the job... + */ + + end_ps(ppd); +} + + +/* + * End of "$Id: commandtops.c 3794 2012-04-23 22:44:16Z msweet $". + */ diff --git a/filter/common.c b/filter/common.c new file mode 100644 index 0000000..689253c --- /dev/null +++ b/filter/common.c @@ -0,0 +1,535 @@ +/* + * "$Id: common.c 6649 2007-07-11 21:46:42Z mike $" + * + * Common filter routines for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * SetCommonOptions() - Set common filter options for media size, + * etc. + * UpdatePageVars() - Update the page variables for the orientation. + * WriteComment() - Write a DSC comment. + * WriteCommon() - Write common procedures... + * WriteLabelProlog() - Write the prolog with the classification + * and page label. + * WriteLabels() - Write the actual page labels. + */ + +/* + * Include necessary headers... + */ + +#include "common.h" +#include + + +/* + * Globals... + */ + +int Orientation = 0, /* 0 = portrait, 1 = landscape, etc. */ + Duplex = 0, /* Duplexed? */ + LanguageLevel = 1, /* Language level of printer */ + ColorDevice = 1; /* Do color text? */ +float PageLeft = 18.0f, /* Left margin */ + PageRight = 594.0f, /* Right margin */ + PageBottom = 36.0f, /* Bottom margin */ + PageTop = 756.0f, /* Top margin */ + PageWidth = 612.0f, /* Total page width */ + PageLength = 792.0f; /* Total page length */ + + +/* + * 'SetCommonOptions()' - Set common filter options for media size, etc. + */ + +ppd_file_t * /* O - PPD file */ +SetCommonOptions( + int num_options, /* I - Number of options */ + cups_option_t *options, /* I - Options */ + int change_size) /* I - Change page size? */ +{ + ppd_file_t *ppd; /* PPD file */ + ppd_size_t *pagesize; /* Current page size */ + const char *val; /* Option value */ + + +#ifdef LC_TIME + setlocale(LC_TIME, ""); +#endif /* LC_TIME */ + + ppd = ppdOpenFile(getenv("PPD")); + + ppdMarkDefaults(ppd); + cupsMarkOptions(ppd, num_options, options); + + if ((pagesize = ppdPageSize(ppd, NULL)) != NULL) + { + PageWidth = pagesize->width; + PageLength = pagesize->length; + PageTop = pagesize->top; + PageBottom = pagesize->bottom; + PageLeft = pagesize->left; + PageRight = pagesize->right; + + fprintf(stderr, "DEBUG: Page = %.0fx%.0f; %.0f,%.0f to %.0f,%.0f\n", + PageWidth, PageLength, PageLeft, PageBottom, PageRight, PageTop); + } + + if (ppd != NULL) + { + ColorDevice = ppd->color_device; + LanguageLevel = ppd->language_level; + } + + if ((val = cupsGetOption("landscape", num_options, options)) != NULL) + { + if (_cups_strcasecmp(val, "no") != 0 && _cups_strcasecmp(val, "off") != 0 && + _cups_strcasecmp(val, "false") != 0) + { + if (ppd && ppd->landscape > 0) + Orientation = 1; + else + Orientation = 3; + } + } + else if ((val = cupsGetOption("orientation-requested", num_options, options)) != NULL) + { + /* + * Map IPP orientation values to 0 to 3: + * + * 3 = 0 degrees = 0 + * 4 = 90 degrees = 1 + * 5 = -90 degrees = 3 + * 6 = 180 degrees = 2 + */ + + Orientation = atoi(val) - 3; + if (Orientation >= 2) + Orientation ^= 1; + } + + if ((val = cupsGetOption("page-left", num_options, options)) != NULL) + { + switch (Orientation & 3) + { + case 0 : + PageLeft = (float)atof(val); + break; + case 1 : + PageBottom = (float)atof(val); + break; + case 2 : + PageRight = PageWidth - (float)atof(val); + break; + case 3 : + PageTop = PageLength - (float)atof(val); + break; + } + } + + if ((val = cupsGetOption("page-right", num_options, options)) != NULL) + { + switch (Orientation & 3) + { + case 0 : + PageRight = PageWidth - (float)atof(val); + break; + case 1 : + PageTop = PageLength - (float)atof(val); + break; + case 2 : + PageLeft = (float)atof(val); + break; + case 3 : + PageBottom = (float)atof(val); + break; + } + } + + if ((val = cupsGetOption("page-bottom", num_options, options)) != NULL) + { + switch (Orientation & 3) + { + case 0 : + PageBottom = (float)atof(val); + break; + case 1 : + PageLeft = (float)atof(val); + break; + case 2 : + PageTop = PageLength - (float)atof(val); + break; + case 3 : + PageRight = PageWidth - (float)atof(val); + break; + } + } + + if ((val = cupsGetOption("page-top", num_options, options)) != NULL) + { + switch (Orientation & 3) + { + case 0 : + PageTop = PageLength - (float)atof(val); + break; + case 1 : + PageRight = PageWidth - (float)atof(val); + break; + case 2 : + PageBottom = (float)atof(val); + break; + case 3 : + PageLeft = (float)atof(val); + break; + } + } + + if (change_size) + UpdatePageVars(); + + if (ppdIsMarked(ppd, "Duplex", "DuplexNoTumble") || + ppdIsMarked(ppd, "Duplex", "DuplexTumble") || + ppdIsMarked(ppd, "JCLDuplex", "DuplexNoTumble") || + ppdIsMarked(ppd, "JCLDuplex", "DuplexTumble") || + ppdIsMarked(ppd, "EFDuplex", "DuplexNoTumble") || + ppdIsMarked(ppd, "EFDuplex", "DuplexTumble") || + ppdIsMarked(ppd, "KD03Duplex", "DuplexNoTumble") || + ppdIsMarked(ppd, "KD03Duplex", "DuplexTumble")) + Duplex = 1; + + return (ppd); +} + + +/* + * 'UpdatePageVars()' - Update the page variables for the orientation. + */ + +void +UpdatePageVars(void) +{ + float temp; /* Swapping variable */ + + + switch (Orientation & 3) + { + case 0 : /* Portait */ + break; + + case 1 : /* Landscape */ + temp = PageLeft; + PageLeft = PageBottom; + PageBottom = temp; + + temp = PageRight; + PageRight = PageTop; + PageTop = temp; + + temp = PageWidth; + PageWidth = PageLength; + PageLength = temp; + break; + + case 2 : /* Reverse Portrait */ + temp = PageWidth - PageLeft; + PageLeft = PageWidth - PageRight; + PageRight = temp; + + temp = PageLength - PageBottom; + PageBottom = PageLength - PageTop; + PageTop = temp; + break; + + case 3 : /* Reverse Landscape */ + temp = PageWidth - PageLeft; + PageLeft = PageWidth - PageRight; + PageRight = temp; + + temp = PageLength - PageBottom; + PageBottom = PageLength - PageTop; + PageTop = temp; + + temp = PageLeft; + PageLeft = PageBottom; + PageBottom = temp; + + temp = PageRight; + PageRight = PageTop; + PageTop = temp; + + temp = PageWidth; + PageWidth = PageLength; + PageLength = temp; + break; + } +} + + +/* + * 'WriteCommon()' - Write common procedures... + */ + +void +WriteCommon(void) +{ + puts("% x y w h ESPrc - Clip to a rectangle.\n" + "userdict/ESPrc/rectclip where{pop/rectclip load}\n" + "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" + "neg 0 rlineto closepath clip newpath}bind}ifelse put"); + puts("% x y w h ESPrf - Fill a rectangle.\n" + "userdict/ESPrf/rectfill where{pop/rectfill load}\n" + "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" + "neg 0 rlineto closepath fill grestore}bind}ifelse put"); + puts("% x y w h ESPrs - Stroke a rectangle.\n" + "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n" + "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" + "neg 0 rlineto closepath stroke grestore}bind}ifelse put"); +} + + +/* + * 'WriteLabelProlog()' - Write the prolog with the classification + * and page label. + */ + +void +WriteLabelProlog(const char *label, /* I - Page label */ + float bottom, /* I - Bottom position in points */ + float top, /* I - Top position in points */ + float width) /* I - Width in points */ +{ + const char *classification; /* CLASSIFICATION environment variable */ + const char *ptr; /* Temporary string pointer */ + + + /* + * First get the current classification... + */ + + if ((classification = getenv("CLASSIFICATION")) == NULL) + classification = ""; + if (strcmp(classification, "none") == 0) + classification = ""; + + /* + * If there is nothing to show, bind an empty 'write labels' procedure + * and return... + */ + + if (!classification[0] && (label == NULL || !label[0])) + { + puts("userdict/ESPwl{}bind put"); + return; + } + + /* + * Set the classification + page label string... + */ + + printf("userdict"); + if (strcmp(classification, "confidential") == 0) + printf("/ESPpl(CONFIDENTIAL"); + else if (strcmp(classification, "classified") == 0) + printf("/ESPpl(CLASSIFIED"); + else if (strcmp(classification, "secret") == 0) + printf("/ESPpl(SECRET"); + else if (strcmp(classification, "topsecret") == 0) + printf("/ESPpl(TOP SECRET"); + else if (strcmp(classification, "unclassified") == 0) + printf("/ESPpl(UNCLASSIFIED"); + else + { + printf("/ESPpl("); + + for (ptr = classification; *ptr; ptr ++) + if (*ptr < 32 || *ptr > 126) + printf("\\%03o", *ptr); + else if (*ptr == '_') + putchar(' '); + else + { + if (*ptr == '(' || *ptr == ')' || *ptr == '\\') + putchar('\\'); + + putchar(*ptr); + } + } + + if (label) + { + if (classification[0]) + printf(" - "); + + /* + * Quote the label string as needed... + */ + + for (ptr = label; *ptr; ptr ++) + if (*ptr < 32 || *ptr > 126) + printf("\\%03o", *ptr); + else + { + if (*ptr == '(' || *ptr == ')' || *ptr == '\\') + putchar('\\'); + + putchar(*ptr); + } + } + + puts(")put"); + + /* + * Then get a 14 point Helvetica-Bold font... + */ + + puts("userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put"); + + /* + * Finally, the procedure to write the labels on the page... + */ + + puts("userdict/ESPwl{"); + puts(" ESPpf setfont"); + printf(" ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n", + width * 0.5f); + puts(" 1 setgray"); + printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0); + printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0); + puts(" 0 setgray"); + printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0); + printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0); + printf(" dup %.0f moveto ESPpl show\n", bottom + 2.0); + printf(" %.0f moveto ESPpl show\n", top - 14.0); + puts("pop"); + puts("}bind put"); +} + + +/* + * 'WriteLabels()' - Write the actual page labels. + */ + +void +WriteLabels(int orient) /* I - Orientation of the page */ +{ + float width, /* Width of page */ + length; /* Length of page */ + + + puts("gsave"); + + if ((orient ^ Orientation) & 1) + { + width = PageLength; + length = PageWidth; + } + else + { + width = PageWidth; + length = PageLength; + } + + switch (orient & 3) + { + case 1 : /* Landscape */ + printf("%.1f 0.0 translate 90 rotate\n", length); + break; + case 2 : /* Reverse Portrait */ + printf("%.1f %.1f translate 180 rotate\n", width, length); + break; + case 3 : /* Reverse Landscape */ + printf("0.0 %.1f translate -90 rotate\n", width); + break; + } + + puts("ESPwl"); + puts("grestore"); +} + + +/* + * 'WriteTextComment()' - Write a DSC text comment. + */ + +void +WriteTextComment(const char *name, /* I - Comment name ("Title", etc.) */ + const char *value) /* I - Comment value */ +{ + int len; /* Current line length */ + + + /* + * DSC comments are of the form: + * + * %%name: value + * + * The name and value must be limited to 7-bit ASCII for most printers, + * so we escape all non-ASCII and ASCII control characters as described + * in the Adobe Document Structuring Conventions specification. + */ + + printf("%%%%%s: (", name); + len = 5 + strlen(name); + + while (*value) + { + if (*value < ' ' || *value >= 127) + { + /* + * Escape this character value... + */ + + if (len >= 251) /* Keep line < 254 chars */ + break; + + printf("\\%03o", *value & 255); + len += 4; + } + else if (*value == '\\') + { + /* + * Escape the backslash... + */ + + if (len >= 253) /* Keep line < 254 chars */ + break; + + putchar('\\'); + putchar('\\'); + len += 2; + } + else + { + /* + * Put this character literally... + */ + + if (len >= 254) /* Keep line < 254 chars */ + break; + + putchar(*value); + len ++; + } + + value ++; + } + + puts(")"); +} + + +/* + * End of "$Id: common.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/filter/common.h b/filter/common.h new file mode 100644 index 0000000..3501085 --- /dev/null +++ b/filter/common.h @@ -0,0 +1,78 @@ +/* + * "$Id: common.h 6649 2007-07-11 21:46:42Z mike $" + * + * Common filter definitions for CUPS. + * + * Copyright 2007-2010 by Apple Inc. + * Copyright 1997-2006 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. + */ + +/* + * Include necessary headers... + */ + +#include +#include +#include +#include + + +/* + * C++ magic... + */ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + +/* + * Globals... + */ + +extern int Orientation, /* 0 = portrait, 1 = landscape, etc. */ + Duplex, /* Duplexed? */ + LanguageLevel, /* Language level of printer */ + ColorDevice; /* Do color text? */ +extern float PageLeft, /* Left margin */ + PageRight, /* Right margin */ + PageBottom, /* Bottom margin */ + PageTop, /* Top margin */ + PageWidth, /* Total page width */ + PageLength; /* Total page length */ + + +/* + * Prototypes... + */ + +extern ppd_file_t *SetCommonOptions(int num_options, cups_option_t *options, + int change_size); +extern void UpdatePageVars(void); +extern void WriteCommon(void); +extern void WriteLabelProlog(const char *label, float bottom, + float top, float width); +extern void WriteLabels(int orient); +extern void WriteTextComment(const char *name, const char *value); + + +/* + * C++ magic... + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +/* + * End of "$Id: common.h 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/filter/error.c b/filter/error.c new file mode 100644 index 0000000..08edebb --- /dev/null +++ b/filter/error.c @@ -0,0 +1,286 @@ +/* + * "$Id: error.c 7460 2008-04-16 02:19:54Z mike $" + * + * Raster error handling for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2007 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. + * + * Contents: + * + * _cupsRasterAddError() - Add an error message to the error buffer. + * _cupsRasterClearError() - Clear the error buffer. + * cupsRasterErrorString() - Return the last error from a raster function. + * get_error_buffer() - Return a pointer to thread local storage. + * raster_init() - Initialize error buffer once. + * raster_destructor() - Free memory allocated by get_error_buffer(). + */ + +/* + * Include necessary headers... + */ + +#include + + +/* + * Local structures... + */ + +typedef struct _cups_raster_error_s /**** Error buffer structure ****/ +{ + char *start, /* Start of buffer */ + *current, /* Current position in buffer */ + *end; /* End of buffer */ +} _cups_raster_error_t; + + +/* + * Local functions... + */ + +static _cups_raster_error_t *get_error_buffer(void); + + +/* + * '_cupsRasterAddError()' - Add an error message to the error buffer. + */ + +void +_cupsRasterAddError(const char *f, /* I - Printf-style error message */ + ...) /* I - Additional arguments as needed */ +{ + _cups_raster_error_t *buf = get_error_buffer(); + /* Error buffer */ + va_list ap; /* Pointer to additional arguments */ + char s[2048]; /* Message string */ + size_t bytes; /* Bytes in message string */ + + + va_start(ap, f); + bytes = vsnprintf(s, sizeof(s), f, ap); + va_end(ap); + + if (bytes <= 0) + return; + + bytes ++; + + if (bytes >= sizeof(s)) + return; + + if (bytes > (size_t)(buf->end - buf->current)) + { + /* + * Allocate more memory... + */ + + char *temp; /* New buffer */ + size_t size; /* Size of buffer */ + + + size = buf->end - buf->start + 2 * bytes + 1024; + + if (buf->start) + temp = realloc(buf->start, size); + else + temp = malloc(size); + + if (!temp) + return; + + /* + * Update pointers... + */ + + buf->end = temp + size; + buf->current = temp + (buf->current - buf->start); + buf->start = temp; + } + + /* + * Append the message to the end of the current string... + */ + + memcpy(buf->current, s, bytes); + buf->current += bytes - 1; +} + + +/* + * '_cupsRasterClearError()' - Clear the error buffer. + */ + +void +_cupsRasterClearError(void) +{ + _cups_raster_error_t *buf = get_error_buffer(); + /* Error buffer */ + + + buf->current = buf->start; + + if (buf->start) + *(buf->start) = '\0'; +} + + +/* + * 'cupsRasterErrorString()' - Return the last error from a raster function. + * + * If there are no recent errors, NULL is returned. + * + * @since CUPS 1.3/OS X 10.5@ + */ + +const char * /* O - Last error */ +cupsRasterErrorString(void) +{ + _cups_raster_error_t *buf = get_error_buffer(); + /* Error buffer */ + + + if (buf->current == buf->start) + return (NULL); + else + return (buf->start); +} + + +#ifdef HAVE_PTHREAD_H +/* + * Implement per-thread globals... + */ + +# include + + +/* + * Local globals... + */ + +static pthread_key_t raster_key = -1; + /* Thread local storage key */ +static pthread_once_t raster_key_once = PTHREAD_ONCE_INIT; + /* One-time initialization object */ + + +/* + * Local functions... + */ + +static void raster_init(void); +static void raster_destructor(void *value); + + +/* + * 'get_error_buffer()' - Return a pointer to thread local storage. + */ + +_cups_raster_error_t * /* O - Pointer to error buffer */ +get_error_buffer(void) +{ + _cups_raster_error_t *buf; /* Pointer to error buffer */ + + + /* + * Initialize the global data exactly once... + */ + + DEBUG_puts("get_error_buffer()"); + + pthread_once(&raster_key_once, raster_init); + + /* + * See if we have allocated the data yet... + */ + + if ((buf = (_cups_raster_error_t *)pthread_getspecific(raster_key)) + == NULL) + { + DEBUG_puts("get_error_buffer: allocating memory for thread..."); + + /* + * No, allocate memory as set the pointer for the key... + */ + + buf = calloc(1, sizeof(_cups_raster_error_t)); + pthread_setspecific(raster_key, buf); + + DEBUG_printf((" buf=%p\n", buf)); + } + + /* + * Return the pointer to the data... + */ + + return (buf); +} + + +/* + * 'raster_init()' - Initialize error buffer once. + */ + +static void +raster_init(void) +{ + pthread_key_create(&raster_key, raster_destructor); + + DEBUG_printf(("raster_init(): raster_key=%x(%u)\n", (unsigned)raster_key, + (unsigned)raster_key)); +} + + +/* + * 'raster_destructor()' - Free memory allocated by get_error_buffer(). + */ + +static void +raster_destructor(void *value) /* I - Data to free */ +{ + _cups_raster_error_t *buf = (_cups_raster_error_t *)value; + /* Error buffer */ + + + DEBUG_printf(("raster_destructor(value=%p)\n", value)); + + if (buf->start) + free(buf->start); + + free(value); +} + + +#else +/* + * Implement static globals... + */ + +/* + * 'get_error_buffer()' - Return a pointer to thread local storage. + */ + +_cups_raster_error_t * /* O - Pointer to error buffer */ +get_error_buffer(void) +{ + static _cups_raster_error_t buf = { 0, 0, 0 }; + /* Error buffer */ + + + return (&buf); +} +#endif /* HAVE_PTHREAD_H */ + + +/* + * End of "$Id: error.c 7460 2008-04-16 02:19:54Z mike $". + */ diff --git a/filter/gziptoany.c b/filter/gziptoany.c new file mode 100644 index 0000000..09a636b --- /dev/null +++ b/filter/gziptoany.c @@ -0,0 +1,112 @@ +/* + * "$Id: gziptoany.c 6649 2007-07-11 21:46:42Z mike $" + * + * GZIP/raw pre-filter for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1993-2007 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. + * + * Contents: + * + * main() - Copy (and uncompress) files to stdout. + */ + +/* + * Include necessary headers... + */ + +#include + + +/* + * 'main()' - Copy (and uncompress) files to stdout. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + cups_file_t *fp; /* File */ + char buffer[8192]; /* Data buffer */ + int bytes; /* Number of bytes read/written */ + int copies; /* Number of copies */ + + + /* + * Check command-line... + */ + + if (argc != 7) + { + _cupsLangPrintf(stderr, + _("Usage: %s job-id user title copies options [file]"), + argv[0]); + return (1); + } + + /* + * Get the copy count; if we have no final content type, this is a + * raw queue or raw print file, so we need to make copies... + */ + + if (!getenv("FINAL_CONTENT_TYPE")) + copies = atoi(argv[4]); + else + copies = 1; + + /* + * Open the file... + */ + + if ((fp = cupsFileOpen(argv[6], "r")) == NULL) + { + _cupsLangPrintError("ERROR", _("Unable to open print file")); + return (1); + } + + /* + * Copy the file to stdout... + */ + + while (copies > 0) + { + if (!getenv("FINAL_CONTENT_TYPE")) + fputs("PAGE: 1 1\n", stderr); + + cupsFileRewind(fp); + + while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) + if (write(1, buffer, bytes) < bytes) + { + _cupsLangPrintFilter(stderr, "ERROR", + _("Unable to write uncompressed print data: %s"), + strerror(errno)); + cupsFileClose(fp); + + return (1); + } + + copies --; + } + + /* + * Close the file and return... + */ + + cupsFileClose(fp); + + return (0); +} + + +/* + * End of "$Id: gziptoany.c 6649 2007-07-11 21:46:42Z mike $". + */ diff --git a/filter/interpret.c b/filter/interpret.c new file mode 100644 index 0000000..7723f65 --- /dev/null +++ b/filter/interpret.c @@ -0,0 +1,1688 @@ +/* + * "$Id: interpret.c 7852 2008-08-21 04:19:45Z mike $" + * + * PPD command interpreter for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1993-2007 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. + * + * Contents: + * + * cupsRasterInterpretPPD() - Interpret PPD commands to create a page header. + * _cupsRasterExecPS() - Execute PostScript code to initialize a page + * header. + * cleartomark_stack() - Clear to the last mark ([) on the stack. + * copy_stack() - Copy the top N stack objects. + * delete_stack() - Free memory used by a stack. + * error_object() - Add an object's value to the current error + * message. + * error_stack() - Add a stack to the current error message. + * index_stack() - Copy the Nth value on the stack. + * new_stack() - Create a new stack. + * pop_stock() - Pop the top object off the stack. + * push_stack() - Push an object on the stack. + * roll_stack() - Rotate stack objects. + * scan_ps() - Scan a string for the next PS object. + * setpagedevice() - Simulate the PostScript setpagedevice operator. + * DEBUG_object() - Print an object value. + * DEBUG_stack() - Print a stack. + */ + +/* + * Include necessary headers... + */ + +#include + + +/* + * Stack values for the PostScript mini-interpreter... + */ + +typedef enum +{ + CUPS_PS_NAME, + CUPS_PS_NUMBER, + CUPS_PS_STRING, + CUPS_PS_BOOLEAN, + CUPS_PS_NULL, + CUPS_PS_START_ARRAY, + CUPS_PS_END_ARRAY, + CUPS_PS_START_DICT, + CUPS_PS_END_DICT, + CUPS_PS_START_PROC, + CUPS_PS_END_PROC, + CUPS_PS_CLEARTOMARK, + CUPS_PS_COPY, + CUPS_PS_DUP, + CUPS_PS_INDEX, + CUPS_PS_POP, + CUPS_PS_ROLL, + CUPS_PS_SETPAGEDEVICE, + CUPS_PS_STOPPED, + CUPS_PS_OTHER +} _cups_ps_type_t; + +typedef struct +{ + _cups_ps_type_t type; /* Object type */ + union + { + int boolean; /* Boolean value */ + char name[64]; /* Name value */ + double number; /* Number value */ + char other[64]; /* Other operator */ + char string[64]; /* Sring value */ + } value; /* Value */ +} _cups_ps_obj_t; + +typedef struct +{ + int num_objs, /* Number of objects on stack */ + alloc_objs; /* Number of allocated objects */ + _cups_ps_obj_t *objs; /* Objects in stack */ +} _cups_ps_stack_t; + + +/* + * Local functions... + */ + +static int cleartomark_stack(_cups_ps_stack_t *st); +static int copy_stack(_cups_ps_stack_t *st, int count); +static void delete_stack(_cups_ps_stack_t *st); +static void error_object(_cups_ps_obj_t *obj); +static void error_stack(_cups_ps_stack_t *st, const char *title); +static _cups_ps_obj_t *index_stack(_cups_ps_stack_t *st, int n); +static _cups_ps_stack_t *new_stack(void); +static _cups_ps_obj_t *pop_stack(_cups_ps_stack_t *st); +static _cups_ps_obj_t *push_stack(_cups_ps_stack_t *st, + _cups_ps_obj_t *obj); +static int roll_stack(_cups_ps_stack_t *st, int c, int s); +static _cups_ps_obj_t *scan_ps(_cups_ps_stack_t *st, char **ptr); +static int setpagedevice(_cups_ps_stack_t *st, + cups_page_header2_t *h, + int *preferred_bits); +#ifdef DEBUG +static void DEBUG_object(_cups_ps_obj_t *obj); +static void DEBUG_stack(_cups_ps_stack_t *st); +#endif /* DEBUG */ + + +/* + * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header. + * + * This function is used by raster image processing (RIP) filters like + * cgpdftoraster and imagetoraster when writing CUPS raster data for a page. + * It is not used by raster printer driver filters which only read CUPS + * raster data. + * + * + * @code cupsRasterInterpretPPD@ does not mark the options in the PPD using + * the "num_options" and "options" arguments. Instead, mark the options with + * @code cupsMarkOptions@ and @code ppdMarkOption@ prior to calling it - + * this allows for per-page options without manipulating the options array. + * + * The "func" argument specifies an optional callback function that is + * called prior to the computation of the final raster data. The function + * can make changes to the @link cups_page_header2_t@ data as needed to use a + * supported raster format and then returns 0 on success and -1 if the + * requested attributes cannot be supported. + * + * + * @code cupsRasterInterpretPPD@ supports a subset of the PostScript language. + * Currently only the @code [@, @code ]@, @code <<@, @code >>@, @code {@, + * @code }@, @code cleartomark@, @code copy@, @code dup@, @code index@, + * @code pop@, @code roll@, @code setpagedevice@, and @code stopped@ operators + * are supported. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +int /* O - 0 on success, -1 on failure */ +cupsRasterInterpretPPD( + cups_page_header2_t *h, /* O - Page header to create */ + ppd_file_t *ppd, /* I - PPD file */ + int num_options, /* I - Number of options */ + cups_option_t *options, /* I - Options */ + cups_interpret_cb_t func) /* I - Optional page header callback (@code NULL@ for none) */ +{ + int status; /* Cummulative status */ + char *code; /* Code to run */ + const char *val; /* Option value */ + ppd_size_t *size; /* Current size */ + float left, /* Left position */ + bottom, /* Bottom position */ + right, /* Right position */ + top; /* Top position */ + int preferred_bits; /* Preferred bits per color */ + + + /* + * Range check input... + */ + + _cupsRasterClearError(); + + if (!h) + { + _cupsRasterAddError("Page header cannot be NULL!\n"); + return (-1); + } + + /* + * Reset the page header to the defaults... + */ + + memset(h, 0, sizeof(cups_page_header2_t)); + + h->NumCopies = 1; + h->PageSize[0] = 612; + h->PageSize[1] = 792; + h->HWResolution[0] = 100; + h->HWResolution[1] = 100; + h->cupsBitsPerColor = 1; + h->cupsColorOrder = CUPS_ORDER_CHUNKED; + h->cupsColorSpace = CUPS_CSPACE_K; + h->cupsBorderlessScalingFactor = 1.0f; + h->cupsPageSize[0] = 612.0f; + h->cupsPageSize[1] = 792.0f; + h->cupsImagingBBox[0] = 0.0f; + h->cupsImagingBBox[1] = 0.0f; + h->cupsImagingBBox[2] = 612.0f; + h->cupsImagingBBox[3] = 792.0f; + + strcpy(h->cupsPageSizeName, "Letter"); + +#ifdef __APPLE__ + /* + * cupsInteger0 is also used for the total page count on OS X; set an + * uncommon default value so we can tell if the driver is using cupsInteger0. + */ + + h->cupsInteger[0] = 0x80000000; +#endif /* __APPLE__ */ + + /* + * Apply patches and options to the page header... + */ + + status = 0; + preferred_bits = 0; + + if (ppd) + { + /* + * Apply any patch code (used to override the defaults...) + */ + + if (ppd->patches) + status |= _cupsRasterExecPS(h, &preferred_bits, ppd->patches); + + /* + * Then apply printer options in the proper order... + */ + + if ((code = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL) + { + status |= _cupsRasterExecPS(h, &preferred_bits, code); + free(code); + } + + if ((code = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL) + { + status |= _cupsRasterExecPS(h, &preferred_bits, code); + free(code); + } + + if ((code = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL) + { + status |= _cupsRasterExecPS(h, &preferred_bits, code); + free(code); + } + + if ((code = ppdEmitString(ppd, PPD_ORDER_PAGE, 0.0)) != NULL) + { + status |= _cupsRasterExecPS(h, &preferred_bits, code); + free(code); + } + } + + /* + * Allow option override for page scaling... + */ + + if ((val = cupsGetOption("cupsBorderlessScalingFactor", num_options, + options)) != NULL) + { + double sc = atof(val); /* Scale factor */ + + if (sc >= 0.1 && sc <= 2.0) + h->cupsBorderlessScalingFactor = (float)sc; + } + + /* + * Get the margins for the current size... + */ + + if ((size = ppdPageSize(ppd, NULL)) != NULL) + { + /* + * Use the margins from the PPD file... + */ + + left = size->left; + bottom = size->bottom; + right = size->right; + top = size->top; + + strlcpy(h->cupsPageSizeName, size->name, sizeof(h->cupsPageSizeName)); + + h->cupsPageSize[0] = size->width; + h->cupsPageSize[1] = size->length; + } + else + { + /* + * Use the default margins... + */ + + left = 0.0f; + bottom = 0.0f; + right = 612.0f; + top = 792.0f; + } + + h->PageSize[0] = (unsigned)(h->cupsPageSize[0] * + h->cupsBorderlessScalingFactor); + h->PageSize[1] = (unsigned)(h->cupsPageSize[1] * + h->cupsBorderlessScalingFactor); + h->Margins[0] = (unsigned)(left * + h->cupsBorderlessScalingFactor); + h->Margins[1] = (unsigned)(bottom * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[0] = (unsigned)(left * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[1] = (unsigned)(bottom * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[2] = (unsigned)(right * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[3] = (unsigned)(top * + h->cupsBorderlessScalingFactor); + h->cupsImagingBBox[0] = (float)left; + h->cupsImagingBBox[1] = (float)bottom; + h->cupsImagingBBox[2] = (float)right; + h->cupsImagingBBox[3] = (float)top; + + /* + * Use the callback to validate the page header... + */ + + if (func && (*func)(h, preferred_bits)) + { + _cupsRasterAddError("Page header callback returned error.\n"); + return (-1); + } + + /* + * Check parameters... + */ + + if (!h->HWResolution[0] || !h->HWResolution[1] || + !h->PageSize[0] || !h->PageSize[1] || + (h->cupsBitsPerColor != 1 && h->cupsBitsPerColor != 2 && + h->cupsBitsPerColor != 4 && h->cupsBitsPerColor != 8 && + h->cupsBitsPerColor != 16) || + h->cupsBorderlessScalingFactor < 0.1 || + h->cupsBorderlessScalingFactor > 2.0) + { + _cupsRasterAddError("Page header uses unsupported values.\n"); + return (-1); + } + + /* + * Compute the bitmap parameters... + */ + + h->cupsWidth = (int)((right - left) * h->cupsBorderlessScalingFactor * + h->HWResolution[0] / 72.0f + 0.5f); + h->cupsHeight = (int)((top - bottom) * h->cupsBorderlessScalingFactor * + h->HWResolution[1] / 72.0f + 0.5f); + + switch (h->cupsColorSpace) + { + case CUPS_CSPACE_W : + case CUPS_CSPACE_K : + case CUPS_CSPACE_WHITE : + case CUPS_CSPACE_GOLD : + case CUPS_CSPACE_SILVER : + case CUPS_CSPACE_SW : + h->cupsNumColors = 1; + h->cupsBitsPerPixel = h->cupsBitsPerColor; + break; + + default : + /* + * Ensure that colorimetric colorspaces use at least 8 bits per + * component... + */ + + if (h->cupsColorSpace >= CUPS_CSPACE_CIEXYZ && + h->cupsBitsPerColor < 8) + h->cupsBitsPerColor = 8; + + /* + * Figure out the number of bits per pixel... + */ + + if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) + { + if (h->cupsBitsPerColor >= 8) + h->cupsBitsPerPixel = h->cupsBitsPerColor * 3; + else + h->cupsBitsPerPixel = h->cupsBitsPerColor * 4; + } + else + h->cupsBitsPerPixel = h->cupsBitsPerColor; + + h->cupsNumColors = 3; + break; + + case CUPS_CSPACE_KCMYcm : + if (h->cupsBitsPerColor == 1) + { + if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) + h->cupsBitsPerPixel = 8; + else + h->cupsBitsPerPixel = 1; + + h->cupsNumColors = 6; + break; + } + + /* + * Fall through to CMYK code... + */ + + case CUPS_CSPACE_RGBA : + case CUPS_CSPACE_RGBW : + case CUPS_CSPACE_CMYK : + case CUPS_CSPACE_YMCK : + case CUPS_CSPACE_KCMY : + case CUPS_CSPACE_GMCK : + case CUPS_CSPACE_GMCS : + if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) + h->cupsBitsPerPixel = h->cupsBitsPerColor * 4; + else + h->cupsBitsPerPixel = h->cupsBitsPerColor; + + h->cupsNumColors = 4; + break; + + case CUPS_CSPACE_DEVICE1 : + case CUPS_CSPACE_DEVICE2 : + case CUPS_CSPACE_DEVICE3 : + case CUPS_CSPACE_DEVICE4 : + case CUPS_CSPACE_DEVICE5 : + case CUPS_CSPACE_DEVICE6 : + case CUPS_CSPACE_DEVICE7 : + case CUPS_CSPACE_DEVICE8 : + case CUPS_CSPACE_DEVICE9 : + case CUPS_CSPACE_DEVICEA : + case CUPS_CSPACE_DEVICEB : + case CUPS_CSPACE_DEVICEC : + case CUPS_CSPACE_DEVICED : + case CUPS_CSPACE_DEVICEE : + case CUPS_CSPACE_DEVICEF : + h->cupsNumColors = h->cupsColorSpace - CUPS_CSPACE_DEVICE1 + 1; + + if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) + h->cupsBitsPerPixel = h->cupsBitsPerColor * h->cupsNumColors; + else + h->cupsBitsPerPixel = h->cupsBitsPerColor; + break; + } + + h->cupsBytesPerLine = (h->cupsBitsPerPixel * h->cupsWidth + 7) / 8; + + if (h->cupsColorOrder == CUPS_ORDER_BANDED) + h->cupsBytesPerLine *= h->cupsNumColors; + + return (status); +} + + +/* + * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header. + */ + +int /* O - 0 on success, -1 on error */ +_cupsRasterExecPS( + cups_page_header2_t *h, /* O - Page header */ + int *preferred_bits,/* O - Preferred bits per color */ + const char *code) /* I - PS code to execute */ +{ + _cups_ps_stack_t *st; /* PostScript value stack */ + _cups_ps_obj_t *obj; /* Object from top of stack */ + char *codecopy, /* Copy of code */ + *codeptr; /* Pointer into copy of code */ + + + DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n", + h, preferred_bits, code ? code : "(null)")); + + /* + * Copy the PostScript code and create a stack... + */ + + if ((codecopy = strdup(code)) == NULL) + { + _cupsRasterAddError("Unable to duplicate code string.\n"); + return (-1); + } + + if ((st = new_stack()) == NULL) + { + _cupsRasterAddError("Unable to create stack.\n"); + free(codecopy); + return (-1); + } + + /* + * Parse the PS string until we run out of data... + */ + + codeptr = codecopy; + + while ((obj = scan_ps(st, &codeptr)) != NULL) + { +#ifdef DEBUG + DEBUG_printf(("_cupsRasterExecPS: Stack (%d objects)\n", st->num_objs)); + DEBUG_object(obj); +#endif /* DEBUG */ + + switch (obj->type) + { + default : + /* Do nothing for regular values */ + break; + + case CUPS_PS_CLEARTOMARK : + pop_stack(st); + + if (cleartomark_stack(st)) + _cupsRasterAddError("cleartomark: Stack underflow!\n"); + +#ifdef DEBUG + DEBUG_puts(" dup: "); + DEBUG_stack(st); +#endif /* DEBUG */ + break; + + case CUPS_PS_COPY : + pop_stack(st); + if ((obj = pop_stack(st)) != NULL) + { + copy_stack(st, (int)obj->value.number); + +#ifdef DEBUG + DEBUG_puts("_cupsRasterExecPS: copy"); + DEBUG_stack(st); +#endif /* DEBUG */ + } + break; + + case CUPS_PS_DUP : + pop_stack(st); + copy_stack(st, 1); + +#ifdef DEBUG + DEBUG_puts("_cupsRasterExecPS: dup"); + DEBUG_stack(st); +#endif /* DEBUG */ + break; + + case CUPS_PS_INDEX : + pop_stack(st); + if ((obj = pop_stack(st)) != NULL) + { + index_stack(st, (int)obj->value.number); + +#ifdef DEBUG + DEBUG_puts("_cupsRasterExecPS: index"); + DEBUG_stack(st); +#endif /* DEBUG */ + } + break; + + case CUPS_PS_POP : + pop_stack(st); + pop_stack(st); + +#ifdef DEBUG + DEBUG_puts("_cupsRasterExecPS: pop"); + DEBUG_stack(st); +#endif /* DEBUG */ + break; + + case CUPS_PS_ROLL : + pop_stack(st); + if ((obj = pop_stack(st)) != NULL) + { + int c; /* Count */ + + + c = (int)obj->value.number; + + if ((obj = pop_stack(st)) != NULL) + { + roll_stack(st, (int)obj->value.number, c); + +#ifdef DEBUG + DEBUG_puts("_cupsRasterExecPS: roll"); + DEBUG_stack(st); +#endif /* DEBUG */ + } + } + break; + + case CUPS_PS_SETPAGEDEVICE : + pop_stack(st); + setpagedevice(st, h, preferred_bits); + +#ifdef DEBUG + DEBUG_puts("_cupsRasterExecPS: setpagedevice"); + DEBUG_stack(st); +#endif /* DEBUG */ + break; + + case CUPS_PS_START_PROC : + case CUPS_PS_END_PROC : + case CUPS_PS_STOPPED : + pop_stack(st); + break; + + case CUPS_PS_OTHER : + _cupsRasterAddError("Unknown operator \"%s\"!\n", obj->value.other); + DEBUG_printf(("_cupsRasterExecPS: Unknown operator \"%s\"!\n", + obj->value.other)); + break; + } + + if (obj && obj->type == CUPS_PS_OTHER) + break; + } + + /* + * Cleanup... + */ + + free(codecopy); + + if (st->num_objs > 0) + { + error_stack(st, "Stack not empty:"); + +#ifdef DEBUG + DEBUG_puts("_cupsRasterExecPS: Stack not empty:"); + DEBUG_stack(st); +#endif /* DEBUG */ + + delete_stack(st); + + return (-1); + } + + delete_stack(st); + + /* + * Return success... + */ + + return (0); +} + + +/* + * 'cleartomark_stack()' - Clear to the last mark ([) on the stack. + */ + +static int /* O - 0 on success, -1 on error */ +cleartomark_stack(_cups_ps_stack_t *st) /* I - Stack */ +{ + _cups_ps_obj_t *obj; /* Current object on stack */ + + + while ((obj = pop_stack(st)) != NULL) + if (obj->type == CUPS_PS_START_ARRAY) + break; + + return (obj ? 0 : -1); +} + + +/* + * 'copy_stack()' - Copy the top N stack objects. + */ + +static int /* O - 0 on success, -1 on error */ +copy_stack(_cups_ps_stack_t *st, /* I - Stack */ + int c) /* I - Number of objects to copy */ +{ + int n; /* Index */ + + + if (c < 0) + return (-1); + else if (c == 0) + return (0); + + if ((n = st->num_objs - c) < 0) + return (-1); + + while (c > 0) + { + if (!push_stack(st, st->objs + n)) + return (-1); + + n ++; + c --; + } + + return (0); +} + + +/* + * 'delete_stack()' - Free memory used by a stack. + */ + +static void +delete_stack(_cups_ps_stack_t *st) /* I - Stack */ +{ + free(st->objs); + free(st); +} + + +/* + * 'error_object()' - Add an object's value to the current error message. + */ + +static void +error_object(_cups_ps_obj_t *obj) /* I - Object to add */ +{ + switch (obj->type) + { + case CUPS_PS_NAME : + _cupsRasterAddError(" /%s", obj->value.name); + break; + + case CUPS_PS_NUMBER : + _cupsRasterAddError(" %g", obj->value.number); + break; + + case CUPS_PS_STRING : + _cupsRasterAddError(" (%s)", obj->value.string); + break; + + case CUPS_PS_BOOLEAN : + if (obj->value.boolean) + _cupsRasterAddError(" true"); + else + _cupsRasterAddError(" false"); + break; + + case CUPS_PS_NULL : + _cupsRasterAddError(" null"); + break; + + case CUPS_PS_START_ARRAY : + _cupsRasterAddError(" ["); + break; + + case CUPS_PS_END_ARRAY : + _cupsRasterAddError(" ]"); + break; + + case CUPS_PS_START_DICT : + _cupsRasterAddError(" <<"); + break; + + case CUPS_PS_END_DICT : + _cupsRasterAddError(" >>"); + break; + + case CUPS_PS_START_PROC : + _cupsRasterAddError(" {"); + break; + + case CUPS_PS_END_PROC : + _cupsRasterAddError(" }"); + break; + + case CUPS_PS_COPY : + _cupsRasterAddError(" --copy--"); + break; + + case CUPS_PS_CLEARTOMARK : + _cupsRasterAddError(" --cleartomark--"); + break; + + case CUPS_PS_DUP : + _cupsRasterAddError(" --dup--"); + break; + + case CUPS_PS_INDEX : + _cupsRasterAddError(" --index--"); + break; + + case CUPS_PS_POP : + _cupsRasterAddError(" --pop--"); + break; + + case CUPS_PS_ROLL : + _cupsRasterAddError(" --roll--"); + break; + + case CUPS_PS_SETPAGEDEVICE : + _cupsRasterAddError(" --setpagedevice--"); + break; + + case CUPS_PS_STOPPED : + _cupsRasterAddError(" --stopped--"); + break; + + case CUPS_PS_OTHER : + _cupsRasterAddError(" --%s--", obj->value.other); + break; + } +} + + +/* + * 'error_stack()' - Add a stack to the current error message... + */ + +static void +error_stack(_cups_ps_stack_t *st, /* I - Stack */ + const char *title) /* I - Title string */ +{ + int c; /* Looping var */ + _cups_ps_obj_t *obj; /* Current object on stack */ + + + _cupsRasterAddError("%s", title); + + for (obj = st->objs, c = st->num_objs; c > 0; c --, obj ++) + error_object(obj); + + _cupsRasterAddError("\n"); +} + + +/* + * 'index_stack()' - Copy the Nth value on the stack. + */ + +static _cups_ps_obj_t * /* O - New object */ +index_stack(_cups_ps_stack_t *st, /* I - Stack */ + int n) /* I - Object index */ +{ + if (n < 0 || (n = st->num_objs - n - 1) < 0) + return (NULL); + + return (push_stack(st, st->objs + n)); +} + + +/* + * 'new_stack()' - Create a new stack. + */ + +static _cups_ps_stack_t * /* O - New stack */ +new_stack(void) +{ + _cups_ps_stack_t *st; /* New stack */ + + + if ((st = calloc(1, sizeof(_cups_ps_stack_t))) == NULL) + return (NULL); + + st->alloc_objs = 32; + + if ((st->objs = calloc(32, sizeof(_cups_ps_obj_t))) == NULL) + { + free(st); + return (NULL); + } + else + return (st); +} + + +/* + * 'pop_stock()' - Pop the top object off the stack. + */ + +static _cups_ps_obj_t * /* O - Object */ +pop_stack(_cups_ps_stack_t *st) /* I - Stack */ +{ + if (st->num_objs > 0) + { + st->num_objs --; + + return (st->objs + st->num_objs); + } + else + return (NULL); +} + + +/* + * 'push_stack()' - Push an object on the stack. + */ + +static _cups_ps_obj_t * /* O - New object */ +push_stack(_cups_ps_stack_t *st, /* I - Stack */ + _cups_ps_obj_t *obj) /* I - Object */ +{ + _cups_ps_obj_t *temp; /* New object */ + + + if (st->num_objs >= st->alloc_objs) + { + + + st->alloc_objs += 32; + + if ((temp = realloc(st->objs, st->alloc_objs * + sizeof(_cups_ps_obj_t))) == NULL) + return (NULL); + + st->objs = temp; + memset(temp + st->num_objs, 0, 32 * sizeof(_cups_ps_obj_t)); + } + + temp = st->objs + st->num_objs; + st->num_objs ++; + + memcpy(temp, obj, sizeof(_cups_ps_obj_t)); + + return (temp); +} + + +/* + * 'roll_stack()' - Rotate stack objects. + */ + +static int /* O - 0 on success, -1 on error */ +roll_stack(_cups_ps_stack_t *st, /* I - Stack */ + int c, /* I - Number of objects */ + int s) /* I - Amount to shift */ +{ + _cups_ps_obj_t *temp; /* Temporary array of objects */ + int n; /* Index into array */ + + + DEBUG_printf((" roll_stack(st=%p, s=%d, c=%d)\n", st, s, c)); + + /* + * Range check input... + */ + + if (c < 0) + return (-1); + else if (c == 0) + return (0); + + if ((n = st->num_objs - c) < 0) + return (-1); + + s %= c; + + if (s == 0) + return (0); + + /* + * Copy N objects and move things around... + */ + + if (s < 0) + { + /* + * Shift down... + */ + + s = -s; + + if ((temp = calloc(s, sizeof(_cups_ps_obj_t))) == NULL) + return (-1); + + memcpy(temp, st->objs + n, s * sizeof(_cups_ps_obj_t)); + memmove(st->objs + n, st->objs + n + s, (c - s) * sizeof(_cups_ps_obj_t)); + memcpy(st->objs + n + c - s, temp, s * sizeof(_cups_ps_obj_t)); + } + else + { + /* + * Shift up... + */ + + if ((temp = calloc(s, sizeof(_cups_ps_obj_t))) == NULL) + return (-1); + + memcpy(temp, st->objs + n + c - s, s * sizeof(_cups_ps_obj_t)); + memmove(st->objs + n + s, st->objs + n, + (c - s) * sizeof(_cups_ps_obj_t)); + memcpy(st->objs + n, temp, s * sizeof(_cups_ps_obj_t)); + } + + free(temp); + + return (0); +} + + +/* + * 'scan_ps()' - Scan a string for the next PS object. + */ + +static _cups_ps_obj_t * /* O - New object or NULL on EOF */ +scan_ps(_cups_ps_stack_t *st, /* I - Stack */ + char **ptr) /* IO - String pointer */ +{ + _cups_ps_obj_t obj; /* Current object */ + char *start, /* Start of object */ + *cur, /* Current position */ + *valptr, /* Pointer into value string */ + *valend; /* End of value string */ + int parens; /* Parenthesis nesting level */ + + + /* + * Skip leading whitespace... + */ + + for (cur = *ptr; *cur; cur ++) + { + if (*cur == '%') + { + /* + * Comment, skip to end of line... + */ + + for (cur ++; *cur && *cur != '\n' && *cur != '\r'; cur ++); + + if (!*cur) + cur --; + } + else if (!isspace(*cur & 255)) + break; + } + + if (!*cur) + { + *ptr = NULL; + + return (NULL); + } + + /* + * See what we have... + */ + + memset(&obj, 0, sizeof(obj)); + + switch (*cur) + { + case '(' : /* (string) */ + obj.type = CUPS_PS_STRING; + start = cur; + + for (cur ++, parens = 1, valptr = obj.value.string, + valend = obj.value.string + sizeof(obj.value.string) - 1; + *cur; + cur ++) + { + if (*cur == ')' && parens == 1) + break; + + if (*cur == '(') + parens ++; + else if (*cur == ')') + parens --; + + if (valptr >= valend) + { + *ptr = start; + + return (NULL); + } + + if (*cur == '\\') + { + /* + * Decode escaped character... + */ + + cur ++; + + if (*cur == 'b') + *valptr++ = '\b'; + else if (*cur == 'f') + *valptr++ = '\f'; + else if (*cur == 'n') + *valptr++ = '\n'; + else if (*cur == 'r') + *valptr++ = '\r'; + else if (*cur == 't') + *valptr++ = '\t'; + else if (*cur >= '0' && *cur <= '7') + { + int ch = *cur - '0'; + + if (cur[1] >= '0' && cur[1] <= '7') + { + cur ++; + ch = (ch << 3) + *cur - '0'; + } + + if (cur[1] >= '0' && cur[1] <= '7') + { + cur ++; + ch = (ch << 3) + *cur - '0'; + } + + *valptr++ = ch; + } + else if (*cur == '\r') + { + if (cur[1] == '\n') + cur ++; + } + else if (*cur != '\n') + *valptr++ = *cur; + } + else + *valptr++ = *cur; + } + + if (*cur != ')') + { + *ptr = start; + + return (NULL); + } + + cur ++; + break; + + case '[' : /* Start array */ + obj.type = CUPS_PS_START_ARRAY; + cur ++; + break; + + case ']' : /* End array */ + obj.type = CUPS_PS_END_ARRAY; + cur ++; + break; + + case '<' : /* Start dictionary or hex string */ + if (cur[1] == '<') + { + obj.type = CUPS_PS_START_DICT; + cur += 2; + } + else + { + obj.type = CUPS_PS_STRING; + start = cur; + + for (cur ++, valptr = obj.value.string, + valend = obj.value.string + sizeof(obj.value.string) - 1; + *cur; + cur ++) + { + int ch; /* Current character */ + + + + if (*cur == '>') + break; + else if (valptr >= valend || !isxdigit(*cur & 255)) + { + *ptr = start; + return (NULL); + } + + if (*cur >= '0' && *cur <= '9') + ch = (*cur - '0') << 4; + else + ch = (tolower(*cur) - 'a' + 10) << 4; + + if (isxdigit(cur[1] & 255)) + { + cur ++; + + if (*cur >= '0' && *cur <= '9') + ch |= *cur - '0'; + else + ch |= tolower(*cur) - 'a' + 10; + } + + *valptr++ = ch; + } + + if (*cur != '>') + { + *ptr = start; + return (NULL); + } + + cur ++; + } + break; + + case '>' : /* End dictionary? */ + if (cur[1] == '>') + { + obj.type = CUPS_PS_END_DICT; + cur += 2; + } + else + { + obj.type = CUPS_PS_OTHER; + obj.value.other[0] = *cur; + + cur ++; + } + break; + + case '{' : /* Start procedure */ + obj.type = CUPS_PS_START_PROC; + cur ++; + break; + + case '}' : /* End procedure */ + obj.type = CUPS_PS_END_PROC; + cur ++; + break; + + case '-' : /* Possible number */ + case '+' : + if (!isdigit(cur[1] & 255) && cur[1] != '.') + { + obj.type = CUPS_PS_OTHER; + obj.value.other[0] = *cur; + + cur ++; + break; + } + + case '0' : /* Number */ + case '1' : + case '2' : + case '3' : + case '4' : + case '5' : + case '6' : + case '7' : + case '8' : + case '9' : + case '.' : + obj.type = CUPS_PS_NUMBER; + + start = cur; + for (cur ++; *cur; cur ++) + if (!isdigit(*cur & 255)) + break; + + if (*cur == '#') + { + /* + * Integer with radix... + */ + + obj.value.number = strtol(cur + 1, &cur, atoi(start)); + break; + } + else if (strchr(".Ee()<>[]{}/%", *cur) || isspace(*cur & 255)) + { + /* + * Integer or real number... + */ + + obj.value.number = _cupsStrScand(start, &cur, localeconv()); + break; + } + else + cur = start; + + default : /* Operator/variable name */ + start = cur; + + if (*cur == '/') + { + obj.type = CUPS_PS_NAME; + valptr = obj.value.name; + valend = obj.value.name + sizeof(obj.value.name) - 1; + cur ++; + } + else + { + obj.type = CUPS_PS_OTHER; + valptr = obj.value.other; + valend = obj.value.other + sizeof(obj.value.other) - 1; + } + + while (*cur) + { + if (strchr("()<>[]{}/%", *cur) || isspace(*cur & 255)) + break; + else if (valptr < valend) + *valptr++ = *cur++; + else + { + *ptr = start; + return (NULL); + } + } + + if (obj.type == CUPS_PS_OTHER) + { + if (!strcmp(obj.value.other, "true")) + { + obj.type = CUPS_PS_BOOLEAN; + obj.value.boolean = 1; + } + else if (!strcmp(obj.value.other, "false")) + { + obj.type = CUPS_PS_BOOLEAN; + obj.value.boolean = 0; + } + else if (!strcmp(obj.value.other, "null")) + obj.type = CUPS_PS_NULL; + else if (!strcmp(obj.value.other, "cleartomark")) + obj.type = CUPS_PS_CLEARTOMARK; + else if (!strcmp(obj.value.other, "copy")) + obj.type = CUPS_PS_COPY; + else if (!strcmp(obj.value.other, "dup")) + obj.type = CUPS_PS_DUP; + else if (!strcmp(obj.value.other, "index")) + obj.type = CUPS_PS_INDEX; + else if (!strcmp(obj.value.other, "pop")) + obj.type = CUPS_PS_POP; + else if (!strcmp(obj.value.other, "roll")) + obj.type = CUPS_PS_ROLL; + else if (!strcmp(obj.value.other, "setpagedevice")) + obj.type = CUPS_PS_SETPAGEDEVICE; + else if (!strcmp(obj.value.other, "stopped")) + obj.type = CUPS_PS_STOPPED; + } + break; + } + + /* + * Save the current position in the string and return the new object... + */ + + *ptr = cur; + + return (push_stack(st, &obj)); +} + + +/* + * 'setpagedevice()' - Simulate the PostScript setpagedevice operator. + */ + +static int /* O - 0 on success, -1 on error */ +setpagedevice( + _cups_ps_stack_t *st, /* I - Stack */ + cups_page_header2_t *h, /* O - Page header */ + int *preferred_bits)/* O - Preferred bits per color */ +{ + int i; /* Index into array */ + _cups_ps_obj_t *obj, /* Current object */ + *end; /* End of dictionary */ + const char *name; /* Attribute name */ + + + /* + * Make sure we have a dictionary on the stack... + */ + + if (st->num_objs == 0) + return (-1); + + obj = end = st->objs + st->num_objs - 1; + + if (obj->type != CUPS_PS_END_DICT) + return (-1); + + obj --; + + while (obj > st->objs) + { + if (obj->type == CUPS_PS_START_DICT) + break; + + obj --; + } + + if (obj < st->objs) + return (-1); + + /* + * Found the start of the dictionary, empty the stack to this point... + */ + + st->num_objs = (int)(obj - st->objs); + + /* + * Now pull /name and value pairs from the dictionary... + */ + + DEBUG_puts("setpagedevice: Dictionary:"); + + for (obj ++; obj < end; obj ++) + { + /* + * Grab the name... + */ + + if (obj->type != CUPS_PS_NAME) + return (-1); + + name = obj->value.name; + obj ++; + +#ifdef DEBUG + DEBUG_printf(("setpagedevice: /%s ", name)); + DEBUG_object(obj); +#endif /* DEBUG */ + + /* + * Then grab the value... + */ + + if (!strcmp(name, "MediaClass") && obj->type == CUPS_PS_STRING) + strlcpy(h->MediaClass, obj->value.string, sizeof(h->MediaClass)); + else if (!strcmp(name, "MediaColor") && obj->type == CUPS_PS_STRING) + strlcpy(h->MediaColor, obj->value.string, sizeof(h->MediaColor)); + else if (!strcmp(name, "MediaType") && obj->type == CUPS_PS_STRING) + strlcpy(h->MediaType, obj->value.string, sizeof(h->MediaType)); + else if (!strcmp(name, "OutputType") && obj->type == CUPS_PS_STRING) + strlcpy(h->OutputType, obj->value.string, sizeof(h->OutputType)); + else if (!strcmp(name, "AdvanceDistance") && obj->type == CUPS_PS_NUMBER) + h->AdvanceDistance = (unsigned)obj->value.number; + else if (!strcmp(name, "AdvanceMedia") && obj->type == CUPS_PS_NUMBER) + h->AdvanceMedia = (unsigned)obj->value.number; + else if (!strcmp(name, "Collate") && obj->type == CUPS_PS_BOOLEAN) + h->Collate = (unsigned)obj->value.boolean; + else if (!strcmp(name, "CutMedia") && obj->type == CUPS_PS_NUMBER) + h->CutMedia = (cups_cut_t)(unsigned)obj->value.number; + else if (!strcmp(name, "Duplex") && obj->type == CUPS_PS_BOOLEAN) + h->Duplex = (unsigned)obj->value.boolean; + else if (!strcmp(name, "HWResolution") && obj->type == CUPS_PS_START_ARRAY) + { + if (obj[1].type == CUPS_PS_NUMBER && obj[2].type == CUPS_PS_NUMBER && + obj[3].type == CUPS_PS_END_ARRAY) + { + h->HWResolution[0] = (unsigned)obj[1].value.number; + h->HWResolution[1] = (unsigned)obj[2].value.number; + obj += 3; + } + else + return (-1); + } + else if (!strcmp(name, "InsertSheet") && obj->type == CUPS_PS_BOOLEAN) + h->InsertSheet = (unsigned)obj->value.boolean; + else if (!strcmp(name, "Jog") && obj->type == CUPS_PS_NUMBER) + h->Jog = (unsigned)obj->value.number; + else if (!strcmp(name, "LeadingEdge") && obj->type == CUPS_PS_NUMBER) + h->LeadingEdge = (unsigned)obj->value.number; + else if (!strcmp(name, "ManualFeed") && obj->type == CUPS_PS_BOOLEAN) + h->ManualFeed = (unsigned)obj->value.boolean; + else if ((!strcmp(name, "cupsMediaPosition") || + !strcmp(name, "MediaPosition")) && obj->type == CUPS_PS_NUMBER) + { + /* + * cupsMediaPosition is supported for backwards compatibility only. + * We added it back in the Ghostscript 5.50 days to work around a + * bug in Ghostscript WRT handling of MediaPosition and setpagedevice. + * + * All new development should set MediaPosition... + */ + + h->MediaPosition = (unsigned)obj->value.number; + } + else if (!strcmp(name, "MediaWeight") && obj->type == CUPS_PS_NUMBER) + h->MediaWeight = (unsigned)obj->value.number; + else if (!strcmp(name, "MirrorPrint") && obj->type == CUPS_PS_BOOLEAN) + h->MirrorPrint = (unsigned)obj->value.boolean; + else if (!strcmp(name, "NegativePrint") && obj->type == CUPS_PS_BOOLEAN) + h->NegativePrint = (unsigned)obj->value.boolean; + else if (!strcmp(name, "NumCopies") && obj->type == CUPS_PS_NUMBER) + h->NumCopies = (unsigned)obj->value.number; + else if (!strcmp(name, "Orientation") && obj->type == CUPS_PS_NUMBER) + h->Orientation = (unsigned)obj->value.number; + else if (!strcmp(name, "OutputFaceUp") && obj->type == CUPS_PS_BOOLEAN) + h->OutputFaceUp = (unsigned)obj->value.boolean; + else if (!strcmp(name, "PageSize") && obj->type == CUPS_PS_START_ARRAY) + { + if (obj[1].type == CUPS_PS_NUMBER && obj[2].type == CUPS_PS_NUMBER && + obj[3].type == CUPS_PS_END_ARRAY) + { + h->cupsPageSize[0] = (float)obj[1].value.number; + h->cupsPageSize[1] = (float)obj[2].value.number; + + h->PageSize[0] = (unsigned)obj[1].value.number; + h->PageSize[1] = (unsigned)obj[2].value.number; + + obj += 3; + } + else + return (-1); + } + else if (!strcmp(name, "Separations") && obj->type == CUPS_PS_BOOLEAN) + h->Separations = (unsigned)obj->value.boolean; + else if (!strcmp(name, "TraySwitch") && obj->type == CUPS_PS_BOOLEAN) + h->TraySwitch = (unsigned)obj->value.boolean; + else if (!strcmp(name, "Tumble") && obj->type == CUPS_PS_BOOLEAN) + h->Tumble = (unsigned)obj->value.boolean; + else if (!strcmp(name, "cupsMediaType") && obj->type == CUPS_PS_NUMBER) + h->cupsMediaType = (unsigned)obj->value.number; + else if (!strcmp(name, "cupsBitsPerColor") && obj->type == CUPS_PS_NUMBER) + h->cupsBitsPerColor = (unsigned)obj->value.number; + else if (!strcmp(name, "cupsPreferredBitsPerColor") && + obj->type == CUPS_PS_NUMBER) + *preferred_bits = (int)obj->value.number; + else if (!strcmp(name, "cupsColorOrder") && obj->type == CUPS_PS_NUMBER) + h->cupsColorOrder = (cups_order_t)(unsigned)obj->value.number; + else if (!strcmp(name, "cupsColorSpace") && obj->type == CUPS_PS_NUMBER) + h->cupsColorSpace = (cups_cspace_t)(unsigned)obj->value.number; + else if (!strcmp(name, "cupsCompression") && obj->type == CUPS_PS_NUMBER) + h->cupsCompression = (unsigned)obj->value.number; + else if (!strcmp(name, "cupsRowCount") && obj->type == CUPS_PS_NUMBER) + h->cupsRowCount = (unsigned)obj->value.number; + else if (!strcmp(name, "cupsRowFeed") && obj->type == CUPS_PS_NUMBER) + h->cupsRowFeed = (unsigned)obj->value.number; + else if (!strcmp(name, "cupsRowStep") && obj->type == CUPS_PS_NUMBER) + h->cupsRowStep = (unsigned)obj->value.number; + else if (!strcmp(name, "cupsBorderlessScalingFactor") && + obj->type == CUPS_PS_NUMBER) + h->cupsBorderlessScalingFactor = (float)obj->value.number; + else if (!strncmp(name, "cupsInteger", 11) && obj->type == CUPS_PS_NUMBER) + { + if ((i = atoi(name + 11)) < 0 || i > 15) + return (-1); + + h->cupsInteger[i] = (unsigned)obj->value.number; + } + else if (!strncmp(name, "cupsReal", 8) && obj->type == CUPS_PS_NUMBER) + { + if ((i = atoi(name + 8)) < 0 || i > 15) + return (-1); + + h->cupsReal[i] = (float)obj->value.number; + } + else if (!strncmp(name, "cupsString", 10) && obj->type == CUPS_PS_STRING) + { + if ((i = atoi(name + 10)) < 0 || i > 15) + return (-1); + + strlcpy(h->cupsString[i], obj->value.string, sizeof(h->cupsString[i])); + } + else if (!strcmp(name, "cupsMarkerType") && obj->type == CUPS_PS_STRING) + strlcpy(h->cupsMarkerType, obj->value.string, sizeof(h->cupsMarkerType)); + else if (!strcmp(name, "cupsPageSizeName") && obj->type == CUPS_PS_STRING) + strlcpy(h->cupsPageSizeName, obj->value.string, + sizeof(h->cupsPageSizeName)); + else if (!strcmp(name, "cupsRenderingIntent") && + obj->type == CUPS_PS_STRING) + strlcpy(h->cupsRenderingIntent, obj->value.string, + sizeof(h->cupsRenderingIntent)); + else + { + /* + * Ignore unknown name+value... + */ + + DEBUG_printf((" Unknown name (\"%s\") or value...\n", name)); + + while (obj[1].type != CUPS_PS_NAME && obj < end) + obj ++; + } + } + + return (0); +} + + +#ifdef DEBUG +/* + * 'DEBUG_object()' - Print an object's value... + */ + +static void +DEBUG_object(_cups_ps_obj_t *obj) /* I - Object to print */ +{ + switch (obj->type) + { + case CUPS_PS_NAME : + DEBUG_printf(("/%s\n", obj->value.name)); + break; + + case CUPS_PS_NUMBER : + DEBUG_printf(("%g\n", obj->value.number)); + break; + + case CUPS_PS_STRING : + DEBUG_printf(("(%s)\n", obj->value.string)); + break; + + case CUPS_PS_BOOLEAN : + if (obj->value.boolean) + DEBUG_puts("true"); + else + DEBUG_puts("false"); + break; + + case CUPS_PS_NULL : + DEBUG_puts("null"); + break; + + case CUPS_PS_START_ARRAY : + DEBUG_puts("["); + break; + + case CUPS_PS_END_ARRAY : + DEBUG_puts("]"); + break; + + case CUPS_PS_START_DICT : + DEBUG_puts("<<"); + break; + + case CUPS_PS_END_DICT : + DEBUG_puts(">>"); + break; + + case CUPS_PS_START_PROC : + DEBUG_puts("{"); + break; + + case CUPS_PS_END_PROC : + DEBUG_puts("}"); + break; + + case CUPS_PS_CLEARTOMARK : + DEBUG_puts("--cleartomark--"); + break; + + case CUPS_PS_COPY : + DEBUG_puts("--copy--"); + break; + + case CUPS_PS_DUP : + DEBUG_puts("--dup--"); + break; + + case CUPS_PS_INDEX : + DEBUG_puts("--index--"); + break; + + case CUPS_PS_POP : + DEBUG_puts("--pop--"); + break; + + case CUPS_PS_ROLL : + DEBUG_puts("--roll--"); + break; + + case CUPS_PS_SETPAGEDEVICE : + DEBUG_puts("--setpagedevice--"); + break; + + case CUPS_PS_STOPPED : + DEBUG_puts("--stopped--"); + break; + + case CUPS_PS_OTHER : + DEBUG_printf(("--%s--\n", obj->value.other)); + break; + } +} + + +/* + * 'DEBUG_stack()' - Print a stack... + */ + +static void +DEBUG_stack(_cups_ps_stack_t *st) /* I - Stack */ +{ + int c; /* Looping var */ + _cups_ps_obj_t *obj; /* Current object on stack */ + + + for (obj = st->objs, c = st->num_objs; c > 0; c --, obj ++) + DEBUG_object(obj); +} +#endif /* DEBUG */ + + +/* + * End of "$Id: interpret.c 7852 2008-08-21 04:19:45Z mike $". + */ diff --git a/filter/libcupsimage2.def b/filter/libcupsimage2.def new file mode 100644 index 0000000..3c20284 --- /dev/null +++ b/filter/libcupsimage2.def @@ -0,0 +1,14 @@ +LIBRARY libcupsimage2 +VERSION 2.3 +EXPORTS +cupsRasterClose +cupsRasterErrorString +cupsRasterInterpretPPD +cupsRasterOpen +cupsRasterOpenIO +cupsRasterReadHeader +cupsRasterReadHeader2 +cupsRasterReadPixels +cupsRasterWriteHeader +cupsRasterWriteHeader2 +cupsRasterWritePixels diff --git a/filter/libcupsimage_s.exp b/filter/libcupsimage_s.exp new file mode 100644 index 0000000..57f4259 --- /dev/null +++ b/filter/libcupsimage_s.exp @@ -0,0 +1,16 @@ +_cupsImagePutCol +_cupsImagePutRow +_cupsImageReadBMP +_cupsImageReadGIF +_cupsImageReadJPEG +_cupsImageReadPIX +_cupsImageReadPNG +_cupsImageReadPNM +_cupsImageReadPhotoCD +_cupsImageReadSGI +_cupsImageReadSunRaster +_cupsImageReadTIFF +_cupsImageZoomDelete +_cupsImageZoomFill +_cupsImageZoomNew +_cupsRasterExecPS diff --git a/filter/postscript-driver.header b/filter/postscript-driver.header new file mode 100644 index 0000000..08e4c17 --- /dev/null +++ b/filter/postscript-driver.header @@ -0,0 +1,32 @@ + + +

Developing PostScript Printer Drivers

+ +

This document describes how to develop printer drivers for PostScript printers. Topics include: printer driver basics, creating new PPD files, importing existing PPD files, using custom filters, implementing color management, and adding OS X features.

+ +
+ + + + + + +
See AlsoProgramming: Developing Raster Printer Drivers
+ Programming: Filter and Backend Programming
+ Programming: Introduction to the PPD Compiler
+ Programming: Raster API
+ References: PPD Compiler Driver Information File Reference
+ Specifications: CUPS PPD Extensions
diff --git a/filter/postscript-driver.shtml b/filter/postscript-driver.shtml new file mode 100644 index 0000000..5e93f85 --- /dev/null +++ b/filter/postscript-driver.shtml @@ -0,0 +1,276 @@ +

Printer Driver Basics

+ +

A CUPS PostScript printer driver consists of a PostScript Printer Description (PPD) file that describes the features and capabilities of the device, zero or more filter programs that prepare print data for the device, and zero or more support files for color management, online help, and so forth. The PPD file includes references to all of the filters and support files used by the driver.

+ +

Every time a user prints something the scheduler program, cupsd(8), determines the format of the print job and the programs required to convert that job into something the printer understands. CUPS includes filter programs for many common formats, for example to convert Portable Document Format (PDF) files into device-independent PostScript, and then from device-independent PostScript to device-dependent PostScript. Figure 1 shows the data flow of a typical print job.

+ +
+ + +
Figure 1: PostScript Filter Chain
PostScript Filter Chain
+ +

The optional PostScript filter can be provided to add printer-specific commands to the PostScript output that cannot be represented in the PPD file or to reorganize the output for special printer features. Typically this is used to support advanced job management or finishing functions on the printer. CUPS includes a generic PostScript filter that handles all PPD-defined commands.

+ +

The optional port monitor handles interface-specific protocol or encoding issues. For example, many PostScript printers support the Binary Communications Protocol (BCP) and Tagged Binary Communications Protocol (TBCP) to allow applications to print 8-bit ("binary") PostScript jobs. CUPS includes port monitors for BCP and TBCP, and you can supply your own port monitors as needed.

+ +

The backend handles communications with the printer, sending print data from the last filter to the printer and relaying back-channel data from the printer to the upstream filters. CUPS includes backend programs for common direct-connect interfaces and network protocols, and you can provide your own backend to support custom interfaces and protocols.

+ +

The scheduler also supports a special "command" file format for sending maintenance commands and status queries to a printer or printer driver. Command print jobs typically use a single command filter program defined in the PPD file to generate the appropriate printer commands and handle any responses from the printer. Figure 2 shows the data flow of a typical command job.

+ +
+ + +
Figure 2: Command Filter Chain
Command Filter Chain
+ +

PostScript printer drivers typically do not require their own command filter since CUPS includes a generic PostScript command filter that supports all of the standard functions using PPD-defined commands.

+ + +

Creating New PPD Files

+ +

We recommend using the CUPS PPD compiler, ppdc(1), to create new PPD files since it manages many of the tedious (and error-prone!) details of paper sizes and localization for you. It also allows you to easily support multiple devices from a single source file. For more information see the "Introduction to the PPD Compiler" document. Listing 1 shows a driver information file for a black-and-white PostScript printer.

+ +

Listing 1: "examples/postscript.drv"

+ +
+// Include standard font and media definitions
+#include <font.defs>
+#include <media.defs>
+
+// Specify this is a PostScript printer driver
+DriverType ps
+
+// List the fonts that are supported, in this case all standard fonts
+Font *
+
+// Manufacturer, model name, and version
+Manufacturer "Foo"
+ModelName "Foo LaserProofer 2000"
+Version 1.0
+
+// PostScript printer attributes
+Attribute DefaultColorSpace "" Gray
+Attribute LandscapeOrientation "" Minus90
+Attribute LanguageLevel "" "3"
+Attribute Product "" "(Foo LaserProofer 2000)"
+Attribute PSVersion "" "(3010) 0"
+Attribute TTRasterizer "" Type42
+
+// Supported page sizes
+*MediaSize Letter
+MediaSize Legal
+MediaSize A4
+
+// Query command for page size
+Attribute "?PageSize" "" "
+      save
+      currentpagedevice /PageSize get aload pop
+      2 copy gt {exch} if (Unknown)
+      23 dict
+              dup [612 792] (Letter) put
+              dup [612 1008] (Legal) put
+              dup [595 842] (A4) put
+              {exch aload pop 4 index sub abs 5 le exch
+               5 index sub abs 5 le and
+              {exch pop exit} {pop} ifelse
+      } bind forall = flush pop pop
+      restore"
+
+// Specify the name of the PPD file we want to generate
+PCFileName "fooproof.ppd"
+
+ +

Required Attributes

+ +

PostScript drivers require the attributes listed in Table 1. If not specified, the defaults for CUPS drivers are used. A typical PostScript driver information file would include the following attributes:

+ +
+Attribute DefaultColorSpace "" Gray
+Attribute LandscapeOrientation "" Minus90
+Attribute LanguageLevel "" "3"
+Attribute Product "" "(Foo LaserProofer 2000)"
+Attribute PSVersion "" "(3010) 0"
+Attribute TTRasterizer "" Type42
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 1: Required PostScript Printer Driver Attributes
AttributeDescription
DefaultColorSpaceThe default colorspace: + Gray, RGB, CMY, or + CMYK. If not specified, then RGB is + assumed.
LandscapeOrientationThe preferred landscape + orientation: Plus90, Minus90, or + Any. If not specified, Plus90 is + assumed.
LanguageLevelThe PostScript language + level supported by the device: 1, 2, or 3. If not + specified, 2 is assumed.
ProductThe string returned by + the PostScript product operator, which + must include parenthesis to conform with + PostScript syntax rules for strings. Multiple + Product attributes may be specified to support + multiple products with the same PPD file. If not + specified, "(ESP Ghostscript)" and "(GNU Ghostscript)" + are assumed.
PSVersionThe PostScript + interpreter version numbers as returned by the + version and revision operators. The + required format is "(version) revision". Multiple + PSVersion attributes may be specified to + support multiple interpreter version numbers. If not + specified, "(3010) 705" and "(3010) 707" are + assumed.
TTRasterizerThe type of TrueType + font rasterizer supported by the device, if any. The + supported values are None, Accept68k, + Type42, and TrueImage. If not + specified, None is assumed.
+ +

Query Commands

+ +

Most PostScript printer PPD files include query commands (?PageSize, etc.) that allow applications to query the printer for its current settings and configuration. Query commands are included in driver information files as attributes. For example, the example in Listing 1 uses the following definition for the PageSize query command:

+ +
+Attribute "?PageSize" "" "
+      save
+      currentpagedevice /PageSize get aload pop
+      2 copy gt {exch} if (Unknown)
+      23 dict
+              dup [612 792] (Letter) put
+              dup [612 1008] (Legal) put
+              dup [595 842] (A4) put
+              {exch aload pop 4 index sub abs 5 le exch
+               5 index sub abs 5 le and
+              {exch pop exit} {pop} ifelse
+      } bind forall = flush pop pop
+      restore"
+
+ +

Query commands can span multiple lines, however no single line may contain more than 255 characters.

+ +

Importing Existing PPD Files

+ +

CUPS includes a utility called ppdi(1) +which allows you to import existing PPD files into the driver information file +format used by the PPD compiler ppdc(1). Once +imported, you can modify, localize, and regenerate the PPD files easily. Type +the following command to import the PPD file mydevice.ppd into the +driver information file mydevice.drv:

+ +
+ppdi -o mydevice.drv mydevice.ppd
+
+ +

If you have a whole directory of PPD files that you would like to import, +you can list multiple filenames or use shell wildcards to import more than one +PPD file on the command-line:

+ +
+ppdi -o mydevice.drv mydevice1.ppd mydevice2.ppd
+ppdi -o mydevice.drv *.ppd
+
+ +

If the driver information file already exists, the new PPD +file entries are appended to the end of the file. Each PPD file +is placed in its own group of curly braces within the driver +information file.

+ + +

Using Custom Filters

+ +

Normally a PostScript printer driver will not utilize any additional print filters. For drivers that provide additional filters such as a CUPS command file filter for doing printer maintenance, you must also list the following Filter directive to handle printing PostScript files:

+ +
+Filter application/vnd.cups-postscript 0 -
+
+ +

Custom Command Filters

+ +

The application/vnd.cups-command file type is used for CUPS command files. Use the following Filter directive to handle CUPS command files:

+ +
+Filter application/vnd.cups-command 100 /path/to/command/filter
+
+ +

To use the standard PostScript command filter, specify commandtops as the path to the command filter.

+ +

Custom PDF Filters

+ +

The application/pdf file type is used for unfiltered PDF files while the application/vnd.cups-pdf file type is used for filtered PDF files. Use the following Filter directive to handle filtered PDF files:

+ +
+Filter application/vnd.cups-pdf 100 /path/to/pdf/filter
+
+ +

For unfiltered PDF files, use:

+ +
+Filter application/pdf 100 /path/to/pdf/filter
+
+ +

Custom PDF filters that accept filtered data do not need to perform number-up processing and other types of page imposition, while those that accept unfiltered data MUST do the number-up processing themselves.

+ +

Custom PostScript Filters

+ +

The application/vnd.cups-postscript file type is used for filtered PostScript files. Use the following Filter directive to handle PostScript files:

+ +
+Filter application/vnd.cups-postscript 100 /path/to/postscript/filter
+
+ + +

Implementing Color Management

+ +

CUPS uses ICC color profiles to provide more accurate color reproduction. The cupsICCProfile attribute defines the color profiles that are available for a given printer, for example:

+ +
+Attribute cupsICCProfile "ColorModel.MediaType.Resolution/Description" /path/to/ICC/profile
+
+ +

where "ColorModel.MediaType.Resolution" defines a selector based on the corresponding option selections. A simple driver might only define profiles for the color models that are supported, for example a printer supporting Gray and RGB might use:

+ +
+Attribute cupsICCProfile "Gray../Grayscale Profile" /path/to/ICC/gray-profile
+Attribute cupsICCProfile "RGB../Full Color Profile" /path/to/ICC/rgb-profile
+
+ +

The options used for profile selection can be customized using the cupsICCQualifier2 and cupsICCQualifier3 attributes.

+ + +

Adding OS X Features

+ +

OS X printer drivers can provide additional attributes to specify additional option panes in the print dialog, an image of the printer, a help book, and option presets for the driver software:

+ +
+Attribute APDialogExtension "" /Library/Printers/Vendor/filename.plugin
+Attribute APHelpBook "" /Library/Printers/Vendor/filename.bundle
+Attribute APPrinterIconPath "" /Library/Printers/Vendor/filename.icns
+Attribute APPrinterPreset "name/text" "*option choice ..."
+
diff --git a/filter/ppd-compiler.header b/filter/ppd-compiler.header new file mode 100644 index 0000000..2caf3ae --- /dev/null +++ b/filter/ppd-compiler.header @@ -0,0 +1,40 @@ + + +

Introduction to the PPD Compiler

+ +

This document describes how to use the CUPS PostScript Printer Description +(PPD) file compiler. The PPD compiler generates PPD files from simple text files +that describe the features and capabilities of one or more printers.

+ +
Note: + +

The PPD compiler and related tools are deprecated and will be removed in a future release of CUPS.

+ +
+ +
+ + + + + + +
See AlsoProgramming: Developing Raster Printer Drivers
+ Programming: Developing PostScript Printer Drivers
+ Programming: Filter and Backend Programming
+ Programming: Raster API
+ References: PPD Compiler Driver Information File Reference
+ Specifications: CUPS PPD Extensions
diff --git a/filter/ppd-compiler.shtml b/filter/ppd-compiler.shtml new file mode 100644 index 0000000..dca2870 --- /dev/null +++ b/filter/ppd-compiler.shtml @@ -0,0 +1,883 @@ +

The Basics

+ +

The PPD compiler, ppdc(1), is a +simple command-line tool that takes a single driver information file, +which by convention uses the extension .drv, and produces one or more +PPD files that may be distributed with your printer drivers for use with CUPS. +For example, you would run the following command to create the English language +PPD files defined by the driver information file mydrivers.drv:

+ +
+ppdc mydrivers.drv
+
+ +

The PPD files are placed in a subdirectory called +ppd. The -d option is used to put the PPD +files in a different location, for example:

+ +
+ppdc -d myppds mydrivers.drv
+
+ +

places the PPD files in a subdirectory named +myppds. Finally, use the -l option to +specify the language localization for the PPD files that are +created, for example:

+ +
+ppdc -d myppds/de -l de mydrivers.drv
+ppdc -d myppds/en -l en mydrivers.drv
+ppdc -d myppds/es -l es mydrivers.drv
+ppdc -d myppds/fr -l fr mydrivers.drv
+ppdc -d myppds/it -l it mydrivers.drv
+
+ +

creates PPD files in German (de), English (en), Spanish (es), +French (fr), and Italian (it) in the corresponding +subdirectories. Specify multiple languages (separated by commas) to produce +"globalized" PPD files:

+ +
+ppdc -d myppds -l de,en,es,fr,it mydrivers.drv
+
+ + +

Driver Information Files

+ +

The driver information files accepted by the PPD compiler are +plain text files that define the various attributes and options +that are included in the PPD files that are generated. A driver +information file can define the information for one or more printers and +their corresponding PPD files.

+ +

Listing 1: "examples/minimum.drv"

+ +
+// Include standard font and media definitions
+#include <font.defs>
+#include <media.defs>
+
+// List the fonts that are supported, in this case all standard fonts...
+Font *
+
+// Manufacturer, model name, and version
+Manufacturer "Foo"
+ModelName "FooJet 2000"
+Version 1.0
+
+// Each filter provided by the driver...
+Filter application/vnd.cups-raster 100 rastertofoo
+
+// Supported page sizes
+*MediaSize Letter
+MediaSize A4
+
+// Supported resolutions
+*Resolution k 8 0 0 0 "600dpi/600 DPI"
+
+// Specify the name of the PPD file we want to generate...
+PCFileName "foojet2k.ppd"
+
+ + +

A Simple Example

+ +

The example in Listing 1 shows a driver information +file which defines the minimum required attributes to provide a valid PPD file. +The first part of the file includes standard definition files for fonts and +media sizes:

+ +
+#include <font.defs>
+#include <media.defs>
+
+ +

The #include directive works just like the C/C++ include directive; +files included using the angle brackets (<filename>) are found +in any of the standard include directories and files included using quotes +("filename") are found in the same directory as the source or include +file. The <font.defs> include file defines the standard fonts +which are included with GPL Ghostscript and the Apple PDF RIP, while the +<media.defs> include file defines the standard media sizes +listed in Appendix B of the Adobe PostScript Printer Description File Format +Specification.

+ +

CUPS provides several other standard include files:

+ +
    + +
  • <epson.h> - Defines all of the rastertoepson driver + constants.
  • + +
  • <escp.h> - Defines all of the rastertoescpx driver + constants.
  • + +
  • <hp.h> - Defines all of the rastertohp driver + constants.
  • + +
  • <label.h> - Defines all of the rastertolabel driver + constants.
  • + +
  • <pcl.h> - Defines all of the rastertopclx driver + constants.
  • + +
  • <raster.defs> - Defines all of the CUPS raster format + constants.
  • + +
+ +

Next we list all of the fonts that are available in the driver; for CUPS +raster drivers, the following line is all that is usually supplied:

+ +
+Font *
+
+ +

The Font directive specifies the name of a single font or the +asterisk to specify all fonts. For example, you would use the following line to +define an additional bar code font that you are supplying with your printer +driver:

+ +
+//   name         encoding  version  charset  status
+Font Barcode-Foo  Special   "(1.0)"  Special  ROM
+
+ +

The name of the font is Barcode-Foo. Since it is not a standard +text font, the encoding and charset name Special is used. The version +number is 1.0 and the status (where the font is located) is +ROM to indicate that the font does not need to be embedded in +documents that use the font for this printer.

+ +

Third comes the manufacturer, model name, and version number information +strings:

+ +
+Manufacturer "Foo"
+ModelName "FooJet 2000"
+Version 1.0
+
+ +

These strings are used when the user (or auto-configuration program) selects +the printer driver for a newly connected device.

+ +

The list of filters comes after the information strings; for the example in +Listing 1, we have a single filter that takes CUPS +raster data:

+ +
+Filter application/vnd.cups-raster 100 rastertofoo
+
+ +

Each filter specified in the driver information file is the equivalent of a +printer driver for that format; if a user submits a print job in a different +format, CUPS figures out the sequence of commands that will produce a supported +format for the least relative cost.

+ +

Once we have defined the driver information we specify the supported options. +For the example driver we support a single resolution of 600 dots per inch and +two media sizes, A4 and Letter:

+ +
+*MediaSize Letter
+MediaSize A4
+
+*Resolution k 8 0 0 0 "600dpi/600 DPI"
+
+ +

The asterisk in front of the MediaSize and Resolution +directives specify that those option choices are the default. The +MediaSize directive is followed by a media size name which is normally +defined in the <media.defs> file and corresponds to a standard +Adobe media size name. If the default media size is Letter, the PPD +compiler will override it to be A4 for non-English localizations for +you automatically.

+ +

The Resolution directive accepts several values after it as +follows:

+ +
    + +
  1. Colorspace for this resolution, if any. In the example file, the + colorspace k is used which corresponds to black. For printer + drivers that support color printing, this field is usually specified as + "-" for "no change".
  2. + +
  3. Bits per color. In the example file, we define 8 bits per color, for + a continuous-tone grayscale output. All versions of CUPS support 1 and + 8 bits per color. CUPS 1.2 and higher (OS X 10.5 and higher) also + supports 16 bits per color.
  4. + +
  5. Rows per band. In the example file, we define 0 rows per band to + indicate that our printer driver does not process the page in + bands.
  6. + +
  7. Row feed. In the example, we define the feed value to be 0 to + indicate that our printer driver does not interleave the output.
  8. + +
  9. Row step. In the example, we define the step value to be 0 to + indicate that our printer driver does not interleave the output. This + value normally indicates the spacing between the nozzles of an inkjet + printer - when combined with the previous two values, it informs the + driver how to stagger the output on the page to produce interleaved + lines on the page for higher-resolution output.
  10. + +
  11. Choice name and text. In the example, we define the choice name and + text to be "600dpi/600 DPI". The name and text are separated by + slash (/) character; if no text is specified, then the name is + used as the text. The PPD compiler parses the name to determine the + actual resolution; the name can be of the form + RESOLUTIONdpi for resolutions that are equal + horizontally and vertically or HRESxVRESdpi for + isometric resolutions. Only integer resolution values are supported, so + a resolution name of 300dpi is valid while 300.1dpi is + not.
  12. + +
+ +

Finally, the PCFileName directive specifies that the named PPD file +should be written for the current driver definitions:

+ +
+PCFileName "foojet2k.ppd"
+
+ +

The filename follows the directive and must conform to the Adobe +filename requirements in the Adobe Postscript Printer Description File Format +Specification. Specifically, the filename may not exceed 8 characters followed +by the extension .ppd. The FileName directive can be used to +specify longer filenames:

+ +
+FileName "FooJet 2000"
+
+ + +

Grouping and Inheritance

+ +

The previous example created a single PPD file. Driver information files can +also define multiple printers by using the PPD compiler grouping functionality. +Directives are grouped using the curly braces ({ and }) and +every group that uses the PCFileName or FileName directives +produces a PPD file with that name. Listing 2 shows a +variation of the original example that uses two groups to define two printers +that share the same printer driver filter but provide two different resolution +options.

+ +

Listing 2: "examples/grouping.drv"

+ +
+
+// Include standard font and media definitions
+#include <font.defs>
+#include <media.defs>
+
+// List the fonts that are supported, in this case all standard fonts...
+Font *
+
+// Manufacturer and version
+Manufacturer "Foo"
+Version 1.0
+
+// Each filter provided by the driver...
+Filter application/vnd.cups-raster 100 rastertofoo
+
+// Supported page sizes
+*MediaSize Letter
+MediaSize A4
+
+{
+  // Supported resolutions
+  *Resolution k 8 0 0 0 "600dpi/600 DPI"
+
+  // Specify the model name and filename...
+  ModelName "FooJet 2000"
+  PCFileName "foojet2k.ppd"
+}
+
+{
+  // Supported resolutions
+  *Resolution k 8 0 0 0 "1200dpi/1200 DPI"
+
+  // Specify the model name and filename...
+  ModelName "FooJet 2001"
+  PCFileName "foojt2k1.ppd"
+}
+
+ +

The second example is essentially the same as the first, except that each +printer model is defined inside of a pair of curly braces. For example, the +first printer is defined using:

+ +
+{
+  // Supported resolutions
+  *Resolution k 8 0 0 0 "600dpi/600 DPI"
+
+  // Specify the model name and filename...
+  ModelName "FooJet 2000"
+  PCFileName "foojet2k.ppd"
+}
+
+ +

The printer inherits all of the definitions from the parent group (the +top part of the file) and adds the additional definitions inside the curly +braces for that printer driver. When we define the second group, it also +inherits the same definitions from the parent group but none of the +definitions from the first driver. Groups can be nested to any number of levels +to support variations of similar models without duplication of information.

+ + +

Color Support

+ +

For printer drivers that support color printing, the +ColorDevice and ColorModel directives should be +used to tell the printing system that color output is desired +and in what formats. Listing 3 shows a +variation of the previous example which includes a color printer +that supports printing at 300 and 600 DPI.

+ +

The key changes are the addition of the ColorDevice +directive:

+ +
+ColorDevice true
+
+ +

which tells the printing system that the printer supports +color printing, and the ColorModel directives:

+ +
+ColorModel Gray/Grayscale w chunky 0
+*ColorModel RGB/Color rgb chunky 0
+
+ +

which tell the printing system which colorspaces are supported by the printer +driver for color printing. Each of the ColorModel directives is +followed by the option name and text (Gray/Grayscale and +RGB/Color), the colorspace name (w and rgb), the +color organization (chunky), and the compression mode number +(0) to be passed to the driver. The option name can be any of the +standard Adobe ColorModel names:

+ +
    + +
  • Gray - Grayscale output. + +
  • RGB - Color output, typically using the RGB + colorspace, but without a separate black channel. + +
  • CMYK - Color output with a separate black + channel. + +
+ +

Custom names can be used, however it is recommended that you use your vendor +prefix for any custom names, for example "fooName".

+ +

The colorspace name can be any of the following universally supported +colorspaces:

+ +
    +
  • w - Luminance
  • + +
  • rgb - Red, green, blue
  • + +
  • k - Black
  • + +
  • cmy - Cyan, magenta, yellow
  • + +
  • cmyk - Cyan, magenta, yellow, black
  • + +
+ +

The color organization can be any of the following values:

+ +
    + +
  • chunky - Color values are passed together on a line + as RGB RGB RGB RGB
  • + +
  • banded - Color values are passed separately + on a line as RRRR GGGG BBBB; not supported by the Apple + RIP filters
  • + +
  • planar - Color values are passed separately + on a page as RRRR RRRR RRRR ... GGGG GGGG GGGG ... BBBB + BBBB BBBB; not supported by the Apple RIP filters
  • + +
+ +

The compression mode value is passed to the driver in the +cupsCompression attribute. It is traditionally used to select an +appropriate compression mode for the color model but can be used for any +purpose, such as specifying a photo mode vs. standard mode.

+ +

Listing 3: "examples/color.drv"

+ +
+
+// Include standard font and media definitions
+#include <font.defs>
+#include <media.defs>
+
+// List the fonts that are supported, in this case all standard fonts...
+Font *
+
+// Manufacturer and version
+Manufacturer "Foo"
+Version 1.0
+
+// Each filter provided by the driver...
+Filter application/vnd.cups-raster 100 rastertofoo
+
+// Supported page sizes
+*MediaSize Letter
+MediaSize A4
+
+{
+  // Supported resolutions
+  *Resolution k 8 0 0 0 "600dpi/600 DPI"
+
+  // Specify the model name and filename...
+  ModelName "FooJet 2000"
+  PCFileName "foojet2k.ppd"
+}
+
+{
+  // Supports color printing
+  ColorDevice true
+
+  // Supported colorspaces
+  ColorModel Gray/Grayscale w chunky 0
+  *ColorModel RGB/Color rgb chunky 0
+
+  // Supported resolutions
+  *Resolution - 8 0 0 0 "300dpi/300 DPI"
+  Resolution - 8 0 0 0 "600dpi/600 DPI"
+
+  // Specify the model name and filename...
+  ModelName "FooJet Color"
+  PCFileName "foojetco.ppd"
+}
+
+ + +

Defining Custom Options and Option Groups

+ +

The Group, Option, and Choice +directives are used to define or select a group, option, or +choice. Listing 4 shows a variation of +the first example that provides two custom options in a group +named "Footasm".

+ +

Listing 4: "examples/custom.drv"

+ +
+
+// Include standard font and media definitions
+#include <font.defs>
+#include <media.defs>
+
+// List the fonts that are supported, in this case all standard fonts...
+Font *
+
+// Manufacturer, model name, and version
+Manufacturer "Foo"
+ModelName "FooJet 2000"
+Version 1.0
+
+// Each filter provided by the driver...
+Filter application/vnd.cups-raster 100 rastertofoo
+
+// Supported page sizes
+*MediaSize Letter
+MediaSize A4
+
+// Supported resolutions
+*Resolution k 8 0 0 0 "600dpi/600 DPI"
+
+// Option Group
+Group "Footasm"
+
+  // Boolean option
+  Option "fooEnhance/Resolution Enhancement" Boolean AnySetup 10
+    *Choice True/Yes "<</cupsCompression 1>>setpagedevice"
+    Choice False/No "<</cupsCompression 0>>setpagedevice"
+
+  // Multiple choice option
+  Option "fooOutputType/Output Quality" PickOne AnySetup 10
+    *Choice "Auto/Automatic Selection"
+            "<</OutputType(Auto)>>setpagedevice""
+    Choice "Text/Optimize for Text"
+            "<</OutputType(Text)>>setpagedevice""
+    Choice "Graph/Optimize for Graphics"
+            "<</OutputType(Graph)>>setpagedevice""
+    Choice "Photo/Optimize for Photos"
+            "<</OutputType(Photo)>>setpagedevice""
+
+// Specify the name of the PPD file we want to generate...
+PCFileName "foojet2k.ppd"
+
+ +

The custom group is introduced by the Group +directive which is followed by the name and optionally text for +the user:

+ +
+Group "Footasm/Footastic Options"
+
+ +

The group name must conform to the PPD specification and +cannot exceed 40 characters in length. If you specify user text, +it cannot exceed 80 characters in length. The groups +General, Extra, and +InstallableOptions are predefined by CUPS; the general +and extra groups are filled by the UI options defined by the PPD +specification. The InstallableOptions group is reserved +for options that define whether accessories for the printer +(duplexer unit, finisher, stapler, etc.) are installed.

+ +

Once the group is specified, the Option directive is +used to introduce a new option:

+ +
+Option "fooEnhance/Resolution Enhancement" Boolean AnySetup 10
+
+ +

The directive is followed by the name of the option and any +optional user text, the option type, the PostScript document group, and +the sort order number. The option name must conform to the PPD specification +and cannot exceed 40 characters in length. If you specify user text, it +cannot exceed 80 characters in length.

+ +

The option type can be Boolean for true/false +selections, PickOne for picking one of many choices, or +PickMany for picking zero or more choices. Boolean +options can have at most two choices with the names +False and True. Pick options can have any +number of choices, although for Windows compatibility reasons +the number of choices should not exceed 255.

+ +

The PostScript document group is typically AnySetup, +meaning that the option can be introduced at any point in the +PostScript document. Other values include PageSetup to +include the option before each page and DocumentSetup +to include the option once at the beginning of the document.

+ +

The sort order number is used to sort the printer commands +associated with each option choice within the PostScript +document. This allows you to setup certain options before others +as required by the printer. For most CUPS raster printer +drivers, the value 10 can be used for all options.

+ +

Once the option is specified, each option choice can be +listed using the Choice directive:

+ +
+*Choice True/Yes "<</cupsCompression 1>>setpagedevice"
+Choice False/No "<</cupsCompression 0>>setpagedevice"
+
+ +

The directive is followed by the choice name and optionally +user text, and the PostScript commands that should be inserted +when printing a file to this printer. The option name must +conform to the PPD specification and cannot exceed 40 characters +in length. If you specify user text, it cannot exceed 80 +characters in length.

+ +

The PostScript commands are also interpreted by any RIP +filters, so these commands typically must be present for all +option choices. Most commands take the form:

+ +
+<</name value>>setpagedevice
+
+ +

where name is the name of the PostScript page device +attribute and value is the numeric or string value for +that attribute.

+ + +

Defining Constants

+ +

Sometimes you will want to define constants for your drivers +so that you can share values in different groups within the same +driver information file, or to share values between different +driver information files using the #include directive. +The #define directive is used to define constants for +use in your printer definitions:

+ +
+#define NAME value
+
+ +

The NAME is any sequence of letters, numbers, and +the underscore. The value is a number or string; if the +value contains spaces you must put double quotes around it, for +example:

+ +
+#define FOO "My String Value"
+
+ +

Constants can also be defined on the command-line using the -D +option:

+ +
+ppdc -DNAME="value" filename.drv
+
+ +

Once defined, you use the notation $NAME to substitute the value of +the constant in the file, for example:

+ +
+#define MANUFACTURER "Foo"
+#define FOO_600      0
+#define FOO_1200     1
+
+{
+  Manufacturer "$MANUFACTURER"
+  ModelNumber $FOO_600
+  ModelName "FooJet 2000"
+  ...
+}
+
+{
+  Manufacturer "$MANUFACTURER"
+  ModelNumber $FOO_1200
+  ModelName "FooJet 2001"
+  ...
+}
+
+ +

Numeric constants can be bitwise OR'd together by placing the constants +inside parenthesis, for example:

+ +
+// ModelNumber capability bits
+#define DUPLEX 1
+#define COLOR  2
+
+...
+
+{
+  // Define a model number specifying the capabilities of the printer...
+  ModelNumber ($DUPLEX $COLOR)
+  ...
+}
+
+ + +

Conditional Statements

+ +

The PPD compiler supports conditional compilation using the #if, +#elif, #else, and #endif directives. The #if +and #elif directives are followed by a constant name or an expression. +For example, to include a group of options when "ADVANCED" is defined:

+ +
+#if ADVANCED
+Group "Advanced/Advanced Options"
+  Option "fooCyanAdjust/Cyan Adjustment"
+    Choice "plus10/+10%" ""
+    Choice "plus5/+5%" ""
+    *Choice "none/No Adjustment" ""
+    Choice "minus5/-5%" ""
+    Choice "minus10/-10%" ""
+  Option "fooMagentaAdjust/Magenta Adjustment"
+    Choice "plus10/+10%" ""
+    Choice "plus5/+5%" ""
+    *Choice "none/No Adjustment" ""
+    Choice "minus5/-5%" ""
+    Choice "minus10/-10%" ""
+  Option "fooYellowAdjust/Yellow Adjustment"
+    Choice "plus10/+10%" ""
+    Choice "plus5/+5%" ""
+    *Choice "none/No Adjustment" ""
+    Choice "minus5/-5%" ""
+    Choice "minus10/-10%" ""
+  Option "fooBlackAdjust/Black Adjustment"
+    Choice "plus10/+10%" ""
+    Choice "plus5/+5%" ""
+    *Choice "none/No Adjustment" ""
+    Choice "minus5/-5%" ""
+    Choice "minus10/-10%" ""
+#endif
+
+ + +

Defining Constraints

+ +

Constraints are strings that are used to specify that one or more option +choices are incompatible, for example two-sided printing on transparency media. +Constraints are also used to prevent the use of uninstalled features such as the +duplexer unit, additional media trays, and so forth.

+ +

The UIConstraints directive is used to specify a constraint that is +placed in the PPD file. The directive is followed by a string using one of the +following formats:

+ +
+UIConstraints "*Option1 *Option2"
+UIConstraints "*Option1 Choice1 *Option2"
+UIConstraints "*Option1 *Option2 Choice2"
+UIConstraints "*Option1 Choice1 *Option2 Choice2"
+
+ +

Each option name is preceded by the asterisk (*). If no choice is +given for an option, then all choices except False and +None will conflict with the other option and choice(s). Since the PPD +compiler automatically adds reciprocal constraints (option A conflicts with +option B, so therefore option B conflicts with option A), you need only specify +the constraint once.

+ +

Listing 5: "examples/constraint.drv"

+ +
+
+// Include standard font and media definitions
+#include <font.defs>
+#include <media.defs>
+
+// List the fonts that are supported, in this case all standard fonts...
+Font *
+
+// Manufacturer, model name, and version
+Manufacturer "Foo"
+ModelName "FooJet 2000"
+Version 1.0
+
+// Each filter provided by the driver...
+Filter application/vnd.cups-raster 100 rastertofoo
+
+// Supported page sizes
+*MediaSize Letter
+MediaSize A4
+
+// Supported resolutions
+*Resolution k 8 0 0 0 "600dpi/600 DPI"
+
+// Installable Option Group
+Group "InstallableOptions/Options Installed"
+
+  // Duplexing unit option
+  Option "OptionDuplexer/Duplexing Unit" Boolean AnySetup 10
+    Choice True/Installed ""
+    *Choice "False/Not Installed" ""
+
+// General Option Group
+Group General
+
+  // Duplexing option
+  Option "Duplex/Two-Sided Printing" PickOne AnySetup 10
+    *Choice "None/No" "<</Duplex false>>setpagedevice""
+    Choice "DuplexNoTumble/Long Edge Binding"
+           "<</Duplex true/Tumble false>>setpagedevice""
+    Choice "DuplexTumble/Short Edge Binding"
+           "<</Duplex true/Tumble true>>setpagedevice""
+
+// Only allow duplexing if the duplexer is installed
+UIConstraints "*Duplex *OptionDuplexer False"
+
+// Specify the name of the PPD file we want to generate...
+PCFileName "foojet2k.ppd"
+
+ +

Listing 5 shows a variation of the first example with +an added Duplex option and installable option for the duplexer, +OptionDuplex. A constraint is added at the end to specify that any +choice of the Duplex option that is not None is incompatible +with the "Duplexer Installed" option set to "Not Installed" +(False):

+ +
+UIConstraints "*Duplex *OptionDuplexer False"
+
+ +

Enhanced Constraints

+ +

CUPS 1.4 supports constraints between 2 or more options using the +Attribute directive. cupsUIConstraints attributes define +the constraints, while cupsUIResolver attributes define option changes +to resolve constraints. For example, we can specify the previous duplex +constraint with a resolver that turns off duplexing with the following two +lines:

+ +
+Attribute cupsUIConstraints DuplexOff "*Duplex *OptionDuplexer False"
+Attribute cupsUIResolver DuplexOff "*Duplex None"
+
+ +

Localization

+ +

The PPD compiler provides localization of PPD files in different languages +through message catalog files in the GNU gettext or Apple .strings +formats. Each user text string and several key PPD attribute values such as +LanguageVersion and LanguageEncoding are looked up in the +corresponding message catalog and the translated text is substituted in the +generated PPD files. One message catalog file can be used by multiple driver +information files, and each file contains a single language translation.

+ +

The ppdpo Utility

+ +

While CUPS includes localizations of all standard media sizes and options in +several languages, your driver information files may provide their own media +sizes and options that need to be localized. CUPS provides a utility program to +aid in the localization of drivers called ppdpo(1). The ppdpo program creates +or updates a message catalog file based upon one or more driver information +files. New messages are added with the word "TRANSLATE" added to the front of +the translation string to make locating new strings for translation easier. The +program accepts the message catalog filename and one or more driver information +files.

+ +

For example, run the following command to create a new German message catalog +called de.po for all of the driver information files in the current +directory:

+ +
+ppdpo -o de.po *.drv
+
+ +

If the file de.po already exists, ppdpo will update the +contents of the file with any new messages that need to be translated. To create +an Apple .strings file instead, specify the output filename with a .strings +extension, for example:

+ +
+ppdpo -o de.strings *.drv
+
+ +

Using Message Catalogs with the PPD Compiler

+ +

Once you have created a message catalog, use the #po directive to declare it in each +driver information file. For example, to declare the German message catalog for +a driver use:

+ +
+#po de "de.po"  // German
+
+ +

In fact, you can use the #po directive as many times as needed:

+ +
+#po de "de.po"  // German
+#po es "es.po"  // Spanish
+#po fr "fr.po"  // French
+#po it "it.po"  // Italian
+#po ja "ja.po"  // Japanese
+
+ +

The filename ("de.po", etc.) can be relative to the location of the driver +information file or an absolute path. Once defined, the PPD compiler will +automatically generate a globalized PPD for every language declared in your +driver information file. To generate a single-language PPD file, simply use the +-l option to list the corresponding locale, for example:

+ +
+ppdc -l de -d ppd/de mydrivers.drv
+
+ +

to generate German PPD files.

diff --git a/filter/pstops.c b/filter/pstops.c new file mode 100644 index 0000000..bcc8d2b --- /dev/null +++ b/filter/pstops.c @@ -0,0 +1,3433 @@ +/* + * "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $" + * + * PostScript filter for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1993-2007 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. + * + * Contents: + * + * main() - Main entry. + * add_page() - Add a page to the pages array. + * cancel_job() - Flag the job as canceled. + * check_range() - Check to see if the current page is selected for + * printing. + * copy_bytes() - Copy bytes from the input file to stdout. + * copy_comments() - Copy all of the comments section. + * copy_dsc() - Copy a DSC-conforming document. + * copy_non_dsc() - Copy a document that does not conform to the DSC. + * copy_page() - Copy a page description. + * copy_prolog() - Copy the document prolog section. + * copy_setup() - Copy the document setup section. + * copy_trailer() - Copy the document trailer. + * do_prolog() - Send the necessary document prolog commands. + * do_setup() - Send the necessary document setup commands. + * doc_printf() - Send a formatted string to stdout and/or the temp + * file. + * doc_puts() - Send a nul-terminated string to stdout and/or the + * temp file. + * doc_write() - Send data to stdout and/or the temp file. + * end_nup() - End processing for N-up printing. + * include_feature() - Include a printer option/feature command. + * parse_text() - Parse a text value in a comment. + * set_pstops_options() - Set pstops options. + * skip_page() - Skip past a page that won't be printed. + * start_nup() - Start processing for N-up printing. + * write_label_prolog() - Write the prolog with the classification and page + * label. + * write_labels() - Write the actual page labels. + * write_options() - Write options provided via %%IncludeFeature. + */ + +/* + * Include necessary headers... + */ + +#include "common.h" +#include +#include +#include +#include +#include +#include + + +/* + * Constants... + */ + +#define PSTOPS_BORDERNONE 0 /* No border or hairline border */ +#define PSTOPS_BORDERTHICK 1 /* Think border */ +#define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */ +#define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */ +#define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */ +#define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */ + +#define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */ +#define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */ +#define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */ +#define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */ +#define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */ +#define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */ +#define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */ +#define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */ + +#define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */ +#define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */ +#define PSTOPS_LAYOUT_VERTICAL 4 + + +/* + * Types... + */ + +typedef struct /**** Page information ****/ +{ + char *label; /* Page label */ + int bounding_box[4]; /* PageBoundingBox */ + off_t offset; /* Offset to start of page */ + ssize_t length; /* Number of bytes for page */ + int num_options; /* Number of options for this page */ + cups_option_t *options; /* Options for this page */ +} pstops_page_t; + +typedef struct /**** Document information ****/ +{ + int page; /* Current page */ + int bounding_box[4]; /* BoundingBox from header */ + int new_bounding_box[4]; /* New composite bounding box */ + int num_options; /* Number of document-wide options */ + cups_option_t *options; /* Document-wide options */ + int normal_landscape, /* Normal rotation for landscape? */ + saw_eof, /* Saw the %%EOF comment? */ + slow_collate, /* Collate copies by hand? */ + slow_duplex, /* Duplex pages slowly? */ + slow_order, /* Reverse pages slowly? */ + use_ESPshowpage; /* Use ESPshowpage? */ + cups_array_t *pages; /* Pages in document */ + cups_file_t *temp; /* Temporary file, if any */ + char tempfile[1024]; /* Temporary filename */ + int job_id; /* Job ID */ + const char *user, /* User name */ + *title; /* Job name */ + int copies; /* Number of copies */ + const char *ap_input_slot, /* AP_FIRSTPAGE_InputSlot value */ + *ap_manual_feed, /* AP_FIRSTPAGE_ManualFeed value */ + *ap_media_color, /* AP_FIRSTPAGE_MediaColor value */ + *ap_media_type, /* AP_FIRSTPAGE_MediaType value */ + *ap_page_region, /* AP_FIRSTPAGE_PageRegion value */ + *ap_page_size; /* AP_FIRSTPAGE_PageSize value */ + int collate, /* Collate copies? */ + emit_jcl, /* Emit JCL commands? */ + fit_to_page; /* Fit pages to media */ + const char *input_slot, /* InputSlot value */ + *manual_feed, /* ManualFeed value */ + *media_color, /* MediaColor value */ + *media_type, /* MediaType value */ + *page_region, /* PageRegion value */ + *page_size; /* PageSize value */ + int mirror, /* doc->mirror/mirror pages */ + number_up, /* Number of pages on each sheet */ + number_up_layout, /* doc->number_up_layout of N-up pages */ + output_order, /* Requested reverse output order? */ + page_border; /* doc->page_border around pages */ + const char *page_label, /* page-label option, if any */ + *page_ranges, /* page-ranges option, if any */ + *page_set; /* page-set option, if any */ +} pstops_doc_t; + + +/* + * Convenience macros... + */ + +#define is_first_page(p) (doc->number_up == 1 || \ + ((p) % doc->number_up) == 1) +#define is_last_page(p) (doc->number_up == 1 || \ + ((p) % doc->number_up) == 0) +#define is_not_last_page(p) (doc->number_up > 1 && \ + ((p) % doc->number_up) != 0) + + +/* + * Local globals... + */ + +static int JobCanceled = 0;/* Set to 1 on SIGTERM */ + + +/* + * Local functions... + */ + +static pstops_page_t *add_page(pstops_doc_t *doc, const char *label); +static void cancel_job(int sig); +static int check_range(pstops_doc_t *doc, int page); +static void copy_bytes(cups_file_t *fp, off_t offset, + size_t length); +static ssize_t copy_comments(cups_file_t *fp, pstops_doc_t *doc, + ppd_file_t *ppd, char *line, + ssize_t linelen, size_t linesize); +static void copy_dsc(cups_file_t *fp, pstops_doc_t *doc, + ppd_file_t *ppd, char *line, ssize_t linelen, + size_t linesize); +static void copy_non_dsc(cups_file_t *fp, pstops_doc_t *doc, + ppd_file_t *ppd, char *line, + ssize_t linelen, size_t linesize); +static ssize_t copy_page(cups_file_t *fp, pstops_doc_t *doc, + ppd_file_t *ppd, int number, char *line, + ssize_t linelen, size_t linesize); +static ssize_t copy_prolog(cups_file_t *fp, pstops_doc_t *doc, + ppd_file_t *ppd, char *line, + ssize_t linelen, size_t linesize); +static ssize_t copy_setup(cups_file_t *fp, pstops_doc_t *doc, + ppd_file_t *ppd, char *line, + ssize_t linelen, size_t linesize); +static ssize_t copy_trailer(cups_file_t *fp, pstops_doc_t *doc, + ppd_file_t *ppd, int number, char *line, + ssize_t linelen, size_t linesize); +static void do_prolog(pstops_doc_t *doc, ppd_file_t *ppd); +static void do_setup(pstops_doc_t *doc, ppd_file_t *ppd); +static void doc_printf(pstops_doc_t *doc, const char *format, ...) + __attribute__ ((__format__ (__printf__, 2, 3))); +static void doc_puts(pstops_doc_t *doc, const char *s); +static void doc_write(pstops_doc_t *doc, const char *s, size_t len); +static void end_nup(pstops_doc_t *doc, int number); +static int include_feature(ppd_file_t *ppd, const char *line, + int num_options, + cups_option_t **options); +static char *parse_text(const char *start, char **end, char *buffer, + size_t bufsize); +static void set_pstops_options(pstops_doc_t *doc, ppd_file_t *ppd, + char *argv[], int num_options, + cups_option_t *options); +static ssize_t skip_page(cups_file_t *fp, char *line, ssize_t linelen, + size_t linesize); +static void start_nup(pstops_doc_t *doc, int number, + int show_border, const int *bounding_box); +static void write_label_prolog(pstops_doc_t *doc, const char *label, + float bottom, float top, + float width); +static void write_labels(pstops_doc_t *doc, int orient); +static void write_options(pstops_doc_t *doc, ppd_file_t *ppd, + int num_options, cups_option_t *options); + + +/* + * 'main()' - Main entry. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line args */ + char *argv[]) /* I - Command-line arguments */ +{ + pstops_doc_t doc; /* Document information */ + cups_file_t *fp; /* Print file */ + ppd_file_t *ppd; /* PPD file */ + int num_options; /* Number of print options */ + cups_option_t *options; /* Print options */ + char line[8192]; /* Line buffer */ + size_t len; /* Length of line buffer */ +#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) + struct sigaction action; /* Actions for POSIX signals */ +#endif /* HAVE_SIGACTION && !HAVE_SIGSET */ + + + /* + * Make sure status messages are not buffered... + */ + + setbuf(stderr, NULL); + + /* + * Ignore broken pipe signals... + */ + + signal(SIGPIPE, SIG_IGN); + + /* + * Check command-line... + */ + + if (argc < 6 || argc > 7) + { + _cupsLangPrintf(stderr, + _("Usage: %s job-id user title copies options [file]"), + argv[0]); + return (1); + } + + /* + * Register a signal handler to cleanly cancel a job. + */ + +#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ + sigset(SIGTERM, cancel_job); +#elif defined(HAVE_SIGACTION) + memset(&action, 0, sizeof(action)); + + sigemptyset(&action.sa_mask); + action.sa_handler = cancel_job; + sigaction(SIGTERM, &action, NULL); +#else + signal(SIGTERM, cancel_job); +#endif /* HAVE_SIGSET */ + + /* + * If we have 7 arguments, print the file named on the command-line. + * Otherwise, send stdin instead... + */ + + if (argc == 6) + fp = cupsFileStdin(); + else + { + /* + * Try to open the print file... + */ + + if ((fp = cupsFileOpen(argv[6], "r")) == NULL) + { + _cupsLangPrintError("ERROR", _("Unable to open print file")); + return (1); + } + } + + /* + * Read the first line to see if we have DSC comments... + */ + + if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0) + { + fputs("DEBUG: The print file is empty.\n", stderr); + return (1); + } + + /* + * Process command-line options... + */ + + options = NULL; + num_options = cupsParseOptions(argv[5], 0, &options); + ppd = SetCommonOptions(num_options, options, 1); + + set_pstops_options(&doc, ppd, argv, num_options, options); + + /* + * Write any "exit server" options that have been selected... + */ + + ppdEmit(ppd, stdout, PPD_ORDER_EXIT); + + /* + * Write any JCL commands that are needed to print PostScript code... + */ + + if (doc.emit_jcl) + ppdEmitJCL(ppd, stdout, doc.job_id, doc.user, doc.title); + + /* + * Start with a DSC header... + */ + + puts("%!PS-Adobe-3.0"); + + /* + * Skip leading PJL in the document... + */ + + while (!strncmp(line, "\033%-12345X", 9) || !strncmp(line, "@PJL ", 5)) + { + /* + * Yup, we have leading PJL fun, so skip it until we hit the line + * with "ENTER LANGUAGE"... + */ + + fputs("DEBUG: Skipping PJL header...\n", stderr); + + while (strstr(line, "ENTER LANGUAGE") == NULL && strncmp(line, "%!", 2)) + if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0) + break; + + if (!strncmp(line, "%!", 2)) + break; + + if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0) + break; + } + + /* + * Now see if the document conforms to the Adobe Document Structuring + * Conventions... + */ + + if (!strncmp(line, "%!PS-Adobe-", 11)) + { + /* + * Yes, filter the document... + */ + + copy_dsc(fp, &doc, ppd, line, len, sizeof(line)); + } + else + { + /* + * No, display an error message and treat the file as if it contains + * a single page... + */ + + copy_non_dsc(fp, &doc, ppd, line, len, sizeof(line)); + } + + /* + * Send %%EOF as needed... + */ + + if (!doc.saw_eof) + puts("%%EOF"); + + /* + * End the job with the appropriate JCL command or CTRL-D... + */ + + if (doc.emit_jcl) + { + if (ppd && ppd->jcl_end) + ppdEmitJCLEnd(ppd, stdout); + else + putchar(0x04); + } + + /* + * Close files and remove the temporary file if needed... + */ + + if (doc.temp) + { + cupsFileClose(doc.temp); + unlink(doc.tempfile); + } + + ppdClose(ppd); + cupsFreeOptions(num_options, options); + + cupsFileClose(fp); + + return (0); +} + + +/* + * 'add_page()' - Add a page to the pages array. + */ + +static pstops_page_t * /* O - New page info object */ +add_page(pstops_doc_t *doc, /* I - Document information */ + const char *label) /* I - Page label */ +{ + pstops_page_t *pageinfo; /* New page info object */ + + + if (!doc->pages) + doc->pages = cupsArrayNew(NULL, NULL); + + if (!doc->pages) + { + _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array")); + exit(1); + } + + if ((pageinfo = calloc(1, sizeof(pstops_page_t))) == NULL) + { + _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info")); + exit(1); + } + + pageinfo->label = strdup(label); + pageinfo->offset = cupsFileTell(doc->temp); + + cupsArrayAdd(doc->pages, pageinfo); + + doc->page ++; + + return (pageinfo); +} + + +/* + * 'cancel_job()' - Flag the job as canceled. + */ + +static void +cancel_job(int sig) /* I - Signal number (unused) */ +{ + (void)sig; + + JobCanceled = 1; +} + + +/* + * 'check_range()' - Check to see if the current page is selected for + * printing. + */ + +static int /* O - 1 if selected, 0 otherwise */ +check_range(pstops_doc_t *doc, /* I - Document information */ + int page) /* I - Page number */ +{ + const char *range; /* Pointer into range string */ + int lower, upper; /* Lower and upper page numbers */ + + + if (doc->page_set) + { + /* + * See if we only print even or odd pages... + */ + + if (!_cups_strcasecmp(doc->page_set, "even") && (page & 1)) + return (0); + + if (!_cups_strcasecmp(doc->page_set, "odd") && !(page & 1)) + return (0); + } + + if (!doc->page_ranges) + return (1); /* No range, print all pages... */ + + for (range = doc->page_ranges; *range != '\0';) + { + if (*range == '-') + { + lower = 1; + range ++; + upper = strtol(range, (char **)&range, 10); + } + else + { + lower = strtol(range, (char **)&range, 10); + + if (*range == '-') + { + range ++; + if (!isdigit(*range & 255)) + upper = 65535; + else + upper = strtol(range, (char **)&range, 10); + } + else + upper = lower; + } + + if (page >= lower && page <= upper) + return (1); + + if (*range == ',') + range ++; + else + break; + } + + return (0); +} + + +/* + * 'copy_bytes()' - Copy bytes from the input file to stdout. + */ + +static void +copy_bytes(cups_file_t *fp, /* I - File to read from */ + off_t offset, /* I - Offset to page data */ + size_t length) /* I - Length of page data */ +{ + char buffer[8192]; /* Data buffer */ + ssize_t nbytes; /* Number of bytes read */ + size_t nleft; /* Number of bytes left/remaining */ + + + nleft = length; + + if (cupsFileSeek(fp, offset) < 0) + { + _cupsLangPrintError("ERROR", _("Unable to see in file")); + return; + } + + while (nleft > 0 || length == 0) + { + if (nleft > sizeof(buffer) || length == 0) + nbytes = sizeof(buffer); + else + nbytes = nleft; + + if ((nbytes = cupsFileRead(fp, buffer, nbytes)) < 1) + return; + + nleft -= nbytes; + + fwrite(buffer, 1, nbytes, stdout); + } +} + + +/* + * 'copy_comments()' - Copy all of the comments section. + * + * This function expects "line" to be filled with a comment line. + * On return, "line" will contain the next line in the file, if any. + */ + +static ssize_t /* O - Length of next line */ +copy_comments(cups_file_t *fp, /* I - File to read from */ + pstops_doc_t *doc, /* I - Document info */ + ppd_file_t *ppd, /* I - PPD file */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + int saw_bounding_box, /* Saw %%BoundingBox: comment? */ + saw_for, /* Saw %%For: comment? */ + saw_pages, /* Saw %%Pages: comment? */ + saw_title; /* Saw %%Title: comment? */ + + + /* + * Loop until we see %%EndComments or a non-comment line... + */ + + saw_bounding_box = 0; + saw_for = 0; + saw_pages = 0; + saw_title = 0; + + while (line[0] == '%') + { + /* + * Strip trailing whitespace... + */ + + while (linelen > 0) + { + linelen --; + + if (!isspace(line[linelen] & 255)) + break; + else + line[linelen] = '\0'; + } + + /* + * Log the header... + */ + + fprintf(stderr, "DEBUG: %s\n", line); + + /* + * Pull the headers out... + */ + + if (!strncmp(line, "%%Pages:", 8)) + { + int pages; /* Number of pages */ + + if (saw_pages) + fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr); + + saw_pages = 1; + + if (Duplex && (pages = atoi(line + 8)) > 0 && pages <= doc->number_up) + { + /* + * Since we will only be printing on a single page, disable duplexing. + */ + + Duplex = 0; + doc->slow_duplex = 0; + + if (cupsGetOption("sides", doc->num_options, doc->options)) + doc->num_options = cupsAddOption("sides", "one-sided", + doc->num_options, &(doc->options)); + + if (cupsGetOption("Duplex", doc->num_options, doc->options)) + doc->num_options = cupsAddOption("Duplex", "None", + doc->num_options, &(doc->options)); + + if (cupsGetOption("EFDuplex", doc->num_options, doc->options)) + doc->num_options = cupsAddOption("EFDuplex", "None", + doc->num_options, &(doc->options)); + + if (cupsGetOption("EFDuplexing", doc->num_options, doc->options)) + doc->num_options = cupsAddOption("EFDuplexing", "False", + doc->num_options, &(doc->options)); + + if (cupsGetOption("KD03Duplex", doc->num_options, doc->options)) + doc->num_options = cupsAddOption("KD03Duplex", "None", + doc->num_options, &(doc->options)); + + if (cupsGetOption("JCLDuplex", doc->num_options, doc->options)) + doc->num_options = cupsAddOption("JCLDuplex", "None", + doc->num_options, &(doc->options)); + + ppdMarkOption(ppd, "Duplex", "None"); + ppdMarkOption(ppd, "EFDuplex", "None"); + ppdMarkOption(ppd, "EFDuplexing", "False"); + ppdMarkOption(ppd, "KD03Duplex", "None"); + ppdMarkOption(ppd, "JCLDuplex", "None"); + } + } + else if (!strncmp(line, "%%BoundingBox:", 14)) + { + if (saw_bounding_box) + fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr); + else if (strstr(line + 14, "(atend)")) + { + /* + * Do nothing for now but use the default imageable area... + */ + } + else if (sscanf(line + 14, "%d%d%d%d", doc->bounding_box + 0, + doc->bounding_box + 1, doc->bounding_box + 2, + doc->bounding_box + 3) != 4) + { + fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr); + + doc->bounding_box[0] = (int)PageLeft; + doc->bounding_box[1] = (int)PageBottom; + doc->bounding_box[2] = (int)PageRight; + doc->bounding_box[3] = (int)PageTop; + } + + saw_bounding_box = 1; + } + else if (!strncmp(line, "%%For:", 6)) + { + saw_for = 1; + doc_printf(doc, "%s\n", line); + } + else if (!strncmp(line, "%%Title:", 8)) + { + saw_title = 1; + doc_printf(doc, "%s\n", line); + } + else if (!strncmp(line, "%cupsRotation:", 14)) + { + /* + * Reset orientation of document? + */ + + int orient = (atoi(line + 14) / 90) & 3; + + if (orient != Orientation) + { + /* + * Yes, update things so that the pages come out right... + */ + + Orientation = (4 - Orientation + orient) & 3; + UpdatePageVars(); + Orientation = orient; + } + } + else if (!strcmp(line, "%%EndComments")) + { + linelen = cupsFileGetLine(fp, line, linesize); + break; + } + else if (strncmp(line, "%!", 2) && strncmp(line, "%cups", 5)) + doc_printf(doc, "%s\n", line); + + if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) + break; + } + + if (!saw_bounding_box) + fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n", + stderr); + + if (!saw_pages) + fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr); + + if (!saw_for) + WriteTextComment("For", doc->user); + + if (!saw_title) + WriteTextComment("Title", doc->title); + + if (doc->copies != 1 && (!doc->collate || !doc->slow_collate)) + { + /* + * Tell the document processor the copy and duplex options + * that are required... + */ + + doc_printf(doc, "%%%%Requirements: numcopies(%d)%s%s\n", doc->copies, + doc->collate ? " collate" : "", + Duplex ? " duplex" : ""); + + /* + * Apple uses RBI comments for various non-PPD options... + */ + + doc_printf(doc, "%%RBINumCopies: %d\n", doc->copies); + } + else + { + /* + * Tell the document processor the duplex option that is required... + */ + + if (Duplex) + doc_puts(doc, "%%Requirements: duplex\n"); + + /* + * Apple uses RBI comments for various non-PPD options... + */ + + doc_puts(doc, "%RBINumCopies: 1\n"); + } + + doc_puts(doc, "%%Pages: (atend)\n"); + doc_puts(doc, "%%BoundingBox: (atend)\n"); + doc_puts(doc, "%%EndComments\n"); + + return (linelen); +} + + +/* + * 'copy_dsc()' - Copy a DSC-conforming document. + * + * This function expects "line" to be filled with the %!PS-Adobe comment line. + */ + +static void +copy_dsc(cups_file_t *fp, /* I - File to read from */ + pstops_doc_t *doc, /* I - Document info */ + ppd_file_t *ppd, /* I - PPD file */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + int number; /* Page number */ + pstops_page_t *pageinfo; /* Page information */ + + + /* + * Make sure we use ESPshowpage for EPS files... + */ + + if (strstr(line, "EPSF")) + { + doc->use_ESPshowpage = 1; + doc->number_up = 1; + } + + /* + * Start sending the document with any commands needed... + */ + + fprintf(stderr, "DEBUG: Before copy_comments - %s", line); + linelen = copy_comments(fp, doc, ppd, line, linelen, linesize); + + /* + * Now find the prolog section, if any... + */ + + fprintf(stderr, "DEBUG: Before copy_prolog - %s", line); + linelen = copy_prolog(fp, doc, ppd, line, linelen, linesize); + + /* + * Then the document setup section... + */ + + fprintf(stderr, "DEBUG: Before copy_setup - %s", line); + linelen = copy_setup(fp, doc, ppd, line, linelen, linesize); + + /* + * Copy until we see %%Page:... + */ + + while (strncmp(line, "%%Page:", 7) && strncmp(line, "%%Trailer", 9)) + { + doc_write(doc, line, linelen); + + if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) + break; + } + + /* + * Then process pages until we have no more... + */ + + number = 0; + + fprintf(stderr, "DEBUG: Before page loop - %s", line); + while (!strncmp(line, "%%Page:", 7)) + { + if (JobCanceled) + break; + + number ++; + + if (check_range(doc, (number - 1) / doc->number_up + 1)) + { + fprintf(stderr, "DEBUG: Copying page %d...\n", number); + linelen = copy_page(fp, doc, ppd, number, line, linelen, linesize); + } + else + { + fprintf(stderr, "DEBUG: Skipping page %d...\n", number); + linelen = skip_page(fp, line, linelen, linesize); + } + } + + /* + * Finish up the last page(s)... + */ + + if (number && is_not_last_page(number) && cupsArrayLast(doc->pages) && + check_range(doc, (number - 1) / doc->number_up + 1)) + { + pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages); + + start_nup(doc, doc->number_up, 0, doc->bounding_box); + doc_puts(doc, "showpage\n"); + end_nup(doc, doc->number_up); + + pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset; + } + + if (doc->slow_duplex && (doc->page & 1)) + { + /* + * Make sure we have an even number of pages... + */ + + pageinfo = add_page(doc, "(filler)"); + + if (!doc->slow_order) + { + if (!ppd || !ppd->num_filters) + fprintf(stderr, "PAGE: %d %d\n", doc->page, + doc->slow_collate ? 1 : doc->copies); + + printf("%%%%Page: (filler) %d\n", doc->page); + } + + start_nup(doc, doc->number_up, 0, doc->bounding_box); + doc_puts(doc, "showpage\n"); + end_nup(doc, doc->number_up); + + pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset; + } + + /* + * Make additional copies as necessary... + */ + + number = doc->slow_order ? 0 : doc->page; + + if (doc->temp && !JobCanceled && cupsArrayCount(doc->pages) > 0) + { + int copy; /* Current copy */ + + + /* + * Reopen the temporary file for reading... + */ + + cupsFileClose(doc->temp); + + doc->temp = cupsFileOpen(doc->tempfile, "r"); + + /* + * Make the copies... + */ + + if (doc->slow_collate) + copy = !doc->slow_order; + else + copy = doc->copies - 1; + + for (; copy < doc->copies; copy ++) + { + if (JobCanceled) + break; + + /* + * Send end-of-job stuff followed by any start-of-job stuff required + * for the JCL options... + */ + + if (number && doc->emit_jcl && ppd && ppd->jcl_end) + { + /* + * Send the trailer... + */ + + puts("%%Trailer"); + printf("%%%%Pages: %d\n", cupsArrayCount(doc->pages)); + if (doc->number_up > 1 || doc->fit_to_page) + printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", + PageLeft, PageBottom, PageRight, PageTop); + else + printf("%%%%BoundingBox: %d %d %d %d\n", + doc->new_bounding_box[0], doc->new_bounding_box[1], + doc->new_bounding_box[2], doc->new_bounding_box[3]); + puts("%%EOF"); + + /* + * Start a new document... + */ + + ppdEmitJCLEnd(ppd, stdout); + ppdEmitJCL(ppd, stdout, doc->job_id, doc->user, doc->title); + + puts("%!PS-Adobe-3.0"); + + number = 0; + } + + /* + * Copy the prolog as needed... + */ + + if (!number) + { + pageinfo = (pstops_page_t *)cupsArrayFirst(doc->pages); + copy_bytes(doc->temp, 0, pageinfo->offset); + } + + /* + * Then copy all of the pages... + */ + + pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayLast(doc->pages) : + (pstops_page_t *)cupsArrayFirst(doc->pages); + + while (pageinfo) + { + if (JobCanceled) + break; + + number ++; + + if (!ppd || !ppd->num_filters) + fprintf(stderr, "PAGE: %d %d\n", number, + doc->slow_collate ? 1 : doc->copies); + + if (doc->number_up > 1) + { + printf("%%%%Page: (%d) %d\n", number, number); + printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n", + PageLeft, PageBottom, PageRight, PageTop); + } + else + { + printf("%%%%Page: %s %d\n", pageinfo->label, number); + printf("%%%%PageBoundingBox: %d %d %d %d\n", + pageinfo->bounding_box[0], pageinfo->bounding_box[1], + pageinfo->bounding_box[2], pageinfo->bounding_box[3]); + } + + copy_bytes(doc->temp, pageinfo->offset, pageinfo->length); + + pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayPrev(doc->pages) : + (pstops_page_t *)cupsArrayNext(doc->pages); + } + } + } + + /* + * Restore the old showpage operator as needed... + */ + + if (doc->use_ESPshowpage) + puts("userdict/showpage/ESPshowpage load put\n"); + + /* + * Write/copy the trailer... + */ + + if (!JobCanceled) + copy_trailer(fp, doc, ppd, number, line, linelen, linesize); +} + + +/* + * 'copy_non_dsc()' - Copy a document that does not conform to the DSC. + * + * This function expects "line" to be filled with the %! comment line. + */ + +static void +copy_non_dsc(cups_file_t *fp, /* I - File to read from */ + pstops_doc_t *doc, /* I - Document info */ + ppd_file_t *ppd, /* I - PPD file */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + int copy; /* Current copy */ + char buffer[8192]; /* Copy buffer */ + int bytes; /* Number of bytes copied */ + + + /* + * First let the user know that they are attempting to print a file + * that may not print correctly... + */ + + fputs("DEBUG: This document does not conform to the Adobe Document " + "Structuring Conventions and may not print correctly.\n", stderr); + + /* + * Then write a standard DSC comment section... + */ + + printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom, + PageRight, PageTop); + + if (doc->slow_collate && doc->copies > 1) + printf("%%%%Pages: %d\n", doc->copies); + else + puts("%%Pages: 1"); + + WriteTextComment("For", doc->user); + WriteTextComment("Title", doc->title); + + if (doc->copies != 1 && (!doc->collate || !doc->slow_collate)) + { + /* + * Tell the document processor the copy and duplex options + * that are required... + */ + + printf("%%%%Requirements: numcopies(%d)%s%s\n", doc->copies, + doc->collate ? " collate" : "", + Duplex ? " duplex" : ""); + + /* + * Apple uses RBI comments for various non-PPD options... + */ + + printf("%%RBINumCopies: %d\n", doc->copies); + } + else + { + /* + * Tell the document processor the duplex option that is required... + */ + + if (Duplex) + puts("%%Requirements: duplex"); + + /* + * Apple uses RBI comments for various non-PPD options... + */ + + puts("%RBINumCopies: 1"); + } + + puts("%%EndComments"); + + /* + * Then the prolog... + */ + + puts("%%BeginProlog"); + + do_prolog(doc, ppd); + + puts("%%EndProlog"); + + /* + * Then the setup section... + */ + + puts("%%BeginSetup"); + + do_setup(doc, ppd); + + puts("%%EndSetup"); + + /* + * Finally, embed a copy of the file inside a %%Page... + */ + + if (!ppd || !ppd->num_filters) + fprintf(stderr, "PAGE: 1 %d\n", doc->temp ? 1 : doc->copies); + + puts("%%Page: 1 1"); + puts("%%BeginPageSetup"); + ppdEmit(ppd, stdout, PPD_ORDER_PAGE); + puts("%%EndPageSetup"); + puts("%%BeginDocument: nondsc"); + + fwrite(line, linelen, 1, stdout); + + if (doc->temp) + cupsFileWrite(doc->temp, line, linelen); + + while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) + { + fwrite(buffer, 1, bytes, stdout); + + if (doc->temp) + cupsFileWrite(doc->temp, buffer, bytes); + } + + puts("%%EndDocument"); + + if (doc->use_ESPshowpage) + { + WriteLabels(Orientation); + puts("ESPshowpage"); + } + + if (doc->temp && !JobCanceled) + { + /* + * Reopen the temporary file for reading... + */ + + cupsFileClose(doc->temp); + + doc->temp = cupsFileOpen(doc->tempfile, "r"); + + /* + * Make the additional copies as needed... + */ + + for (copy = 1; copy < doc->copies; copy ++) + { + if (JobCanceled) + break; + + if (!ppd || !ppd->num_filters) + fputs("PAGE: 1 1\n", stderr); + + printf("%%%%Page: %d %d\n", copy + 1, copy + 1); + puts("%%BeginPageSetup"); + ppdEmit(ppd, stdout, PPD_ORDER_PAGE); + puts("%%EndPageSetup"); + puts("%%BeginDocument: nondsc"); + + copy_bytes(doc->temp, 0, 0); + + puts("%%EndDocument"); + + if (doc->use_ESPshowpage) + { + WriteLabels(Orientation); + puts("ESPshowpage"); + } + } + } + + /* + * Restore the old showpage operator as needed... + */ + + if (doc->use_ESPshowpage) + puts("userdict/showpage/ESPshowpage load put\n"); +} + + +/* + * 'copy_page()' - Copy a page description. + * + * This function expects "line" to be filled with a %%Page comment line. + * On return, "line" will contain the next line in the file, if any. + */ + +static ssize_t /* O - Length of next line */ +copy_page(cups_file_t *fp, /* I - File to read from */ + pstops_doc_t *doc, /* I - Document info */ + ppd_file_t *ppd, /* I - PPD file */ + int number, /* I - Current page number */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + char label[256], /* Page label string */ + *ptr; /* Pointer into line */ + int level; /* Embedded document level */ + pstops_page_t *pageinfo; /* Page information */ + int first_page; /* First page on N-up output? */ + int has_page_setup = 0; /* Does the page have %%Begin/EndPageSetup? */ + int bounding_box[4]; /* PageBoundingBox */ + + + /* + * Get the page label for this page... + */ + + first_page = is_first_page(number); + + if (!parse_text(line + 7, &ptr, label, sizeof(label))) + { + fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr); + label[0] = '\0'; + number = doc->page; + } + else if (strtol(ptr, &ptr, 10) == LONG_MAX || !isspace(*ptr & 255)) + { + fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr); + number = doc->page; + } + + /* + * Create or update the current output page... + */ + + if (first_page) + pageinfo = add_page(doc, label); + else + pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages); + + /* + * Handle first page override... + */ + + if (doc->ap_input_slot || doc->ap_manual_feed) + { + if (doc->page == 1) + { + /* + * First page/sheet gets AP_FIRSTPAGE_* options... + */ + + pageinfo->num_options = cupsAddOption("InputSlot", doc->ap_input_slot, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("ManualFeed", + doc->ap_input_slot ? "False" : + doc->ap_manual_feed, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("MediaColor", doc->ap_media_color, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("MediaType", doc->ap_media_type, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("PageRegion", doc->ap_page_region, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("PageSize", doc->ap_page_size, + pageinfo->num_options, + &(pageinfo->options)); + } + else if (doc->page == (Duplex + 2)) + { + /* + * Second page/sheet gets default options... + */ + + pageinfo->num_options = cupsAddOption("InputSlot", doc->input_slot, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("ManualFeed", + doc->input_slot ? "False" : + doc->manual_feed, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("MediaColor", doc->media_color, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("MediaType", doc->media_type, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("PageRegion", doc->page_region, + pageinfo->num_options, + &(pageinfo->options)); + pageinfo->num_options = cupsAddOption("PageSize", doc->page_size, + pageinfo->num_options, + &(pageinfo->options)); + } + } + + /* + * Scan comments until we see something other than %%Page*: or + * %%Include*... + */ + + memcpy(bounding_box, doc->bounding_box, sizeof(bounding_box)); + + while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) + { + if (!strncmp(line, "%%PageBoundingBox:", 18)) + { + /* + * %%PageBoundingBox: llx lly urx ury + */ + + if (sscanf(line + 18, "%d%d%d%d", bounding_box + 0, + bounding_box + 1, bounding_box + 2, + bounding_box + 3) != 4) + { + fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr); + memcpy(bounding_box, doc->bounding_box, + sizeof(bounding_box)); + } + else if (doc->number_up == 1 && !doc->fit_to_page && Orientation) + { + int temp_bbox[4]; /* Temporary bounding box */ + + + memcpy(temp_bbox, bounding_box, sizeof(temp_bbox)); + + fprintf(stderr, "DEBUG: Orientation = %d\n", Orientation); + fprintf(stderr, "DEBUG: original bounding_box = [ %d %d %d %d ]\n", + bounding_box[0], bounding_box[1], + bounding_box[2], bounding_box[3]); + fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n", + PageWidth, PageLength); + + switch (Orientation) + { + case 1 : /* Landscape */ + bounding_box[0] = PageLength - temp_bbox[3]; + bounding_box[1] = temp_bbox[0]; + bounding_box[2] = PageLength - temp_bbox[1]; + bounding_box[3] = temp_bbox[2]; + break; + + case 2 : /* Reverse Portrait */ + bounding_box[0] = PageWidth - temp_bbox[2]; + bounding_box[1] = PageLength - temp_bbox[3]; + bounding_box[2] = PageWidth - temp_bbox[0]; + bounding_box[3] = PageLength - temp_bbox[1]; + break; + + case 3 : /* Reverse Landscape */ + bounding_box[0] = temp_bbox[1]; + bounding_box[1] = PageWidth - temp_bbox[2]; + bounding_box[2] = temp_bbox[3]; + bounding_box[3] = PageWidth - temp_bbox[0]; + break; + } + + fprintf(stderr, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n", + bounding_box[0], bounding_box[1], + bounding_box[2], bounding_box[3]); + } + } +#if 0 + else if (!strncmp(line, "%%PageCustomColors:", 19) || + !strncmp(line, "%%PageMedia:", 12) || + !strncmp(line, "%%PageOrientation:", 18) || + !strncmp(line, "%%PageProcessColors:", 20) || + !strncmp(line, "%%PageRequirements:", 18) || + !strncmp(line, "%%PageResources:", 16)) + { + /* + * Copy literal... + */ + } +#endif /* 0 */ + else if (!strncmp(line, "%%PageCustomColors:", 19)) + { + /* + * %%PageCustomColors: ... + */ + } + else if (!strncmp(line, "%%PageMedia:", 12)) + { + /* + * %%PageMedia: ... + */ + } + else if (!strncmp(line, "%%PageOrientation:", 18)) + { + /* + * %%PageOrientation: ... + */ + } + else if (!strncmp(line, "%%PageProcessColors:", 20)) + { + /* + * %%PageProcessColors: ... + */ + } + else if (!strncmp(line, "%%PageRequirements:", 18)) + { + /* + * %%PageRequirements: ... + */ + } + else if (!strncmp(line, "%%PageResources:", 16)) + { + /* + * %%PageResources: ... + */ + } + else if (!strncmp(line, "%%IncludeFeature:", 17)) + { + /* + * %%IncludeFeature: *MainKeyword OptionKeyword + */ + + if (doc->number_up == 1 &&!doc->fit_to_page) + pageinfo->num_options = include_feature(ppd, line, + pageinfo->num_options, + &(pageinfo->options)); + } + else if (!strncmp(line, "%%BeginPageSetup", 16)) + { + has_page_setup = 1; + break; + } + else + break; + } + + if (doc->number_up == 1) + { + /* + * Update the document's composite and page bounding box... + */ + + memcpy(pageinfo->bounding_box, bounding_box, + sizeof(pageinfo->bounding_box)); + + if (bounding_box[0] < doc->new_bounding_box[0]) + doc->new_bounding_box[0] = bounding_box[0]; + if (bounding_box[1] < doc->new_bounding_box[1]) + doc->new_bounding_box[1] = bounding_box[1]; + if (bounding_box[2] > doc->new_bounding_box[2]) + doc->new_bounding_box[2] = bounding_box[2]; + if (bounding_box[3] > doc->new_bounding_box[3]) + doc->new_bounding_box[3] = bounding_box[3]; + } + + /* + * Output the page header as needed... + */ + + if (!doc->slow_order && first_page) + { + if (!ppd || !ppd->num_filters) + fprintf(stderr, "PAGE: %d %d\n", doc->page, + doc->slow_collate ? 1 : doc->copies); + + if (doc->number_up > 1) + { + printf("%%%%Page: (%d) %d\n", doc->page, doc->page); + printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n", + PageLeft, PageBottom, PageRight, PageTop); + } + else + { + printf("%%%%Page: %s %d\n", pageinfo->label, doc->page); + printf("%%%%PageBoundingBox: %d %d %d %d\n", + pageinfo->bounding_box[0], pageinfo->bounding_box[1], + pageinfo->bounding_box[2], pageinfo->bounding_box[3]); + } + } + + /* + * Copy any page setup commands... + */ + + if (first_page) + doc_puts(doc, "%%BeginPageSetup\n"); + + if (has_page_setup) + { + int feature = 0; /* In a Begin/EndFeature block? */ + + while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) + { + if (!strncmp(line, "%%EndPageSetup", 14)) + break; + else if (!strncmp(line, "%%BeginFeature:", 15)) + { + feature = 1; + + if (doc->number_up > 1 || doc->fit_to_page) + continue; + } + else if (!strncmp(line, "%%EndFeature", 12)) + { + feature = 0; + + if (doc->number_up > 1 || doc->fit_to_page) + continue; + } + else if (!strncmp(line, "%%IncludeFeature:", 17)) + { + pageinfo->num_options = include_feature(ppd, line, + pageinfo->num_options, + &(pageinfo->options)); + continue; + } + else if (!strncmp(line, "%%Include", 9)) + continue; + + if (line[0] != '%' && !feature) + break; + + if (!feature || (doc->number_up == 1 && !doc->fit_to_page)) + doc_write(doc, line, linelen); + } + + /* + * Skip %%EndPageSetup... + */ + + if (linelen > 0 && !strncmp(line, "%%EndPageSetup", 14)) + linelen = cupsFileGetLine(fp, line, linesize); + } + + if (first_page) + { + char *page_setup; /* PageSetup commands to send */ + + + if (pageinfo->num_options > 0) + write_options(doc, ppd, pageinfo->num_options, pageinfo->options); + + /* + * Output commands for the current page... + */ + + page_setup = ppdEmitString(ppd, PPD_ORDER_PAGE, 0); + + if (page_setup) + { + doc_puts(doc, page_setup); + free(page_setup); + } + } + + /* + * Prep for the start of the page description... + */ + + start_nup(doc, number, 1, bounding_box); + + if (first_page) + doc_puts(doc, "%%EndPageSetup\n"); + + /* + * Read the rest of the page description... + */ + + level = 0; + + do + { + if (level == 0 && + (!strncmp(line, "%%Page:", 7) || + !strncmp(line, "%%Trailer", 9) || + !strncmp(line, "%%EOF", 5))) + break; + else if (!strncmp(line, "%%BeginDocument", 15) || + !strncmp(line, "%ADO_BeginApplication", 21)) + { + doc_write(doc, line, linelen); + + level ++; + } + else if ((!strncmp(line, "%%EndDocument", 13) || + !strncmp(line, "%ADO_EndApplication", 19)) && level > 0) + { + doc_write(doc, line, linelen); + + level --; + } + else if (!strncmp(line, "%%BeginBinary:", 14) || + (!strncmp(line, "%%BeginData:", 12) && + !strstr(line, "ASCII") && !strstr(line, "Hex"))) + { + /* + * Copy binary data... + */ + + int bytes; /* Bytes of data */ + + + doc_write(doc, line, linelen); + + bytes = atoi(strchr(line, ':') + 1); + + while (bytes > 0) + { + if (bytes > linesize) + linelen = cupsFileRead(fp, line, linesize); + else + linelen = cupsFileRead(fp, line, bytes); + + if (linelen < 1) + { + line[0] = '\0'; + perror("ERROR: Early end-of-file while reading binary data"); + return (0); + } + + doc_write(doc, line, linelen); + + bytes -= linelen; + } + } + else + doc_write(doc, line, linelen); + } + while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0); + + /* + * Finish up this page and return... + */ + + end_nup(doc, number); + + pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset; + + return (linelen); +} + + +/* + * 'copy_prolog()' - Copy the document prolog section. + * + * This function expects "line" to be filled with a %%BeginProlog comment line. + * On return, "line" will contain the next line in the file, if any. + */ + +static ssize_t /* O - Length of next line */ +copy_prolog(cups_file_t *fp, /* I - File to read from */ + pstops_doc_t *doc, /* I - Document info */ + ppd_file_t *ppd, /* I - PPD file */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + while (strncmp(line, "%%BeginProlog", 13)) + { + if (!strncmp(line, "%%BeginSetup", 12) || !strncmp(line, "%%Page:", 7)) + break; + + doc_write(doc, line, linelen); + + if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) + break; + } + + doc_puts(doc, "%%BeginProlog\n"); + + do_prolog(doc, ppd); + + if (!strncmp(line, "%%BeginProlog", 13)) + { + while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) + { + if (!strncmp(line, "%%EndProlog", 11) || + !strncmp(line, "%%BeginSetup", 12) || + !strncmp(line, "%%Page:", 7)) + break; + + doc_write(doc, line, linelen); + } + + if (!strncmp(line, "%%EndProlog", 11)) + linelen = cupsFileGetLine(fp, line, linesize); + else + fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr); + } + + doc_puts(doc, "%%EndProlog\n"); + + return (linelen); +} + + +/* + * 'copy_setup()' - Copy the document setup section. + * + * This function expects "line" to be filled with a %%BeginSetup comment line. + * On return, "line" will contain the next line in the file, if any. + */ + +static ssize_t /* O - Length of next line */ +copy_setup(cups_file_t *fp, /* I - File to read from */ + pstops_doc_t *doc, /* I - Document info */ + ppd_file_t *ppd, /* I - PPD file */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + int num_options; /* Number of options */ + cups_option_t *options; /* Options */ + + + while (strncmp(line, "%%BeginSetup", 12)) + { + if (!strncmp(line, "%%Page:", 7)) + break; + + doc_write(doc, line, linelen); + + if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) + break; + } + + doc_puts(doc, "%%BeginSetup\n"); + + do_setup(doc, ppd); + + num_options = 0; + options = NULL; + + if (!strncmp(line, "%%BeginSetup", 12)) + { + while (strncmp(line, "%%EndSetup", 10)) + { + if (!strncmp(line, "%%Page:", 7)) + break; + else if (!strncmp(line, "%%IncludeFeature:", 17)) + { + /* + * %%IncludeFeature: *MainKeyword OptionKeyword + */ + + if (doc->number_up == 1 && !doc->fit_to_page) + num_options = include_feature(ppd, line, num_options, &options); + } + else if (strncmp(line, "%%BeginSetup", 12)) + doc_write(doc, line, linelen); + + if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) + break; + } + + if (!strncmp(line, "%%EndSetup", 10)) + linelen = cupsFileGetLine(fp, line, linesize); + else + fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr); + } + + if (num_options > 0) + { + write_options(doc, ppd, num_options, options); + cupsFreeOptions(num_options, options); + } + + doc_puts(doc, "%%EndSetup\n"); + + return (linelen); +} + + +/* + * 'copy_trailer()' - Copy the document trailer. + * + * This function expects "line" to be filled with a %%Trailer comment line. + * On return, "line" will contain the next line in the file, if any. + */ + +static ssize_t /* O - Length of next line */ +copy_trailer(cups_file_t *fp, /* I - File to read from */ + pstops_doc_t *doc, /* I - Document info */ + ppd_file_t *ppd, /* I - PPD file */ + int number, /* I - Number of pages */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + /* + * Write the trailer comments... + */ + + puts("%%Trailer"); + + while (linelen > 0) + { + if (!strncmp(line, "%%EOF", 5)) + break; + else if (strncmp(line, "%%Trailer", 9) && + strncmp(line, "%%Pages:", 8) && + strncmp(line, "%%BoundingBox:", 14)) + fwrite(line, 1, linelen, stdout); + + linelen = cupsFileGetLine(fp, line, linesize); + } + + fprintf(stderr, "DEBUG: Wrote %d pages...\n", number); + + printf("%%%%Pages: %d\n", number); + if (doc->number_up > 1 || doc->fit_to_page) + printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", + PageLeft, PageBottom, PageRight, PageTop); + else + printf("%%%%BoundingBox: %d %d %d %d\n", + doc->new_bounding_box[0], doc->new_bounding_box[1], + doc->new_bounding_box[2], doc->new_bounding_box[3]); + + return (linelen); +} + + +/* + * 'do_prolog()' - Send the necessary document prolog commands. + */ + +static void +do_prolog(pstops_doc_t *doc, /* I - Document information */ + ppd_file_t *ppd) /* I - PPD file */ +{ + char *ps; /* PS commands */ + + + /* + * Send the document prolog commands... + */ + + if (ppd && ppd->patches) + { + doc_puts(doc, "%%BeginFeature: *JobPatchFile 1\n"); + doc_puts(doc, ppd->patches); + doc_puts(doc, "\n%%EndFeature\n"); + } + + if ((ps = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL) + { + doc_puts(doc, ps); + free(ps); + } + + /* + * Define ESPshowpage here so that applications that define their + * own procedure to do a showpage pick it up... + */ + + if (doc->use_ESPshowpage) + doc_puts(doc, "userdict/ESPshowpage/showpage load put\n" + "userdict/showpage{}put\n"); +} + + +/* + * 'do_setup()' - Send the necessary document setup commands. + */ + +static void +do_setup(pstops_doc_t *doc, /* I - Document information */ + ppd_file_t *ppd) /* I - PPD file */ +{ + char *ps; /* PS commands */ + + + /* + * Disable CTRL-D so that embedded files don't cause printing + * errors... + */ + + doc_puts(doc, "% Disable CTRL-D as an end-of-file marker...\n"); + doc_puts(doc, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n"); + + /* + * Mark job options... + */ + + cupsMarkOptions(ppd, doc->num_options, doc->options); + + /* + * Send all the printer-specific setup commands... + */ + + if ((ps = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL) + { + doc_puts(doc, ps); + free(ps); + } + + if ((ps = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL) + { + doc_puts(doc, ps); + free(ps); + } + + /* + * Set the number of copies for the job... + */ + + if (doc->copies != 1 && (!doc->collate || !doc->slow_collate)) + { + doc_printf(doc, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc->copies); + doc_printf(doc, + "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n" + "{1 dict begin/NumCopies exch def currentdict end " + "setpagedevice}\n" + "{userdict/#copies 3 -1 roll put}ifelse\n", doc->copies); + doc_puts(doc, "%RBIEndNonPPDFeature\n"); + } + + /* + * If we are doing N-up printing, disable setpagedevice... + */ + + if (doc->number_up > 1) + { + doc_puts(doc, "userdict/CUPSsetpagedevice/setpagedevice load put\n"); + doc_puts(doc, "userdict/setpagedevice{pop}bind put\n"); + } + + /* + * Make sure we have rectclip and rectstroke procedures of some sort... + */ + + doc_puts(doc, + "% x y w h ESPrc - Clip to a rectangle.\n" + "userdict/ESPrc/rectclip where{pop/rectclip load}\n" + "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" + "neg 0 rlineto closepath clip newpath}bind}ifelse put\n"); + + doc_puts(doc, + "% x y w h ESPrf - Fill a rectangle.\n" + "userdict/ESPrf/rectfill where{pop/rectfill load}\n" + "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" + "neg 0 rlineto closepath fill grestore}bind}ifelse put\n"); + + doc_puts(doc, + "% x y w h ESPrs - Stroke a rectangle.\n" + "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n" + "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" + "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n"); + + /* + * Write the page and label prologs... + */ + + if (doc->number_up == 2 || doc->number_up == 6) + { + /* + * For 2- and 6-up output, rotate the labels to match the orientation + * of the pages... + */ + + if (Orientation & 1) + write_label_prolog(doc, doc->page_label, PageBottom, + PageWidth - PageLength + PageTop, PageLength); + else + write_label_prolog(doc, doc->page_label, PageLeft, PageRight, + PageLength); + } + else + write_label_prolog(doc, doc->page_label, PageBottom, PageTop, PageWidth); +} + + +/* + * 'doc_printf()' - Send a formatted string to stdout and/or the temp file. + * + * This function should be used for all page-level output that is affected + * by ordering, collation, etc. + */ + +static void +doc_printf(pstops_doc_t *doc, /* I - Document information */ + const char *format, /* I - Printf-style format string */ + ...) /* I - Additional arguments as needed */ +{ + va_list ap; /* Pointer to arguments */ + char buffer[1024]; /* Output buffer */ + size_t bytes; /* Number of bytes to write */ + + + va_start(ap, format); + bytes = vsnprintf(buffer, sizeof(buffer), format, ap); + va_end(ap); + + if (bytes > sizeof(buffer)) + { + _cupsLangPrintFilter(stderr, "ERROR", + _("Buffer overflow detected, aborting.")); + exit(1); + } + + doc_write(doc, buffer, bytes); +} + + +/* + * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file. + * + * This function should be used for all page-level output that is affected + * by ordering, collation, etc. + */ + +static void +doc_puts(pstops_doc_t *doc, /* I - Document information */ + const char *s) /* I - String to send */ +{ + doc_write(doc, s, strlen(s)); +} + + +/* + * 'doc_write()' - Send data to stdout and/or the temp file. + */ + +static void +doc_write(pstops_doc_t *doc, /* I - Document information */ + const char *s, /* I - Data to send */ + size_t len) /* I - Number of bytes to send */ +{ + if (!doc->slow_order) + fwrite(s, 1, len, stdout); + + if (doc->temp) + cupsFileWrite(doc->temp, s, len); +} + + +/* + * 'end_nup()' - End processing for N-up printing. + */ + +static void +end_nup(pstops_doc_t *doc, /* I - Document information */ + int number) /* I - Page number */ +{ + if (doc->number_up > 1) + doc_puts(doc, "userdict/ESPsave get restore\n"); + + switch (doc->number_up) + { + case 1 : + if (doc->use_ESPshowpage) + { + write_labels(doc, Orientation); + doc_puts(doc, "ESPshowpage\n"); + } + break; + + case 2 : + case 6 : + if (is_last_page(number) && doc->use_ESPshowpage) + { + if (Orientation & 1) + { + /* + * Rotate the labels back to portrait... + */ + + write_labels(doc, Orientation - 1); + } + else if (Orientation == 0) + { + /* + * Rotate the labels to landscape... + */ + + write_labels(doc, doc->normal_landscape ? 1 : 3); + } + else + { + /* + * Rotate the labels to landscape... + */ + + write_labels(doc, doc->normal_landscape ? 3 : 1); + } + + doc_puts(doc, "ESPshowpage\n"); + } + break; + + default : + if (is_last_page(number) && doc->use_ESPshowpage) + { + write_labels(doc, Orientation); + doc_puts(doc, "ESPshowpage\n"); + } + break; + } + + fflush(stdout); +} + + +/* + * 'include_feature()' - Include a printer option/feature command. + */ + +static int /* O - New number of options */ +include_feature( + ppd_file_t *ppd, /* I - PPD file */ + const char *line, /* I - DSC line */ + int num_options, /* I - Number of options */ + cups_option_t **options) /* IO - Options */ +{ + char name[255], /* Option name */ + value[255]; /* Option value */ + ppd_option_t *option; /* Option in file */ + + + /* + * Get the "%%IncludeFeature: *Keyword OptionKeyword" values... + */ + + if (sscanf(line + 17, "%254s%254s", name, value) != 2) + { + fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr); + return (num_options); + } + + /* + * Find the option and choice... + */ + + if ((option = ppdFindOption(ppd, name + 1)) == NULL) + { + _cupsLangPrintFilter(stderr, "WARNING", _("Unknown option \"%s\"."), + name + 1); + return (num_options); + } + + if (option->section == PPD_ORDER_EXIT || + option->section == PPD_ORDER_JCL) + { + _cupsLangPrintFilter(stderr, "WARNING", + _("Option \"%s\" cannot be included via " + "%%%%IncludeFeature."), name + 1); + return (num_options); + } + + if (!ppdFindChoice(option, value)) + { + _cupsLangPrintFilter(stderr, "WARNING", + _("Unknown choice \"%s\" for option \"%s\"."), + value, name + 1); + return (num_options); + } + + /* + * Add the option to the option array and return... + */ + + return (cupsAddOption(name + 1, value, num_options, options)); +} + + +/* + * 'parse_text()' - Parse a text value in a comment. + * + * This function parses a DSC text value as defined on page 36 of the + * DSC specification. Text values are either surrounded by parenthesis + * or whitespace-delimited. + * + * The value returned is the literal characters for the entire text + * string, including any parenthesis and escape characters. + */ + +static char * /* O - Value or NULL on error */ +parse_text(const char *start, /* I - Start of text value */ + char **end, /* O - End of text value */ + char *buffer, /* I - Buffer */ + size_t bufsize) /* I - Size of buffer */ +{ + char *bufptr, /* Pointer in buffer */ + *bufend; /* End of buffer */ + int level; /* Parenthesis level */ + + + /* + * Skip leading whitespace... + */ + + while (isspace(*start & 255)) + start ++; + + /* + * Then copy the value... + */ + + level = 0; + bufptr = buffer; + bufend = buffer + bufsize - 1; + + while (bufptr < bufend) + { + if (isspace(*start & 255) && !level) + break; + + *bufptr++ = *start; + + if (*start == '(') + level ++; + else if (*start == ')') + { + if (!level) + { + start ++; + break; + } + else + level --; + } + else if (*start == '\\') + { + /* + * Copy escaped character... + */ + + int i; /* Looping var */ + + + for (i = 1; + i <= 3 && isdigit(start[i] & 255) && bufptr < bufend; + *bufptr++ = start[i], i ++); + } + + start ++; + } + + *bufptr = '\0'; + + /* + * Return the value and new pointer into the line... + */ + + if (end) + *end = (char *)start; + + if (bufptr == bufend) + return (NULL); + else + return (buffer); +} + + +/* + * 'set_pstops_options()' - Set pstops options. + */ + +static void +set_pstops_options( + pstops_doc_t *doc, /* I - Document information */ + ppd_file_t *ppd, /* I - PPD file */ + char *argv[], /* I - Command-line arguments */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + const char *val; /* Option value */ + int intval; /* Integer option value */ + ppd_attr_t *attr; /* PPD attribute */ + ppd_option_t *option; /* PPD option */ + ppd_choice_t *choice; /* PPD choice */ + const char *content_type; /* Original content type */ + int max_copies; /* Maximum number of copies supported */ + + + /* + * Initialize document information structure... + */ + + memset(doc, 0, sizeof(pstops_doc_t)); + + doc->job_id = atoi(argv[1]); + doc->user = argv[2]; + doc->title = argv[3]; + doc->copies = atoi(argv[4]); + + if (ppd && ppd->landscape > 0) + doc->normal_landscape = 1; + + doc->bounding_box[0] = (int)PageLeft; + doc->bounding_box[1] = (int)PageBottom; + doc->bounding_box[2] = (int)PageRight; + doc->bounding_box[3] = (int)PageTop; + + doc->new_bounding_box[0] = INT_MAX; + doc->new_bounding_box[1] = INT_MAX; + doc->new_bounding_box[2] = INT_MIN; + doc->new_bounding_box[3] = INT_MIN; + + /* + * AP_FIRSTPAGE_* and the corresponding non-first-page options. + */ + + doc->ap_input_slot = cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options, + options); + doc->ap_manual_feed = cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options, + options); + doc->ap_media_color = cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options, + options); + doc->ap_media_type = cupsGetOption("AP_FIRSTPAGE_MediaType", num_options, + options); + doc->ap_page_region = cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options, + options); + doc->ap_page_size = cupsGetOption("AP_FIRSTPAGE_PageSize", num_options, + options); + + if ((choice = ppdFindMarkedChoice(ppd, "InputSlot")) != NULL) + doc->input_slot = choice->choice; + if ((choice = ppdFindMarkedChoice(ppd, "ManualFeed")) != NULL) + doc->manual_feed = choice->choice; + if ((choice = ppdFindMarkedChoice(ppd, "MediaColor")) != NULL) + doc->media_color = choice->choice; + if ((choice = ppdFindMarkedChoice(ppd, "MediaType")) != NULL) + doc->media_type = choice->choice; + if ((choice = ppdFindMarkedChoice(ppd, "PageRegion")) != NULL) + doc->page_region = choice->choice; + if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) != NULL) + doc->page_size = choice->choice; + + /* + * collate, multiple-document-handling + */ + + if ((val = cupsGetOption("multiple-document-handling", num_options, options)) != NULL) + { + /* + * This IPP attribute is unnecessarily complicated... + * + * single-document, separate-documents-collated-copies, and + * single-document-new-sheet all require collated copies. + * + * separate-documents-uncollated-copies allows for uncollated copies. + */ + + doc->collate = _cups_strcasecmp(val, "separate-documents-uncollated-copies") != 0; + } + + if ((val = cupsGetOption("Collate", num_options, options)) != NULL && + (!_cups_strcasecmp(val, "true") ||!_cups_strcasecmp(val, "on") || + !_cups_strcasecmp(val, "yes"))) + doc->collate = 1; + + /* + * emit-jcl + */ + + if ((val = cupsGetOption("emit-jcl", num_options, options)) != NULL && + (!_cups_strcasecmp(val, "false") || !_cups_strcasecmp(val, "off") || + !_cups_strcasecmp(val, "no") || !strcmp(val, "0"))) + doc->emit_jcl = 0; + else + doc->emit_jcl = 1; + + /* + * fit-to-page/ipp-attribute-fidelity + * + * (Only for original PostScript content) + */ + + if ((content_type = getenv("CONTENT_TYPE")) == NULL) + content_type = "application/postscript"; + + if (!_cups_strcasecmp(content_type, "application/postscript")) + { + if ((val = cupsGetOption("fit-to-page", num_options, options)) != NULL && + !_cups_strcasecmp(val, "true")) + doc->fit_to_page = 1; + else if ((val = cupsGetOption("ipp-attribute-fidelity", num_options, + options)) != NULL && + !_cups_strcasecmp(val, "true")) + doc->fit_to_page = 1; + } + + /* + * mirror/MirrorPrint + */ + + if ((choice = ppdFindMarkedChoice(ppd, "MirrorPrint")) != NULL) + { + val = choice->choice; + choice->marked = 0; + } + else + val = cupsGetOption("mirror", num_options, options); + + if (val && (!_cups_strcasecmp(val, "true") || !_cups_strcasecmp(val, "on") || + !_cups_strcasecmp(val, "yes"))) + doc->mirror = 1; + + /* + * number-up + */ + + if ((val = cupsGetOption("number-up", num_options, options)) != NULL) + { + switch (intval = atoi(val)) + { + case 1 : + case 2 : + case 4 : + case 6 : + case 9 : + case 16 : + doc->number_up = intval; + break; + default : + _cupsLangPrintFilter(stderr, "ERROR", + _("Unsupported number-up value %d, using " + "number-up=1."), intval); + doc->number_up = 1; + break; + } + } + else + doc->number_up = 1; + + /* + * number-up-layout + */ + + if ((val = cupsGetOption("number-up-layout", num_options, options)) != NULL) + { + if (!_cups_strcasecmp(val, "lrtb")) + doc->number_up_layout = PSTOPS_LAYOUT_LRTB; + else if (!_cups_strcasecmp(val, "lrbt")) + doc->number_up_layout = PSTOPS_LAYOUT_LRBT; + else if (!_cups_strcasecmp(val, "rltb")) + doc->number_up_layout = PSTOPS_LAYOUT_RLTB; + else if (!_cups_strcasecmp(val, "rlbt")) + doc->number_up_layout = PSTOPS_LAYOUT_RLBT; + else if (!_cups_strcasecmp(val, "tblr")) + doc->number_up_layout = PSTOPS_LAYOUT_TBLR; + else if (!_cups_strcasecmp(val, "tbrl")) + doc->number_up_layout = PSTOPS_LAYOUT_TBRL; + else if (!_cups_strcasecmp(val, "btlr")) + doc->number_up_layout = PSTOPS_LAYOUT_BTLR; + else if (!_cups_strcasecmp(val, "btrl")) + doc->number_up_layout = PSTOPS_LAYOUT_BTRL; + else + { + _cupsLangPrintFilter(stderr, "ERROR", + _("Unsupported number-up-layout value %s, using " + "number-up-layout=lrtb."), val); + doc->number_up_layout = PSTOPS_LAYOUT_LRTB; + } + } + else + doc->number_up_layout = PSTOPS_LAYOUT_LRTB; + + /* + * OutputOrder + */ + + if ((val = cupsGetOption("OutputOrder", num_options, options)) != NULL) + { + if (!_cups_strcasecmp(val, "Reverse")) + doc->output_order = 1; + } + else if (ppd) + { + /* + * Figure out the right default output order from the PPD file... + */ + + if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL && + (attr = ppdFindAttr(ppd, "PageStackOrder", choice->choice)) != NULL && + attr->value) + doc->output_order = !_cups_strcasecmp(attr->value, "Reverse"); + else if ((attr = ppdFindAttr(ppd, "DefaultOutputOrder", NULL)) != NULL && + attr->value) + doc->output_order = !_cups_strcasecmp(attr->value, "Reverse"); + } + + /* + * page-border + */ + + if ((val = cupsGetOption("page-border", num_options, options)) != NULL) + { + if (!_cups_strcasecmp(val, "none")) + doc->page_border = PSTOPS_BORDERNONE; + else if (!_cups_strcasecmp(val, "single")) + doc->page_border = PSTOPS_BORDERSINGLE; + else if (!_cups_strcasecmp(val, "single-thick")) + doc->page_border = PSTOPS_BORDERSINGLE2; + else if (!_cups_strcasecmp(val, "double")) + doc->page_border = PSTOPS_BORDERDOUBLE; + else if (!_cups_strcasecmp(val, "double-thick")) + doc->page_border = PSTOPS_BORDERDOUBLE2; + else + { + _cupsLangPrintFilter(stderr, "ERROR", + _("Unsupported page-border value %s, using " + "page-border=none."), val); + doc->page_border = PSTOPS_BORDERNONE; + } + } + else + doc->page_border = PSTOPS_BORDERNONE; + + /* + * page-label + */ + + doc->page_label = cupsGetOption("page-label", num_options, options); + + /* + * page-ranges + */ + + doc->page_ranges = cupsGetOption("page-ranges", num_options, options); + + /* + * page-set + */ + + doc->page_set = cupsGetOption("page-set", num_options, options); + + /* + * Now figure out if we have to force collated copies, etc. + */ + + if ((attr = ppdFindAttr(ppd, "cupsMaxCopies", NULL)) != NULL) + max_copies = atoi(attr->value); + else if (ppd && ppd->manual_copies) + max_copies = 1; + else + max_copies = 9999; + + if (doc->copies > max_copies) + doc->collate = 1; + else if (ppd && ppd->manual_copies && Duplex && doc->copies > 1) + { + /* + * Force collated copies when printing a duplexed document to + * a non-PS printer that doesn't do hardware copy generation. + * Otherwise the copies will end up on the front/back side of + * each page. + */ + + doc->collate = 1; + } + + /* + * See if we have to filter the fast or slow way... + */ + + if (doc->collate && doc->copies > 1) + { + /* + * See if we need to manually collate the pages... + */ + + doc->slow_collate = 1; + + if (doc->copies <= max_copies && + (choice = ppdFindMarkedChoice(ppd, "Collate")) != NULL && + !_cups_strcasecmp(choice->choice, "True")) + { + /* + * Hardware collate option is selected, see if the option is + * conflicting - if not, collate in hardware. Otherwise, + * turn the hardware collate option off... + */ + + if ((option = ppdFindOption(ppd, "Collate")) != NULL && + !option->conflicted) + doc->slow_collate = 0; + else + ppdMarkOption(ppd, "Collate", "False"); + } + } + else + doc->slow_collate = 0; + + if (!ppdFindOption(ppd, "OutputOrder") && doc->output_order) + doc->slow_order = 1; + else + doc->slow_order = 0; + + if (Duplex && + (doc->slow_collate || doc->slow_order || + ((attr = ppdFindAttr(ppd, "cupsEvenDuplex", NULL)) != NULL && + attr->value && !_cups_strcasecmp(attr->value, "true")))) + doc->slow_duplex = 1; + else + doc->slow_duplex = 0; + + /* + * Create a temporary file for page data if we need to filter slowly... + */ + + if (doc->slow_order || doc->slow_collate) + { + if ((doc->temp = cupsTempFile2(doc->tempfile, + sizeof(doc->tempfile))) == NULL) + { + perror("DEBUG: Unable to create temporary file"); + exit(1); + } + } + + /* + * Figure out if we should use ESPshowpage or not... + */ + + if (doc->page_label || getenv("CLASSIFICATION") || doc->number_up > 1 || + doc->page_border) + { + /* + * Yes, use ESPshowpage... + */ + + doc->use_ESPshowpage = 1; + } + + fprintf(stderr, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n", + doc->slow_collate, doc->slow_duplex, doc->slow_order); +} + + +/* + * 'skip_page()' - Skip past a page that won't be printed. + */ + +static ssize_t /* O - Length of next line */ +skip_page(cups_file_t *fp, /* I - File to read from */ + char *line, /* I - Line buffer */ + ssize_t linelen, /* I - Length of initial line */ + size_t linesize) /* I - Size of line buffer */ +{ + int level; /* Embedded document level */ + + + level = 0; + + while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) + { + if (level == 0 && + (!strncmp(line, "%%Page:", 7) || !strncmp(line, "%%Trailer", 9))) + break; + else if (!strncmp(line, "%%BeginDocument", 15) || + !strncmp(line, "%ADO_BeginApplication", 21)) + level ++; + else if ((!strncmp(line, "%%EndDocument", 13) || + !strncmp(line, "%ADO_EndApplication", 19)) && level > 0) + level --; + else if (!strncmp(line, "%%BeginBinary:", 14) || + (!strncmp(line, "%%BeginData:", 12) && + !strstr(line, "ASCII") && !strstr(line, "Hex"))) + { + /* + * Skip binary data... + */ + + int bytes; /* Bytes of data */ + + + bytes = atoi(strchr(line, ':') + 1); + + while (bytes > 0) + { + if (bytes > linesize) + linelen = cupsFileRead(fp, line, linesize); + else + linelen = cupsFileRead(fp, line, bytes); + + if (linelen < 1) + { + line[0] = '\0'; + perror("ERROR: Early end-of-file while reading binary data"); + return (0); + } + + bytes -= linelen; + } + } + } + + return (linelen); +} + + +/* + * 'start_nup()' - Start processing for N-up printing. + */ + +static void +start_nup(pstops_doc_t *doc, /* I - Document information */ + int number, /* I - Page number */ + int show_border, /* I - Show the border? */ + const int *bounding_box) /* I - BoundingBox value */ +{ + int pos; /* Position on page */ + int x, y; /* Relative position of subpage */ + float w, l, /* Width and length of subpage */ + tx, ty; /* Translation values for subpage */ + float pagew, /* Printable width of page */ + pagel; /* Printable height of page */ + int bboxx, /* BoundingBox X origin */ + bboxy, /* BoundingBox Y origin */ + bboxw, /* BoundingBox width */ + bboxl; /* BoundingBox height */ + float margin = 0; /* Current margin for border */ + + + if (doc->number_up > 1) + doc_puts(doc, "userdict/ESPsave save put\n"); + + pos = (number - 1) % doc->number_up; + pagew = PageRight - PageLeft; + pagel = PageTop - PageBottom; + + if (doc->fit_to_page) + { + bboxx = bounding_box[0]; + bboxy = bounding_box[1]; + bboxw = bounding_box[2] - bounding_box[0]; + bboxl = bounding_box[3] - bounding_box[1]; + } + else + { + bboxx = 0; + bboxy = 0; + bboxw = PageWidth; + bboxl = PageLength; + } + + fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel); + fprintf(stderr, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n", + bboxx, bboxy, bboxw, bboxl); + fprintf(stderr, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n", + PageLeft, PageRight); + fprintf(stderr, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n", + PageTop, PageBottom); + fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n", + PageWidth, PageLength); + + switch (Orientation) + { + case 1 : /* Landscape */ + doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", PageLength); + break; + case 2 : /* Reverse Portrait */ + doc_printf(doc, "%.1f %.1f translate 180 rotate\n", PageWidth, + PageLength); + break; + case 3 : /* Reverse Landscape */ + doc_printf(doc, "0.0 %.1f translate -90 rotate\n", PageWidth); + break; + } + + /* + * Mirror the page as needed... + */ + + if (doc->mirror) + doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth); + + /* + * Offset and scale as necessary for fit_to_page/fit-to-page/number-up... + */ + + if (Duplex && doc->number_up > 1 && ((number / doc->number_up) & 1)) + doc_printf(doc, "%.1f %.1f translate\n", PageWidth - PageRight, PageBottom); + else if (doc->number_up > 1 || doc->fit_to_page) + doc_printf(doc, "%.1f %.1f translate\n", PageLeft, PageBottom); + + switch (doc->number_up) + { + default : + if (doc->fit_to_page) + { + w = pagew; + l = w * bboxl / bboxw; + + if (l > pagel) + { + l = pagel; + w = l * bboxw / bboxl; + } + + tx = 0.5 * (pagew - w); + ty = 0.5 * (pagel - l); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", tx, ty, + w / bboxw, l / bboxl); + } + else + w = PageWidth; + break; + + case 2 : + if (Orientation & 1) + { + x = pos & 1; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + x = 1 - x; + + w = pagel; + l = w * bboxl / bboxw; + + if (l > (pagew * 0.5)) + { + l = pagew * 0.5; + w = l * bboxw / bboxl; + } + + tx = 0.5 * (pagew * 0.5 - l); + ty = 0.5 * (pagel - w); + + if (doc->normal_landscape) + doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel); + else + doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", + ty, tx + pagew * 0.5 * x, w / bboxw, l / bboxl); + } + else + { + x = pos & 1; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 1 - x; + + l = pagew; + w = l * bboxw / bboxl; + + if (w > (pagel * 0.5)) + { + w = pagel * 0.5; + l = w * bboxl / bboxw; + } + + tx = 0.5 * (pagel * 0.5 - w); + ty = 0.5 * (pagew - l); + + if (doc->normal_landscape) + doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew); + else + doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", + tx + pagel * 0.5 * x, ty, w / bboxw, l / bboxl); + } + break; + + case 4 : + if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) + { + x = (pos / 2) & 1; + y = pos & 1; + } + else + { + x = pos & 1; + y = (pos / 2) & 1; + } + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 1 - x; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + y = 1 - y; + + w = pagew * 0.5; + l = w * bboxl / bboxw; + + if (l > (pagel * 0.5)) + { + l = pagel * 0.5; + w = l * bboxw / bboxl; + } + + tx = 0.5 * (pagew * 0.5 - w); + ty = 0.5 * (pagel * 0.5 - l); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", + tx + x * pagew * 0.5, ty + y * pagel * 0.5, + w / bboxw, l / bboxl); + break; + + case 6 : + if (Orientation & 1) + { + if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) + { + x = pos / 3; + y = pos % 3; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 1 - x; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + y = 2 - y; + } + else + { + x = pos & 1; + y = pos / 2; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 1 - x; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + y = 2 - y; + } + + w = pagel * 0.5; + l = w * bboxl / bboxw; + + if (l > (pagew * 0.333)) + { + l = pagew * 0.333; + w = l * bboxw / bboxl; + } + + tx = 0.5 * (pagel - 2 * w); + ty = 0.5 * (pagew - 3 * l); + + if (doc->normal_landscape) + doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel); + else + doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", + tx + x * w, ty + y * l, l / bboxl, w / bboxw); + } + else + { + if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) + { + x = pos / 2; + y = pos & 1; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 2 - x; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + y = 1 - y; + } + else + { + x = pos % 3; + y = pos / 3; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 2 - x; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + y = 1 - y; + } + + l = pagew * 0.5; + w = l * bboxw / bboxl; + + if (w > (pagel * 0.333)) + { + w = pagel * 0.333; + l = w * bboxl / bboxw; + } + + tx = 0.5 * (pagel - 3 * w); + ty = 0.5 * (pagew - 2 * l); + + if (doc->normal_landscape) + doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew); + else + doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", + tx + w * x, ty + l * y, w / bboxw, l / bboxl); + + } + break; + + case 9 : + if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) + { + x = (pos / 3) % 3; + y = pos % 3; + } + else + { + x = pos % 3; + y = (pos / 3) % 3; + } + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 2 - x; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + y = 2 - y; + + w = pagew * 0.333; + l = w * bboxl / bboxw; + + if (l > (pagel * 0.333)) + { + l = pagel * 0.333; + w = l * bboxw / bboxl; + } + + tx = 0.5 * (pagew * 0.333 - w); + ty = 0.5 * (pagel * 0.333 - l); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", + tx + x * pagew * 0.333, ty + y * pagel * 0.333, + w / bboxw, l / bboxl); + break; + + case 16 : + if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) + { + x = (pos / 4) & 3; + y = pos & 3; + } + else + { + x = pos & 3; + y = (pos / 4) & 3; + } + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) + x = 3 - x; + + if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) + y = 3 - y; + + w = pagew * 0.25; + l = w * bboxl / bboxw; + + if (l > (pagel * 0.25)) + { + l = pagel * 0.25; + w = l * bboxw / bboxl; + } + + tx = 0.5 * (pagew * 0.25 - w); + ty = 0.5 * (pagel * 0.25 - l); + + doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", + tx + x * pagew * 0.25, ty + y * pagel * 0.25, + w / bboxw, l / bboxl); + break; + } + + /* + * Draw borders as necessary... + */ + + if (doc->page_border && show_border) + { + int rects; /* Number of border rectangles */ + float fscale; /* Scaling value for points */ + + + rects = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1; + fscale = PageWidth / w; + margin = 2.25 * fscale; + + /* + * Set the line width and color... + */ + + doc_puts(doc, "gsave\n"); + doc_printf(doc, "%.3f setlinewidth 0 setgray newpath\n", + (doc->page_border & PSTOPS_BORDERTHICK) ? 0.5 * fscale : + 0.24 * fscale); + + /* + * Draw border boxes... + */ + + for (; rects > 0; rects --, margin += 2 * fscale) + if (doc->number_up > 1) + doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n", + margin, + margin, + bboxw - 2 * margin, + bboxl - 2 * margin); + else + doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n", + PageLeft + margin, + PageBottom + margin, + PageRight - PageLeft - 2 * margin, + PageTop - PageBottom - 2 * margin); + + /* + * Restore pen settings... + */ + + doc_puts(doc, "grestore\n"); + } + + if (doc->fit_to_page) + { + /* + * Offset the page by its bounding box... + */ + + doc_printf(doc, "%d %d translate\n", -bounding_box[0], + -bounding_box[1]); + } + + if (doc->fit_to_page || doc->number_up > 1) + { + /* + * Clip the page to the page's bounding box... + */ + + doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrc\n", + bboxx + margin, bboxy + margin, + bboxw - 2 * margin, bboxl - 2 * margin); + } +} + + +/* + * 'write_label_prolog()' - Write the prolog with the classification + * and page label. + */ + +static void +write_label_prolog(pstops_doc_t *doc, /* I - Document info */ + const char *label, /* I - Page label */ + float bottom, /* I - Bottom position in points */ + float top, /* I - Top position in points */ + float width) /* I - Width in points */ +{ + const char *classification; /* CLASSIFICATION environment variable */ + const char *ptr; /* Temporary string pointer */ + + + /* + * First get the current classification... + */ + + if ((classification = getenv("CLASSIFICATION")) == NULL) + classification = ""; + if (strcmp(classification, "none") == 0) + classification = ""; + + /* + * If there is nothing to show, bind an empty 'write labels' procedure + * and return... + */ + + if (!classification[0] && (label == NULL || !label[0])) + { + doc_puts(doc, "userdict/ESPwl{}bind put\n"); + return; + } + + /* + * Set the classification + page label string... + */ + + doc_puts(doc, "userdict"); + if (!strcmp(classification, "confidential")) + doc_puts(doc, "/ESPpl(CONFIDENTIAL"); + else if (!strcmp(classification, "classified")) + doc_puts(doc, "/ESPpl(CLASSIFIED"); + else if (!strcmp(classification, "secret")) + doc_puts(doc, "/ESPpl(SECRET"); + else if (!strcmp(classification, "topsecret")) + doc_puts(doc, "/ESPpl(TOP SECRET"); + else if (!strcmp(classification, "unclassified")) + doc_puts(doc, "/ESPpl(UNCLASSIFIED"); + else + { + doc_puts(doc, "/ESPpl("); + + for (ptr = classification; *ptr; ptr ++) + { + if (*ptr < 32 || *ptr > 126) + doc_printf(doc, "\\%03o", *ptr); + else if (*ptr == '_') + doc_puts(doc, " "); + else if (*ptr == '(' || *ptr == ')' || *ptr == '\\') + doc_printf(doc, "\\%c", *ptr); + else + doc_printf(doc, "%c", *ptr); + } + } + + if (label) + { + if (classification[0]) + doc_puts(doc, " - "); + + /* + * Quote the label string as needed... + */ + + for (ptr = label; *ptr; ptr ++) + { + if (*ptr < 32 || *ptr > 126) + doc_printf(doc, "\\%03o", *ptr); + else if (*ptr == '(' || *ptr == ')' || *ptr == '\\') + doc_printf(doc, "\\%c", *ptr); + else + doc_printf(doc, "%c", *ptr); + } + } + + doc_puts(doc, ")put\n"); + + /* + * Then get a 14 point Helvetica-Bold font... + */ + + doc_puts(doc, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n"); + + /* + * Finally, the procedure to write the labels on the page... + */ + + doc_puts(doc, "userdict/ESPwl{\n"); + doc_puts(doc, " ESPpf setfont\n"); + doc_printf(doc, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n", + width * 0.5f); + doc_puts(doc, " 1 setgray\n"); + doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0); + doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0); + doc_puts(doc, " 0 setgray\n"); + doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0); + doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0); + doc_printf(doc, " dup %.0f moveto ESPpl show\n", bottom + 2.0); + doc_printf(doc, " %.0f moveto ESPpl show\n", top - 14.0); + doc_puts(doc, "pop\n"); + doc_puts(doc, "}bind put\n"); +} + + +/* + * 'write_labels()' - Write the actual page labels. + * + * This function is a copy of the one in common.c since we need to + * use doc_puts/doc_printf instead of puts/printf... + */ + +static void +write_labels(pstops_doc_t *doc, /* I - Document information */ + int orient) /* I - Orientation of the page */ +{ + float width, /* Width of page */ + length; /* Length of page */ + + + doc_puts(doc, "gsave\n"); + + if ((orient ^ Orientation) & 1) + { + width = PageLength; + length = PageWidth; + } + else + { + width = PageWidth; + length = PageLength; + } + + switch (orient & 3) + { + case 1 : /* Landscape */ + doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", length); + break; + case 2 : /* Reverse Portrait */ + doc_printf(doc, "%.1f %.1f translate 180 rotate\n", width, length); + break; + case 3 : /* Reverse Landscape */ + doc_printf(doc, "0.0 %.1f translate -90 rotate\n", width); + break; + } + + doc_puts(doc, "ESPwl\n"); + doc_puts(doc, "grestore\n"); +} + + +/* + * 'write_options()' - Write options provided via %%IncludeFeature. + */ + +static void +write_options( + pstops_doc_t *doc, /* I - Document */ + ppd_file_t *ppd, /* I - PPD file */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + int i; /* Looping var */ + ppd_option_t *option; /* PPD option */ + int min_order; /* Minimum OrderDependency value */ + char *doc_setup, /* DocumentSetup commands to send */ + *any_setup; /* AnySetup commands to send */ + + + /* + * Figure out the minimum OrderDependency value... + */ + + if ((option = ppdFindOption(ppd, "PageRegion")) != NULL) + min_order = option->order; + else + min_order = 999.0f; + + for (i = 0; i < num_options; i ++) + if ((option = ppdFindOption(ppd, options[i].name)) != NULL && + option->order < min_order) + min_order = option->order; + + /* + * Mark and extract them... + */ + + cupsMarkOptions(ppd, num_options, options); + + doc_setup = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, min_order); + any_setup = ppdEmitString(ppd, PPD_ORDER_ANY, min_order); + + /* + * Then send them out... + */ + + if (doc->number_up > 1) + { + /* + * Temporarily restore setpagedevice so we can set the options... + */ + + doc_puts(doc, "userdict/setpagedevice/CUPSsetpagedevice load put\n"); + } + + if (doc_setup) + { + doc_puts(doc, doc_setup); + free(doc_setup); + } + + if (any_setup) + { + doc_puts(doc, any_setup); + free(any_setup); + } + + if (doc->number_up > 1) + { + /* + * Disable setpagedevice again... + */ + + doc_puts(doc, "userdict/setpagedevice{pop}bind put\n"); + } +} + + +/* + * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $". + */ diff --git a/filter/raster-driver.header b/filter/raster-driver.header new file mode 100644 index 0000000..b15c50d --- /dev/null +++ b/filter/raster-driver.header @@ -0,0 +1,32 @@ + + +

Developing Raster Printer Drivers

+ +

This document describes how to develop printer drivers for raster printers. Topics include: printer driver basics, creating new PPD files, using filters, implementing color management, and adding OS X features.

+ +
+ + + + + + +
See AlsoProgramming: Developing PostScript Printer Drivers
+ Programming: Filter and Backend Programming
+ Programming: Introduction to the PPD Compiler
+ Programming: Raster API
+ References: PPD Compiler Driver Information File Reference
+ Specifications: CUPS PPD Extensions
diff --git a/filter/raster-driver.shtml b/filter/raster-driver.shtml new file mode 100644 index 0000000..f56982a --- /dev/null +++ b/filter/raster-driver.shtml @@ -0,0 +1,194 @@ +

Printer Driver Basics

+ +

A CUPS raster printer driver consists of a PostScript Printer Description (PPD) file that describes the features and capabilities of the device, one or more filter programs that prepare print data for the device, and zero or more support files for color management, online help, and so forth. The PPD file includes references to all of the filters and support files used by the driver.

+ +

Every time a user prints something the scheduler program, cupsd(8), determines the format of the print job and the programs required to convert that job into something the printer understands. CUPS includes filter programs for many common formats, for example to convert Portable Document Format (PDF) files into CUPS raster data. Figure 1 shows the data flow of a typical print job.

+ +
+ + +
Figure 1: Raster Filter Chain
Raster Filter Chain
+ +

The raster filter converts CUPS raster data into a format the printer understands, for example HP-PCL. CUPS includes several sample raster filters supporting standard page description languages (PDLs). Table 1 shows the raster filters that are bundled with CUPS and the languages they support.

+ +
+ + + + + + + + + + + +
Table 1: Standard CUPS Raster Filters
FilterPDLsppdc DriverTypeppdc #include file
rastertoepsonESC/P, ESC/P2epsonepson.h
rastertoescpxESC/P, ESC/P2, EPSON Remote Modeescpescp.h
rastertohpHP-PCL3, HP-PCL5hphp.h
rastertolabelCPCL, Dymo, EPL1, EPL2, Intellitech PCL, ZPLlabellabel.h
rastertopclxHP-RTL, HP-PCL3, HP-PCL3GUI, HP-PCL5, HP-PCL5c, HP-PCL5epclpcl.h
+ +

The optional port monitor handles interface-specific protocol or encoding issues. For example, some raster printers use the 1284.4 communications protocol.

+ +

The backend handles communications with the printer, sending print data from the last filter to the printer and relaying back-channel data from the printer to the upstream filters. CUPS includes backend programs for common direct-connect interfaces and network protocols, and you can provide your own backend to support custom interfaces and protocols.

+ +

The scheduler also supports a special "command" file format for sending maintenance commands and status queries to a printer or printer driver. Command print jobs typically use a single command filter program defined in the PPD file to generate the appropriate printer commands and handle any responses from the printer. Figure 2 shows the data flow of a typical command job.

+ +
+ + +
Figure 2: Command Filter Chain
Command Filter Chain
+ +

Raster printer drivers must provide their own command filter.

+ + +

Creating New PPD Files

+ +

We recommend using the CUPS PPD compiler, ppdc(1), to create new PPD files since it manages many of the tedious (and error-prone!) details of paper sizes and localization for you. It also allows you to easily support multiple devices from a single source file. For more information see the "Introduction to the PPD Compiler" document. Listing 1 shows a driver information file for several similar black-and-white HP-PCL5 laser printers.

+ +

Listing 1: "examples/laserjet-basic.drv"

+ +
+// Include standard font and media definitions
+#include <font.defs>
+#include <media.defs>
+
+// Include HP-PCL driver definitions
+#include <pcl.h>
+
+// Specify that this driver uses the HP-PCL driver...
+DriverType pcl
+
+// Specify the driver options via the model number...
+ModelNumber ($PCL_PAPER_SIZE $PCL_PJL $PCL_PJL_RESOLUTION)
+
+// List the fonts that are supported, in this case all standard fonts...
+Font *
+
+// Manufacturer and driver version
+Manufacturer "HP"
+Version 1.0
+
+// Supported page sizes and their margins
+HWMargins 18 12 18 12
+*MediaSize Letter
+MediaSize Legal
+MediaSize Executive
+MediaSize Monarch
+MediaSize Statement
+MediaSize FanFoldGermanLegal
+
+HWMargins 18 12.72 18 12.72
+MediaSize Env10
+
+HWMargins 9.72 12 9.72 12
+MediaSize A4
+MediaSize A5
+MediaSize B5
+MediaSize EnvC5
+MediaSize EnvDL
+MediaSize EnvISOB5
+MediaSize Postcard
+MediaSize DoublePostcard
+
+// Only black-and-white output with mode 3 compression...
+ColorModel Gray k chunky 3
+
+// Supported resolutions
+Resolution - 1 0 0 0 "300dpi/300 DPI"
+*Resolution - 8 0 0 0 "600dpi/600 DPI"
+
+// Supported input slots
+*InputSlot 7 "Auto/Automatic Selection"
+InputSlot 2 "Manual/Tray 1 - Manual Feed"
+InputSlot 4 "Upper/Tray 1"
+InputSlot 1 "Lower/Tray 2"
+InputSlot 5 "LargeCapacity/Tray 3"
+
+// Tray 3 is an option...
+Installable "OptionLargeCapacity/Tray 3 Installed"
+UIConstraints "*OptionLargeCapacity False *InputSlot LargeCapacity"
+
+{
+  // HP LaserJet 2100 Series
+  Throughput 10
+  ModelName "LaserJet 2100 Series"
+  PCFileName "hpljt211.ppd"
+}
+
+{
+  // LaserJet 2200 and 2300 series have duplexer option...
+  Duplex normal
+  Installable "OptionDuplex/Duplexer Installed"
+  UIConstraints "*OptionDuplex False *Duplex"
+
+  {
+    // HP LaserJet 2200 Series
+    Throughput 19
+    ModelName "LaserJet 2200 Series"
+    PCFileName "hpljt221.ppd"
+  }
+
+  {
+    // HP LaserJet 2300 Series
+    Throughput 25
+    ModelName "LaserJet 2300 Series"
+    PCFileName "hpljt231.ppd"
+  }
+}
+
+ + +

Using Filters

+ +

The standard CUPS raster filters can be specified using the +DriverType directive, for example:

+ +
+// Specify that this driver uses the HP-PCL driver...
+DriverType pcl
+
+ +

Table 1 shows the driver types for each of the standard CUPS raster filters. For drivers that do not use the standard raster filters, the "custom" type is used with Filter directives:

+ +
+DriverType custom
+Filter application/vnd.cups-raster 100 /path/to/raster/filter
+Filter application/vnd.cups-command 100 /path/to/command/filter
+
+ + +

Implementing Color Management

+ +

CUPS uses ICC color profiles to provide more accurate color reproduction. The cupsICCProfile attribute defines the color profiles that are available for a given printer, for example:

+ +
+Attribute cupsICCProfile "ColorModel.MediaType.Resolution/Description" /path/to/ICC/profile
+
+ +

where "ColorModel.MediaType.Resolution" defines a selector based on the corresponding option selections. A simple driver might only define profiles for the color models that are supported, for example a printer supporting Gray and RGB might use:

+ +
+Attribute cupsICCProfile "Gray../Grayscale Profile" /path/to/ICC/gray-profile
+Attribute cupsICCProfile "RGB../Full Color Profile" /path/to/ICC/rgb-profile
+
+ +

The options used for profile selection can be customized using the cupsICCQualifier2 and cupsICCQualifier3 attributes.

+ +

Since OS X 10.5Custom Color Matching Support

+ +

OS X printer drivers that are based on an existing standard RGB colorspace can tell the system to use the corresponding colorspace instead of an arbitrary ICC color profile when doing color management. The APSupportsCustomColorMatching and APDefaultCustomColorMatchingProfile attributes can be used to enable this mode:

+ +
+Attribute APSupportsCustomColorMatching "" true
+Attribute APDefaultCustomColorMatchingProfile "" sRGB
+
+ + +

Adding OS X Features

+ +

OS X printer drivers can provide additional attributes to specify additional option panes in the print dialog, an image of the printer, a help book, and option presets for the driver software:

+ +
+Attribute APDialogExtension "" /Library/Printers/Vendor/filename.plugin
+Attribute APHelpBook "" /Library/Printers/Vendor/filename.bundle
+Attribute APPrinterIconPath "" /Library/Printers/Vendor/filename.icns
+Attribute APPrinterPreset "name/text" "*option choice ..."
+
diff --git a/filter/raster.c b/filter/raster.c new file mode 100644 index 0000000..1ecd90d --- /dev/null +++ b/filter/raster.c @@ -0,0 +1,1479 @@ +/* + * "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $" + * + * Raster file routines for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products. + * + * This file is part of the CUPS Imaging library. + * + * 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. + * + * Contents: + * + * cupsRasterClose() - Close a raster stream. + * cupsRasterOpen() - Open a raster stream using a file descriptor. + * cupsRasterOpenIO() - Open a raster stream using a callback function. + * cupsRasterReadHeader() - Read a raster page header and store it in a + * version 1 page header structure. + * cupsRasterReadHeader2() - Read a raster page header and store it in a + * version 2 page header structure. + * cupsRasterReadPixels() - Read raster pixels. + * cupsRasterWriteHeader() - Write a raster page header from a version 1 + * page header structure. + * cupsRasterWriteHeader2() - Write a raster page header from a version 2 + * page header structure. + * cupsRasterWritePixels() - Write raster pixels. + * cups_raster_read_header() - Read a raster page header. + * cups_raster_read() - Read through the raster buffer. + * cups_raster_update() - Update the raster header and row count for the + * current page. + * cups_raster_write() - Write a row of compressed raster data... + * cups_read_fd() - Read bytes from a file. + * cups_swap() - Swap bytes in raster data... + * cups_write_fd() - Write bytes to a file. + */ + +/* + * Include necessary headers... + */ + +#include +#ifdef HAVE_STDINT_H +# include +#endif /* HAVE_STDINT_H */ + + +/* + * Private structures... + */ + +struct _cups_raster_s /**** Raster stream data ****/ +{ + unsigned sync; /* Sync word from start of stream */ + void *ctx; /* File descriptor */ + cups_raster_iocb_t iocb; /* IO callback */ + cups_mode_t mode; /* Read/write mode */ + cups_page_header2_t header; /* Raster header for current page */ + int count, /* Current row run-length count */ + remaining, /* Remaining rows in page image */ + bpp; /* Bytes per pixel/color */ + unsigned char *pixels, /* Pixels for current row */ + *pend, /* End of pixel buffer */ + *pcurrent; /* Current byte in pixel buffer */ + int compressed, /* Non-zero if data is compressed */ + swapped; /* Non-zero if data is byte-swapped */ + unsigned char *buffer, /* Read/write buffer */ + *bufptr, /* Current (read) position in buffer */ + *bufend; /* End of current (read) buffer */ + size_t bufsize; /* Buffer size */ +}; + + +/* + * Local functions... + */ + +static int cups_raster_io(cups_raster_t *r, unsigned char *buf, int bytes); +static unsigned cups_raster_read_header(cups_raster_t *r); +static int cups_raster_read(cups_raster_t *r, unsigned char *buf, + int bytes); +static void cups_raster_update(cups_raster_t *r); +static int cups_raster_write(cups_raster_t *r, + const unsigned char *pixels); +static ssize_t cups_read_fd(void *ctx, unsigned char *buf, size_t bytes); +static void cups_swap(unsigned char *buf, int bytes); +static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes); + + +/* + * 'cupsRasterClose()' - Close a raster stream. + * + * The file descriptor associated with the raster stream must be closed + * separately as needed. + */ + +void +cupsRasterClose(cups_raster_t *r) /* I - Stream to close */ +{ + if (r != NULL) + { + if (r->buffer) + free(r->buffer); + + if (r->pixels) + free(r->pixels); + + free(r); + } +} + + +/* + * 'cupsRasterOpen()' - Open a raster stream using a file descriptor. + * + * This function associates a raster stream with the given file descriptor. + * For most printer driver filters, "fd" will be 0 (stdin). For most raster + * image processor (RIP) filters that generate raster data, "fd" will be 1 + * (stdout). + * + * When writing raster data, the @code CUPS_RASTER_WRITE@, + * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can + * be used - compressed and PWG output is generally 25-50% smaller but adds a + * 100-300% execution time overhead. + */ + +cups_raster_t * /* O - New stream */ +cupsRasterOpen(int fd, /* I - File descriptor */ + cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@, + @code CUPS_RASTER_WRITE@, + @code CUPS_RASTER_WRITE_COMPRESSED@, + or @code CUPS_RASTER_WRITE_PWG@ */ +{ + if (mode == CUPS_RASTER_READ) + return (cupsRasterOpenIO(cups_read_fd, (void *)((intptr_t)fd), mode)); + else + return (cupsRasterOpenIO(cups_write_fd, (void *)((intptr_t)fd), mode)); +} + + +/* + * 'cupsRasterOpenIO()' - Open a raster stream using a callback function. + * + * This function associates a raster stream with the given callback function and + * context pointer. + * + * When writing raster data, the @code CUPS_RASTER_WRITE@, + * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can + * be used - compressed and PWG output is generally 25-50% smaller but adds a + * 100-300% execution time overhead. + */ + +cups_raster_t * /* O - New stream */ +cupsRasterOpenIO( + cups_raster_iocb_t iocb, /* I - Read/write callback */ + void *ctx, /* I - Context pointer for callback */ + cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@, + @code CUPS_RASTER_WRITE@, + @code CUPS_RASTER_WRITE_COMPRESSED@, + or @code CUPS_RASTER_WRITE_PWG@ */ +{ + cups_raster_t *r; /* New stream */ + + + _cupsRasterClearError(); + + if ((r = calloc(sizeof(cups_raster_t), 1)) == NULL) + { + _cupsRasterAddError("Unable to allocate memory for raster stream: %s\n", + strerror(errno)); + return (NULL); + } + + r->ctx = ctx; + r->iocb = iocb; + r->mode = mode; + + if (mode == CUPS_RASTER_READ) + { + /* + * Open for read - get sync word... + */ + + if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) != + sizeof(r->sync)) + { + _cupsRasterAddError("Unable to read header from raster stream: %s\n", + strerror(errno)); + free(r); + return (NULL); + } + + if (r->sync != CUPS_RASTER_SYNC && + r->sync != CUPS_RASTER_REVSYNC && + r->sync != CUPS_RASTER_SYNCv1 && + r->sync != CUPS_RASTER_REVSYNCv1 && + r->sync != CUPS_RASTER_SYNCv2 && + r->sync != CUPS_RASTER_REVSYNCv2) + { + _cupsRasterAddError("Unknown raster format %08x!\n", r->sync); + free(r); + return (NULL); + } + + if (r->sync == CUPS_RASTER_SYNCv2 || + r->sync == CUPS_RASTER_REVSYNCv2) + r->compressed = 1; + + if (r->sync == CUPS_RASTER_REVSYNC || + r->sync == CUPS_RASTER_REVSYNCv1 || + r->sync == CUPS_RASTER_REVSYNCv2) + r->swapped = 1; + + DEBUG_printf(("r->swapped=%d, r->sync=%08x\n", r->swapped, r->sync)); + } + else + { + /* + * Open for write - put sync word... + */ + + switch (mode) + { + default : + case CUPS_RASTER_WRITE : + r->sync = CUPS_RASTER_SYNC; + break; + + case CUPS_RASTER_WRITE_COMPRESSED : + r->compressed = 1; + r->sync = CUPS_RASTER_SYNCv2; + break; + + case CUPS_RASTER_WRITE_PWG : + r->compressed = 1; + r->sync = htonl(CUPS_RASTER_SYNC_PWG); + r->swapped = r->sync != CUPS_RASTER_SYNC_PWG; + break; + } + + if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) + < sizeof(r->sync)) + { + _cupsRasterAddError("Unable to write raster stream header: %s\n", + strerror(errno)); + free(r); + return (NULL); + } + } + + return (r); +} + + +/* + * 'cupsRasterReadHeader()' - Read a raster page header and store it in a + * version 1 page header structure. + * + * This function is deprecated. Use @link cupsRasterReadHeader2@ instead. + * + * Version 1 page headers were used in CUPS 1.0 and 1.1 and contain a subset + * of the version 2 page header data. This function handles reading version 2 + * page headers and copying only the version 1 data into the provided buffer. + * + * @deprecated@ + */ + +unsigned /* O - 1 on success, 0 on failure/end-of-file */ +cupsRasterReadHeader( + cups_raster_t *r, /* I - Raster stream */ + cups_page_header_t *h) /* I - Pointer to header data */ +{ + /* + * Get the raster header... + */ + + if (!cups_raster_read_header(r)) + return (0); + + /* + * Copy the header to the user-supplied buffer... + */ + + memcpy(h, &(r->header), sizeof(cups_page_header_t)); + + return (1); +} + + +/* + * 'cupsRasterReadHeader2()' - Read a raster page header and store it in a + * version 2 page header structure. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +unsigned /* O - 1 on success, 0 on failure/end-of-file */ +cupsRasterReadHeader2( + cups_raster_t *r, /* I - Raster stream */ + cups_page_header2_t *h) /* I - Pointer to header data */ +{ + /* + * Get the raster header... + */ + + if (!cups_raster_read_header(r)) + return (0); + + /* + * Copy the header to the user-supplied buffer... + */ + + memcpy(h, &(r->header), sizeof(cups_page_header2_t)); + + return (1); +} + + +/* + * 'cupsRasterReadPixels()' - Read raster pixels. + * + * For best performance, filters should read one or more whole lines. + * The "cupsBytesPerLine" value from the page header can be used to allocate + * the line buffer and as the number of bytes to read. + */ + +unsigned /* O - Number of bytes read */ +cupsRasterReadPixels(cups_raster_t *r, /* I - Raster stream */ + unsigned char *p, /* I - Pointer to pixel buffer */ + unsigned len) /* I - Number of bytes to read */ +{ + int bytes; /* Bytes read */ + unsigned cupsBytesPerLine; /* cupsBytesPerLine value */ + unsigned remaining; /* Bytes remaining */ + unsigned char *ptr, /* Pointer to read buffer */ + byte, /* Byte from file */ + *temp; /* Pointer into buffer */ + int count; /* Repetition count */ + + + if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0 || + r->header.cupsBytesPerLine == 0) + return (0); + + if (!r->compressed) + { + /* + * Read without compression... + */ + + r->remaining -= len / r->header.cupsBytesPerLine; + + if (cups_raster_io(r, p, len) < (ssize_t)len) + return (0); + + /* + * Swap bytes as needed... + */ + + if (r->swapped && + (r->header.cupsBitsPerColor == 16 || + r->header.cupsBitsPerPixel == 12 || + r->header.cupsBitsPerPixel == 16)) + cups_swap(p, len); + + /* + * Return... + */ + + return (len); + } + + /* + * Read compressed data... + */ + + remaining = len; + cupsBytesPerLine = r->header.cupsBytesPerLine; + + while (remaining > 0 && r->remaining > 0) + { + if (r->count == 0) + { + /* + * Need to read a new row... + */ + + if (remaining == cupsBytesPerLine) + ptr = p; + else + ptr = r->pixels; + + /* + * Read using a modified PackBits compression... + */ + + if (!cups_raster_read(r, &byte, 1)) + return (0); + + r->count = byte + 1; + + if (r->count > 1) + ptr = r->pixels; + + temp = ptr; + bytes = cupsBytesPerLine; + + while (bytes > 0) + { + /* + * Get a new repeat count... + */ + + if (!cups_raster_read(r, &byte, 1)) + return (0); + + if (byte & 128) + { + /* + * Copy N literal pixels... + */ + + count = (257 - byte) * r->bpp; + + if (count > bytes) + count = bytes; + + if (!cups_raster_read(r, temp, count)) + return (0); + + temp += count; + bytes -= count; + } + else + { + /* + * Repeat the next N bytes... + */ + + count = (byte + 1) * r->bpp; + if (count > bytes) + count = bytes; + + if (count < r->bpp) + break; + + bytes -= count; + + if (!cups_raster_read(r, temp, r->bpp)) + return (0); + + temp += r->bpp; + count -= r->bpp; + + while (count > 0) + { + memcpy(temp, temp - r->bpp, r->bpp); + temp += r->bpp; + count -= r->bpp; + } + } + } + + /* + * Swap bytes as needed... + */ + + if ((r->header.cupsBitsPerColor == 16 || + r->header.cupsBitsPerPixel == 12 || + r->header.cupsBitsPerPixel == 16) && + r->swapped) + cups_swap(ptr, bytes); + + /* + * Update pointers... + */ + + if (remaining >= cupsBytesPerLine) + { + bytes = cupsBytesPerLine; + r->pcurrent = r->pixels; + r->count --; + r->remaining --; + } + else + { + bytes = remaining; + r->pcurrent = r->pixels + bytes; + } + + /* + * Copy data as needed... + */ + + if (ptr != p) + memcpy(p, ptr, bytes); + } + else + { + /* + * Copy fragment from buffer... + */ + + if ((unsigned)(bytes = (int)(r->pend - r->pcurrent)) > remaining) + bytes = remaining; + + memcpy(p, r->pcurrent, bytes); + r->pcurrent += bytes; + + if (r->pcurrent >= r->pend) + { + r->pcurrent = r->pixels; + r->count --; + r->remaining --; + } + } + + remaining -= bytes; + p += bytes; + } + + return (len); +} + + +/* + * 'cupsRasterWriteHeader()' - Write a raster page header from a version 1 page + * header structure. + * + * This function is deprecated. Use @link cupsRasterWriteHeader2@ instead. + * + * @deprecated@ + */ + +unsigned /* O - 1 on success, 0 on failure */ +cupsRasterWriteHeader( + cups_raster_t *r, /* I - Raster stream */ + cups_page_header_t *h) /* I - Raster page header */ +{ + if (r == NULL || r->mode == CUPS_RASTER_READ) + return (0); + + /* + * Make a copy of the header, and compute the number of raster + * lines in the page image... + */ + + memset(&(r->header), 0, sizeof(r->header)); + memcpy(&(r->header), h, sizeof(cups_page_header_t)); + + cups_raster_update(r); + + /* + * Write the raster header... + */ + + if (r->mode == CUPS_RASTER_WRITE_PWG) + { + /* + * PWG raster data is always network byte order with much of the page header + * zeroed. + */ + + cups_page_header2_t fh; /* File page header */ + + memset(&fh, 0, sizeof(fh)); + + strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass)); + /* PwgRaster */ + strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor)); + strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType)); + strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType)); + /* PrintContentType */ + + fh.CutMedia = htonl(r->header.CutMedia); + fh.Duplex = htonl(r->header.Duplex); + fh.HWResolution[0] = htonl(r->header.HWResolution[0]); + fh.HWResolution[1] = htonl(r->header.HWResolution[1]); + fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]); + fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]); + fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]); + fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]); + fh.InsertSheet = htonl(r->header.InsertSheet); + fh.Jog = htonl(r->header.Jog); + fh.LeadingEdge = htonl(r->header.LeadingEdge); + fh.ManualFeed = htonl(r->header.ManualFeed); + fh.MediaPosition = htonl(r->header.MediaPosition); + fh.MediaWeight = htonl(r->header.MediaWeight); + fh.NumCopies = htonl(r->header.NumCopies); + fh.Orientation = htonl(r->header.Orientation); + fh.PageSize[0] = htonl(r->header.PageSize[0]); + fh.PageSize[1] = htonl(r->header.PageSize[1]); + fh.Tumble = htonl(r->header.Tumble); + fh.cupsWidth = htonl(r->header.cupsWidth); + fh.cupsHeight = htonl(r->header.cupsHeight); + fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor); + fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel); + fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine); + fh.cupsColorOrder = htonl(r->header.cupsColorOrder); + fh.cupsColorSpace = htonl(r->header.cupsColorSpace); + fh.cupsNumColors = htonl(r->header.cupsNumColors); + fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]); + /* TotalPageCount */ + fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]); + /* CrossFeedTransform */ + fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]); + /* FeedTransform */ + fh.cupsInteger[3] = htonl(r->header.cupsInteger[3]); + /* ImageBoxLeft */ + fh.cupsInteger[4] = htonl(r->header.cupsInteger[4]); + /* ImageBoxTop */ + fh.cupsInteger[5] = htonl(r->header.cupsInteger[5]); + /* ImageBoxRight */ + fh.cupsInteger[6] = htonl(r->header.cupsInteger[6]); + /* ImageBoxBottom */ + fh.cupsInteger[7] = htonl(r->header.cupsInteger[7]); + /* BlackPrimary */ + fh.cupsInteger[8] = htonl(r->header.cupsInteger[8]); + /* PrintQuality */ + fh.cupsInteger[14] = htonl(r->header.cupsInteger[14]); + /* VendorIdentifier */ + fh.cupsInteger[15] = htonl(r->header.cupsInteger[15]); + /* VendorLength */ + + memcpy(fh.cupsReal, r->header.cupsReal, + sizeof(fh.cupsReal) + sizeof(fh.cupsString)); + /* VendorData */ + + strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent, + sizeof(fh.cupsRenderingIntent)); + strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName, + sizeof(fh.cupsPageSizeName)); + + return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh)); + } + else + return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header)) + == sizeof(r->header)); +} + + +/* + * 'cupsRasterWriteHeader2()' - Write a raster page header from a version 2 + * page header structure. + * + * The page header can be initialized using @link cupsRasterInterpretPPD@. + * + * @since CUPS 1.2/OS X 10.5@ + */ + +unsigned /* O - 1 on success, 0 on failure */ +cupsRasterWriteHeader2( + cups_raster_t *r, /* I - Raster stream */ + cups_page_header2_t *h) /* I - Raster page header */ +{ + if (r == NULL || r->mode == CUPS_RASTER_READ) + return (0); + + /* + * Make a copy of the header, and compute the number of raster + * lines in the page image... + */ + + memcpy(&(r->header), h, sizeof(cups_page_header2_t)); + + cups_raster_update(r); + + /* + * Write the raster header... + */ + + if (r->mode == CUPS_RASTER_WRITE_PWG) + { + /* + * PWG raster data is always network byte order with most of the page header + * zeroed. + */ + + cups_page_header2_t fh; /* File page header */ + + memset(&fh, 0, sizeof(fh)); + strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass)); + strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor)); + strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType)); + strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType)); + strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent, + sizeof(fh.cupsRenderingIntent)); + strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName, + sizeof(fh.cupsPageSizeName)); + + fh.CutMedia = htonl(r->header.CutMedia); + fh.Duplex = htonl(r->header.Duplex); + fh.HWResolution[0] = htonl(r->header.HWResolution[0]); + fh.HWResolution[1] = htonl(r->header.HWResolution[1]); + fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]); + fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]); + fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]); + fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]); + fh.InsertSheet = htonl(r->header.InsertSheet); + fh.Jog = htonl(r->header.Jog); + fh.LeadingEdge = htonl(r->header.LeadingEdge); + fh.ManualFeed = htonl(r->header.ManualFeed); + fh.MediaPosition = htonl(r->header.MediaPosition); + fh.MediaWeight = htonl(r->header.MediaWeight); + fh.NumCopies = htonl(r->header.NumCopies); + fh.Orientation = htonl(r->header.Orientation); + fh.PageSize[0] = htonl(r->header.PageSize[0]); + fh.PageSize[1] = htonl(r->header.PageSize[1]); + fh.Tumble = htonl(r->header.Tumble); + fh.cupsWidth = htonl(r->header.cupsWidth); + fh.cupsHeight = htonl(r->header.cupsHeight); + fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor); + fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel); + fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine); + fh.cupsColorOrder = htonl(r->header.cupsColorOrder); + fh.cupsColorSpace = htonl(r->header.cupsColorSpace); + fh.cupsNumColors = htonl(r->header.cupsNumColors); + fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]); + fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]); + fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]); + fh.cupsInteger[3] = htonl((unsigned)(r->header.cupsImagingBBox[0] * + r->header.HWResolution[0])); + fh.cupsInteger[4] = htonl((unsigned)(r->header.cupsImagingBBox[1] * + r->header.HWResolution[1])); + fh.cupsInteger[5] = htonl((unsigned)(r->header.cupsImagingBBox[2] * + r->header.HWResolution[0])); + fh.cupsInteger[6] = htonl((unsigned)(r->header.cupsImagingBBox[3] * + r->header.HWResolution[1])); + fh.cupsInteger[7] = htonl(0xffffff); + + return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh)); + } + else + return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header)) + == sizeof(r->header)); +} + + +/* + * 'cupsRasterWritePixels()' - Write raster pixels. + * + * For best performance, filters should write one or more whole lines. + * The "cupsBytesPerLine" value from the page header can be used to allocate + * the line buffer and as the number of bytes to write. + */ + +unsigned /* O - Number of bytes written */ +cupsRasterWritePixels(cups_raster_t *r, /* I - Raster stream */ + unsigned char *p, /* I - Bytes to write */ + unsigned len)/* I - Number of bytes to write */ +{ + int bytes; /* Bytes read */ + unsigned remaining; /* Bytes remaining */ + + + DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n", + r, p, len, r->remaining)); + + if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0) + return (0); + + if (!r->compressed) + { + /* + * Without compression, just write the raster data raw unless the data needs + * to be swapped... + */ + + r->remaining -= len / r->header.cupsBytesPerLine; + + if (r->swapped && + (r->header.cupsBitsPerColor == 16 || + r->header.cupsBitsPerPixel == 12 || + r->header.cupsBitsPerPixel == 16)) + { + unsigned char *bufptr; /* Pointer into write buffer */ + unsigned count; /* Remaining count */ + + /* + * Allocate a write buffer as needed... + */ + + if ((size_t)len > r->bufsize) + { + if (r->buffer) + bufptr = realloc(r->buffer, len); + else + bufptr = malloc(len); + + if (!bufptr) + return (0); + + r->buffer = bufptr; + r->bufsize = len; + } + + /* + * Byte swap the pixels... + */ + + for (bufptr = r->buffer, count = len; count > 1; count -= 2, bufptr += 2) + { + bufptr[1] = *p++; + bufptr[0] = *p++; + } + + if (count) /* This should never happen... */ + *bufptr = *p; + + /* + * Write the byte-swapped buffer... + */ + + return (cups_raster_io(r, r->buffer, len)); + } + else + return (cups_raster_io(r, p, len)); + } + + /* + * Otherwise, compress each line... + */ + + for (remaining = len; remaining > 0; remaining -= bytes, p += bytes) + { + /* + * Figure out the number of remaining bytes on the current line... + */ + + if ((bytes = remaining) > (int)(r->pend - r->pcurrent)) + bytes = (int)(r->pend - r->pcurrent); + + if (r->count > 0) + { + /* + * Check to see if this line is the same as the previous line... + */ + + if (memcmp(p, r->pcurrent, bytes)) + { + if (!cups_raster_write(r, r->pixels)) + return (0); + + r->count = 0; + } + else + { + /* + * Mark more bytes as the same... + */ + + r->pcurrent += bytes; + + if (r->pcurrent >= r->pend) + { + /* + * Increase the repeat count... + */ + + r->count ++; + r->pcurrent = r->pixels; + + /* + * Flush out this line if it is the last one... + */ + + r->remaining --; + + if (r->remaining == 0) + return (cups_raster_write(r, r->pixels)); + else if (r->count == 256) + { + if (cups_raster_write(r, r->pixels) == 0) + return (0); + + r->count = 0; + } + } + + continue; + } + } + + if (r->count == 0) + { + /* + * Copy the raster data to the buffer... + */ + + memcpy(r->pcurrent, p, bytes); + + r->pcurrent += bytes; + + if (r->pcurrent >= r->pend) + { + /* + * Increase the repeat count... + */ + + r->count ++; + r->pcurrent = r->pixels; + + /* + * Flush out this line if it is the last one... + */ + + r->remaining --; + + if (r->remaining == 0) + return (cups_raster_write(r, r->pixels)); + } + } + } + + return (len); +} + + +/* + * 'cups_raster_read_header()' - Read a raster page header. + */ + +static unsigned /* O - 1 on success, 0 on fail */ +cups_raster_read_header( + cups_raster_t *r) /* I - Raster stream */ +{ + int len; /* Length for read/swap */ + + + if (r == NULL || r->mode != CUPS_RASTER_READ) + return (0); + + /* + * Get the length of the raster header... + */ + + if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1) + len = sizeof(cups_page_header_t); + else + len = sizeof(cups_page_header2_t); + + /* + * Read the header... + */ + + memset(&(r->header), 0, sizeof(r->header)); + + if (cups_raster_read(r, (unsigned char *)&(r->header), len) < len) + return (0); + + /* + * Swap bytes as needed... + */ + + if (r->swapped) + { + unsigned *s, /* Current word */ + temp; /* Temporary copy */ + + + DEBUG_puts("Swapping header bytes..."); + + for (len = 81, s = &(r->header.AdvanceDistance); + len > 0; + len --, s ++) + { + DEBUG_printf(("%08x =>", *s)); + + temp = *s; + *s = ((temp & 0xff) << 24) | + ((temp & 0xff00) << 8) | + ((temp & 0xff0000) >> 8) | + ((temp & 0xff000000) >> 24); + + DEBUG_printf((" %08x\n", *s)); + } + } + + /* + * Update the header and row count... + */ + + cups_raster_update(r); + + return (r->header.cupsBytesPerLine != 0 && r->header.cupsHeight != 0); +} + + +/* + * 'cups_raster_io()' - Read/write bytes from a context, handling interruptions. + */ + +static int /* O - Bytes read or -1 */ +cups_raster_io(cups_raster_t *r, /* I - Raster stream */ + unsigned char *buf, /* I - Buffer for read/write */ + int bytes) /* I - Number of bytes to read/write */ +{ + ssize_t count; /* Number of bytes read/written */ + size_t total; /* Total bytes read/written */ + + + DEBUG_printf(("4cups_raster_io(r=%p, buf=%p, bytes=%d)", r, buf, bytes)); + + for (total = 0; total < (size_t)bytes; total += count, buf += count) + { + count = (*r->iocb)(r->ctx, buf, bytes - total); + + DEBUG_printf(("5cups_raster_io: count=%d, total=%d", (int)count, + (int)total)); + if (count == 0) + return (0); + else if (count < 0) + return (-1); + } + + return ((int)total); +} + + +/* + * 'cups_raster_read()' - Read through the raster buffer. + */ + +static int /* O - Number of bytes read */ +cups_raster_read(cups_raster_t *r, /* I - Raster stream */ + unsigned char *buf, /* I - Buffer */ + int bytes) /* I - Number of bytes to read */ +{ + int count, /* Number of bytes read */ + remaining, /* Remaining bytes in buffer */ + total; /* Total bytes read */ + + + DEBUG_printf(("cups_raster_read(r=%p, buf=%p, bytes=%d)\n", r, buf, bytes)); + + if (!r->compressed) + return (cups_raster_io(r, buf, bytes)); + + /* + * Allocate a read buffer as needed... + */ + + count = 2 * r->header.cupsBytesPerLine; + + if ((size_t)count > r->bufsize) + { + int offset = (int)(r->bufptr - r->buffer); + /* Offset to current start of buffer */ + int end = (int)(r->bufend - r->buffer); + /* Offset to current end of buffer */ + unsigned char *rptr; /* Pointer in read buffer */ + + if (r->buffer) + rptr = realloc(r->buffer, count); + else + rptr = malloc(count); + + if (!rptr) + return (0); + + r->buffer = rptr; + r->bufptr = rptr + offset; + r->bufend = rptr + end; + r->bufsize = count; + } + + /* + * Loop until we have read everything... + */ + + for (total = 0, remaining = (int)(r->bufend - r->bufptr); + total < bytes; + total += count, buf += count) + { + count = bytes - total; + + DEBUG_printf(("count=%d, remaining=%d, buf=%p, bufptr=%p, bufend=%p...\n", + count, remaining, buf, r->bufptr, r->bufend)); + + if (remaining == 0) + { + if (count < 16) + { + /* + * Read into the raster buffer and then copy... + */ + + remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize); + if (remaining <= 0) + return (0); + + r->bufptr = r->buffer; + r->bufend = r->buffer + remaining; + } + else + { + /* + * Read directly into "buf"... + */ + + count = (*r->iocb)(r->ctx, buf, count); + + if (count <= 0) + return (0); + + continue; + } + } + + /* + * Copy bytes from raster buffer to "buf"... + */ + + if (count > remaining) + count = remaining; + + if (count == 1) + { + /* + * Copy 1 byte... + */ + + *buf = *(r->bufptr)++; + remaining --; + } + else if (count < 128) + { + /* + * Copy up to 127 bytes without using memcpy(); this is + * faster because it avoids an extra function call and is + * often further optimized by the compiler... + */ + + unsigned char *bufptr; /* Temporary buffer pointer */ + + remaining -= count; + + for (bufptr = r->bufptr; count > 0; count --, total ++) + *buf++ = *bufptr++; + + r->bufptr = bufptr; + } + else + { + /* + * Use memcpy() for a large read... + */ + + memcpy(buf, r->bufptr, count); + r->bufptr += count; + remaining -= count; + } + } + + return (total); +} + + +/* + * 'cups_raster_update()' - Update the raster header and row count for the + * current page. + */ + +static void +cups_raster_update(cups_raster_t *r) /* I - Raster stream */ +{ + if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 || + r->header.cupsNumColors == 0) + { + /* + * Set the "cupsNumColors" field according to the colorspace... + */ + + switch (r->header.cupsColorSpace) + { + case CUPS_CSPACE_W : + case CUPS_CSPACE_K : + case CUPS_CSPACE_WHITE : + case CUPS_CSPACE_GOLD : + case CUPS_CSPACE_SILVER : + case CUPS_CSPACE_SW : + r->header.cupsNumColors = 1; + break; + + case CUPS_CSPACE_RGB : + case CUPS_CSPACE_CMY : + case CUPS_CSPACE_YMC : + case CUPS_CSPACE_CIEXYZ : + case CUPS_CSPACE_CIELab : + case CUPS_CSPACE_SRGB : + case CUPS_CSPACE_ADOBERGB : + case CUPS_CSPACE_ICC1 : + case CUPS_CSPACE_ICC2 : + case CUPS_CSPACE_ICC3 : + case CUPS_CSPACE_ICC4 : + case CUPS_CSPACE_ICC5 : + case CUPS_CSPACE_ICC6 : + case CUPS_CSPACE_ICC7 : + case CUPS_CSPACE_ICC8 : + case CUPS_CSPACE_ICC9 : + case CUPS_CSPACE_ICCA : + case CUPS_CSPACE_ICCB : + case CUPS_CSPACE_ICCC : + case CUPS_CSPACE_ICCD : + case CUPS_CSPACE_ICCE : + case CUPS_CSPACE_ICCF : + r->header.cupsNumColors = 3; + break; + + case CUPS_CSPACE_RGBA : + case CUPS_CSPACE_RGBW : + case CUPS_CSPACE_CMYK : + case CUPS_CSPACE_YMCK : + case CUPS_CSPACE_KCMY : + case CUPS_CSPACE_GMCK : + case CUPS_CSPACE_GMCS : + r->header.cupsNumColors = 4; + break; + + case CUPS_CSPACE_KCMYcm : + if (r->header.cupsBitsPerPixel < 8) + r->header.cupsNumColors = 6; + else + r->header.cupsNumColors = 4; + break; + + case CUPS_CSPACE_DEVICE1 : + case CUPS_CSPACE_DEVICE2 : + case CUPS_CSPACE_DEVICE3 : + case CUPS_CSPACE_DEVICE4 : + case CUPS_CSPACE_DEVICE5 : + case CUPS_CSPACE_DEVICE6 : + case CUPS_CSPACE_DEVICE7 : + case CUPS_CSPACE_DEVICE8 : + case CUPS_CSPACE_DEVICE9 : + case CUPS_CSPACE_DEVICEA : + case CUPS_CSPACE_DEVICEB : + case CUPS_CSPACE_DEVICEC : + case CUPS_CSPACE_DEVICED : + case CUPS_CSPACE_DEVICEE : + case CUPS_CSPACE_DEVICEF : + r->header.cupsNumColors = r->header.cupsColorSpace - + CUPS_CSPACE_DEVICE1 + 1; + break; + } + } + + /* + * Set the number of bytes per pixel/color... + */ + + if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED) + r->bpp = (r->header.cupsBitsPerPixel + 7) / 8; + else + r->bpp = (r->header.cupsBitsPerColor + 7) / 8; + + /* + * Set the number of remaining rows... + */ + + if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR) + r->remaining = r->header.cupsHeight * r->header.cupsNumColors; + else + r->remaining = r->header.cupsHeight; + + /* + * Allocate the compression buffer... + */ + + if (r->compressed) + { + if (r->pixels != NULL) + free(r->pixels); + + r->pixels = calloc(r->header.cupsBytesPerLine, 1); + r->pcurrent = r->pixels; + r->pend = r->pixels + r->header.cupsBytesPerLine; + r->count = 0; + } +} + + +/* + * 'cups_raster_write()' - Write a row of compressed raster data... + */ + +static int /* O - Number of bytes written */ +cups_raster_write( + cups_raster_t *r, /* I - Raster stream */ + const unsigned char *pixels) /* I - Pixel data to write */ +{ + const unsigned char *start, /* Start of sequence */ + *ptr, /* Current pointer in sequence */ + *pend, /* End of raster buffer */ + *plast; /* Pointer to last pixel */ + unsigned char *wptr; /* Pointer into write buffer */ + int bpp, /* Bytes per pixel */ + count; /* Count */ + + + DEBUG_printf(("cups_raster_write(r=%p, pixels=%p)\n", r, pixels)); + + /* + * Allocate a write buffer as needed... + */ + + count = r->header.cupsBytesPerLine * 2; + if ((size_t)count > r->bufsize) + { + if (r->buffer) + wptr = realloc(r->buffer, count); + else + wptr = malloc(count); + + if (!wptr) + return (-1); + + r->buffer = wptr; + r->bufsize = count; + } + + /* + * Write the row repeat count... + */ + + bpp = r->bpp; + pend = pixels + r->header.cupsBytesPerLine; + plast = pend - bpp; + wptr = r->buffer; + *wptr++ = r->count - 1; + + /* + * Write using a modified PackBits compression... + */ + + for (ptr = pixels; ptr < pend;) + { + start = ptr; + ptr += bpp; + + if (ptr == pend) + { + /* + * Encode a single pixel at the end... + */ + + *wptr++ = 0; + for (count = bpp; count > 0; count --) + *wptr++ = *start++; + } + else if (!memcmp(start, ptr, bpp)) + { + /* + * Encode a sequence of repeating pixels... + */ + + for (count = 2; count < 128 && ptr < plast; count ++, ptr += bpp) + if (memcmp(ptr, ptr + bpp, bpp)) + break; + + *wptr++ = count - 1; + for (count = bpp; count > 0; count --) + *wptr++ = *ptr++; + } + else + { + /* + * Encode a sequence of non-repeating pixels... + */ + + for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp) + if (!memcmp(ptr, ptr + bpp, bpp)) + break; + + if (ptr >= plast && count < 128) + { + count ++; + ptr += bpp; + } + + *wptr++ = 257 - count; + + count *= bpp; + memcpy(wptr, start, count); + wptr += count; + } + } + + return (cups_raster_io(r, r->buffer, (int)(wptr - r->buffer))); +} + + +/* + * 'cups_read_fd()' - Read bytes from a file. + */ + +static ssize_t /* O - Bytes read or -1 */ +cups_read_fd(void *ctx, /* I - File descriptor as pointer */ + unsigned char *buf, /* I - Buffer for read */ + size_t bytes) /* I - Maximum number of bytes to read */ +{ + int fd = (int)((intptr_t)ctx); + /* File descriptor */ + ssize_t count; /* Number of bytes read */ + + +#ifdef WIN32 /* Sigh */ + while ((count = read(fd, buf, (unsigned)bytes)) < 0) +#else + while ((count = read(fd, buf, bytes)) < 0) +#endif /* WIN32 */ + if (errno != EINTR && errno != EAGAIN) + return (-1); + + return (count); +} + + +/* + * 'cups_swap()' - Swap bytes in raster data... + */ + +static void +cups_swap(unsigned char *buf, /* I - Buffer to swap */ + int bytes) /* I - Number of bytes to swap */ +{ + unsigned char even, odd; /* Temporary variables */ + + + bytes /= 2; + + while (bytes > 0) + { + even = buf[0]; + odd = buf[1]; + buf[0] = odd; + buf[1] = even; + + buf += 2; + bytes --; + } +} + + +/* + * 'cups_write_fd()' - Write bytes to a file. + */ + +static ssize_t /* O - Bytes written or -1 */ +cups_write_fd(void *ctx, /* I - File descriptor pointer */ + unsigned char *buf, /* I - Bytes to write */ + size_t bytes) /* I - Number of bytes to write */ +{ + int fd = (int)((intptr_t)ctx); + /* File descriptor */ + ssize_t count; /* Number of bytes written */ + + +#ifdef WIN32 /* Sigh */ + while ((count = write(fd, buf, (unsigned)bytes)) < 0) +#else + while ((count = write(fd, buf, bytes)) < 0) +#endif /* WIN32 */ + if (errno != EINTR && errno != EAGAIN) + return (-1); + + return (count); +} + + +/* + * End of "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $". + */ diff --git a/filter/rasterbench.c b/filter/rasterbench.c new file mode 100644 index 0000000..0e3c47a --- /dev/null +++ b/filter/rasterbench.c @@ -0,0 +1,355 @@ +/* + * "$Id: rasterbench.c 7376 2008-03-19 21:07:45Z mike $" + * + * Raster benchmark program for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2006 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. + * + * Contents: + * + * main() - Benchmark the raster read/write functions. + * compute_median() - Compute the median time for a test. + * read_test() - Benchmark the raster read functions. + * write_test() - Benchmark the raster write functions. + */ + +/* + * Include necessary headers... + */ + +#include +#include +#include +#include +#include +#include +#include + + +/* + * Constants... + */ + +#define TEST_WIDTH 1024 +#define TEST_HEIGHT 1024 +#define TEST_PAGES 16 +#define TEST_PASSES 20 + + +/* + * Local functions... + */ + +static double compute_median(double *secs); +static double get_time(void); +static void read_test(int fd); +static int run_read_test(void); +static void write_test(int fd, cups_mode_t mode); + + +/* + * 'main()' - Benchmark the raster read/write functions. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line args */ + char *argv[]) /* I - Command-line arguments */ +{ + int i; /* Looping var */ + int ras_fd, /* File descriptor for read process */ + status; /* Exit status of read process */ + double start_secs, /* Start time */ + write_secs, /* Write time */ + read_secs, /* Read time */ + pass_secs[TEST_PASSES]; /* Total test times */ + cups_mode_t mode; /* Write mode */ + + + /* + * See if we have anything on the command-line... + */ + + if (argc > 2 || (argc == 2 && strcmp(argv[1], "-z"))) + { + puts("Usage: rasterbench [-z]"); + return (1); + } + + mode = argc > 1 ? CUPS_RASTER_WRITE_COMPRESSED : CUPS_RASTER_WRITE; + + /* + * Ignore SIGPIPE... + */ + + signal(SIGPIPE, SIG_IGN); + + /* + * Run the tests several times to get a good average... + */ + + printf("Test read/write speed of %d pages, %dx%d pixels...\n\n", + TEST_PAGES, TEST_WIDTH, TEST_HEIGHT); + for (i = 0; i < TEST_PASSES; i ++) + { + printf("PASS %2d: ", i + 1); + fflush(stdout); + + ras_fd = run_read_test(); + start_secs = get_time(); + + write_test(ras_fd, mode); + + write_secs = get_time(); + printf(" %.3f write,", write_secs - start_secs); + fflush(stdout); + + close(ras_fd); + wait(&status); + + read_secs = get_time(); + pass_secs[i] = read_secs - start_secs; + printf(" %.3f read, %.3f total\n", read_secs - write_secs, pass_secs[i]); + } + + printf("\nMedian Total Time: %.3f seconds per document\n", + compute_median(pass_secs)); + + return (0); +} + + +/* + * 'compute_median()' - Compute the median time for a test. + */ + +static double /* O - Median time in seconds */ +compute_median(double *secs) /* I - Array of time samples */ +{ + int i, j; /* Looping vars */ + double temp; /* Swap variable */ + + + /* + * Sort the array into ascending order using a quicky bubble sort... + */ + + for (i = 0; i < (TEST_PASSES - 1); i ++) + for (j = i + 1; j < TEST_PASSES; j ++) + if (secs[i] > secs[j]) + { + temp = secs[i]; + secs[i] = secs[j]; + secs[j] = temp; + } + + /* + * Return the average of the middle two samples... + */ + + return (0.5 * (secs[TEST_PASSES / 2 - 1] + secs[TEST_PASSES / 2])); +} + + +/* + * 'get_time()' - Get the current time in seconds. + */ + +static double /* O - Time in seconds */ +get_time(void) +{ + struct timeval curtime; /* Current time */ + + + gettimeofday(&curtime, NULL); + return (curtime.tv_sec + 0.000001 * curtime.tv_usec); +} + + +/* + * 'read_test()' - Benchmark the raster read functions. + */ + +static void +read_test(int fd) /* I - File descriptor to read from */ +{ + int y; /* Looping var */ + cups_raster_t *r; /* Raster stream */ + cups_page_header2_t header; /* Page header */ + unsigned char buffer[8 * TEST_WIDTH]; + /* Read buffer */ + + + /* + * Test read speed... + */ + + if ((r = cupsRasterOpen(fd, CUPS_RASTER_READ)) == NULL) + { + perror("Unable to create raster input stream"); + return; + } + + while (cupsRasterReadHeader2(r, &header)) + { + for (y = 0; y < header.cupsHeight; y ++) + cupsRasterReadPixels(r, buffer, header.cupsBytesPerLine); + } + + cupsRasterClose(r); +} + + +/* + * 'run_read_test()' - Run the read test as a child process via pipes. + */ + +static int /* O - Standard input of child */ +run_read_test(void) +{ + int ras_pipes[2]; /* Raster data pipes */ + int pid; /* Child process ID */ + + + if (pipe(ras_pipes)) + return (-1); + + if ((pid = fork()) < 0) + { + /* + * Fork error - return -1 on error... + */ + + close(ras_pipes[0]); + close(ras_pipes[1]); + + return (-1); + } + else if (pid == 0) + { + /* + * Child comes here - read data from the input pipe... + */ + + close(ras_pipes[1]); + read_test(ras_pipes[0]); + exit(0); + } + else + { + /* + * Parent comes here - return the output pipe... + */ + + close(ras_pipes[0]); + return (ras_pipes[1]); + } +} + + +/* + * 'write_test()' - Benchmark the raster write functions. + */ + +static void +write_test(int fd, /* I - File descriptor to write to */ + cups_mode_t mode) /* I - Write mode */ +{ + int page, x, y; /* Looping vars */ + int count; /* Number of bytes to set */ + cups_raster_t *r; /* Raster stream */ + cups_page_header2_t header; /* Page header */ + unsigned char data[32][8 * TEST_WIDTH]; + /* Raster data to write */ + + + /* + * Create a combination of random data and repeated data to simulate + * text with some whitespace. + */ + + CUPS_SRAND(time(NULL)); + + memset(data, 0, sizeof(data)); + + for (y = 0; y < 28; y ++) + { + for (x = CUPS_RAND() & 127, count = (CUPS_RAND() & 15) + 1; + x < sizeof(data[0]); + x ++, count --) + { + if (count <= 0) + { + x += (CUPS_RAND() & 15) + 1; + count = (CUPS_RAND() & 15) + 1; + + if (x >= sizeof(data[0])) + break; + } + + data[y][x] = CUPS_RAND(); + } + } + + /* + * Test write speed... + */ + + if ((r = cupsRasterOpen(fd, mode)) == NULL) + { + perror("Unable to create raster output stream"); + return; + } + + for (page = 0; page < TEST_PAGES; page ++) + { + memset(&header, 0, sizeof(header)); + header.cupsWidth = TEST_WIDTH; + header.cupsHeight = TEST_HEIGHT; + header.cupsBytesPerLine = TEST_WIDTH; + + if (page & 1) + { + header.cupsBytesPerLine *= 4; + header.cupsColorSpace = CUPS_CSPACE_CMYK; + header.cupsColorOrder = CUPS_ORDER_CHUNKED; + } + else + { + header.cupsColorSpace = CUPS_CSPACE_K; + header.cupsColorOrder = CUPS_ORDER_BANDED; + } + + if (page & 2) + { + header.cupsBytesPerLine *= 2; + header.cupsBitsPerColor = 16; + header.cupsBitsPerPixel = (page & 1) ? 64 : 16; + } + else + { + header.cupsBitsPerColor = 8; + header.cupsBitsPerPixel = (page & 1) ? 32 : 8; + } + + cupsRasterWriteHeader2(r, &header); + + for (y = 0; y < TEST_HEIGHT; y ++) + cupsRasterWritePixels(r, data[y & 31], header.cupsBytesPerLine); + } + + cupsRasterClose(r); +} + + +/* + * End of "$Id: rasterbench.c 7376 2008-03-19 21:07:45Z mike $". + */ diff --git a/filter/rastertoepson.c b/filter/rastertoepson.c new file mode 100644 index 0000000..08135bc --- /dev/null +++ b/filter/rastertoepson.c @@ -0,0 +1,1157 @@ +/* + * "$Id: rastertoepson.c 7450 2008-04-14 19:39:02Z mike $" + * + * EPSON ESC/P and ESC/P2 filter for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1993-2007 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. + * + * Contents: + * + * Setup() - Prepare the printer for printing. + * StartPage() - Start a page of graphics. + * EndPage() - Finish a page of graphics. + * Shutdown() - Shutdown the printer. + * CompressData() - Compress a line of graphics. + * OutputLine() - Output a line of graphics. + * main() - Main entry and processing of driver. + */ + +/* + * Include necessary headers... + */ + +#include +#include +#include +#include +#include +#include +#include +#include + + +/* + * Model numbers... + */ + +#define EPSON_9PIN 0 +#define EPSON_24PIN 1 +#define EPSON_COLOR 2 +#define EPSON_PHOTO 3 +#define EPSON_ICOLOR 4 +#define EPSON_IPHOTO 5 + + +/* + * Macros... + */ + +#define pwrite(s,n) fwrite((s), 1, (n), stdout) + + +/* + * Globals... + */ + +unsigned char *Planes[6], /* Output buffers */ + *CompBuffer, /* Compression buffer */ + *LineBuffers[2]; /* Line bitmap buffers */ +int Model, /* Model number */ + NumPlanes, /* Number of color planes */ + Feed, /* Number of lines to skip */ + EjectPage; /* Eject the page when done? */ +int DotBit, /* Bit in buffers */ + DotBytes, /* # bytes in a dot column */ + DotColumns, /* # columns in 1/60 inch */ + LineCount, /* # of lines processed */ + EvenOffset, /* Offset into 'even' buffers */ + OddOffset, /* Offset into 'odd' buffers */ + Shingling, /* Shingle output? */ + Canceled; /* Has the current job been canceled? */ + + +/* + * Prototypes... + */ + +void Setup(void); +void StartPage(const ppd_file_t *ppd, const cups_page_header2_t *header); +void EndPage(const cups_page_header2_t *header); +void Shutdown(void); + +void CancelJob(int sig); +void CompressData(const unsigned char *line, int length, int plane, + int type, int xstep, int ystep); +void OutputLine(const cups_page_header2_t *header); +void OutputRows(const cups_page_header2_t *header, int row); + + +/* + * 'Setup()' - Prepare the printer for printing. + */ + +void +Setup(void) +{ + const char *device_uri; /* The device for the printer... */ + + + /* + * EPSON USB printers need an additional command issued at the + * beginning of each job to exit from "packet" mode... + */ + + if ((device_uri = getenv("DEVICE_URI")) != NULL && + strncmp(device_uri, "usb:", 4) == 0 && Model >= EPSON_ICOLOR) + pwrite("\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@", 29); +} + + +/* + * 'StartPage()' - Start a page of graphics. + */ + +void +StartPage( + const ppd_file_t *ppd, /* I - PPD file */ + const cups_page_header2_t *header) /* I - Page header */ +{ + int n, t; /* Numbers */ + int plane; /* Looping var */ + + + /* + * Send a reset sequence. + */ + + if (ppd && ppd->nickname && strstr(ppd->nickname, "OKIDATA") != NULL) + printf("\033{A"); /* Set EPSON emulation mode */ + + printf("\033@"); + + /* + * See which type of printer we are using... + */ + + switch (Model) + { + case EPSON_9PIN : + case EPSON_24PIN : + printf("\033P\022"); /* Set 10 CPI */ + + if (header->HWResolution[0] == 360 || header->HWResolution[0] == 240) + { + printf("\033x1"); /* LQ printing */ + printf("\033U1"); /* Unidirectional */ + } + else + { + printf("\033x0"); /* Draft printing */ + printf("\033U0"); /* Bidirectional */ + } + + printf("\033l%c\033Q%c", 0, /* Side margins */ + (int)(10.0 * header->PageSize[0] / 72.0 + 0.5)); + printf("\033\062\033C%c", /* Page length in 1/6th inches */ + (int)(header->PageSize[1] / 12.0 + 0.5)); + printf("\033N%c", 0); /* Bottom margin */ + printf("\033O"); /* No perforation skip */ + + /* + * Setup various buffer limits... + */ + + DotBytes = header->cupsRowCount / 8; + DotColumns = header->HWResolution[0] / 60; + Shingling = 0; + + if (Model == EPSON_9PIN) + printf("\033\063\030"); /* Set line feed */ + else + switch (header->HWResolution[0]) + { + case 60: + case 120 : + case 240 : + printf("\033\063\030"); /* Set line feed */ + break; + + case 180 : + case 360 : + Shingling = 1; + + if (header->HWResolution[1] == 180) + printf("\033\063\010");/* Set line feed */ + else + printf("\033+\010"); /* Set line feed */ + break; + } + break; + + default : + /* + * Set graphics mode... + */ + + pwrite("\033(G\001\000\001", 6); /* Graphics mode */ + + /* + * Set the media size... + */ + + if (Model < EPSON_ICOLOR) + { + pwrite("\033(U\001\000", 5); /* Resolution/units */ + putchar(3600 / header->HWResolution[1]); + } + else + { + pwrite("\033(U\005\000", 5); + putchar(1440 / header->HWResolution[1]); + putchar(1440 / header->HWResolution[1]); + putchar(1440 / header->HWResolution[0]); + putchar(0xa0); /* n/1440ths... */ + putchar(0x05); + } + + n = header->PageSize[1] * header->HWResolution[1] / 72.0; + + pwrite("\033(C\002\000", 5); /* Page length */ + putchar(n); + putchar(n >> 8); + + if (ppd) + t = (ppd->sizes[1].length - ppd->sizes[1].top) * + header->HWResolution[1] / 72.0; + else + t = 0; + + pwrite("\033(c\004\000", 5); /* Top & bottom margins */ + putchar(t); + putchar(t >> 8); + putchar(n); + putchar(n >> 8); + + if (header->HWResolution[1] == 720) + { + pwrite("\033(i\001\000\001", 6); /* Microweave */ + pwrite("\033(e\002\000\000\001", 7); /* Small dots */ + } + + pwrite("\033(V\002\000\000\000", 7); /* Set absolute position 0 */ + + DotBytes = 0; + DotColumns = 0; + Shingling = 0; + break; + } + + /* + * Set other stuff... + */ + + if (header->cupsColorSpace == CUPS_CSPACE_CMY) + NumPlanes = 3; + else if (header->cupsColorSpace == CUPS_CSPACE_KCMY) + NumPlanes = 4; + else if (header->cupsColorSpace == CUPS_CSPACE_KCMYcm) + NumPlanes = 6; + else + NumPlanes = 1; + + Feed = 0; /* No blank lines yet */ + + /* + * Allocate memory for a line/row of graphics... + */ + + if ((Planes[0] = malloc(header->cupsBytesPerLine)) == NULL) + { + fputs("ERROR: Unable to allocate memory\n", stderr); + exit(1); + } + + for (plane = 1; plane < NumPlanes; plane ++) + Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes; + + if (header->cupsCompression || DotBytes) + { + if ((CompBuffer = calloc(2, header->cupsWidth)) == NULL) + { + fputs("ERROR: Unable to allocate memory\n", stderr); + exit(1); + } + } + else + CompBuffer = NULL; + + if (DotBytes) + { + if ((LineBuffers[0] = calloc(DotBytes, + header->cupsWidth * (Shingling + 1))) == NULL) + { + fputs("ERROR: Unable to allocate memory\n", stderr); + exit(1); + } + + LineBuffers[1] = LineBuffers[0] + DotBytes * header->cupsWidth; + DotBit = 128; + LineCount = 0; + EvenOffset = 0; + OddOffset = 0; + } +} + + +/* + * 'EndPage()' - Finish a page of graphics. + */ + +void +EndPage( + const cups_page_header2_t *header) /* I - Page header */ +{ + if (DotBytes && header) + { + /* + * Flush remaining graphics as needed... + */ + + if (!Shingling) + { + if (DotBit < 128 || EvenOffset) + OutputRows(header, 0); + } + else if (OddOffset > EvenOffset) + { + OutputRows(header, 1); + OutputRows(header, 0); + } + else + { + OutputRows(header, 0); + OutputRows(header, 1); + } + } + + /* + * Eject the current page... + */ + + putchar(12); /* Form feed */ + fflush(stdout); + + /* + * Free memory... + */ + + free(Planes[0]); + + if (CompBuffer) + free(CompBuffer); + + if (DotBytes) + free(LineBuffers[0]); +} + + +/* + * 'Shutdown()' - Shutdown the printer. + */ + +void +Shutdown(void) +{ + /* + * Send a reset sequence. + */ + + printf("\033@"); +} + + +/* + * 'CancelJob()' - Cancel the current job... + */ + +void +CancelJob(int sig) /* I - Signal */ +{ + (void)sig; + + Canceled = 1; +} + + +/* + * 'CompressData()' - Compress a line of graphics. + */ + +void +CompressData(const unsigned char *line, /* I - Data to compress */ + int length,/* I - Number of bytes */ + int plane, /* I - Color plane */ + int type, /* I - Type of compression */ + int xstep, /* I - X resolution */ + int ystep) /* I - Y resolution */ +{ + const unsigned char *line_ptr, /* Current byte pointer */ + *line_end, /* End-of-line byte pointer */ + *start; /* Start of compression sequence */ + unsigned char *comp_ptr, /* Pointer into compression buffer */ + temp; /* Current byte */ + int count; /* Count of bytes for output */ + static int ctable[6] = { 0, 2, 1, 4, 18, 17 }; + /* KCMYcm color values */ + + + /* + * Setup pointers... + */ + + line_ptr = line; + line_end = line + length; + + /* + * Do depletion for 720 DPI printing... + */ + + if (ystep == 5) + { + for (comp_ptr = (unsigned char *)line; comp_ptr < line_end;) + { + /* + * Grab the current byte... + */ + + temp = *comp_ptr; + + /* + * Check adjacent bits... + */ + + if ((temp & 0xc0) == 0xc0) + temp &= 0xbf; + if ((temp & 0x60) == 0x60) + temp &= 0xdf; + if ((temp & 0x30) == 0x30) + temp &= 0xef; + if ((temp & 0x18) == 0x18) + temp &= 0xf7; + if ((temp & 0x0c) == 0x0c) + temp &= 0xfb; + if ((temp & 0x06) == 0x06) + temp &= 0xfd; + if ((temp & 0x03) == 0x03) + temp &= 0xfe; + + *comp_ptr++ = temp; + + /* + * Check the last bit in the current byte and the first bit in the + * next byte... + */ + + if ((temp & 0x01) && comp_ptr < line_end && *comp_ptr & 0x80) + *comp_ptr &= 0x7f; + } + } + + switch (type) + { + case 0 : + /* + * Do no compression... + */ + break; + + case 1 : + /* + * Do TIFF pack-bits encoding... + */ + + comp_ptr = CompBuffer; + + while (line_ptr < line_end) + { + if ((line_ptr + 1) >= line_end) + { + /* + * Single byte on the end... + */ + + *comp_ptr++ = 0x00; + *comp_ptr++ = *line_ptr++; + } + else if (line_ptr[0] == line_ptr[1]) + { + /* + * Repeated sequence... + */ + + line_ptr ++; + count = 2; + + while (line_ptr < (line_end - 1) && + line_ptr[0] == line_ptr[1] && + count < 127) + { + line_ptr ++; + count ++; + } + + *comp_ptr++ = 257 - count; + *comp_ptr++ = *line_ptr++; + } + else + { + /* + * Non-repeated sequence... + */ + + start = line_ptr; + line_ptr ++; + count = 1; + + while (line_ptr < (line_end - 1) && + line_ptr[0] != line_ptr[1] && + count < 127) + { + line_ptr ++; + count ++; + } + + *comp_ptr++ = count - 1; + + memcpy(comp_ptr, start, count); + comp_ptr += count; + } + } + + line_ptr = CompBuffer; + line_end = comp_ptr; + break; + } + + putchar(0x0d); /* Move print head to left margin */ + + if (Model < EPSON_ICOLOR) + { + /* + * Do graphics the "old" way... + */ + + if (NumPlanes > 1) + { + /* + * Set the color... + */ + + if (plane > 3) + printf("\033(r%c%c%c%c", 2, 0, 1, ctable[plane] & 15); + /* Set extended color */ + else if (NumPlanes == 3) + printf("\033r%c", ctable[plane + 1]); + /* Set color */ + else + printf("\033r%c", ctable[plane]); /* Set color */ + } + + /* + * Send a raster plane... + */ + + length *= 8; + printf("\033."); /* Raster graphics */ + putchar(type); + putchar(ystep); + putchar(xstep); + putchar(1); + putchar(length); + putchar(length >> 8); + } + else + { + /* + * Do graphics the "new" way... + */ + + printf("\033i"); + putchar(ctable[plane]); + putchar(type); + putchar(1); + putchar(length & 255); + putchar(length >> 8); + putchar(1); + putchar(0); + } + + pwrite(line_ptr, line_end - line_ptr); + fflush(stdout); +} + + +/* + * 'OutputLine()' - Output a line of graphics. + */ + +void +OutputLine( + const cups_page_header2_t *header) /* I - Page header */ +{ + if (header->cupsRowCount) + { + int width; + unsigned char *tempptr, + *evenptr, + *oddptr; + register int x; + unsigned char bit; + const unsigned char *pixel; + unsigned char *temp; + + + /* + * Collect bitmap data in the line buffers and write after each buffer. + */ + + for (x = header->cupsWidth, bit = 128, pixel = Planes[0], + temp = CompBuffer; + x > 0; + x --, temp ++) + { + if (*pixel & bit) + *temp |= DotBit; + + if (bit > 1) + bit >>= 1; + else + { + bit = 128; + pixel ++; + } + } + + if (DotBit > 1) + DotBit >>= 1; + else + { + /* + * Copy the holding buffer to the output buffer, shingling as necessary... + */ + + if (Shingling && LineCount != 0) + { + /* + * Shingle the output... + */ + + if (LineCount & 1) + { + evenptr = LineBuffers[1] + OddOffset; + oddptr = LineBuffers[0] + EvenOffset + DotBytes; + } + else + { + evenptr = LineBuffers[0] + EvenOffset; + oddptr = LineBuffers[1] + OddOffset + DotBytes; + } + + for (width = header->cupsWidth, tempptr = CompBuffer; + width > 0; + width -= 2, tempptr += 2, oddptr += DotBytes * 2, + evenptr += DotBytes * 2) + { + evenptr[0] = tempptr[0]; + oddptr[0] = tempptr[1]; + } + } + else + { + /* + * Don't shingle the output... + */ + + for (width = header->cupsWidth, tempptr = CompBuffer, + evenptr = LineBuffers[0] + EvenOffset; + width > 0; + width --, tempptr ++, evenptr += DotBytes) + *evenptr = tempptr[0]; + } + + if (Shingling && LineCount != 0) + { + EvenOffset ++; + OddOffset ++; + + if (EvenOffset == DotBytes) + { + EvenOffset = 0; + OutputRows(header, 0); + } + + if (OddOffset == DotBytes) + { + OddOffset = 0; + OutputRows(header, 1); + } + } + else + { + EvenOffset ++; + + if (EvenOffset == DotBytes) + { + EvenOffset = 0; + OutputRows(header, 0); + } + } + + DotBit = 128; + LineCount ++; + + memset(CompBuffer, 0, header->cupsWidth); + } + } + else + { + int plane; /* Current plane */ + int bytes; /* Bytes per plane */ + int xstep, ystep; /* X & Y resolutions */ + + + /* + * Write a single line of bitmap data as needed... + */ + + xstep = 3600 / header->HWResolution[0]; + ystep = 3600 / header->HWResolution[1]; + bytes = header->cupsBytesPerLine / NumPlanes; + + for (plane = 0; plane < NumPlanes; plane ++) + { + /* + * Skip blank data... + */ + + if (!Planes[plane][0] && + memcmp(Planes[plane], Planes[plane] + 1, bytes - 1) == 0) + continue; + + /* + * Output whitespace as needed... + */ + + if (Feed > 0) + { + pwrite("\033(v\002\000", 5); /* Relative vertical position */ + putchar(Feed); + putchar(Feed >> 8); + + Feed = 0; + } + + CompressData(Planes[plane], bytes, plane, header->cupsCompression, xstep, + ystep); + } + + Feed ++; + } +} + + +/* + * 'OutputRows()' - Output 8, 24, or 48 rows. + */ + +void +OutputRows( + const cups_page_header2_t *header, /* I - Page image header */ + int row) /* I - Row number (0 or 1) */ +{ + unsigned i, n; /* Looping vars */ + int dot_count, /* Number of bytes to print */ + dot_min; /* Minimum number of bytes */ + unsigned char *dot_ptr, /* Pointer to print data */ + *ptr; /* Current data */ + + + dot_min = DotBytes * DotColumns; + + if (LineBuffers[row][0] != 0 || + memcmp(LineBuffers[row], LineBuffers[row] + 1, + header->cupsWidth * DotBytes - 1)) + { + /* + * Skip leading space... + */ + + i = 0; + dot_count = header->cupsWidth * DotBytes; + dot_ptr = LineBuffers[row]; + + while (dot_count >= dot_min && dot_ptr[0] == 0 && + memcmp(dot_ptr, dot_ptr + 1, dot_min - 1) == 0) + { + i ++; + dot_ptr += dot_min; + dot_count -= dot_min; + } + + /* + * Skip trailing space... + */ + + while (dot_count >= dot_min && dot_ptr[dot_count - dot_min] == 0 && + memcmp(dot_ptr + dot_count - dot_min, + dot_ptr + dot_count - dot_min + 1, dot_min - 1) == 0) + dot_count -= dot_min; + + /* + * Position print head for printing... + */ + + if (i == 0) + putchar('\r'); + else + { + putchar(0x1b); + putchar('$'); + putchar(i & 255); + putchar(i >> 8); + } + + /* + * Start bitmap graphics for this line... + */ + + printf("\033*"); /* Select bit image */ + switch (header->HWResolution[0]) + { + case 60 : /* 60x60/72 DPI gfx */ + putchar(0); + break; + case 120 : /* 120x60/72 DPI gfx */ + putchar(1); + break; + case 180 : /* 180 DPI gfx */ + putchar(39); + break; + case 240 : /* 240x72 DPI gfx */ + putchar(3); + break; + case 360 : /* 360x180/360 DPI gfx */ + if (header->HWResolution[1] == 180) + { + if (Shingling && LineCount != 0) + putchar(40); /* 360x180 fast */ + else + putchar(41); /* 360x180 slow */ + } + else + { + if (Shingling && LineCount != 0) + putchar(72); /* 360x360 fast */ + else + putchar(73); /* 360x360 slow */ + } + break; + } + + n = (unsigned)dot_count / DotBytes; + putchar(n & 255); + putchar(n / 256); + + /* + * Write the graphics data... + */ + + if (header->HWResolution[0] == 120 || + header->HWResolution[0] == 240) + { + /* + * Need to interleave the dots to avoid hosing the print head... + */ + + for (n = dot_count / 2, ptr = dot_ptr; n > 0; n --, ptr += 2) + { + putchar(*ptr); + putchar(0); + } + + /* + * Move the head back and print the odd bytes... + */ + + if (i == 0) + putchar('\r'); + else + { + putchar(0x1b); + putchar('$'); + putchar(i & 255); + putchar(i >> 8); + } + + if (header->HWResolution[0] == 120) + printf("\033*\001"); /* Select bit image */ + else + printf("\033*\003"); /* Select bit image */ + + n = (unsigned)dot_count / DotBytes; + putchar(n & 255); + putchar(n / 256); + + for (n = dot_count / 2, ptr = dot_ptr + 1; n > 0; n --, ptr += 2) + { + putchar(0); + putchar(*ptr); + } + } + else + pwrite(dot_ptr, dot_count); + } + + /* + * Feed the paper... + */ + + putchar('\n'); + + if (Shingling && row == 1) + { + if (header->HWResolution[1] == 360) + printf("\n\n\n\n"); + else + printf("\n"); + } + + fflush(stdout); + + /* + * Clear the buffer... + */ + + memset(LineBuffers[row], 0, header->cupsWidth * DotBytes); +} + + +/* + * 'main()' - Main entry and processing of driver. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int fd; /* File descriptor */ + cups_raster_t *ras; /* Raster stream for printing */ + cups_page_header2_t header; /* Page header from file */ + ppd_file_t *ppd; /* PPD file */ + int page; /* Current page */ + int y; /* Current line */ +#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) + struct sigaction action; /* Actions for POSIX signals */ +#endif /* HAVE_SIGACTION && !HAVE_SIGSET */ + + + /* + * Make sure status messages are not buffered... + */ + + setbuf(stderr, NULL); + + /* + * Check command-line... + */ + + if (argc < 6 || argc > 7) + { + /* + * We don't have the correct number of arguments; write an error message + * and return. + */ + + _cupsLangPrintFilter(stderr, "ERROR", + _("%s job-id user title copies options [file]"), + "rastertoepson"); + return (1); + } + + /* + * Open the page stream... + */ + + if (argc == 7) + { + if ((fd = open(argv[6], O_RDONLY)) == -1) + { + _cupsLangPrintError("ERROR", _("Unable to open raster file")); + sleep(1); + return (1); + } + } + else + fd = 0; + + ras = cupsRasterOpen(fd, CUPS_RASTER_READ); + + /* + * Register a signal handler to eject the current page if the + * job is cancelled. + */ + + Canceled = 0; + +#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ + sigset(SIGTERM, CancelJob); +#elif defined(HAVE_SIGACTION) + memset(&action, 0, sizeof(action)); + + sigemptyset(&action.sa_mask); + action.sa_handler = CancelJob; + sigaction(SIGTERM, &action, NULL); +#else + signal(SIGTERM, CancelJob); +#endif /* HAVE_SIGSET */ + + /* + * Initialize the print device... + */ + + ppd = ppdOpenFile(getenv("PPD")); + if (!ppd) + { + ppd_status_t status; /* PPD error */ + int linenum; /* Line number */ + + _cupsLangPrintFilter(stderr, "ERROR", + _("The PPD file could not be opened.")); + + status = ppdLastError(&linenum); + + fprintf(stderr, "DEBUG: %s on line %d.\n", ppdErrorString(status), linenum); + + return (1); + } + + Model = ppd->model_number; + + Setup(); + + /* + * Process pages as needed... + */ + + page = 0; + + while (cupsRasterReadHeader2(ras, &header)) + { + /* + * Write a status message with the page number and number of copies. + */ + + if (Canceled) + break; + + page ++; + + fprintf(stderr, "PAGE: %d %d\n", page, header.NumCopies); + _cupsLangPrintFilter(stderr, "INFO", _("Starting page %d."), page); + + /* + * Start the page... + */ + + StartPage(ppd, &header); + + /* + * Loop for each line on the page... + */ + + for (y = 0; y < header.cupsHeight; y ++) + { + /* + * Let the user know how far we have progressed... + */ + + if (Canceled) + break; + + if ((y & 127) == 0) + { + _cupsLangPrintFilter(stderr, "INFO", + _("Printing page %d, %d%% complete."), + page, 100 * y / header.cupsHeight); + fprintf(stderr, "ATTR: job-media-progress=%d\n", + 100 * y / header.cupsHeight); + } + + /* + * Read a line of graphics... + */ + + if (cupsRasterReadPixels(ras, Planes[0], header.cupsBytesPerLine) < 1) + break; + + /* + * Write it to the printer... + */ + + OutputLine(&header); + } + + /* + * Eject the page... + */ + + _cupsLangPrintFilter(stderr, "INFO", _("Finished page %d."), page); + + EndPage(&header); + + if (Canceled) + break; + } + + /* + * Shutdown the printer... + */ + + Shutdown(); + + ppdClose(ppd); + + /* + * Close the raster stream... + */ + + cupsRasterClose(ras); + if (fd != 0) + close(fd); + + /* + * If no pages were printed, send an error message... + */ + + if (page == 0) + { + _cupsLangPrintFilter(stderr, "ERROR", _("No pages were found.")); + return (1); + } + else + return (0); +} + + +/* + * End of "$Id: rastertoepson.c 7450 2008-04-14 19:39:02Z mike $". + */ diff --git a/filter/rastertohp.c b/filter/rastertohp.c new file mode 100644 index 0000000..b7390c1 --- /dev/null +++ b/filter/rastertohp.c @@ -0,0 +1,886 @@ +/* + * "$Id: rastertohp.c 7834 2008-08-04 21:02:09Z mike $" + * + * Hewlett-Packard Page Control Language filter for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 1993-2007 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. + * + * Contents: + * + * Setup() - Prepare the printer for printing. + * StartPage() - Start a page of graphics. + * EndPage() - Finish a page of graphics. + * Shutdown() - Shutdown the printer. + * CancelJob() - Cancel the current job... + * CompressData() - Compress a line of graphics. + * OutputLine() - Output a line of graphics. + * main() - Main entry and processing of driver. + */ + +/* + * Include necessary headers... + */ + +#include +#include +#include +#include +#include +#include +#include +#include + + +/* + * Globals... + */ + +unsigned char *Planes[4], /* Output buffers */ + *CompBuffer, /* Compression buffer */ + *BitBuffer; /* Buffer for output bits */ +int NumPlanes, /* Number of color planes */ + ColorBits, /* Number of bits per color */ + Feed, /* Number of lines to skip */ + Duplex, /* Current duplex mode */ + Page, /* Current page number */ + Canceled; /* Has the current job been canceled? */ + + +/* + * Prototypes... + */ + +void Setup(void); +void StartPage(ppd_file_t *ppd, cups_page_header2_t *header); +void EndPage(void); +void Shutdown(void); + +void CancelJob(int sig); +void CompressData(unsigned char *line, int length, int plane, int type); +void OutputLine(cups_page_header2_t *header); + + +/* + * 'Setup()' - Prepare the printer for printing. + */ + +void +Setup(void) +{ + /* + * Send a PCL reset sequence. + */ + + putchar(0x1b); + putchar('E'); +} + + +/* + * 'StartPage()' - Start a page of graphics. + */ + +void +StartPage(ppd_file_t *ppd, /* I - PPD file */ + cups_page_header2_t *header) /* I - Page header */ +{ + int plane; /* Looping var */ + + + /* + * Show page device dictionary... + */ + + fprintf(stderr, "DEBUG: StartPage...\n"); + fprintf(stderr, "DEBUG: MediaClass = \"%s\"\n", header->MediaClass); + fprintf(stderr, "DEBUG: MediaColor = \"%s\"\n", header->MediaColor); + fprintf(stderr, "DEBUG: MediaType = \"%s\"\n", header->MediaType); + fprintf(stderr, "DEBUG: OutputType = \"%s\"\n", header->OutputType); + + fprintf(stderr, "DEBUG: AdvanceDistance = %d\n", header->AdvanceDistance); + fprintf(stderr, "DEBUG: AdvanceMedia = %d\n", header->AdvanceMedia); + fprintf(stderr, "DEBUG: Collate = %d\n", header->Collate); + fprintf(stderr, "DEBUG: CutMedia = %d\n", header->CutMedia); + fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex); + fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0], + header->HWResolution[1]); + fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", + header->ImagingBoundingBox[0], header->ImagingBoundingBox[1], + header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]); + fprintf(stderr, "DEBUG: InsertSheet = %d\n", header->InsertSheet); + fprintf(stderr, "DEBUG: Jog = %d\n", header->Jog); + fprintf(stderr, "DEBUG: LeadingEdge = %d\n", header->LeadingEdge); + fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0], + header->Margins[1]); + fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed); + fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition); + fprintf(stderr, "DEBUG: MediaWeight = %d\n", header->MediaWeight); + fprintf(stderr, "DEBUG: MirrorPrint = %d\n", header->MirrorPrint); + fprintf(stderr, "DEBUG: NegativePrint = %d\n", header->NegativePrint); + fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies); + fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation); + fprintf(stderr, "DEBUG: OutputFaceUp = %d\n", header->OutputFaceUp); + fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0], + header->PageSize[1]); + fprintf(stderr, "DEBUG: Separations = %d\n", header->Separations); + fprintf(stderr, "DEBUG: TraySwitch = %d\n", header->TraySwitch); + fprintf(stderr, "DEBUG: Tumble = %d\n", header->Tumble); + fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth); + fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight); + fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType); + fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor); + fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel); + fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine); + fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder); + fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace); + fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression); + + /* + * Setup printer/job attributes... + */ + + Duplex = header->Duplex; + ColorBits = header->cupsBitsPerColor; + + if ((!Duplex || (Page & 1)) && header->MediaPosition) + printf("\033&l%dH", /* Set media position */ + header->MediaPosition); + + if (Duplex && ppd && ppd->model_number == 2) + { + /* + * Handle duplexing on new DeskJet printers... + */ + + printf("\033&l-2H"); /* Load media */ + + if (Page & 1) + printf("\033&l2S"); /* Set duplex mode */ + } + + if (!Duplex || (Page & 1) || (ppd && ppd->model_number == 2)) + { + /* + * Set the media size... + */ + + printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */ + printf("\033&l0O"); /* Set portrait orientation */ + + switch (header->PageSize[1]) + { + case 540 : /* Monarch Envelope */ + printf("\033&l80A"); /* Set page size */ + break; + + case 595 : /* A5 */ + printf("\033&l25A"); /* Set page size */ + break; + + case 624 : /* DL Envelope */ + printf("\033&l90A"); /* Set page size */ + break; + + case 649 : /* C5 Envelope */ + printf("\033&l91A"); /* Set page size */ + break; + + case 684 : /* COM-10 Envelope */ + printf("\033&l81A"); /* Set page size */ + break; + + case 709 : /* B5 Envelope */ + printf("\033&l100A"); /* Set page size */ + break; + + case 756 : /* Executive */ + printf("\033&l1A"); /* Set page size */ + break; + + case 792 : /* Letter */ + printf("\033&l2A"); /* Set page size */ + break; + + case 842 : /* A4 */ + printf("\033&l26A"); /* Set page size */ + break; + + case 1008 : /* Legal */ + printf("\033&l3A"); /* Set page size */ + break; + + case 1191 : /* A3 */ + printf("\033&l27A"); /* Set page size */ + break; + + case 1224 : /* Tabloid */ + printf("\033&l6A"); /* Set page size */ + break; + } + + printf("\033&l%dP", /* Set page length */ + header->PageSize[1] / 12); + printf("\033&l0E"); /* Set top margin to 0 */ + } + + if (!Duplex || (Page & 1)) + { + /* + * Set other job options... + */ + + printf("\033&l%dX", header->NumCopies); /* Set number copies */ + + if (header->cupsMediaType && + (!ppd || ppd->model_number != 2 || header->HWResolution[0] == 600)) + printf("\033&l%dM", /* Set media type */ + header->cupsMediaType); + + if (!ppd || ppd->model_number != 2) + { + int mode = Duplex ? 1 + header->Tumble != 0 : 0; + + printf("\033&l%dS", mode); /* Set duplex mode */ + printf("\033&l0L"); /* Turn off perforation skip */ + } + } + else if (!ppd || ppd->model_number != 2) + printf("\033&a2G"); /* Set back side */ + + /* + * Set graphics mode... + */ + + if (ppd && ppd->model_number == 2) + { + /* + * Figure out the number of color planes... + */ + + if (header->cupsColorSpace == CUPS_CSPACE_KCMY) + NumPlanes = 4; + else + NumPlanes = 1; + + /* + * Set the resolution and top-of-form... + */ + + printf("\033&u%dD", header->HWResolution[0]); + /* Resolution */ + printf("\033&l0e0L"); /* Reset top and don't skip */ + printf("\033*p0Y\033*p0X"); /* Set top of form */ + + /* + * Send 26-byte configure image data command with horizontal and + * vertical resolutions as well as a color count... + */ + + printf("\033*g26W"); + putchar(2); /* Format 2 */ + putchar(NumPlanes); /* Output planes */ + + putchar(header->HWResolution[0] >> 8); /* Black resolution */ + putchar(header->HWResolution[0]); + putchar(header->HWResolution[1] >> 8); + putchar(header->HWResolution[1]); + putchar(0); + putchar(1 << ColorBits); /* # of black levels */ + + putchar(header->HWResolution[0] >> 8); /* Cyan resolution */ + putchar(header->HWResolution[0]); + putchar(header->HWResolution[1] >> 8); + putchar(header->HWResolution[1]); + putchar(0); + putchar(1 << ColorBits); /* # of cyan levels */ + + putchar(header->HWResolution[0] >> 8); /* Magenta resolution */ + putchar(header->HWResolution[0]); + putchar(header->HWResolution[1] >> 8); + putchar(header->HWResolution[1]); + putchar(0); + putchar(1 << ColorBits); /* # of magenta levels */ + + putchar(header->HWResolution[0] >> 8); /* Yellow resolution */ + putchar(header->HWResolution[0]); + putchar(header->HWResolution[1] >> 8); + putchar(header->HWResolution[1]); + putchar(0); + putchar(1 << ColorBits); /* # of yellow levels */ + + printf("\033&l0H"); /* Set media position */ + } + else + { + printf("\033*t%dR", header->HWResolution[0]); + /* Set resolution */ + + if (header->cupsColorSpace == CUPS_CSPACE_KCMY) + { + NumPlanes = 4; + printf("\033*r-4U"); /* Set KCMY graphics */ + } + else if (header->cupsColorSpace == CUPS_CSPACE_CMY) + { + NumPlanes = 3; + printf("\033*r-3U"); /* Set CMY graphics */ + } + else + NumPlanes = 1; /* Black&white graphics */ + + /* + * Set size and position of graphics... + */ + + printf("\033*r%dS", header->cupsWidth); /* Set width */ + printf("\033*r%dT", header->cupsHeight); /* Set height */ + + printf("\033&a0H"); /* Set horizontal position */ + + if (ppd) + printf("\033&a%.0fV", /* Set vertical position */ + 10.0 * (ppd->sizes[0].length - ppd->sizes[0].top)); + else + printf("\033&a0V"); /* Set top-of-page */ + } + + printf("\033*r1A"); /* Start graphics */ + + if (header->cupsCompression) + printf("\033*b%dM", /* Set compression */ + header->cupsCompression); + + Feed = 0; /* No blank lines yet */ + + /* + * Allocate memory for a line of graphics... + */ + + if ((Planes[0] = malloc(header->cupsBytesPerLine)) == NULL) + { + fputs("ERROR: Unable to allocate memory\n", stderr); + exit(1); + } + + for (plane = 1; plane < NumPlanes; plane ++) + Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes; + + if (ColorBits > 1) + BitBuffer = malloc(ColorBits * ((header->cupsWidth + 7) / 8)); + else + BitBuffer = NULL; + + if (header->cupsCompression) + CompBuffer = malloc(header->cupsBytesPerLine * 2); + else + CompBuffer = NULL; +} + + +/* + * 'EndPage()' - Finish a page of graphics. + */ + +void +EndPage(void) +{ + /* + * Eject the current page... + */ + + if (NumPlanes > 1) + { + printf("\033*rC"); /* End color GFX */ + + if (!(Duplex && (Page & 1))) + printf("\033&l0H"); /* Eject current page */ + } + else + { + printf("\033*r0B"); /* End GFX */ + + if (!(Duplex && (Page & 1))) + printf("\014"); /* Eject current page */ + } + + fflush(stdout); + + /* + * Free memory... + */ + + free(Planes[0]); + + if (BitBuffer) + free(BitBuffer); + + if (CompBuffer) + free(CompBuffer); +} + + +/* + * 'Shutdown()' - Shutdown the printer. + */ + +void +Shutdown(void) +{ + /* + * Send a PCL reset sequence. + */ + + putchar(0x1b); + putchar('E'); +} + + +/* + * 'CancelJob()' - Cancel the current job... + */ + +void +CancelJob(int sig) /* I - Signal */ +{ + (void)sig; + + Canceled = 1; +} + + +/* + * 'CompressData()' - Compress a line of graphics. + */ + +void +CompressData(unsigned char *line, /* I - Data to compress */ + int length, /* I - Number of bytes */ + int plane, /* I - Color plane */ + int type) /* I - Type of compression */ +{ + unsigned char *line_ptr, /* Current byte pointer */ + *line_end, /* End-of-line byte pointer */ + *comp_ptr, /* Pointer into compression buffer */ + *start; /* Start of compression sequence */ + int count; /* Count of bytes for output */ + + + switch (type) + { + default : + /* + * Do no compression... + */ + + line_ptr = line; + line_end = line + length; + break; + + case 1 : + /* + * Do run-length encoding... + */ + + line_end = line + length; + for (line_ptr = line, comp_ptr = CompBuffer; + line_ptr < line_end; + comp_ptr += 2, line_ptr += count) + { + for (count = 1; + (line_ptr + count) < line_end && + line_ptr[0] == line_ptr[count] && + count < 256; + count ++); + + comp_ptr[0] = count - 1; + comp_ptr[1] = line_ptr[0]; + } + + line_ptr = CompBuffer; + line_end = comp_ptr; + break; + + case 2 : + /* + * Do TIFF pack-bits encoding... + */ + + line_ptr = line; + line_end = line + length; + comp_ptr = CompBuffer; + + while (line_ptr < line_end) + { + if ((line_ptr + 1) >= line_end) + { + /* + * Single byte on the end... + */ + + *comp_ptr++ = 0x00; + *comp_ptr++ = *line_ptr++; + } + else if (line_ptr[0] == line_ptr[1]) + { + /* + * Repeated sequence... + */ + + line_ptr ++; + count = 2; + + while (line_ptr < (line_end - 1) && + line_ptr[0] == line_ptr[1] && + count < 127) + { + line_ptr ++; + count ++; + } + + *comp_ptr++ = 257 - count; + *comp_ptr++ = *line_ptr++; + } + else + { + /* + * Non-repeated sequence... + */ + + start = line_ptr; + line_ptr ++; + count = 1; + + while (line_ptr < (line_end - 1) && + line_ptr[0] != line_ptr[1] && + count < 127) + { + line_ptr ++; + count ++; + } + + *comp_ptr++ = count - 1; + + memcpy(comp_ptr, start, count); + comp_ptr += count; + } + } + + line_ptr = CompBuffer; + line_end = comp_ptr; + break; + } + + /* + * Set the length of the data and write a raster plane... + */ + + printf("\033*b%d%c", (int)(line_end - line_ptr), plane); + fwrite(line_ptr, line_end - line_ptr, 1, stdout); +} + + +/* + * 'OutputLine()' - Output a line of graphics. + */ + +void +OutputLine(cups_page_header2_t *header) /* I - Page header */ +{ + int plane, /* Current plane */ + bytes, /* Bytes to write */ + count; /* Bytes to convert */ + unsigned char bit, /* Current plane data */ + bit0, /* Current low bit data */ + bit1, /* Current high bit data */ + *plane_ptr, /* Pointer into Planes */ + *bit_ptr; /* Pointer into BitBuffer */ + + + /* + * Output whitespace as needed... + */ + + if (Feed > 0) + { + printf("\033*b%dY", Feed); + Feed = 0; + } + + /* + * Write bitmap data as needed... + */ + + bytes = (header->cupsWidth + 7) / 8; + + for (plane = 0; plane < NumPlanes; plane ++) + if (ColorBits == 1) + { + /* + * Send bits as-is... + */ + + CompressData(Planes[plane], bytes, plane < (NumPlanes - 1) ? 'V' : 'W', + header->cupsCompression); + } + else + { + /* + * Separate low and high bit data into separate buffers. + */ + + for (count = header->cupsBytesPerLine / NumPlanes, + plane_ptr = Planes[plane], bit_ptr = BitBuffer; + count > 0; + count -= 2, plane_ptr += 2, bit_ptr ++) + { + bit = plane_ptr[0]; + + bit0 = ((bit & 64) << 1) | ((bit & 16) << 2) | ((bit & 4) << 3) | ((bit & 1) << 4); + bit1 = (bit & 128) | ((bit & 32) << 1) | ((bit & 8) << 2) | ((bit & 2) << 3); + + if (count > 1) + { + bit = plane_ptr[1]; + + bit0 |= (bit & 1) | ((bit & 4) >> 1) | ((bit & 16) >> 2) | ((bit & 64) >> 3); + bit1 |= ((bit & 2) >> 1) | ((bit & 8) >> 2) | ((bit & 32) >> 3) | ((bit & 128) >> 4); + } + + bit_ptr[0] = bit0; + bit_ptr[bytes] = bit1; + } + + /* + * Send low and high bits... + */ + + CompressData(BitBuffer, bytes, 'V', header->cupsCompression); + CompressData(BitBuffer + bytes, bytes, plane < (NumPlanes - 1) ? 'V' : 'W', + header->cupsCompression); + } + + fflush(stdout); +} + + +/* + * 'main()' - Main entry and processing of driver. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int fd; /* File descriptor */ + cups_raster_t *ras; /* Raster stream for printing */ + cups_page_header2_t header; /* Page header from file */ + int y; /* Current line */ + ppd_file_t *ppd; /* PPD file */ +#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) + struct sigaction action; /* Actions for POSIX signals */ +#endif /* HAVE_SIGACTION && !HAVE_SIGSET */ + + + /* + * Make sure status messages are not buffered... + */ + + setbuf(stderr, NULL); + + /* + * Check command-line... + */ + + if (argc < 6 || argc > 7) + { + /* + * We don't have the correct number of arguments; write an error message + * and return. + */ + + _cupsLangPrintFilter(stderr, "ERROR", + _("%s job-id user title copies options [file]"), + "rastertohp"); + return (1); + } + + /* + * Open the page stream... + */ + + if (argc == 7) + { + if ((fd = open(argv[6], O_RDONLY)) == -1) + { + _cupsLangPrintError("ERROR", _("Unable to open raster file")); + sleep(1); + return (1); + } + } + else + fd = 0; + + ras = cupsRasterOpen(fd, CUPS_RASTER_READ); + + /* + * Register a signal handler to eject the current page if the + * job is cancelled. + */ + + Canceled = 0; + +#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ + sigset(SIGTERM, CancelJob); +#elif defined(HAVE_SIGACTION) + memset(&action, 0, sizeof(action)); + + sigemptyset(&action.sa_mask); + action.sa_handler = CancelJob; + sigaction(SIGTERM, &action, NULL); +#else + signal(SIGTERM, CancelJob); +#endif /* HAVE_SIGSET */ + + /* + * Initialize the print device... + */ + + ppd = ppdOpenFile(getenv("PPD")); + if (!ppd) + { + ppd_status_t status; /* PPD error */ + int linenum; /* Line number */ + + _cupsLangPrintFilter(stderr, "ERROR", + _("The PPD file could not be opened.")); + + status = ppdLastError(&linenum); + + fprintf(stderr, "DEBUG: %s on line %d.\n", ppdErrorString(status), linenum); + + return (1); + } + + Setup(); + + /* + * Process pages as needed... + */ + + Page = 0; + + while (cupsRasterReadHeader2(ras, &header)) + { + /* + * Write a status message with the page number and number of copies. + */ + + if (Canceled) + break; + + Page ++; + + fprintf(stderr, "PAGE: %d %d\n", Page, header.NumCopies); + _cupsLangPrintFilter(stderr, "INFO", _("Starting page %d."), Page); + + /* + * Start the page... + */ + + StartPage(ppd, &header); + + /* + * Loop for each line on the page... + */ + + for (y = 0; y < header.cupsHeight; y ++) + { + /* + * Let the user know how far we have progressed... + */ + + if (Canceled) + break; + + if ((y & 127) == 0) + { + _cupsLangPrintFilter(stderr, "INFO", + _("Printing page %d, %d%% complete."), + Page, 100 * y / header.cupsHeight); + fprintf(stderr, "ATTR: job-media-progress=%d\n", + 100 * y / header.cupsHeight); + } + + /* + * Read a line of graphics... + */ + + if (cupsRasterReadPixels(ras, Planes[0], header.cupsBytesPerLine) < 1) + break; + + /* + * See if the line is blank; if not, write it to the printer... + */ + + if (Planes[0][0] || + memcmp(Planes[0], Planes[0] + 1, header.cupsBytesPerLine - 1)) + OutputLine(&header); + else + Feed ++; + } + + /* + * Eject the page... + */ + + _cupsLangPrintFilter(stderr, "INFO", _("Finished page %d."), Page); + + EndPage(); + + if (Canceled) + break; + } + + /* + * Shutdown the printer... + */ + + Shutdown(); + + if (ppd) + ppdClose(ppd); + + /* + * Close the raster stream... + */ + + cupsRasterClose(ras); + if (fd != 0) + close(fd); + + /* + * If no pages were printed, send an error message... + */ + + if (Page == 0) + { + _cupsLangPrintFilter(stderr, "ERROR", _("No pages were found.")); + return (1); + } + else + return (0); +} + + +/* + * End of "$Id: rastertohp.c 7834 2008-08-04 21:02:09Z mike $". + */ diff --git a/filter/rastertolabel.c b/filter/rastertolabel.c new file mode 100644 index 0000000..4c0860c --- /dev/null +++ b/filter/rastertolabel.c @@ -0,0 +1,1312 @@ +/* + * "$Id: rastertolabel.c 7720 2008-07-11 22:46:21Z mike $" + * + * Label printer filter for CUPS. + * + * Copyright 2007-2012 by Apple Inc. + * Copyright 2001-2007 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. + * + * Contents: + * + * Setup() - Prepare the printer for printing. + * StartPage() - Start a page of graphics. + * EndPage() - Finish a page of graphics. + * CancelJob() - Cancel the current job... + * OutputLine() - Output a line of graphics. + * PCLCompress() - Output a PCL (mode 3) compressed line. + * ZPLCompress() - Output a run-length compression sequence. + * main() - Main entry and processing of driver. + */ + +/* + * Include necessary headers... + */ + +#include +#include +#include +#include +#include +#include +#include +#include + + +/* + * This driver filter currently supports Dymo, Intellitech, and Zebra + * label printers. + * + * The Dymo portion of the driver has been tested with the 300, 330, + * and 330 Turbo label printers; it may also work with other models. + * The Dymo printers support printing at 136, 203, and 300 DPI. + * + * The Intellitech portion of the driver has been tested with the + * Intellibar 408, 412, and 808 and supports their PCL variant. + * + * The Zebra portion of the driver has been tested with the LP-2844, + * LP-2844Z, QL-320, and QL-420 label printers; it may also work with + * other models. The driver supports EPL line mode, EPL page mode, + * ZPL, and CPCL as defined in Zebra's online developer documentation. + */ + +/* + * Model number constants... + */ + +#define DYMO_3x0 0 /* Dymo Labelwriter 300/330/330 Turbo */ + +#define ZEBRA_EPL_LINE 0x10 /* Zebra EPL line mode printers */ +#define ZEBRA_EPL_PAGE 0x11 /* Zebra EPL page mode printers */ +#define ZEBRA_ZPL 0x12 /* Zebra ZPL-based printers */ +#define ZEBRA_CPCL 0x13 /* Zebra CPCL-based printers */ + +#define INTELLITECH_PCL 0x20 /* Intellitech PCL-based printers */ + + +/* + * Globals... + */ + +unsigned char *Buffer; /* Output buffer */ +unsigned char *CompBuffer; /* Compression buffer */ +unsigned char *LastBuffer; /* Last buffer */ +int LastSet; /* Number of repeat characters */ +int ModelNumber, /* cupsModelNumber attribute */ + Page, /* Current page */ + Feed, /* Number of lines to skip */ + Canceled; /* Non-zero if job is canceled */ + + +/* + * Prototypes... + */ + +void Setup(ppd_file_t *ppd); +void StartPage(ppd_file_t *ppd, cups_page_header2_t *header); +void EndPage(ppd_file_t *ppd, cups_page_header2_t *header); +void CancelJob(int sig); +void OutputLine(ppd_file_t *ppd, cups_page_header2_t *header, int y); +void PCLCompress(unsigned char *line, int length); +void ZPLCompress(char repeat_char, int repeat_count); + + +/* + * 'Setup()' - Prepare the printer for printing. + */ + +void +Setup(ppd_file_t *ppd) /* I - PPD file */ +{ + int i; /* Looping var */ + + + /* + * Get the model number from the PPD file... + */ + + if (ppd) + ModelNumber = ppd->model_number; + + /* + * Initialize based on the model number... + */ + + switch (ModelNumber) + { + case DYMO_3x0 : + /* + * Clear any remaining data... + */ + + for (i = 0; i < 100; i ++) + putchar(0x1b); + + /* + * Reset the printer... + */ + + fputs("\033@", stdout); + break; + + case ZEBRA_EPL_LINE : + break; + + case ZEBRA_EPL_PAGE : + break; + + case ZEBRA_ZPL : + break; + + case ZEBRA_CPCL : + break; + + case INTELLITECH_PCL : + /* + * Send a PCL reset sequence. + */ + + putchar(0x1b); + putchar('E'); + break; + } +} + + +/* + * 'StartPage()' - Start a page of graphics. + */ + +void +StartPage(ppd_file_t *ppd, /* I - PPD file */ + cups_page_header2_t *header) /* I - Page header */ +{ + ppd_choice_t *choice; /* Marked choice */ + int length; /* Actual label length */ + + + /* + * Show page device dictionary... + */ + + fprintf(stderr, "DEBUG: StartPage...\n"); + fprintf(stderr, "DEBUG: MediaClass = \"%s\"\n", header->MediaClass); + fprintf(stderr, "DEBUG: MediaColor = \"%s\"\n", header->MediaColor); + fprintf(stderr, "DEBUG: MediaType = \"%s\"\n", header->MediaType); + fprintf(stderr, "DEBUG: OutputType = \"%s\"\n", header->OutputType); + + fprintf(stderr, "DEBUG: AdvanceDistance = %d\n", header->AdvanceDistance); + fprintf(stderr, "DEBUG: AdvanceMedia = %d\n", header->AdvanceMedia); + fprintf(stderr, "DEBUG: Collate = %d\n", header->Collate); + fprintf(stderr, "DEBUG: CutMedia = %d\n", header->CutMedia); + fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex); + fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0], + header->HWResolution[1]); + fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", + header->ImagingBoundingBox[0], header->ImagingBoundingBox[1], + header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]); + fprintf(stderr, "DEBUG: InsertSheet = %d\n", header->InsertSheet); + fprintf(stderr, "DEBUG: Jog = %d\n", header->Jog); + fprintf(stderr, "DEBUG: LeadingEdge = %d\n", header->LeadingEdge); + fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0], + header->Margins[1]); + fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed); + fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition); + fprintf(stderr, "DEBUG: MediaWeight = %d\n", header->MediaWeight); + fprintf(stderr, "DEBUG: MirrorPrint = %d\n", header->MirrorPrint); + fprintf(stderr, "DEBUG: NegativePrint = %d\n", header->NegativePrint); + fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies); + fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation); + fprintf(stderr, "DEBUG: OutputFaceUp = %d\n", header->OutputFaceUp); + fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0], + header->PageSize[1]); + fprintf(stderr, "DEBUG: Separations = %d\n", header->Separations); + fprintf(stderr, "DEBUG: TraySwitch = %d\n", header->TraySwitch); + fprintf(stderr, "DEBUG: Tumble = %d\n", header->Tumble); + fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth); + fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight); + fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType); + fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor); + fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel); + fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine); + fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder); + fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace); + fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression); + fprintf(stderr, "DEBUG: cupsRowCount = %d\n", header->cupsRowCount); + fprintf(stderr, "DEBUG: cupsRowFeed = %d\n", header->cupsRowFeed); + fprintf(stderr, "DEBUG: cupsRowStep = %d\n", header->cupsRowStep); + + switch (ModelNumber) + { + case DYMO_3x0 : + /* + * Setup printer/job attributes... + */ + + length = header->PageSize[1] * header->HWResolution[1] / 72; + + printf("\033L%c%c", length >> 8, length); + printf("\033D%c", header->cupsBytesPerLine); + + printf("\033%c", header->cupsCompression + 'c'); /* Darkness */ + break; + + case ZEBRA_EPL_LINE : + /* + * Set print rate... + */ + + if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL && + strcmp(choice->choice, "Default")) + printf("\033S%.0f", atof(choice->choice) * 2.0 - 2.0); + + /* + * Set darkness... + */ + + if (header->cupsCompression > 0 && header->cupsCompression <= 100) + printf("\033D%d", 7 * header->cupsCompression / 100); + + /* + * Set left margin to 0... + */ + + fputs("\033M01", stdout); + + /* + * Start buffered output... + */ + + fputs("\033B", stdout); + break; + + case ZEBRA_EPL_PAGE : + /* + * Start a new label... + */ + + puts(""); + puts("N"); + + /* + * Set hardware options... + */ + + if (!strcmp(header->MediaType, "Direct")) + puts("OD"); + + /* + * Set print rate... + */ + + if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL && + strcmp(choice->choice, "Default")) + { + float val = atof(choice->choice); + + if (val >= 3.0) + printf("S%.0f\n", val); + else + printf("S%.0f\n", val * 2.0 - 2.0); + } + + /* + * Set darkness... + */ + + if (header->cupsCompression > 0 && header->cupsCompression <= 100) + printf("D%d\n", 15 * header->cupsCompression / 100); + + /* + * Set label size... + */ + + printf("q%d\n", (header->cupsWidth + 7) & ~7); + break; + + case ZEBRA_ZPL : + /* + * Set darkness... + */ + + if (header->cupsCompression > 0 && header->cupsCompression <= 100) + printf("~SD%02d\n", 30 * header->cupsCompression / 100); + + /* + * Start bitmap graphics... + */ + + printf("~DGR:CUPS.GRF,%d,%d,\n", + header->cupsHeight * header->cupsBytesPerLine, + header->cupsBytesPerLine); + + /* + * Allocate compression buffers... + */ + + CompBuffer = malloc(2 * header->cupsBytesPerLine + 1); + LastBuffer = malloc(header->cupsBytesPerLine); + LastSet = 0; + break; + + case ZEBRA_CPCL : + /* + * Start label... + */ + + printf("! 0 %u %u %u %u\r\n", header->HWResolution[0], + header->HWResolution[1], header->cupsHeight, + header->NumCopies); + printf("PAGE-WIDTH %d\r\n", header->cupsWidth); + printf("PAGE-HEIGHT %d\r\n", header->cupsWidth); + break; + + case INTELLITECH_PCL : + /* + * Set the media size... + */ + + printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */ + printf("\033&l0O"); /* Set portrait orientation */ + + switch (header->PageSize[1]) + { + case 540 : /* Monarch Envelope */ + printf("\033&l80A"); /* Set page size */ + break; + + case 624 : /* DL Envelope */ + printf("\033&l90A"); /* Set page size */ + break; + + case 649 : /* C5 Envelope */ + printf("\033&l91A"); /* Set page size */ + break; + + case 684 : /* COM-10 Envelope */ + printf("\033&l81A"); /* Set page size */ + break; + + case 756 : /* Executive */ + printf("\033&l1A"); /* Set page size */ + break; + + case 792 : /* Letter */ + printf("\033&l2A"); /* Set page size */ + break; + + case 842 : /* A4 */ + printf("\033&l26A"); /* Set page size */ + break; + + case 1008 : /* Legal */ + printf("\033&l3A"); /* Set page size */ + break; + + default : /* Custom size */ + printf("\033!f%dZ", header->PageSize[1] * 300 / 72); + break; + } + + printf("\033&l%dP", /* Set page length */ + header->PageSize[1] / 12); + printf("\033&l0E"); /* Set top margin to 0 */ + printf("\033&l%dX", header->NumCopies); + /* Set number copies */ + printf("\033&l0L"); /* Turn off perforation skip */ + + /* + * Print settings... + */ + + if (Page == 1) + { + if (header->cupsRowFeed) /* inPrintRate */ + printf("\033!p%dS", header->cupsRowFeed); + + if (header->cupsCompression != ~0) + /* inPrintDensity */ + printf("\033&d%dA", 30 * header->cupsCompression / 100 - 15); + + if ((choice = ppdFindMarkedChoice(ppd, "inPrintMode")) != NULL) + { + if (!strcmp(choice->choice, "Standard")) + fputs("\033!p0M", stdout); + else if (!strcmp(choice->choice, "Tear")) + { + fputs("\033!p1M", stdout); + + if (header->cupsRowCount) /* inTearInterval */ + printf("\033!n%dT", header->cupsRowCount); + } + else + { + fputs("\033!p2M", stdout); + + if (header->cupsRowStep) /* inCutInterval */ + printf("\033!n%dC", header->cupsRowStep); + } + } + } + + /* + * Setup graphics... + */ + + printf("\033*t%dR", header->HWResolution[0]); + /* Set resolution */ + + printf("\033*r%dS", header->cupsWidth); + /* Set width */ + printf("\033*r%dT", header->cupsHeight); + /* Set height */ + + printf("\033&a0H"); /* Set horizontal position */ + printf("\033&a0V"); /* Set vertical position */ + printf("\033*r1A"); /* Start graphics */ + printf("\033*b3M"); /* Set compression */ + + /* + * Allocate compression buffers... + */ + + CompBuffer = malloc(2 * header->cupsBytesPerLine + 1); + LastBuffer = malloc(header->cupsBytesPerLine); + LastSet = 0; + break; + } + + /* + * Allocate memory for a line of graphics... + */ + + Buffer = malloc(header->cupsBytesPerLine); + Feed = 0; +} + + +/* + * 'EndPage()' - Finish a page of graphics. + */ + +void +EndPage(ppd_file_t *ppd, /* I - PPD file */ + cups_page_header2_t *header) /* I - Page header */ +{ + int val; /* Option value */ + ppd_choice_t *choice; /* Marked choice */ + + + switch (ModelNumber) + { + case DYMO_3x0 : + /* + * Eject the current page... + */ + + fputs("\033E", stdout); + break; + + case ZEBRA_EPL_LINE : + /* + * End buffered output, eject the label... + */ + + fputs("\033E\014", stdout); + break; + + case ZEBRA_EPL_PAGE : + /* + * Print the label... + */ + + puts("P1"); + break; + + case ZEBRA_ZPL : + if (Canceled) + { + /* + * Cancel bitmap download... + */ + + puts("~DN"); + break; + } + + /* + * Start label... + */ + + puts("^XA"); + + /* + * Set print rate... + */ + + if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL && + strcmp(choice->choice, "Default")) + { + val = atoi(choice->choice); + printf("^PR%d,%d,%d\n", val, val, val); + } + + /* + * Put label home in default position (0,0)... + */ + + printf("^LH0,0\n"); + + /* + * Set media tracking... + */ + + if (ppdIsMarked(ppd, "zeMediaTracking", "Continuous")) + { + /* + * Add label length command for continuous... + */ + + printf("^LL%d\n", header->cupsHeight); + printf("^MNN\n"); + } + else if (ppdIsMarked(ppd, "zeMediaTracking", "Web")) + printf("^MNY\n"); + else if (ppdIsMarked(ppd, "zeMediaTracking", "Mark")) + printf("^MNM\n"); + + /* + * Set label top + */ + + if (header->cupsRowStep != 200) + printf("^LT%u\n", header->cupsRowStep); + + /* + * Set media type... + */ + + if (!strcmp(header->MediaType, "Thermal")) + printf("^MTT\n"); + else if (!strcmp(header->MediaType, "Direct")) + printf("^MTD\n"); + + /* + * Set print mode... + */ + + if ((choice = ppdFindMarkedChoice(ppd, "zePrintMode")) != NULL && + strcmp(choice->choice, "Saved")) + { + printf("^MM"); + + if (!strcmp(choice->choice, "Tear")) + printf("T,Y\n"); + else if (!strcmp(choice->choice, "Peel")) + printf("P,Y\n"); + else if (!strcmp(choice->choice, "Rewind")) + printf("R,Y\n"); + else if (!strcmp(choice->choice, "Applicator")) + printf("A,Y\n"); + else + printf("C,Y\n"); + } + + /* + * Set tear-off adjust position... + */ + + if (header->AdvanceDistance != 1000) + { + if ((int)header->AdvanceDistance < 0) + printf("~TA%04d\n", (int)header->AdvanceDistance); + else + printf("~TA%03d\n", (int)header->AdvanceDistance); + } + + /* + * Allow for reprinting after an error... + */ + + if (ppdIsMarked(ppd, "zeErrorReprint", "Always")) + printf("^JZY\n"); + else if (ppdIsMarked(ppd, "zeErrorReprint", "Never")) + printf("^JZN\n"); + + /* + * Print multiple copies + */ + + if (header->NumCopies > 1) + printf("^PQ%d, 0, 0, N\n", header->NumCopies); + + /* + * Display the label image... + */ + + puts("^FO0,0^XGR:CUPS.GRF,1,1^FS"); + + /* + * End the label and eject... + */ + + puts("^IDR:CUPS.GRF^FS"); + puts("^XZ"); + + /* + * Free compression buffers... + */ + + free(CompBuffer); + free(LastBuffer); + break; + + case ZEBRA_CPCL : + /* + * Set tear-off adjust position... + */ + + if (header->AdvanceDistance != 1000) + printf("PRESENT-AT %d 1\r\n", (int)header->AdvanceDistance); + + /* + * Allow for reprinting after an error... + */ + + if (ppdIsMarked(ppd, "zeErrorReprint", "Always")) + puts("ON-OUT-OF-PAPER WAIT\r"); + else if (ppdIsMarked(ppd, "zeErrorReprint", "Never")) + puts("ON-OUT-OF-PAPER PURGE\r"); + + /* + * Cut label? + */ + + if (header->CutMedia) + puts("CUT\r"); + + /* + * Set darkness... + */ + + if (header->cupsCompression > 0) + printf("TONE %u\r\n", 2 * header->cupsCompression); + + /* + * Set print rate... + */ + + if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL && + strcmp(choice->choice, "Default")) + { + val = atoi(choice->choice); + printf("SPEED %d\r\n", val); + } + + /* + * Print the label... + */ + + if ((choice = ppdFindMarkedChoice(ppd, "zeMediaTracking")) == NULL || + strcmp(choice->choice, "Continuous")) + puts("FORM\r"); + + puts("PRINT\r"); + break; + + case INTELLITECH_PCL : + printf("\033*rB"); /* End GFX */ + printf("\014"); /* Eject current page */ + break; + } + + fflush(stdout); + + /* + * Free memory... + */ + + free(Buffer); +} + + +/* + * 'CancelJob()' - Cancel the current job... + */ + +void +CancelJob(int sig) /* I - Signal */ +{ + /* + * Tell the main loop to stop... + */ + + (void)sig; + + Canceled = 1; +} + + +/* + * 'OutputLine()' - Output a line of graphics... + */ + +void +OutputLine(ppd_file_t *ppd, /* I - PPD file */ + cups_page_header2_t *header, /* I - Page header */ + int y) /* I - Line number */ +{ + int i; /* Looping var */ + unsigned char *ptr; /* Pointer into buffer */ + unsigned char *compptr; /* Pointer into compression buffer */ + char repeat_char; /* Repeated character */ + int repeat_count; /* Number of repeated characters */ + static const char *hex = "0123456789ABCDEF"; + /* Hex digits */ + + + switch (ModelNumber) + { + case DYMO_3x0 : + /* + * See if the line is blank; if not, write it to the printer... + */ + + if (Buffer[0] || + memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1)) + { + if (Feed) + { + while (Feed > 255) + { + printf("\033f\001%c", 255); + Feed -= 255; + } + + printf("\033f\001%c", Feed); + Feed = 0; + } + + putchar(0x16); + fwrite(Buffer, header->cupsBytesPerLine, 1, stdout); + fflush(stdout); + +#ifdef __sgi + /* + * This hack works around a bug in the IRIX serial port driver when + * run at high baud rates (e.g. 115200 baud)... This results in + * slightly slower label printing, but at least the labels come + * out properly. + */ + + sginap(1); +#endif /* __sgi */ + } + else + Feed ++; + break; + + case ZEBRA_EPL_LINE : + printf("\033g%03d", header->cupsBytesPerLine); + fwrite(Buffer, 1, header->cupsBytesPerLine, stdout); + fflush(stdout); + break; + + case ZEBRA_EPL_PAGE : + if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine)) + { + printf("GW0,%d,%d,1\n", y, header->cupsBytesPerLine); + for (i = header->cupsBytesPerLine, ptr = Buffer; i > 0; i --, ptr ++) + putchar(~*ptr); + putchar('\n'); + fflush(stdout); + } + break; + + case ZEBRA_ZPL : + /* + * Determine if this row is the same as the previous line. + * If so, output a ':' and return... + */ + + if (LastSet) + { + if (!memcmp(Buffer, LastBuffer, header->cupsBytesPerLine)) + { + putchar(':'); + return; + } + } + + /* + * Convert the line to hex digits... + */ + + for (ptr = Buffer, compptr = CompBuffer, i = header->cupsBytesPerLine; + i > 0; + i --, ptr ++) + { + *compptr++ = hex[*ptr >> 4]; + *compptr++ = hex[*ptr & 15]; + } + + *compptr = '\0'; + + /* + * Run-length compress the graphics... + */ + + for (compptr = CompBuffer + 1, repeat_char = CompBuffer[0], repeat_count = 1; + *compptr; + compptr ++) + if (*compptr == repeat_char) + repeat_count ++; + else + { + ZPLCompress(repeat_char, repeat_count); + repeat_char = *compptr; + repeat_count = 1; + } + + if (repeat_char == '0') + { + /* + * Handle 0's on the end of the line... + */ + + if (repeat_count & 1) + { + repeat_count --; + putchar('0'); + } + + if (repeat_count > 0) + putchar(','); + } + else + ZPLCompress(repeat_char, repeat_count); + + fflush(stdout); + + /* + * Save this line for the next round... + */ + + memcpy(LastBuffer, Buffer, header->cupsBytesPerLine); + LastSet = 1; + break; + + case ZEBRA_CPCL : + if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine)) + { + printf("CG %u 1 0 %d ", header->cupsBytesPerLine, y); + fwrite(Buffer, 1, header->cupsBytesPerLine, stdout); + puts("\r"); + fflush(stdout); + } + break; + + case INTELLITECH_PCL : + if (Buffer[0] || + memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1)) + { + if (Feed) + { + printf("\033*b%dY", Feed); + Feed = 0; + LastSet = 0; + } + + PCLCompress(Buffer, header->cupsBytesPerLine); + } + else + Feed ++; + break; + } +} + + +/* + * 'PCLCompress()' - Output a PCL (mode 3) compressed line. + */ + +void +PCLCompress(unsigned char *line, /* I - Line to compress */ + int length) /* I - Length of line */ +{ + unsigned char *line_ptr, /* Current byte pointer */ + *line_end, /* End-of-line byte pointer */ + *comp_ptr, /* Pointer into compression buffer */ + *start, /* Start of compression sequence */ + *seed; /* Seed buffer pointer */ + int count, /* Count of bytes for output */ + offset; /* Offset of bytes for output */ + + + /* + * Do delta-row compression... + */ + + line_ptr = line; + line_end = line + length; + + comp_ptr = CompBuffer; + seed = LastBuffer; + + while (line_ptr < line_end) + { + /* + * Find the next non-matching sequence... + */ + + start = line_ptr; + + if (!LastSet) + { + /* + * The seed buffer is invalid, so do the next 8 bytes, max... + */ + + offset = 0; + + if ((count = line_end - line_ptr) > 8) + count = 8; + + line_ptr += count; + } + else + { + /* + * The seed buffer is valid, so compare against it... + */ + + while (*line_ptr == *seed && + line_ptr < line_end) + { + line_ptr ++; + seed ++; + } + + if (line_ptr == line_end) + break; + + offset = line_ptr - start; + + /* + * Find up to 8 non-matching bytes... + */ + + start = line_ptr; + count = 0; + while (*line_ptr != *seed && + line_ptr < line_end && + count < 8) + { + line_ptr ++; + seed ++; + count ++; + } + } + + /* + * Place mode 3 compression data in the buffer; see HP manuals + * for details... + */ + + if (offset >= 31) + { + /* + * Output multi-byte offset... + */ + + *comp_ptr++ = ((count - 1) << 5) | 31; + + offset -= 31; + while (offset >= 255) + { + *comp_ptr++ = 255; + offset -= 255; + } + + *comp_ptr++ = offset; + } + else + { + /* + * Output single-byte offset... + */ + + *comp_ptr++ = ((count - 1) << 5) | offset; + } + + memcpy(comp_ptr, start, count); + comp_ptr += count; + } + + /* + * Set the length of the data and write it... + */ + + printf("\033*b%dW", (int)(comp_ptr - CompBuffer)); + fwrite(CompBuffer, comp_ptr - CompBuffer, 1, stdout); + + /* + * Save this line as a "seed" buffer for the next... + */ + + memcpy(LastBuffer, line, length); + LastSet = 1; +} + + +/* + * 'ZPLCompress()' - Output a run-length compression sequence. + */ + +void +ZPLCompress(char repeat_char, /* I - Character to repeat */ + int repeat_count) /* I - Number of repeated characters */ +{ + if (repeat_count > 1) + { + /* + * Print as many z's as possible - they are the largest denomination + * representing 400 characters (zC stands for 400 adjacent C's) + */ + + while (repeat_count >= 400) + { + putchar('z'); + repeat_count -= 400; + } + + /* + * Then print 'g' through 'y' as multiples of 20 characters... + */ + + if (repeat_count >= 20) + { + putchar('f' + repeat_count / 20); + repeat_count %= 20; + } + + /* + * Finally, print 'G' through 'Y' as 1 through 19 characters... + */ + + if (repeat_count > 0) + putchar('F' + repeat_count); + } + + /* + * Then the character to be repeated... + */ + + putchar(repeat_char); +} + + +/* + * 'main()' - Main entry and processing of driver. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line arguments */ + char *argv[]) /* I - Command-line arguments */ +{ + int fd; /* File descriptor */ + cups_raster_t *ras; /* Raster stream for printing */ + cups_page_header2_t header; /* Page header from file */ + int y; /* Current line */ + ppd_file_t *ppd; /* PPD file */ + int num_options; /* Number of options */ + cups_option_t *options; /* Options */ +#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) + struct sigaction action; /* Actions for POSIX signals */ +#endif /* HAVE_SIGACTION && !HAVE_SIGSET */ + + + /* + * Make sure status messages are not buffered... + */ + + setbuf(stderr, NULL); + + /* + * Check command-line... + */ + + if (argc < 6 || argc > 7) + { + /* + * We don't have the correct number of arguments; write an error message + * and return. + */ + + _cupsLangPrintFilter(stderr, "ERROR", + _("%s job-id user title copies options [file]"), + "rastertolabel"); + return (1); + } + + /* + * Open the page stream... + */ + + if (argc == 7) + { + if ((fd = open(argv[6], O_RDONLY)) == -1) + { + _cupsLangPrintError("ERROR", _("Unable to open raster file")); + sleep(1); + return (1); + } + } + else + fd = 0; + + ras = cupsRasterOpen(fd, CUPS_RASTER_READ); + + /* + * Register a signal handler to eject the current page if the + * job is cancelled. + */ + + Canceled = 0; + +#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ + sigset(SIGTERM, CancelJob); +#elif defined(HAVE_SIGACTION) + memset(&action, 0, sizeof(action)); + + sigemptyset(&action.sa_mask); + action.sa_handler = CancelJob; + sigaction(SIGTERM, &action, NULL); +#else + signal(SIGTERM, CancelJob); +#endif /* HAVE_SIGSET */ + + /* + * Open the PPD file and apply options... + */ + + num_options = cupsParseOptions(argv[5], 0, &options); + + ppd = ppdOpenFile(getenv("PPD")); + if (!ppd) + { + ppd_status_t status; /* PPD error */ + int linenum; /* Line number */ + + _cupsLangPrintFilter(stderr, "ERROR", + _("The PPD file could not be opened.")); + + status = ppdLastError(&linenum); + + fprintf(stderr, "DEBUG: %s on line %d.\n", ppdErrorString(status), linenum); + + return (1); + } + + ppdMarkDefaults(ppd); + cupsMarkOptions(ppd, num_options, options); + + /* + * Initialize the print device... + */ + + Setup(ppd); + + /* + * Process pages as needed... + */ + + Page = 0; + + while (cupsRasterReadHeader2(ras, &header)) + { + /* + * Write a status message with the page number and number of copies. + */ + + if (Canceled) + break; + + Page ++; + + fprintf(stderr, "PAGE: %d 1\n", Page); + _cupsLangPrintFilter(stderr, "INFO", _("Starting page %d."), Page); + + /* + * Start the page... + */ + + StartPage(ppd, &header); + + /* + * Loop for each line on the page... + */ + + for (y = 0; y < header.cupsHeight && !Canceled; y ++) + { + /* + * Let the user know how far we have progressed... + */ + + if (Canceled) + break; + + if ((y & 15) == 0) + { + _cupsLangPrintFilter(stderr, "INFO", + _("Printing page %d, %d%% complete."), + Page, 100 * y / header.cupsHeight); + fprintf(stderr, "ATTR: job-media-progress=%d\n", + 100 * y / header.cupsHeight); + } + + /* + * Read a line of graphics... + */ + + if (cupsRasterReadPixels(ras, Buffer, header.cupsBytesPerLine) < 1) + break; + + /* + * Write it to the printer... + */ + + OutputLine(ppd, &header, y); + } + + /* + * Eject the page... + */ + + _cupsLangPrintFilter(stderr, "INFO", _("Finished page %d."), Page); + + EndPage(ppd, &header); + + if (Canceled) + break; + } + + /* + * Close the raster stream... + */ + + cupsRasterClose(ras); + if (fd != 0) + close(fd); + + /* + * Close the PPD file and free the options... + */ + + ppdClose(ppd); + cupsFreeOptions(num_options, options); + + /* + * If no pages were printed, send an error message... + */ + + if (Page == 0) + { + _cupsLangPrintFilter(stderr, "ERROR", _("No pages were found.")); + return (1); + } + else + return (0); +} + + +/* + * End of "$Id: rastertolabel.c 7720 2008-07-11 22:46:21Z mike $". + */ diff --git a/filter/rastertopwg.c b/filter/rastertopwg.c new file mode 100644 index 0000000..97dab1c --- /dev/null +++ b/filter/rastertopwg.c @@ -0,0 +1,461 @@ +/* + * "$Id: rastertopwg.c 3427 2011-09-20 18:40:57Z msweet $" + * + * CUPS raster to PWG raster format filter for CUPS. + * + * Copyright 2011 Apple Inc. + * + * 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. + * + * Contents: + * + * main() - Main entry for filter. + */ + +/* + * Include necessary headers... + */ + +#include +#include +#include +#include + + +/* + * 'main()' - Main entry for filter. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line args */ + char *argv[]) /* I - Command-line arguments */ +{ + int fd; /* Raster file */ + cups_raster_t *inras, /* Input raster stream */ + *outras; /* Output raster stream */ + cups_page_header2_t inheader, /* Input raster page header */ + outheader; /* Output raster page header */ + int y; /* Current line */ + unsigned char *line; /* Line buffer */ + int page = 0, /* Current page */ + page_width, /* Actual page width */ + page_height, /* Actual page height */ + page_top, /* Top margin */ + page_bottom, /* Bottom margin */ + page_left, /* Left margin */ + linesize, /* Bytes per line */ + lineoffset; /* Offset into line */ + unsigned char white; /* White pixel */ + ppd_file_t *ppd; /* PPD file */ + ppd_attr_t *back; /* cupsBackSize attribute */ + _ppd_cache_t *cache; /* PPD cache */ + _pwg_size_t *pwg_size; /* PWG media size */ + _pwg_media_t *pwg_media; /* PWG media name */ + int num_options; /* Number of options */ + cups_option_t *options = NULL;/* Options */ + const char *val; /* Option value */ + + + if (argc < 6 || argc > 7) + { + puts("Usage: rastertopwg job user title copies options [filename]"); + return (1); + } + else if (argc == 7) + { + if ((fd = open(argv[6], O_RDONLY)) < 0) + { + perror("ERROR: Unable to open print file"); + return (1); + } + } + else + fd = 0; + + inras = cupsRasterOpen(fd, CUPS_RASTER_READ); + outras = cupsRasterOpen(1, CUPS_RASTER_WRITE_PWG); + + ppd = ppdOpenFile(getenv("PPD")); + back = ppdFindAttr(ppd, "cupsBackSide", NULL); + + num_options = cupsParseOptions(argv[5], 0, &options); + + ppdMarkDefaults(ppd); + cupsMarkOptions(ppd, num_options, options); + + cache = ppd ? ppd->cache : NULL; + + while (cupsRasterReadHeader2(inras, &inheader)) + { + /* + * Compute the real raster size... + */ + + page ++; + + fprintf(stderr, "PAGE: %d %d\n", page, inheader.NumCopies); + + page_width = (int)(inheader.cupsPageSize[0] * inheader.HWResolution[0] / + 72.0); + page_height = (int)(inheader.cupsPageSize[1] * inheader.HWResolution[1] / + 72.0); + page_left = (int)(inheader.cupsImagingBBox[0] * + inheader.HWResolution[0] / 72.0); + page_bottom = (int)(inheader.cupsImagingBBox[1] * + inheader.HWResolution[1] / 72.0); + page_top = page_height - page_bottom - inheader.cupsHeight; + linesize = (page_width * inheader.cupsBitsPerPixel + 7) / 8; + lineoffset = page_left * inheader.cupsBitsPerPixel / 8; /* Round down */ + + switch (inheader.cupsColorSpace) + { + case CUPS_CSPACE_W : + case CUPS_CSPACE_RGB : + case CUPS_CSPACE_SW : + case CUPS_CSPACE_SRGB : + case CUPS_CSPACE_ADOBERGB : + white = 255; + break; + + case CUPS_CSPACE_K : + case CUPS_CSPACE_CMYK : + case CUPS_CSPACE_DEVICE1 : + case CUPS_CSPACE_DEVICE2 : + case CUPS_CSPACE_DEVICE3 : + case CUPS_CSPACE_DEVICE4 : + case CUPS_CSPACE_DEVICE5 : + case CUPS_CSPACE_DEVICE6 : + case CUPS_CSPACE_DEVICE7 : + case CUPS_CSPACE_DEVICE8 : + case CUPS_CSPACE_DEVICE9 : + case CUPS_CSPACE_DEVICEA : + case CUPS_CSPACE_DEVICEB : + case CUPS_CSPACE_DEVICEC : + case CUPS_CSPACE_DEVICED : + case CUPS_CSPACE_DEVICEE : + case CUPS_CSPACE_DEVICEF : + white = 0; + break; + + default : + _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data.")); + fprintf(stderr, "DEBUG: Unsupported cupsColorSpace %d on page %d.\n", + inheader.cupsColorSpace, page); + return (1); + } + + if (inheader.cupsColorOrder != CUPS_ORDER_CHUNKED) + { + _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data.")); + fprintf(stderr, "DEBUG: Unsupported cupsColorOrder %d on page %d.\n", + inheader.cupsColorOrder, page); + return (1); + } + + if (inheader.cupsBitsPerPixel != 1 && + inheader.cupsBitsPerColor != 8 && inheader.cupsBitsPerColor != 16) + { + _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data.")); + fprintf(stderr, "DEBUG: Unsupported cupsBitsPerColor %d on page %d.\n", + inheader.cupsBitsPerColor, page); + return (1); + } + + memcpy(&outheader, &inheader, sizeof(outheader)); + outheader.cupsWidth = page_width; + outheader.cupsHeight = page_height; + outheader.cupsBytesPerLine = linesize; + + outheader.cupsInteger[14] = 0; /* VendorIdentifier */ + outheader.cupsInteger[15] = 0; /* VendorLength */ + + if ((val = cupsGetOption("print-content-optimize", num_options, + options)) != NULL) + { + if (!strcmp(val, "automatic")) + strlcpy(outheader.OutputType, "Automatic", + sizeof(outheader.OutputType)); + else if (!strcmp(val, "graphics")) + strlcpy(outheader.OutputType, "Graphics", sizeof(outheader.OutputType)); + else if (!strcmp(val, "photo")) + strlcpy(outheader.OutputType, "Photo", sizeof(outheader.OutputType)); + else if (!strcmp(val, "text")) + strlcpy(outheader.OutputType, "Text", sizeof(outheader.OutputType)); + else if (!strcmp(val, "text-and-graphics")) + strlcpy(outheader.OutputType, "TextAndGraphics", + sizeof(outheader.OutputType)); + else + { + fprintf(stderr, "DEBUG: Unsupported print-content-type \"%s\".\n", val); + outheader.OutputType[0] = '\0'; + } + } + + if ((val = cupsGetOption("print-quality", num_options, options)) != NULL) + { + int quality = atoi(val); /* print-quality value */ + + if (quality >= IPP_QUALITY_DRAFT && quality <= IPP_QUALITY_HIGH) + outheader.cupsInteger[8] = quality; + else + { + fprintf(stderr, "DEBUG: Unsupported print-quality %d.\n", quality); + outheader.cupsInteger[8] = 0; + } + } + + if ((val = cupsGetOption("print-rendering-intent", num_options, + options)) != NULL) + { + if (!strcmp(val, "absolute")) + strlcpy(outheader.cupsRenderingIntent, "Absolute", + sizeof(outheader.cupsRenderingIntent)); + else if (!strcmp(val, "automatic")) + strlcpy(outheader.cupsRenderingIntent, "Automatic", + sizeof(outheader.cupsRenderingIntent)); + else if (!strcmp(val, "perceptual")) + strlcpy(outheader.cupsRenderingIntent, "Perceptual", + sizeof(outheader.cupsRenderingIntent)); + else if (!strcmp(val, "relative")) + strlcpy(outheader.cupsRenderingIntent, "Relative", + sizeof(outheader.cupsRenderingIntent)); + else if (!strcmp(val, "relative-bpc")) + strlcpy(outheader.cupsRenderingIntent, "RelativeBpc", + sizeof(outheader.cupsRenderingIntent)); + else if (!strcmp(val, "saturation")) + strlcpy(outheader.cupsRenderingIntent, "Saturation", + sizeof(outheader.cupsRenderingIntent)); + else + { + fprintf(stderr, "DEBUG: Unsupported print-rendering-intent \"%s\".\n", + val); + outheader.cupsRenderingIntent[0] = '\0'; + } + } + + if (inheader.cupsPageSizeName[0] && + (pwg_size = _ppdCacheGetSize(cache, inheader.cupsPageSizeName)) != NULL) + { + strlcpy(outheader.cupsPageSizeName, pwg_size->map.pwg, + sizeof(outheader.cupsPageSizeName)); + } + else + { + pwg_media = _pwgMediaForSize((int)(2540.0 * inheader.cupsPageSize[0] / + 72.0), + (int)(2540.0 * inheader.cupsPageSize[1] / + 72.0)); + + if (pwg_media) + strlcpy(outheader.cupsPageSizeName, pwg_media->pwg, + sizeof(outheader.cupsPageSizeName)); + else + { + fprintf(stderr, "DEBUG: Unsupported PageSize %.2fx%.2f.\n", + inheader.cupsPageSize[0], inheader.cupsPageSize[1]); + outheader.cupsPageSizeName[0] = '\0'; + } + } + + if (inheader.Duplex && !(page & 1) && + back && _cups_strcasecmp(back->value, "Normal")) + { + if (_cups_strcasecmp(back->value, "Flipped")) + { + if (inheader.Tumble) + { + outheader.cupsInteger[1] = -1;/* CrossFeedTransform */ + outheader.cupsInteger[2] = 1; /* FeedTransform */ + + outheader.cupsInteger[3] = page_width - page_left - + inheader.cupsWidth; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_top; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_width - page_left; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_bottom; + /* ImageBoxBottom */ + } + else + { + outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ + outheader.cupsInteger[2] = -1;/* FeedTransform */ + + outheader.cupsInteger[3] = page_left; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_bottom; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_left + inheader.cupsWidth; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_top; + /* ImageBoxBottom */ + } + } + else if (_cups_strcasecmp(back->value, "ManualTumble")) + { + if (inheader.Tumble) + { + outheader.cupsInteger[1] = -1;/* CrossFeedTransform */ + outheader.cupsInteger[2] = -1;/* FeedTransform */ + + outheader.cupsInteger[3] = page_width - page_left - + inheader.cupsWidth; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_bottom; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_width - page_left; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_top; + /* ImageBoxBottom */ + } + else + { + outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ + outheader.cupsInteger[2] = 1; /* FeedTransform */ + + outheader.cupsInteger[3] = page_left; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_top; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_left + inheader.cupsWidth; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_bottom; + /* ImageBoxBottom */ + } + } + else if (_cups_strcasecmp(back->value, "Rotated")) + { + if (inheader.Tumble) + { + outheader.cupsInteger[1] = -1;/* CrossFeedTransform */ + outheader.cupsInteger[2] = -1;/* FeedTransform */ + + outheader.cupsInteger[3] = page_width - page_left - + inheader.cupsWidth; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_bottom; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_width - page_left; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_top; + /* ImageBoxBottom */ + } + else + { + outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ + outheader.cupsInteger[2] = 1; /* FeedTransform */ + + outheader.cupsInteger[3] = page_left; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_top; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_left + inheader.cupsWidth; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_bottom; + /* ImageBoxBottom */ + } + } + else + { + /* + * Unsupported value... + */ + + fprintf(stderr, "DEBUG: Unsupported cupsBackSide \"%s\".\n", back->value); + + outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ + outheader.cupsInteger[2] = 1; /* FeedTransform */ + + outheader.cupsInteger[3] = page_left; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_top; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_left + inheader.cupsWidth; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_bottom; + /* ImageBoxBottom */ + } + } + else + { + outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ + outheader.cupsInteger[2] = 1; /* FeedTransform */ + + outheader.cupsInteger[3] = page_left; + /* ImageBoxLeft */ + outheader.cupsInteger[4] = page_top; + /* ImageBoxTop */ + outheader.cupsInteger[5] = page_left + inheader.cupsWidth; + /* ImageBoxRight */ + outheader.cupsInteger[6] = page_height - page_bottom; + /* ImageBoxBottom */ + } + + if (!cupsRasterWriteHeader2(outras, &outheader)) + { + _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); + fprintf(stderr, "DEBUG: Unable to write header for page %d.\n", page); + return (1); + } + + /* + * Copy raster data... + */ + + line = malloc(linesize); + + memset(line, white, linesize); + for (y = page_top; y > 0; y --) + if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine)) + { + _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); + fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n", + page_top - y + 1, page); + return (1); + } + + for (y = inheader.cupsHeight; y > 0; y --) + { + cupsRasterReadPixels(inras, line + lineoffset, inheader.cupsBytesPerLine); + if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine)) + { + _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); + fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n", + inheader.cupsHeight - y + page_top + 1, page); + return (1); + } + } + + memset(line, white, linesize); + for (y = page_bottom; y > 0; y --) + if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine)) + { + _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); + fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n", + page_bottom - y + page_top + inheader.cupsHeight + 1, page); + return (1); + } + + free(line); + } + + cupsRasterClose(inras); + if (fd) + close(fd); + + cupsRasterClose(outras); + + return (0); +} + + +/* + * End of "$Id: rastertopwg.c 3427 2011-09-20 18:40:57Z msweet $". + */ diff --git a/filter/spec-ppd.header b/filter/spec-ppd.header new file mode 100644 index 0000000..40433aa --- /dev/null +++ b/filter/spec-ppd.header @@ -0,0 +1,32 @@ + + +

CUPS PPD Extensions

+ +

This specification describes the attributes and extensions that CUPS adds to Adobe TechNote #5003: PostScript Printer Description File Format Specification Version 4.3. PostScript Printer Description ("PPD") files describe the capabilities of each printer and are used by CUPS to support printer-specific features and intelligent filtering.

+ + diff --git a/filter/spec-ppd.shtml b/filter/spec-ppd.shtml new file mode 100644 index 0000000..b6bdcc4 --- /dev/null +++ b/filter/spec-ppd.shtml @@ -0,0 +1,1956 @@ +

PPD File Syntax

+ +

The PPD format is text-based and uses lines of up to 255 characters terminated by a carriage return, linefeed, or combination of carriage return and line feed. The following ABNF definition [RFC5234] defines the general format of lines in a PPD file:

+ +
+PPD-FILE = HEADER +(DATA / COMMENT / LINE-END)
+
+HEADER   = "*PPD-Adobe:" *WSP DQUOTE VERSION DQUOTE LINE-END
+
+VERSION  = "4.0" / "4.1" / "4.2" / "4.3"
+
+COMMENT  = "*%" *TCHAR LINE-END
+
+DATA     = "*" 1*KCHAR [ WSP 1*KCHAR [ "/" 1*TCHAR ] ] ":"
+           1*(*WSP VALUE) LINE-END
+
+VALUE    = 1*TCHAR / DQUOTE 1*SCHAR DQUOTE
+
+KCHAR    = ALPHA / DIGIT / "_" / "." / "-"
+
+SCHAR    = LINE-END / WSP / %x21.23-7E.A0-FF
+
+TCHAR    = %x20-7E.A0-FF
+
+LINE-END = CR / LF / CR LF
+
+ + +

Auto-Configuration

+ +

CUPS supports several methods of auto-configuration via PPD keywords.

+ +

OS X 10.5APAutoSetupTool

+ +

*APAutoSetupTool: "/LibraryPrinters/vendor/filename"

+ +

This OS X keyword defines a program that sets the default option choices. It is run when a printer is added from the Add Printer window or the Nearby Printers list in the Print dialog.

+ +

The program is provided with two arguments: the printer's device URI and the PPD file to be used for the printer. The program must write an updated PPD file to stdout.

+ +

Examples:

+ +
+*% Use our setup tool when adding a printer
+*APAutoSetupTool: "/Library/Printers/vendor/Tools/autosetuptool"
+
+ +

OS X 10.2/CUPS 1.4?MainKeyword

+ +

*?MainKeyword: "
+ PostScript query code that writes a message using the = operator...
+"
+*End

+ +

The ?MainKeyword keyword defines PostScript code that determines the currently selected/enabled option keyword (choice) for the main keyword (option). It is typically used when communicating with USB, serial, Appletalk, and AppSocket (port 9100) printers.

+ +

The PostScript code typically sends its response back using the = operator.

+ +

Example:

+ +
+*OpenUI OptionDuplex/Duplexer Installed: Boolean
+*DuplexOptionDuplex: False
+*OptionDuplex False/Not Installed: ""
+*OptionDuplex True/Installed: ""
+
+*% Query the printer for the presence of the duplexer option...
+*?OptionDuplex: "
+  currentpagedevice /Duplex known
+  {(True)} {(False)} ifelse
+  = flush
+"
+*End
+*CloseUI: OptionDuplex
+
+ +

OS X 10.4/CUPS 1.5OIDMainKeyword

+ +

*?OIDMainKeyword: ".n.n.n..."
+*OIDMainKeyword OptionKeyword1: "value"
+...
+*OIDMainKeyword OptionKeywordN: "value"

+ +

The OIDMainKeyword keyword is used to define SNMP OIDs that map to installable options. The first (query) line defines the OID to lookup on the network device. The second and subsequent keywords define a mapping from OID value to option keyword. Since SNMP is an IP-based network protocol, this method is typically only used to configure AppSocket, IPP, and LPD network printers.

+ +

Examples:

+ +
+*% Get the installed memory on the printer...
+*?OIDInstalledMemory: ".1.3.6.1.2.1.25.2.2.0"
+*OIDInstalledMemory 16MB: "16384 KBytes"
+*OIDInstalledMemory 32MB: "32768 KBytes"
+*OIDInstalledMemory 48MB: "49152 KBytes"
+*OIDInstalledMemory 72MB: "73728 KBytes"
+
+ + +

Color Profiles

+ +

CUPS supports three types of color profiles. The first type is based on sRGB and is used by the standard CUPS raster filters and GPL Ghostscript. The second type is based on ICC profiles and is used by the Quartz-based filters on MacOS X. The final type is based on well-known colorspaces such as sRGB and Adobe RGB.

+ +
Note: + +

At this time, none of the CUPS raster filters support ICC profiles. This will be addressed as time and resources permit.

+ +
+ +

DeprecatedcupsColorProfile

+ +

*cupsColorProfile Resolution/MediaType: "density gamma m00 m01 m02 m10 m11 m12 m20 m21 m22"

+ +

This string keyword specifies an sRGB-based color profile consisting of gamma and density controls and a 3x3 CMY color transform matrix. This keyword is not supported on OS X.

+ +

The Resolution and MediaType values may be "-" to act as a wildcard. Otherwise they must match one of the Resolution or MediaType option keywords defined in the PPD file.

+ +

The density and gamma values define gamma and +density adjustment function such that:

+ +
+f(x) = density * x gamma
+
+ +

The m00 through m22 values define a 3x3 transformation matrix for the CMY color values. The density function is applied after the CMY transformation:

+ +
+| m00 m01 m02 |
+| m10 m11 m12 |
+| m20 m21 m22 |
+
+ +

Examples:

+ +
+*% Specify a profile for printing at 360dpi on all media types
+*cupsColorProfile 360dpi/-: "1.0 1.5 1.0 0.0 -0.2 -0.4 1.0 0.0 -0.2 0.0 1.0"
+
+*% Specify a profile for printing at 720dpi on Glossy media
+*cupsColorProfile 720dpi/Glossy: "1.0 2.5 1.0 0.0 -0.2 -0.4 1.0 0.0 -0.2 0.0 1.0"
+
+*% Specify a default profile for printing at all other resolutions and media types
+*cupsColorProfile -/-: "0.9 2.0 1.0 0.0 -0.2 -0.4 1.0 0.0 -0.2 0.0 1.0"
+
+ + +

OS X 10.3/CUPS 1.2cupsICCProfile

+ +

*cupsICCProfile ColorModel.MediaType.Resolution/Description: "filename"

+ +

This keyword specifies an ICC color profile that is used to convert the document colors to the device colorspace. The ColorModel, MediaType, and Resolution option keywords specify a selector for color profiles. If omitted, the color profile will match any option keyword for the corresponding main keyword.

+ +

The Description specifies human-readable text that is associated with the color profile. The filename portion specifies the ICC color profile to use; if the filename is not absolute, it is loaded relative to the /usr/share/cups/profiles directory.

+ +

Examples:

+ +
+*% Specify a profile for CMYK printing at 360dpi on all media types
+*cupsICCProfile CMYK..360dpi/360dpi CMYK: "/Library/Printers/vendor/Profiles/foo-360-cmyk.icc"
+
+*% Specify a profile for RGB printing at 720dpi on Glossy media
+*cupsColorProfile RGB.Glossy.720dpi/720dpi Glossy: "/Library/Printers/vendor/Profiles/foo-720-glossy-rgb.icc"
+
+*% Specify a default profile for printing at all other resolutions and media types
+*cupsICCProfile ../Default: "/Library/Printers/vendor/Profiles/foo-default.icc"
+
+ +

Customizing the Profile Selection Keywords

+ +

The ColorModel, MediaType, and Resolution main keywords can be reassigned to different main keywords, allowing drivers to do color profile selection based on different parameters. The cupsICCQualifier1, cupsICCQualifier2, and cupsICCQualifier3 keywords define the mapping from selector to main keyword:

+ +
+*cupsICCQualifier1: MainKeyword1
+*cupsICCQualifier2: MainKeyword2
+*cupsICCQualifier3: MainKeyword3
+
+ +

The default mapping is as follows:

+ +
+*cupsICCQualifier1: ColorModel
+*cupsICCQualifier2: MediaType
+*cupsICCQualifier3: Resolution
+
+ +

OS X 10.4Custom Color Matching Support

+ +

*APSupportsCustomColorMatching: true
+*APCustomColorMatchingName name/text: ""
+*APCustomColorMatchingProfile: profile
+*APDefaultCustomColorMatchingProfile: profile

+ +

These keywords tell the OS X raster filters that the printer driver provides its own custom color matching and that generic color profiles should be used when generating 1-, 3-, and 4-component raster data as requested by the driver. The APCustomColorMatchingProfile and APDefaultColorMatchingProfile keywords specify alternate color profiles (sRGB or AdobeRGB) to use for 3-color (RGB) raster data.

+ +
Note: + +

Prior to OS X 10.6, the default RGB color space was Apple's "GenericRGB". The new default in OS X 10.6 and later is "sRGB". For more information, see "OS X v10.6: About gamma 2.2" on Apple's support site.

+ +
+ +

OS X 10.5APCustomColorMatchingName

+ +

*APCustomColorMatchingName name/text: ""

+ +

This keyword defines an alternate name for the color matching provided by a driver in the Color Matching print panel. The default is to use the name "Vendor Matching" or its localized equivalent.

+ +

Examples:

+ +
+*% Define the names for our color matching...
+*APCustomColorMatchingName name/AcmeColor(tm): ""
+*fr.APCustomColorMatchingName name/La AcmeColor(tm): ""
+
+ +

OS X 10.5APCustomColorMatchingProfile

+ +

*APCustomColorMatchingProfile: name

+ +

This keyword defines a supported RGB color profile that can be used when doing custom color matching. Currently only sRGB, AdobeRGB, and GenericRGB are supported. If not specified, RGB data will use the GenericRGB colorspace.

+ +
Note: + +

If you provide multiple APCustomColorMatchingProfile keywords, you are responsible for providing the necessary user interface controls to select the profile in a print dialog pane. Add the named profile to the print settings using the key kPMCustomColorMatchingProfileKey.

+ +
+ +

Examples:

+ +
+*% Use sRGB for RGB color by default, but support both sRGB and AdobeRGB
+*APSupportsCustomColorMatching: true
+*APDefaultCustomColorMatchingProfile: sRGB
+*APCustomColorMatchingProfile: sRGB
+*APCustomColorMatchingProfile: AdobeRGB
+
+ +

OS X 10.5APDefaultCustomColorMatchingProfile

+ +

*APDefaultCustomColorMatchingProfile: name

+ +

This keyword defines the default RGB color profile that will be used when doing custom color matching. Currently only sRGB, AdobeRGB, and GenericRGB are supported.

+ +

Examples:

+ +
+*% Use sRGB for RGB color by default
+*APSupportsCustomColorMatching: true
+*APDefaultCustomColorMatchingProfile: sRGB
+
+ +

OS X 10.4APSupportsCustomColorMatching

+ +

*APSupportsCustomColorMatching: boolean

+ +

This keyword specifies that the driver provides its own custom color matching. When true, the default hand-off colorspace will be GenericGray, GenericRGB, or GenericCMYK depending on the number of components the driver requests. The APDefaultCustomColorMatchingProfile keyword can be used to override the default 3-component (RGB) colorspace.

+ +

The default for APSupportsCustomColorMatching is false.

+ +

Examples:

+ +
+*APSupportsCustomColorMatching: true
+*APDefaultCustomColorMatchingProfile: sRGB
+
+ + +

Constraints

+ +

Constraints are option choices that are not allowed by the driver or device, for example printing 2-sided transparencies. All versions of CUPS support constraints defined by the legacy Adobe UIConstraints and NonUIConstraints keywords which support conflicts between any two option choices, for example:

+ +
+*% Do not allow 2-sided printing on transparency media
+*UIConstraints: "*Duplex *MediaType Transparency"
+*UIConstraints: "*MediaType Transparency *Duplex"
+
+ +

While nearly all constraints can be expressed using these keywords, there are valid scenarios requiring constraints between more than two option choices. In addition, resolution of constraints is problematic since users and software have to guess how a particular constraint is best resolved.

+ +

CUPS 1.4 and higher define two new keywords for constraints, cupsUIConstraints and cupsUIResolver. Each cupsUIConstraints keyword points to a cupsUIResolver keyword which specifies alternate options that resolve the conflict condition. The same cupsUIResolver can be used by multiple cupsUIConstraints.

+ +
Note: + +

When developing PPD files that contain constraints, it is very important to use the cupstestppd(1) program to verify that your constraints are accurate and cannot result in unresolvable option selections.

+ +
+ +

CUPS 1.4/OS X 10.6cupsUIConstraints

+ +

*cupsUIConstraints resolver: "*Keyword1 *Keyword2 ..."
+*cupsUIConstraints resolver: "*Keyword1 OptionKeyword1 *Keyword2 ..."
+*cupsUIConstraints resolver: "*Keyword1 *Keyword2 OptionKeyword2 ..."
+*cupsUIConstraints resolver: "*Keyword1 OptionKeyword1 *Keyword2 OptionKeyword2 ..."
+*cupsUIConstraints: "*InstallableKeyword1 OptionKeyword1 *Keyword2 OptionKeyword2 ..."

+ +

Lists two or more options which conflict. The "resolver" string is a (possibly unique) keyword which specifies which options to change when the constraint exists. When no resolver is provided, CUPS first tries the default choice followed by testing each option choice to resolve the conflict.

+ +

Examples:

+ +
+*% Specify that 2-sided printing cannot happen on transparencies
+*cupsUIConstraints transparency: "*Duplex *MediaType Transparency"
+
+*% Specify that envelope printing cannot happen from the paper trays
+*cupsUIConstraints envelope: "*PageSize Env10 *InputSlot Tray1"
+*cupsUIConstraints envelope: "*PageSize Env10 *InputSlot Tray1"
+*cupsUIConstraints envelope: "*PageSize EnvDL *InputSlot Tray2"
+*cupsUIConstraints envelope: "*PageSize EnvDL *InputSlot Tray2"
+
+*% Specify an installable option constraint for the envelope feeder
+*cupsUIConstraints: "*InputSlot EnvFeeder *InstalledEnvFeeder"
+
+*% Specify that photo printing cannot happen on plain paper or transparencies at 1200dpi
+*cupsUIConstraints photo: "*OutputMode Photo *MediaType Plain *Resolution 1200dpi"
+*cupsUIConstraints photo: "*OutputMode Photo *MediaType Transparency *Resolution 1200dpi"
+
+ +

CUPS 1.4/OS X 10.6cupsUIResolver

+ +

*cupsUIResolver resolver: "*Keyword1 OptionKeyword1 *Keyword2 OptionKeyword2 ..."

+ +

Specifies two or more options to mark/select to resolve a constraint. The "resolver" string identifies a particular action to take for one or more cupsUIConstraints. The same action can be used for multiple constraints. The option keyword pairs are treated as an ordered list of option selections to try - only the first N selections will be used, where N is the minimum number of selections required. Because cupsResolveConflicts() will not change the most recent option selection passed to it, at least two options from the constraints must be listed to avoid situations where conflicts cannot be resolved.

+ +

Examples:

+ +
+*% Specify the options to change for the 2-sided transparency constraint
+*cupsUIResolver transparency: "*Duplex None *MediaType Plain"
+
+*% Specify the options to change for the envelope printing constraints.  Notice
+*% that we try to change the InputSlot to either the envelope feeder or the
+*% manual feed first, then we change the page size...
+*cupsUIResolver envelope: "*InputSlot EnvFeeder *InputSlot ManualFeed *PageSize Letter"
+
+*% Specify the options to change for the photo printing constraints
+*cupsUIResolver photo: "*OutputMode Best *Resolution 600dpi"
+
+ + +

Globalized PPD Support

+ +

CUPS 1.2 and higher adds support for PPD files containing multiple languages by following the following additional rules:

+ +
    + +
  1. The LanguageVersion MUST be English
  2. + +
  3. The LanguageEncoding MUST be ISOLatin1
  4. + +
  5. The cupsLanguages keyword MUST be provided and list each of the supported locales in the PPD file
  6. + +
  7. Main and option keywords MUST NOT exceed 34 (instead of 40) characters to allow room for the locale prefixes in translation keywords
  8. + +
  9. The main keyword "Translation" MUST NOT be used
  10. + +
  11. Translation strings included with the main and option keywords MUST NOT contain characters outside the ASCII subset of ISOLatin1 and UTF-8; developers wishing to use characters outside ASCII MUST provide a separate set of English localization keywords for the affected keywords.
  12. + +
  13. Localizations are specified using a locale prefix of the form "ll" or "ll_CC." where "ll" is the 2-letter ISO language code and "CC" is the 2-letter ISO country code
      +
    • A generic language translation ("ll") SHOULD be provided with country-specific differences ("ll_CC") provided only as needed
    • +
    • For historical reasons, the "zh" and "zh_CN" locales map to Simplified Chinese while the "zh_TW" locale maps to Traditional Chinese
    • +
  14. + +
  15. Locale-specific translation strings MUST be encoded using UTF-8.
  16. + +
  17. Main keywords MUST be localized using one of the following forms: +

    *ll.Translation MainKeyword/translation text: ""
    + *ll_CC.Translation MainKeyword/translation text: ""

  18. + +
  19. Option keywords MUST be localized using one of the following forms: +

    *ll.MainKeyword OptionKeyword/translation text: ""
    + *ll_CC.MainKeyword OptionKeyword/translation text: ""

  20. + +
  21. Localization keywords MAY appear anywhere after the first line of the PPD file
  22. + +
+ +
Note: + +

We use a LanguageEncoding value of ISOLatin1 and limit the allowed base translation strings to ASCII to avoid character coding issues that would otherwise occur. In addition, requiring the base translation strings to be in English allows for easier fallback translation when no localization is provided in the PPD file for a given locale.

+ +
+ +

Examples:

+ +
+*LanguageVersion: English
+*LanguageEncoding: ISOLatin1
+*cupsLanguages: "de fr_CA"
+*ModelName: "Foobar Laser 9999"
+
+*% Localize ModelName for French and German
+*fr_CA.Translation ModelName/La Foobar Laser 9999: ""
+*de.Translation ModelName/Foobar LaserDrucken 9999: ""
+
+*cupsIPPReason com.vendor-error/A serious error occurred: "/help/com.vendor/error.html"
+*% Localize printer-state-reason for French and German
+*fr_CA.cupsIPPReason com.vendor-error/Une erreur sèrieuse s'est produite: "/help/com.vendor/error.html"
+*de.cupsIPPReason com.vendor-error/Eine ernste Störung trat: "/help/com.vendor/error.html"
+
+...
+
+*OpenUI *InputSlot/Paper Source: PickOne
+*OrderDependency: 10 AnySetup *InputSlot
+*DefaultInputSlot: Auto
+*% Localize InputSlot for French and German
+*fr_CA.Translation InputSlot/Papier source: ""
+*de.Translation InputSlot/Papiereinzug: ""
+*InputSlot Auto/Default: "<</ManualFeed false>>setpagedevice"
+*% Localize InputSlot=Auto for French and German
+*fr_CA.InputSlot Auto/Par Defaut: ""
+*de.InputSlot Auto/Standard: ""
+*InputSlot Manual/Manual Feed: "<</ManualFeed true>>setpagedevice"
+*% Localize InputSlot=Manual for French and German
+*fr_CA.InputSlot Manual/Manuel mecanisme de alimentation: ""
+*de.InputSlot Manual/Manueller Einzug: ""
+*CloseUI: *InputSlot
+
+ + +

CUPS 1.3/OS X 10.6Custom Options

+ +

CUPS supports custom options using an extension of the CustomPageSize and ParamCustomPageSize syntax:

+ +
+*CustomFoo True: "command"
+*ParamCustomFoo Name1/Text 1: order type minimum maximum
+*ParamCustomFoo Name2/Text 2: order type minimum maximum
+...
+*ParamCustomFoo NameN/Text N: order type minimum maximum
+
+ +

When the base option is part of the JCLSetup section, the "command" string contains JCL commands with "\order" placeholders for each numbered parameter. The CUPS API handles any necessary value quoting for HP-PJL commands. For example, if the JCL command string is "@PJL SET PASSCODE=\1" and the first +option value is "1234" then CUPS will output the string "@PJL SET PASSCODE=1234".

+ +

For non-JCLSetup options, the "order" value is a number from 1 to N and specifies the order of values as they are placed on the stack before the command. For example, if the PostScript command string is "<</cupsReal1 2 1 roll>>setpagedevice" and the option value is "2.0" then CUPS will output the string "2.0 <</cupsReal1 2 1 roll>>setpagedevice".

+ +

The "type" is one of the following keywords:

+ +
    + +
  • curve - a real value from "minimum" to "maximum" representing a gamma correction curve using the function: f(x) = x value
  • + +
  • int - an integer value from "minimum" to "maximum"
  • + +
  • invcurve - a real value from "minimum" to "maximum" representing a gamma correction curve using the function: f(x) = x 1 / value
  • + +
  • passcode - a string of numbers value with a minimum of "minimum" numbers and a maximum of "maximum" numbers ("minimum" and "maximum" are numbers and passcode strings are not displayed in the user interface)
  • + +
  • password - a string value with a minimum of "minimum" characters and a maximum of "maximum" characters ("minimum" and "maximum" are numbers and password strings are not displayed in the user interface)
  • + +
  • points - a measurement value in points from "minimum" to "maximum"
  • + +
  • real - a real value from "minimum" to "maximum"
  • + +
  • string - a string value with a minimum of "minimum" characters and a maximum of "maximum" characters ("minimum" and "maximum" are numbers)
  • + +
+ +

Examples:

+ +
+*% Base JCL key code option
+*JCLOpenUI JCLPasscode/Key Code: PickOne
+*OrderDependency: 10 JCLSetup *JCLPasscode
+*DefaultJCLPasscode: None
+*JCLPasscode None/No Code: ""
+*JCLPasscode 1111: "@PJL SET PASSCODE = 1111<0A>"
+*JCLPasscode 2222: "@PJL SET PASSCODE = 2222<0A>"
+*JCLPasscode 3333: "@PJL SET PASSCODE = 3333<0A>"
+*JCLCloseUI: *JCLPasscode
+
+*% Custom JCL key code option
+*CustomJCLPasscode True: "@PJL SET PASSCODE = \1<0A>"
+*ParamCustomJCLPasscode Code/Key Code: 1 passcode 4 4
+
+
+*% Base PostScript watermark option
+*OpenUI WatermarkText/Watermark Text: PickOne
+*OrderDependency: 10 AnySetup *WatermarkText
+*DefaultWatermarkText: None
+*WatermarkText None: ""
+*WatermarkText Draft: "<</cupsString1(Draft)>>setpagedevice"
+*CloseUI: *WatermarkText
+
+*% Custom PostScript watermark option
+*CustomWatermarkText True: "<</cupsString1 3 -1 roll>>setpagedevice"
+*ParamCustomWatermarkText Text: 1 string 0 32
+
+
+*% Base PostScript gamma/density option
+*OpenUI GammaDensity/Gamma and Density: PickOne
+*OrderDependency: 10 AnySetup *GammaDensity
+*DefaultGammaDensity: Normal
+*GammaDensity Normal/Normal: "<</cupsReal1 1.0/cupsReal2 1.0>>setpagedevice"
+*GammaDensity Light/Lighter: "<</cupsReal1 0.9/cupsReal2 0.67>>setpagedevice"
+*GammaDensity Dark/Darker: "<</cupsReal1 1.1/cupsReal2 1.5>>setpagedevice"
+*CloseUI: *GammaDensity
+
+*% Custom PostScript gamma/density option
+*CustomGammaDensity True: "<</cupsReal1 3 -1 roll/cupsReal2 5 -1>>setpagedevice"
+*ParamCustomGammaDensity Gamma: 1 curve 0.1 10
+*ParamCustomGammaDensity Density: 2 real 0 2
+
+ + +

Writing PostScript Option Commands for Raster Drivers

+ +

PPD files are used for both PostScript and non-PostScript printers. For CUPS raster drivers, you use a subset of the PostScript language to set page device keywords such as page size, resolution, and so forth. For example, the following code sets the page size to A4 size:

+ +
+*PageSize A4: "<</PageSize[595 842]>>setpagedevice"
+
+ +

Custom options typically use other operators to organize the values into a key/value dictionary for setpagedevice. For example, our previous CustomWatermarkText option code uses the roll operator to move the custom string value into the dictionary for setpagedevice:

+ +
+*CustomWatermarkText True: "<</cupsString1 3 -1 roll>>setpagedevice"
+
+ +

For a custom string value of "My Watermark", CUPS will produce the following PostScript code for the option:

+ +
+(My Watermark)
+<</cupsString1 3 -1 roll>>setpagedevice
+
+ +

The code moves the string value ("My Watermark") from the bottom of the stack to the top, creating a dictionary that looks like:

+ +
+<</cupsString1(My Watermark)>>setpagedevice
+
+ +

The resulting dictionary sets the page device attributes that are sent to your raster driver in the page header.

+ +

Custom Page Size Code

+ +

There are many possible implementations of the CustomPageSize code. For CUPS raster drivers, the following code is recommended:

+ +
+*ParamCustomPageSize Width:        1 points min-width max-width
+*ParamCustomPageSize Height:       2 points min-height max-height
+*ParamCustomPageSize WidthOffset:  3 points 0 0
+*ParamCustomPageSize HeightOffset: 4 points 0 0
+*ParamCustomPageSize Orientation:  5 int 0 0
+*CustomPageSize True: "pop pop pop <</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice"
+
+ +

Supported PostScript Operators

+ +

CUPS supports the following PostScript operators in addition to the usual PostScript number, string (literal and hex-encoded), boolean, null, and name values:

+ +
    + +
  • << - Start a dictionary.
  • + +
  • >> - End a dictionary.
  • + +
  • [ - Start an array.
  • + +
  • ] - End an array.
  • + +
  • copy - Copy the top N objects on the stack.
  • + +
  • dup - Copy the top object on the stack.
  • + +
  • index - Copy the Nth from the top object on the stack.
  • + +
  • pop - Pop the top object on the stack.
  • + +
  • roll - Shift the top N objects on the stack.
  • + +
  • setpagedevice - Set the page header values according to the key/value dictionary on the stack.
  • + +
+ +
Note: + +

Never use the unsupported dict or put +operators in your option code. These operators are typically used in +option code dating back to Level 1 PostScript printers, which did not +support the simpler << or >> operators. +If you have old option code using dict or put, you can +rewrite it very easily to use the newer << and +>> operators instead. For example, the following code +to set the page size:

+ + + +
+1 dict dup /PageSize [612 792] put setpagedevice
+
+ +

can be rewritten as:

+ +
+<< /PageSize [612 792] >> setpagedevice
+
+ +
+ +

Supported Page Device Attributes

+ +

Table 2 shows the supported page device attributes along with PostScript code examples.

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 2: Supported Page Device Attributes
Name(s)TypeDescriptionExample(s)
AdvanceDistanceIntegerSpecifies the number of points to advance roll media after printing.<</AdvanceDistance 18>>setpagedevice
AdvanceMediaIntegerSpecifies when to advance the media: 0 = never, 1 = after the file, 2 = after the job, 3 = after the set, and 4 = after the page.<</AdvanceMedia 4>>setpagedevice
CollateBooleanSpecifies whether collated copies are required.<</Collate true>>setpagedevice
CutMediaIntegerSpecifies when to cut the media: 0 = never, 1 = after the file, 2 = after the job, 3 = after the set, and 4 = after the page.<</CutMedia 1>>setpagedevice
DuplexBooleanSpecifies whether 2-sided printing is required.<</Duplex true>>setpagedevice
HWResolutionInteger ArraySpecifies the resolution of the page image in pixels per inch.<</HWResolution[1200 1200]>>setpagedevice
InsertSheetBooleanSpecifies whether to insert a blank sheet before the job.<</InsertSheet true>>setpagedevice
JogIntegerSpecifies when to shift the media in the output bin: 0 = never, 1 = after the file, 2 = after the job, 3 = after the set, and 4 = after the page.<</Jog 2>>setpagedevice
LeadingEdgeIntegerSpecifies the leading edge of the media: 0 = top, 1 = right, 2 = bottom, 3 = left.<</LeadingEdge 0>>setpagedevice
ManualFeedBooleanSpecifies whether media should be drawn from the manual feed tray. Note: The MediaPosition attribute is preferred over the ManualFeed attribute.<</ManualFeed true>>setpagedevice
MediaClassStringSpecifies a named media.<</MediaClass (Invoices)>>setpagedevice
MediaColorStringSpecifies the color of the media.<</MediaColor >>setpagedevice
MediaPositionIntegerSpecifies the tray or source of the media.<</MediaPosition 12>>setpagedevice
MediaTypeStringSpecifies the general media type.<</MediaType (Glossy)>>setpagedevice
MediaWeightIntegerSpecifies the media weight in grams per meter2.<</MediaWeight 100>>setpagedevice
MirrorPrintBooleanSpecifies whether to flip the output image horizontally.<</MirrorPrint true>>setpagedevice
NegativePrintBooleanSpecifies whether to invert the output image.<</NegativePrint true>>setpagedevice
NumCopiesIntegerSpecifies the number of copies to produce of each page.<</NumCopies 100>>setpagedevice
OrientationIntegerSpecifies the orientation of the output: 0 = portrait, 1 = landscape rotated counter-clockwise, 2 = upside-down, 3 = landscape rotated clockwise.<</Orientation 3>>setpagedevice
OutputFaceUpBooleanSpecifies whether to place the media face-up in the output bin/tray.<</OutputFaceUp true>>setpagedevice
OutputTypeStringSpecifies the output type name.<</OutputType (Photo)>>setpagedevice
PageSizeInteger/Real ArraySpecifies the width and length/height of the page in points.<</PageSize[595 842]>>setpagedevice
SeparationsBooleanSpecifies whether to produce color separations.<</Separations true>>setpagedevice
TraySwitchBooleanSpecifies whether to switch trays automatically.<</TraySwitch true>>setpagedevice
TumbleBooleanSpecifies whether the back sides of pages are rotated 180 degrees.<</Tumble true>>setpagedevice
cupsBorderlessScalingFactorRealSpecifies the amount to scale the page image dimensions.<</cupsBorderlessScalingFactor 1.01>>setpagedevice
cupsColorOrderIntegerSpecifies the order of colors: 0 = chunked, 1 = banded, 2 = planar.<</cupsColorOrder 0>>setpagedevice
cupsColorSpaceIntegerSpecifies the page image colorspace: 0 = W, 1 = RGB, 2 = RGBA, 3 = K, 4 = CMY, 5 = YMC, 6 = CMYK, 7 = YMCK, 8 = KCMY, 9 = KCMYcm, 10 = GMCK, 11 = GMCS, 12 = White, 13 = Gold, 14 = Silver, 15 = CIE XYZ, 16 = CIE Lab, 17 = RGBW, 32 to 46 = CIE Lab (1 to 15 inks)<</cupsColorSpace 1 >>setpagedevice
cupsCompressionIntegerSpecifies a driver compression type/mode.<</cupsCompression 2>>setpagedevice
cupsInteger0
+ ...
+ cupsInteger15
IntegerSpecifies driver integer values.<</cupsInteger11 1234>>setpagedevice
cupsMarkerTypeStringSpecifies the type of ink/toner to use.<</cupsMarkerType (Black+Color)>>setpagedevice
cupsMediaTypeIntegerSpecifies a numeric media type.<</cupsMediaType 999>>setpagedevice
cupsPageSizeNameStringSpecifies the name of the page size.<</cupsPageSizeName (A4.Full)>>setpagedevice
cupsPreferredBitsPerColorIntegerSpecifies the preferred number of bits per color, typically 8 or 16.<</cupsPreferredBitsPerColor 16>>setpagedevice
cupsReal0
+ ...
+ cupsReal15
RealSpecifies driver real number values.<</cupsReal15 1.234>>setpagedevice
cupsRenderingIntentStringSpecifies the color rendering intent.<</cupsRenderingIntent (AbsoluteColorimetric)>>setpagedevice
cupsRowCountIntegerSpecifies the number of rows of raster data to print on each line for some drivers.<</cupsRowCount 24>>setpagedevice
cupsRowFeedIntegerSpecifies the number of rows to feed between passes for some drivers.<</cupsRowFeed 17>>setpagedevice
cupsRowStepIntegerSpecifies the number of lines between columns/rows on the print head for some drivers.<</cupsRowStep 2>>setpagedevice
cupsString0
+ ...
+ cupsString15
StringSpecifies driver string values.<</cupsString0(String Value)>>setpagedevice
+ + +

Media Keywords

+ +

The CUPS media keywords allow drivers to specify alternate custom page +size limits based on up to two options.

+ +

CUPS 1.4/OS X 10.6cupsMediaQualifier2

+ +

*cupsMediaQualifier2: MainKeyword

+ +

This keyword specifies the second option to use for overriding the +custom page size limits.

+ +

Example:

+ +
+*% Specify alternate custom page size limits based on InputSlot and Quality
+*cupsMediaQualifier2: InputSlot
+*cupsMediaQualifier3: Quality
+*cupsMaxSize .Manual.: "1000 1000"
+*cupsMinSize .Manual.: "100 100"
+*cupsMinSize .Manual.Photo: "200 200"
+*cupsMinSize ..Photo: "300 300"
+
+ +

CUPS 1.4/OS X 10.6cupsMediaQualifier3

+ +

*cupsMediaQualifier3: MainKeyword

+ +

This keyword specifies the third option to use for overriding the +custom page size limits.

+ +

Example:

+ +
+*% Specify alternate custom page size limits based on InputSlot and Quality
+*cupsMediaQualifier2: InputSlot
+*cupsMediaQualifier3: Quality
+*cupsMaxSize .Manual.: "1000 1000"
+*cupsMinSize .Manual.: "100 100"
+*cupsMinSize .Manual.Photo: "200 200"
+*cupsMinSize ..Photo: "300 300"
+
+ +

CUPS 1.4/OS X 10.6cupsMinSize

+ +

*cupsMinSize .Qualifier2.Qualifier3: "width length"
+*cupsMinSize .Qualifier2.: "width length"
+*cupsMinSize ..Qualifier3: "width length"

+ +

This keyword specifies alternate minimum custom page sizes in points. +The cupsMediaQualifier2 and +cupsMediaQualifier3 keywords +are used to identify options to use for matching.

+ +

Example:

+ +
+*% Specify alternate custom page size limits based on InputSlot and Quality
+*cupsMediaQualifier2: InputSlot
+*cupsMediaQualifier3: Quality
+*cupsMaxSize .Manual.: "1000 1000"
+*cupsMinSize .Manual.: "100 100"
+*cupsMinSize .Manual.Photo: "200 200"
+*cupsMinSize ..Photo: "300 300"
+
+ +

CUPS 1.4/OS X 10.6cupsMaxSize

+ +

*cupsMaxSize .Qualifier2.Qualifier3: "width length"
+*cupsMaxSize .Qualifier2.: "width length"
+*cupsMaxSize ..Qualifier3: "width length"

+ +

This keyword specifies alternate maximum custom page sizes in points. +The cupsMediaQualifier2 and +cupsMediaQualifier3 keywords +are used to identify options to use for matching.

+ +

Example:

+ +
+*% Specify alternate custom page size limits based on InputSlot and Quality
+*cupsMediaQualifier2: InputSlot
+*cupsMediaQualifier3: Quality
+*cupsMaxSize .Manual.: "1000 1000"
+*cupsMinSize .Manual.: "100 100"
+*cupsMinSize .Manual.Photo: "200 200"
+*cupsMinSize ..Photo: "300 300"
+
+ + +

CUPS 1.4/OS X 10.6cupsPageSizeCategory

+ +

*cupsPageSizeCategory name/text: "name name2 ... nameN"

+ +

This keyword lists related paper size names that should be grouped together in the Print or Page Setup dialogs. The "name" portion of the keyword specifies the root/default size for the grouping. On OS X the grouped paper sizes are shown in a submenu of the main paper size. When omitted, sizes with the same dimensions are automatically grouped together, for example "Letter" and "Letter.Borderless".

+ +

Example:

+ +
+*% Specify grouping of borderless/non-borderless sizes
+*cupsPageSizeCategory Letter/US Letter: "Letter Letter.Borderless"
+*cupsPageSizeCategory A4/A4: "A4 A4.Borderless"
+
+ + +

General Attributes

+ +

CUPS 1.3/OS X 10.5cupsBackSide

+ +

*cupsBackSide: keyword

+ +

This keyword requests special handling of the back side of pages +when doing duplexed (2-sided) output. Table 1 +shows the supported keyword values for this keyword and their effect +on the raster data sent to your driver. For example, when cupsBackSide +is Rotated and Tumble is false, your driver +will receive print data starting at the bottom right corner of the page, with +each line going right-to-left instead of left-to-right. The default value is +Normal.

+ +
Note: + +

cupsBackSide replaces the older cupsFlipDuplex +keyword - if cupsBackSide is specified, cupsFlipDuplex +will be ignored.

+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 1: Back Side Raster Coordinate System
cupsBackSideTumble ValueImage Presentation
NormalfalseLeft-to-right, top-to-bottom
NormaltrueLeft-to-right, top-to-bottom
ManualTumblefalseLeft-to-right, top-to-bottom
ManualTumbletrueRight-to-left, bottom-to-top
RotatedfalseRight-to-left, bottom-to-top
RotatedtrueRight-to-left, top-to-bottom
Flipped *falseLeft-to-right, bottom-to-top
Flipped *trueRight-to-left, top-to-bottom
+
+ +

* - Not supported in OS X 10.5.x and earlier

+ +
+ + +
Figure 1: Back side images
Back side images
+ +

Examples:

+ +
+*% Flip the page image for the back side of duplexed output
+*cupsBackSide: Flipped
+
+*% Rotate the page image for the back side of duplexed output
+*cupsBackSide: Rotated
+
+ +

Also see the related APDuplexRequiresFlippedMargin +keyword.

+ +

CUPS 1.4/OS X 10.6cupsCommands

+ +

*cupsCommands: "name name2 ... nameN"

+ +

This string keyword specifies the commands that are supported by the +CUPS command file filter for this device. The command names are separated +by whitespace.

+ +

Example:

+ +
+*% Specify the list of commands we support
+*cupsCommands: "AutoConfigure Clean PrintSelfTestPage ReportLevels com.vendor.foo"
+
+ + +

CUPS 1.3/OS X 10.5cupsEvenDuplex

+ +

*cupsEvenDuplex: boolean

+ +

This boolean keyword notifies the RIP filters that the +destination printer requires an even number of pages when 2-sided +printing is selected. The default value is false.

+ +

Example:

+ +
+*% Always send an even number of pages when duplexing
+*cupsEvenDuplex: true
+
+ +

cupsFax

+ +

*cupsFax: boolean

+ +

This boolean keyword specifies whether the PPD defines a facsimile device. The default is false.

+ +

Examples:

+ +
+*cupsFax: true
+
+ +

cupsFilter

+ +

*cupsFilter: "source/type cost program"

+ +

This string keyword provides a conversion rule from the +given source type to the printer's native format using the +filter "program". If a printer supports the source type directly, +the special filter program "-" may be specified.

+ +

Examples:

+ +
+*% Standard raster printer driver filter
+*cupsFilter: "application/vnd.cups-raster 100 rastertofoo"
+
+*% Plain text filter
+*cupsFilter: "text/plain 10 texttofoo"
+
+*% Pass-through filter for PostScript printers
+*cupsFilter: "application/vnd.cups-postscript 0 -"
+
+ +

CUPS 1.5cupsFilter2

+ +

*cupsFilter2: "source/type destination/type cost program"

+ +

This string keyword provides a conversion rule from the given source type to the printer's native format using the filter "program". If a printer supports the source type directly, the special filter program "-" may be specified. The destination type is automatically created as needed and is passed to the filters and backend as the FINAL_CONTENT_TYPE value.

+ +
Note: + +

The presence of a single cupsFilter2 keyword in the PPD file will hide any cupsFilter keywords from the CUPS scheduler. When using cupsFilter2 to provide filters specific for CUPS 1.5 and later, provide a cupsFilter2 line for every filter and a cupsFilter line for each filter that is compatible with older versions of CUPS.

+ +
+ +

Examples:

+ +
+*% Standard raster printer driver filter
+*cupsFilter2: "application/vnd.cups-raster application/vnd.foo 100 rastertofoo"
+
+*% Plain text filter
+*cupsFilter2: "text/plain application/vnd.foo 10 texttofoo"
+
+*% Pass-through filter for PostScript printers
+*cupsFilter2: "application/vnd.cups-postscript application/postscript 0 -"
+
+ +

DeprecatedcupsFlipDuplex

+ +

*cupsFlipDuplex: boolean

+ +

Due to implementation differences between OS X and Ghostscript, +the cupsFlipDuplex keyword is deprecated. Instead, use +the cupsBackSide keyword to specify +the coordinate system (pixel layout) of the page data on the back side of +duplex pages.

+ +

The value true maps to a cupsBackSide value +of Rotated on OS X and Flipped with +Ghostscript.

+ +

The default value is false.

+ +
Note: + +

OS X drivers that previously used +cupsFlipDuplex may wish to provide both the old and +new keywords for maximum compatibility, for example:

+ +
+*cupsBackSide: Rotated
+*cupsFlipDuplex: true
+
+ +

Similarly, drivers written for other operating systems using +Ghostscript can use:

+ +
+*cupsBackSide: Flipped
+*cupsFlipDuplex: true
+
+ +

CUPS 1.3/OS X 10.5cupsIPPFinishings

+ +

*cupsIPPFinishings number/text: "*Option Choice ..."

+ +

This keyword defines a mapping from IPP finishings +values to PPD options and choices.

+ +

Examples:

+ +
+*cupsIPPFinishings 4/staple: "*StapleLocation SinglePortrait"
+*cupsIPPFinishings 5/punch: "*PunchMedia Yes *PunchLocation LeftSide"
+*cupsIPPFinishings 20/staple-top-left: "*StapleLocation SinglePortrait"
+*cupsIPPFinishings 21/staple-bottom-left: "*StapleLocation SingleLandscape"
+
+ +

CUPS 1.3/OS X 10.5cupsIPPReason

+ +

*cupsIPPReason reason/Reason Text: "optional URIs"

+ +

This optional keyword maps custom +printer-state-reasons keywords that are generated by +the driver to human readable text. The optional URIs string +contains zero or more URIs separated by a newline. Each URI can +be a CUPS server absolute path to a help file under the +scheduler's DocumentRoot directory, a full HTTP URL +("http://www.domain.com/path/to/help/page.html"), or any other +valid URI which directs the user at additional information +concerning the condition that is being reported.

+ +

Since the reason text is limited to 80 characters by the PPD specification, longer text strings can be included by URI-encoding the text with the "text" scheme, for example "text:some%20text". Multiple text URIs are combined by the ppdLocalizeIPPReason into a single string that can be displayed to the user.

+ +

Examples:

+ +
+*% Map com.vendor-error to text but no page
+*cupsIPPReason com.vendor-error/A serious error occurred: ""
+
+*% Map com.vendor-error to more than 80 characters of text but no page
+*cupsIPPReason com.vendor-error/A serious error occurred: "text:Now%20is%20the%20time
+text:for%20all%20good%20men%20to%20come%20to%20the%20aid%20of%20their%20country."
+
+*% Map com.vendor-error to text and a local page
+*cupsIPPReason com.vendor-error/A serious error occurred: "/help/com.vendor/error.html"
+
+*% Map com.vendor-error to text and a remote page
+*cupsIPPReason com.vendor-error/A serious error occurred: "http://www.vendor.com/help"
+
+*% Map com.vendor-error to text and a local, Apple help book, and remote page
+*APHelpBook: "file:///Library/Printers/vendor/Help.bundle"
+*cupsIPPReason com.vendor-error/A serious error occurred: "/help/com.vendor/error.html
+help:anchor='com.vendor-error'%20bookID=Vendor%20Help
+http://www.vendor.com/help"
+*End
+
+ +

CUPS 1.5cupsIPPSupplies

+ +

*cupsIPPSupplies: boolean

+ +

This keyword tells the IPP backend whether it should report the current marker-xxx supply attribute values. The default value is True. + +

Example:

+ +
+*% Do not use IPP marker-xxx attributes to report supply levels
+*cupsIPPSupplies: False
+
+ +

CUPS 1.2/OS X 10.5cupsLanguages

+ +

*cupsLanguages: "locale list"

+ +

This keyword describes which language localizations are +included in the PPD. The "locale list" string is a space-delimited +list of locale names ("en", "en_US", "fr_CA", etc.)

+ +

Example:

+ +
+*% Specify Canadian, UK, and US English, and Canadian and French French
+*cupsLanguages: "en_CA en_UK en_US fr_CA fr_FR"
+
+ +

cupsManualCopies

+ +

*cupsManualCopies: boolean

+ +

This boolean keyword notifies the RIP filters that the +destination printer does not support copy generation in +hardware. The default value is false.

+ +

Example:

+ +
+*% Tell the RIP filters to generate the copies for us
+*cupsManualCopies: true
+
+ +

CUPS 1.4/OS X 10.6cupsMarkerName

+ +

*cupsMarkerName/Name Text: ""

+ +

This optional keyword maps marker-names strings that are +generated by the driver to human readable text.

+ +

Examples:

+ +
+*% Map cyanToner to "Cyan Toner"
+*cupsMarkerName cyanToner/Cyan Toner: ""
+
+ +

CUPS 1.4/OS X 10.6cupsMarkerNotice

+ +

*cupsMarkerNotice: "disclaimer text"

+ +

This optional keyword provides disclaimer text for the supply level +information provided by the driver, typically something like "supply levels +are approximate".

+ +

Examples:

+ +
+*cupsMarkerNotice: "Supply levels are approximate."
+
+ +

CUPS 1.6/OS X 10.8cupsMaxCopies

+ +

*cupsMaxCopies: integer

+ +

This integer keyword notifies the filters that the destination printer supports up to N copies in hardware. The default value is 9999.

+ +

Example:

+ +
+*% Tell the RIP filters we can do up to 99 copies
+*cupsMaxCopies: 99
+
+ +

cupsModelNumber

+ +

*cupsModelNumber: number

+ +

This integer keyword specifies a printer-specific model +number. This number can be used by a filter program to adjust +the output for a specific model of printer.

+ +

Example:

+ +
+*% Specify an integer for a driver-specific model number
+*cupsModelNumber: 1234
+
+ + +

CUPS 1.3/OS X 10.5cupsPJLCharset

+ +

*cupsPJLCharset: "ISO character set name"

+ +

This string keyword specifies the character set that is used +for strings in PJL commands. If not specified, US-ASCII is +assumed.

+ +

Example:

+ +
+*% Specify UTF-8 is used in PJL strings
+*cupsPJLCharset: "UTF-8"
+
+ +

CUPS 1.4/OS X 10.6cupsPJLDisplay

+ +

*cupsPJLDisplay: "what"

+ +

This optional keyword specifies which command is used to display the +job ID, name, and user on the printer's control panel. "What" is either "none" +to disable this functionality, "job" to use "@PJL JOB DISPLAY", or "rdymsg" +to use "@PJL RDYMSG DISPLAY". The default is "job".

+ +

Examples:

+ +
+*% Display job information using @PJL SET RDYMSG DISPLAY="foo"
+*cupsPJLDisplay: "rdymsg"
+
+*% Display job information display
+*cupsPJLDisplay: "none"
+
+ +

CUPS 1.2/OS X 10.5cupsPortMonitor

+ +

*cupsPortMonitor urischeme/Descriptive Text: "port monitor"

+ +

This string keyword specifies printer-specific "port +monitor" filters that may be used with the printer. The CUPS +scheduler also looks for the Protocols keyword to see +if the BCP or TBCP protocols are supported. If +so, the corresponding port monitor ("bcp" and "tbcp", +respectively) is listed in the printer's +port-monitor-supported keyword.

+ +

The "urischeme" portion of the keyword specifies the URI scheme +that this port monitor should be used for. Typically this is used to +pre-select a particular port monitor for each type of connection that +is supported by the printer. The "port monitor" string can be "none" +to disable the port monitor for the given URI scheme.

+ +

Examples:

+ +
+*% Specify a PostScript printer that supports the TBCP protocol
+*Protocols: TBCP PJL
+
+*% Specify that TBCP should be used for socket connections but not USB
+*cupsPortMonitor socket/AppSocket Printing: "tbcp"
+*cupsPortMonitor usb/USB Printing: "none"
+
+*% Specify a printer-specific port monitor for an Epson USB printer
+*cupsPortMonitor usb/USB Status Monitor: "epson-usb"
+
+ +

CUPS 1.3/OS X 10.5cupsPreFilter

+ +

*cupsPreFilter: "source/type cost program"

+ +

This string keyword provides a pre-filter rule. The pre-filter +program will be inserted in the conversion chain immediately +before the filter that accepts the given MIME type.

+ +

Examples:

+ +
+*% PDF pre-filter
+*cupsPreFilter: "application/pdf 100 mypdfprefilter"
+
+*% PNG pre-filter
+*cupsPreFilter: "image/png 0 mypngprefilter"
+
+ + +

CUPS 1.5cupsPrintQuality

+ +

*cupsPrintQuality keyword/text: "code"

+ +

This UI keyword defines standard print qualities that directly map from the IPP "print-quality" job template keyword. Standard keyword values are "Draft", "Normal", and "High" which are mapped from the IPP "print-quality" values 3, 4, and 5 respectively. Each cupsPrintQuality option typically sets output mode and resolution parameters in the page device dictionary, eliminating the need for separate (and sometimes confusing) output mode and resolution options.

+ +
Note: + +

Unlike all of the other keywords defined in this document, cupsPrintQuality is a UI keyword that MUST be enclosed inside the PPD OpenUI and CloseUI keywords.

+ +
+ +

Examples:

+ +
+*OpenUI *cupsPrintQuality/Print Quality: PickOne
+*OrderDependency: 10 AnySetup *cupsPrintQuality
+*DefaultcupsPrintQuality: Normal
+*cupsPrintQuality Draft/Draft: "code"
+*cupsPrintQuality Normal/Normal: "code"
+*cupsPrintQuality High/Photo: "code"
+*CloseUI: *cupsPrintQuality
+
+ +

CUPS 1.5cupsSingleFile

+ +

*cupsSingleFile: Boolean

+ +

This boolean keyword tells the scheduler whether to print multiple files in a job together or singly. The default is "False" which uses a single instance of the backend for all files in the print job. Setting this keyword to "True" will result in separate instances of the backend for each file in the print job.

+ +

Examples:

+ +
+*% Send all print data to a single backend
+*cupsSingleFile: False
+
+*% Send each file using a separate backend
+*cupsSingleFile: True
+
+ +

CUPS 1.4/OS X 10.6cupsSNMPSupplies

+ +

*cupsSNMPSupplies: boolean

+ +

This keyword tells the standard network backends whether they should query +the standard SNMP Printer MIB OIDs for supply levels. The default value is +True. + +

Example:

+ +
+*% Do not use SNMP queries to report supply levels
+*cupsSNMPSupplies: False
+
+ +

cupsVersion

+ +

*cupsVersion: major.minor

+ +

This required keyword describes which version of the CUPS +PPD file extensions was used. Currently it must be the string +"1.0", "1.1", "1.2", "1.3", "1.4", "1.5", or "1.6".

+ +

Example:

+ +
+*% Specify a CUPS 1.2 driver
+*cupsVersion: "1.2"
+
+ + +

CUPS 1.6/OS X 10.8JCLToPDFInterpreter

+ +

*JCLToPDFInterpreter: "JCL"

+ +

This keyword provfides the JCL command to insert a PDF job file into a printer-ready data stream. The JCL command is added after the JCLBegin value and any commands for JCL options in the PPD file.

+ +

Example:

+ +
+*% PJL command to start the PDF interpreter
+*JCLToPDFInterpreter: "@PJL ENTER LANGUAGE = PDF<0A>"
+
+ + +

OS X Attributes

+ +

OS X 10.3APDialogExtension

+ +

*APDialogExtension: "/Library/Printers/vendor/filename.plugin"

+ +

This keyword defines additional option panes that are displayed in the +print dialog. Each keyword adds one or more option panes. See the "OutputBinsPDE" +example and Apple +Technical Q&A QA1352 for information on writing your own print dialog +plug-ins.

+ +
Note: + +

Starting with OS X 10.5, each plug-in must be compiled "4-way fat" +(32-bit and 64-bit for both PowerPC and Intel) with garbage collection enabled +in order to be usable with all applications.

+ +
+ +

Examples:

+ +
+*% Add two panes for finishing and driver options
+*APDialogExtension: "/Library/Printers/vendor/finishing.plugin"
+*APDialogExtension: "/Library/Printers/vendor/options.plugin"
+
+ +

OS X 10.4APDuplexRequiresFlippedMargin

+ +

*APDuplexRequiresFlippedMargin: boolean

+ +

This boolean keyword notifies the RIP filters that the +destination printer requires the top and bottom margins of the +ImageableArea to be swapped for the back page. The +default is true when cupsBackSide is Flipped +and false otherwise. Table 2 shows how +APDuplexRequiresFlippedMargin interacts with cupsBackSide +and the Tumble page attribute.

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table 2: Margin Flipping Modes
APDuplexRequiresFlippedMargincupsBackSideTumble ValueMargins
falseanyanyNormal
anyNormalanyNormal
trueManualDuplexfalseNormal
trueManualDuplextrueFlipped
trueRotatedfalseFlipped
trueRotatedtrueNormal
true or unspecifiedFlippedanyFlipped
+ +

Example:

+ +
+*% Rotate the back side images
+*cupsBackSide: Rotated
+
+*% Don't swap the top and bottom margins for the back side
+*APDuplexRequiresFlippedMargin: false
+
+ +

Also see the related cupsBackSide +keyword.

+ +

APHelpBook

+ +

*APHelpBook: "bundle URL"

+ +

This string keyword specifies the Apple help book bundle to use when +looking up IPP reason codes for this printer driver. The +cupsIPPReason keyword maps +"help" URIs to this file.

+ +

Example:

+ +
+*APHelpBook: "file:///Library/Printers/vendor/Help.bundle"
+
+ +

OS X 10.6APICADriver

+ +

*APICADriver: boolean

+ +

This keyword specifies whether the device has a matching Image Capture +Architecture (ICA) driver for scanning. The default is False.

+ +

Examples:

+ +
+*APICADriver: True
+*APScanAppBundleID: "com.apple.ImageCaptureApp"
+
+ +

OS X 10.3APPrinterIconPath

+ +

*APPrinterIconPath: "/Library/Printers/vendor/filename.icns"

+ +

This keyword defines the location of a printer icon file to use when +displaying the printer. The file must be in the Apple icon format.

+ +

Examples:

+ +
+*% Apple icon file
+*APPrinterIconPath: "/Library/Printers/vendor/Icons/filename.icns"
+
+ +

OS X 10.4APPrinterLowInkTool

+ +

*APPrinterLowInkTool: "/Library/Printers/vendor/program"

+ +

This keyword defines an program that checks the ink/toner/marker levels +on a printer, returning an XML document with those levels. See the "InkTool" +example and +Apple +Technical Note TN2144 for more information.

+ +

Examples:

+ +
+*% Use a vendor monitoring program
+*APPrinterLowInkTool: "/Library/Printers/vendor/Tools/lowinktool"
+
+ +

OS X 10.5APPrinterPreset

+ +

*APPrinterPreset name/text: "*Option Choice ..."

+ +

This keyword defines presets for multiple options that show up +in the print dialog of applications (such as iPhoto) that set the job +style hint to NSPrintPhotoJobStyleHint. Each preset maps to one or +more pairs of PPD options and choices as well as providing key/value data for +the application. The following standard preset names are currently defined:

+ +
    + +
  • General_with_Paper_Auto-Detect; Normal quality general printing with auto-detected media.
  • + +
  • General_with_Paper_Auto-Detect_-_Draft; Draft quality general printing with auto-detected media.
  • + +
  • General_on_Plain_Paper; Normal quality general printing on plain paper.
  • + +
  • General_on_Plain_Paper_-_Draft; Draft quality general printing on plain paper.
  • + +
  • Photo_with_Paper_Auto-Detect; Normal quality photo printing with auto-detected media.
  • + +
  • Photo_with_Paper_Auto-Detect_-_Fine; High quality photo printing with auto-detected media.
  • + +
  • Photo_on_Plain_Paper; Normal quality photo printing on plain paper.
  • + +
  • Photo_on_Plain_Paper_-_Fine; High quality photo printing on plain paper.
  • + +
  • Photo_on_Photo_Paper; Normal quality photo printing on glossy photo paper.
  • + +
  • Photo_on_Photo_Paper_-_Fine; High quality photo printing on glossy photo paper.
  • + +
  • Photo_on_Matte_Paper; Normal quality photo printing on matte paper.
  • + +
  • Photo_on_Matte_Paper_-_Fine; High quality photo printing on matte paper.
  • + +
+ +

The value string consists of pairs of keywords, either an option name and +choice (*MainKeyword OptionKeyword) or a preset identifier and value +(com.apple.print.preset.foo value). The following preset identifiers are currently used:

+ +
    + +
  • com.apple.print.preset.graphicsType; specifies the type of printing used for this printing - "General" for general purpose printing and "Photo" for photo printing.
  • + +
  • com.apple.print.preset.media-front-coating; specifies the media type selected by this preset - "none" (plain paper), "glossy", "high-gloss", "semi-gloss", "satin", "matte", and "autodetect".
  • + +
  • com.apple.print.preset.output-mode; specifies the output mode for this preset - "color" (default for color printers) or "monochrome" (grayscale, default for B&W printers).
  • + +
  • com.apple.print.preset.quality; specifies the overall print quality selected by this preset - "low" (draft), "mid" (normal), or "high".
  • + +
+ +

Presets, like options, can also be localized in multiple languages.

+ +

Examples:

+ +
+*APPrinterPreset Photo_on_Photo_Paper/Photo on Photo Paper: "
+  *MediaType Glossy
+  *ColorModel RGB
+  *Resolution 300dpi
+  com.apple.print.preset.graphicsType Photo
+  com.apple.print.preset.quality mid
+  com.apple.print.preset.media-front-coating glossy"
+*End
+*fr.APPrinterPreset Photo_on_Photo_Paper/Photo sur papier photographique: ""
+
+ +

OS X 10.3APPrinterUtilityPath

+ +

*APPrinterPrinterUtilityPath: "/Library/Printers/vendor/filename.app"

+ +

This keyword defines a GUI application that can be used to do printer +maintenance functions such as cleaning the print head(s). See ... for more +information.

+ +

Examples:

+ +
+*% Define the printer utility application
+*APPrinterPrinterUtilityPath: "/Library/Printers/vendor/Tools/utility.app"
+
+ +

OS X 10.6APScannerOnly

+ +

*APScannerOnly: boolean

+ +

This keyword specifies whether the device has scanning but no printing +capabilities. The default is False.

+ +

Examples:

+ +
+*APICADriver: True
+*APScannerOnly: True
+
+ +

OS X 10.3APScanAppBundleID

+ +

*APScanAppBundleID: "bundle ID"

+ +

This keyword defines the application to use when scanning pages from +the device.

+ +

Examples:

+ +
+*APICADriver: True
+*APScanAppBundleID: "com.apple.ImageCaptureApp"
+
+ + +

Change History

+ +

Changes in CUPS 1.6

+ + + + +

Changes in CUPS 1.5

+ +
    + +
  • Changes all instances of PPD attributes to PPD keywords, to be consistent with the parent specification from Adobe.
  • + +
+ + +

Changes in CUPS 1.4.5

+ + + + +

Changes in CUPS 1.4

+ + + + +

Changes in CUPS 1.3.1

+ +
    + +
  • Added missing OS X AP keywords.
  • + +
  • Added section on auto-configuration including the + OIDMainKeyword and ?MainKeyword + keywords.
  • + +
  • Minor reorganization.
  • + +
+ + +

Changes in CUPS 1.3

+ + + +

Changes in CUPS 1.2.8

+ +
    + +
  • Added section on supported PostScript commands for raster + drivers
  • + +
+ +

Changes in CUPS 1.2

+ + + +

Changes in CUPS 1.1

+ + diff --git a/filter/testraster.c b/filter/testraster.c new file mode 100644 index 0000000..4519aa2 --- /dev/null +++ b/filter/testraster.c @@ -0,0 +1,1078 @@ +/* + * "$Id: testraster.c 7376 2008-03-19 21:07:45Z mike $" + * + * Raster test program routines for CUPS. + * + * Copyright 2007-2011 by Apple Inc. + * Copyright 1997-2007 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. + * + * Contents: + * + * main() - Test the raster functions. + * do_ppd_tests() - Test the default option commands in a PPD file. + * do_ps_tests() - Test standard PostScript commands. + * do_ras_file() - Test reading of a raster file. + * do_raster_tests() - Test reading and writing of raster data. + * print_changes() - Print differences in the page header. + */ + +/* + * Include necessary headers... + */ + +#include + + +/* + * Test PS commands and header... + */ + +static const char *dsc_code = +"[{\n" +"%%BeginFeature: *PageSize Tabloid\n" +"<>setpagedevice\n" +"%%EndFeature\n" +"} stopped cleartomark\n"; +static const char *setpagedevice_code = +"<<" +"/MediaClass(Media Class)" +"/MediaColor((Media Color))" +"/MediaType(Media\\\\Type)" +"/OutputType<416263>" +"/AdvanceDistance 1000" +"/AdvanceMedia 1" +"/Collate false" +"/CutMedia 2" +"/Duplex true" +"/HWResolution[100 200]" +"/InsertSheet true" +"/Jog 3" +"/LeadingEdge 1" +"/ManualFeed true" +"/MediaPosition 8#777" +"/MediaWeight 16#fe01" +"/MirrorPrint true" +"/NegativePrint true" +"/NumCopies 1" +"/Orientation 1" +"/OutputFaceUp true" +"/PageSize[612 792.1]" +"/Separations true" +"/TraySwitch true" +"/Tumble true" +"/cupsMediaType 2" +"/cupsColorOrder 1" +"/cupsColorSpace 1" +"/cupsCompression 1" +"/cupsRowCount 1" +"/cupsRowFeed 1" +"/cupsRowStep 1" +"/cupsBorderlessScalingFactor 1.001" +"/cupsInteger0 1" +"/cupsInteger1 2" +"/cupsInteger2 3" +"/cupsInteger3 4" +"/cupsInteger4 5" +"/cupsInteger5 6" +"/cupsInteger6 7" +"/cupsInteger7 8" +"/cupsInteger8 9" +"/cupsInteger9 10" +"/cupsInteger10 11" +"/cupsInteger11 12" +"/cupsInteger12 13" +"/cupsInteger13 14" +"/cupsInteger14 15" +"/cupsInteger15 16" +"/cupsReal0 1.1" +"/cupsReal1 2.1" +"/cupsReal2 3.1" +"/cupsReal3 4.1" +"/cupsReal4 5.1" +"/cupsReal5 6.1" +"/cupsReal6 7.1" +"/cupsReal7 8.1" +"/cupsReal8 9.1" +"/cupsReal9 10.1" +"/cupsReal10 11.1" +"/cupsReal11 12.1" +"/cupsReal12 13.1" +"/cupsReal13 14.1" +"/cupsReal14 15.1" +"/cupsReal15 16.1" +"/cupsString0(1)" +"/cupsString1(2)" +"/cupsString2(3)" +"/cupsString3(4)" +"/cupsString4(5)" +"/cupsString5(6)" +"/cupsString6(7)" +"/cupsString7(8)" +"/cupsString8(9)" +"/cupsString9(10)" +"/cupsString10(11)" +"/cupsString11(12)" +"/cupsString12(13)" +"/cupsString13(14)" +"/cupsString14(15)" +"/cupsString15(16)" +"/cupsMarkerType(Marker Type)" +"/cupsRenderingIntent(Rendering Intent)" +"/cupsPageSizeName(Letter)" +"/cupsPreferredBitsPerColor 17" +">> setpagedevice"; + +static cups_page_header2_t setpagedevice_header = +{ + "Media Class", /* MediaClass */ + "(Media Color)", /* MediaColor */ + "Media\\Type", /* MediaType */ + "Abc", /* OutputType */ + 1000, /* AdvanceDistance */ + CUPS_ADVANCE_FILE, /* AdvanceMedia */ + CUPS_FALSE, /* Collate */ + CUPS_CUT_JOB, /* CutMedia */ + CUPS_TRUE, /* Duplex */ + { 100, 200 }, /* HWResolution */ + { 0, 0, 0, 0 }, /* ImagingBoundingBox */ + CUPS_TRUE, /* InsertSheet */ + CUPS_JOG_SET, /* Jog */ + CUPS_EDGE_RIGHT, /* LeadingEdge */ + { 0, 0 }, /* Margins */ + CUPS_TRUE, /* ManualFeed */ + 0777, /* MediaPosition */ + 0xfe01, /* MediaWeight */ + CUPS_TRUE, /* MirrorPrint */ + CUPS_TRUE, /* NegativePrint */ + 1, /* NumCopies */ + CUPS_ORIENT_90, /* Orientation */ + CUPS_TRUE, /* OutputFaceUp */ + { 612, 792 }, /* PageSize */ + CUPS_TRUE, /* Separations */ + CUPS_TRUE, /* TraySwitch */ + CUPS_TRUE, /* Tumble */ + 0, /* cupsWidth */ + 0, /* cupsHeight */ + 2, /* cupsMediaType */ + 0, /* cupsBitsPerColor */ + 0, /* cupsBitsPerPixel */ + 0, /* cupsBytesPerLine */ + CUPS_ORDER_BANDED, /* cupsColorOrder */ + CUPS_CSPACE_RGB, /* cupsColorSpace */ + 1, /* cupsCompression */ + 1, /* cupsRowCount */ + 1, /* cupsRowFeed */ + 1, /* cupsRowStep */ + 0, /* cupsNumColors */ + 1.001, /* cupsBorderlessScalingFactor */ + { 612.0, 792.1 }, /* cupsPageSize */ + { 0.0, 0.0, 0.0, 0.0 }, /* cupsImagingBBox */ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, + /* cupsInteger[16] */ + { 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, 10.1, 11.1, 12.1, 13.1, + 14.1, 15.1, 16.1 }, /* cupsReal[16] */ + { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", + "14", "15", "16" }, /* cupsString[16] */ + "Marker Type", /* cupsMarkerType */ + "Rendering Intent", /* cupsRenderingIntent */ + "Letter" /* cupsPageSizeName */ +}; + + +/* + * Local functions... + */ + +static int do_ppd_tests(const char *filename, int num_options, + cups_option_t *options); +static int do_ps_tests(void); +static int do_ras_file(const char *filename); +static int do_raster_tests(cups_mode_t mode); +static void print_changes(cups_page_header2_t *header, + cups_page_header2_t *expected); + + +/* + * 'main()' - Test the raster functions. + */ + +int /* O - Exit status */ +main(int argc, /* I - Number of command-line args */ + char *argv[]) /* I - Command-line arguments */ +{ + int errors; /* Number of errors */ + const char *ext; /* Filename extension */ + + + if (argc == 1) + { + errors = do_ps_tests(); + errors += do_raster_tests(CUPS_RASTER_WRITE); + errors += do_raster_tests(CUPS_RASTER_WRITE_COMPRESSED); + errors += do_raster_tests(CUPS_RASTER_WRITE_PWG); + } + else + { + int i; /* Looping var */ + int num_options; /* Number of options */ + cups_option_t *options; /* Options */ + + + for (errors = 0, num_options = 0, options = NULL, i = 1; i < argc; i ++) + { + if (argv[i][0] == '-') + { + if (argv[i][1] == 'o') + { + if (argv[i][2]) + num_options = cupsParseOptions(argv[i] + 2, num_options, &options); + else + { + i ++; + if (i < argc) + num_options = cupsParseOptions(argv[i], num_options, &options); + else + { + puts("Usage: testraster [-o name=value ...] [filename.ppd ...]"); + puts(" testraster [filename.ras ...]"); + return (1); + } + } + } + else + { + puts("Usage: testraster [-o name=value ...] [filename.ppd ...]"); + puts(" testraster [filename.ras ...]"); + return (1); + } + } + else if ((ext = strrchr(argv[i], '.')) != NULL) + { + if (!strcmp(ext, ".ppd")) + errors += do_ppd_tests(argv[i], num_options, options); + else + errors += do_ras_file(argv[i]); + } + else + { + puts("Usage: testraster [-o name=value ...] [filename.ppd ...]"); + puts(" testraster [filename.ras ...]"); + return (1); + } + } + + cupsFreeOptions(num_options, options); + } + + return (errors); +} + + +/* + * 'do_ppd_tests()' - Test the default option commands in a PPD file. + */ + +static int /* O - Number of errors */ +do_ppd_tests(const char *filename, /* I - PPD file */ + int num_options, /* I - Number of options */ + cups_option_t *options) /* I - Options */ +{ + ppd_file_t *ppd; /* PPD file data */ + cups_page_header2_t header; /* Page header */ + + + printf("\"%s\": ", filename); + fflush(stdout); + + if ((ppd = ppdOpenFile(filename)) == NULL) + { + ppd_status_t status; /* Status from PPD loader */ + int line; /* Line number containing error */ + + + status = ppdLastError(&line); + + puts("FAIL (bad PPD file)"); + printf(" %s on line %d\n", ppdErrorString(status), line); + + return (1); + } + + ppdMarkDefaults(ppd); + cupsMarkOptions(ppd, num_options, options); + + if (cupsRasterInterpretPPD(&header, ppd, 0, NULL, NULL)) + { + puts("FAIL (error from function)"); + puts(cupsRasterErrorString()); + + return (1); + } + else + { + puts("PASS"); + + return (0); + } +} + + +/* + * 'do_ps_tests()' - Test standard PostScript commands. + */ + +static int +do_ps_tests(void) +{ + cups_page_header2_t header; /* Page header */ + int preferred_bits; /* Preferred bits */ + int errors = 0; /* Number of errors */ + + + /* + * Test PS exec code... + */ + + fputs("_cupsRasterExecPS(\"setpagedevice\"): ", stdout); + fflush(stdout); + + memset(&header, 0, sizeof(header)); + header.Collate = CUPS_TRUE; + preferred_bits = 0; + + if (_cupsRasterExecPS(&header, &preferred_bits, setpagedevice_code)) + { + puts("FAIL (error from function)"); + puts(cupsRasterErrorString()); + errors ++; + } + else if (preferred_bits != 17 || + memcmp(&header, &setpagedevice_header, sizeof(header))) + { + puts("FAIL (bad header)"); + + if (preferred_bits != 17) + printf(" cupsPreferredBitsPerColor %d, expected 17\n", + preferred_bits); + + print_changes(&setpagedevice_header, &header); + errors ++; + } + else + puts("PASS"); + + fputs("_cupsRasterExecPS(\"roll\"): ", stdout); + fflush(stdout); + + if (_cupsRasterExecPS(&header, &preferred_bits, + "792 612 0 0 0\n" + "pop pop pop\n" + "<>" + "setpagedevice\n")) + { + puts("FAIL (error from function)"); + puts(cupsRasterErrorString()); + errors ++; + } + else if (header.PageSize[0] != 792 || header.PageSize[1] != 612) + { + printf("FAIL (PageSize [%d %d], expected [792 612])\n", header.PageSize[0], + header.PageSize[1]); + errors ++; + } + else + puts("PASS"); + + fputs("_cupsRasterExecPS(\"dup index\"): ", stdout); + fflush(stdout); + + if (_cupsRasterExecPS(&header, &preferred_bits, + "true false dup\n" + "<>setpagedevice\n" + "pop pop pop")) + { + puts("FAIL (error from function)"); + puts(cupsRasterErrorString()); + errors ++; + } + else + { + if (!header.Collate) + { + printf("FAIL (Collate false, expected true)\n"); + errors ++; + } + + if (header.Duplex) + { + printf("FAIL (Duplex true, expected false)\n"); + errors ++; + } + + if (header.Tumble) + { + printf("FAIL (Tumble true, expected false)\n"); + errors ++; + } + + if(header.Collate && !header.Duplex && !header.Tumble) + puts("PASS"); + } + + fputs("_cupsRasterExecPS(\"%%Begin/EndFeature code\"): ", stdout); + fflush(stdout); + + if (_cupsRasterExecPS(&header, &preferred_bits, dsc_code)) + { + puts("FAIL (error from function)"); + puts(cupsRasterErrorString()); + errors ++; + } + else if (header.PageSize[0] != 792 || header.PageSize[1] != 1224) + { + printf("FAIL (bad PageSize [%d %d], expected [792 1224])\n", + header.PageSize[0], header.PageSize[1]); + errors ++; + } + else + puts("PASS"); + + return (errors); +} + + +/* + * 'do_ras_file()' - Test reading of a raster file. + */ + +static int /* O - Number of errors */ +do_ras_file(const char *filename) /* I - Filename */ +{ + unsigned y; /* Looping vars */ + int fd; /* File descriptor */ + cups_raster_t *ras; /* Raster stream */ + cups_page_header2_t header; /* Page header */ + unsigned char *data; /* Raster data */ + int errors = 0; /* Number of errors */ + unsigned pages = 0; /* Number of pages */ + + + if ((fd = open(filename, O_RDONLY)) < 0) + { + printf("%s: %s\n", filename, strerror(errno)); + return (1); + } + + if ((ras = cupsRasterOpen(fd, CUPS_RASTER_READ)) == NULL) + { + printf("%s: cupsRasterOpen failed.\n", filename); + close(fd); + return (1); + } + + printf("%s:\n", filename); + + while (cupsRasterReadHeader2(ras, &header)) + { + pages ++; + data = malloc(header.cupsBytesPerLine); + + printf(" Page %u: %ux%ux%u@%ux%udpi", pages, + header.cupsWidth, header.cupsHeight, header.cupsBitsPerPixel, + header.HWResolution[0], header.HWResolution[1]); + fflush(stdout); + + for (y = 0; y < header.cupsHeight; y ++) + if (cupsRasterReadPixels(ras, data, header.cupsBytesPerLine) < + header.cupsBytesPerLine) + break; + + if (y < header.cupsHeight) + printf(" ERROR AT LINE %d\n", y); + else + putchar('\n'); + + free(data); + } + + cupsRasterClose(ras); + close(fd); + + return (errors); +} + + +/* + * 'do_raster_tests()' - Test reading and writing of raster data. + */ + +static int /* O - Number of errors */ +do_raster_tests(cups_mode_t mode) /* O - Write mode */ +{ + int page, x, y; /* Looping vars */ + FILE *fp; /* Raster file */ + cups_raster_t *r; /* Raster stream */ + cups_page_header2_t header, /* Page header */ + expected; /* Expected page header */ + unsigned char data[2048]; /* Raster data */ + int errors = 0; /* Number of errors */ + + + /* + * Test writing... + */ + + printf("cupsRasterOpen(%s): ", + mode == CUPS_RASTER_WRITE ? "CUPS_RASTER_WRITE" : + mode == CUPS_RASTER_WRITE ? "CUPS_RASTER_WRITE_COMPRESSED" : + "CUPS_RASTER_WRITE_PWG"); + fflush(stdout); + + if ((fp = fopen("test.raster", "wb")) == NULL) + { + printf("FAIL (%s)\n", strerror(errno)); + return (1); + } + + if ((r = cupsRasterOpen(fileno(fp), mode)) == NULL) + { + printf("FAIL (%s)\n", strerror(errno)); + fclose(fp); + return (1); + } + + puts("PASS"); + + for (page = 0; page < 4; page ++) + { + memset(&header, 0, sizeof(header)); + header.cupsWidth = 256; + header.cupsHeight = 256; + header.cupsBytesPerLine = 256; + + if (page & 1) + { + header.cupsBytesPerLine *= 2; + header.cupsColorSpace = CUPS_CSPACE_CMYK; + header.cupsColorOrder = CUPS_ORDER_CHUNKED; + header.cupsNumColors = 4; + } + else + { + header.cupsColorSpace = CUPS_CSPACE_K; + header.cupsColorOrder = CUPS_ORDER_BANDED; + header.cupsNumColors = 1; + } + + if (page & 2) + { + header.cupsBytesPerLine *= 2; + header.cupsBitsPerColor = 16; + header.cupsBitsPerPixel = (page & 1) ? 64 : 16; + } + else + { + header.cupsBitsPerColor = 8; + header.cupsBitsPerPixel = (page & 1) ? 32 : 8; + } + + if (cupsRasterWriteHeader2(r, &header)) + puts("cupsRasterWriteHeader2: PASS"); + else + { + puts("cupsRasterWriteHeader2: FAIL"); + errors ++; + } + + fputs("cupsRasterWritePixels: ", stdout); + fflush(stdout); + + memset(data, 0, header.cupsBytesPerLine); + for (y = 0; y < 64; y ++) + if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) + break; + + if (y < 64) + { + puts("FAIL"); + errors ++; + } + else + { + for (x = 0; x < header.cupsBytesPerLine; x ++) + data[x] = x; + + for (y = 0; y < 64; y ++) + if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) + break; + + if (y < 64) + { + puts("FAIL"); + errors ++; + } + else + { + memset(data, 255, header.cupsBytesPerLine); + for (y = 0; y < 64; y ++) + if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) + break; + + if (y < 64) + { + puts("FAIL"); + errors ++; + } + else + { + for (x = 0; x < header.cupsBytesPerLine; x ++) + data[x] = x / 4; + + for (y = 0; y < 64; y ++) + if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) + break; + + if (y < 64) + { + puts("FAIL"); + errors ++; + } + else + puts("PASS"); + } + } + } + } + + cupsRasterClose(r); + fclose(fp); + + /* + * Test reading... + */ + + fputs("cupsRasterOpen(CUPS_RASTER_READ): ", stdout); + fflush(stdout); + + if ((fp = fopen("test.raster", "rb")) == NULL) + { + printf("FAIL (%s)\n", strerror(errno)); + return (1); + } + + if ((r = cupsRasterOpen(fileno(fp), CUPS_RASTER_READ)) == NULL) + { + printf("FAIL (%s)\n", strerror(errno)); + fclose(fp); + return (1); + } + + puts("PASS"); + + for (page = 0; page < 4; page ++) + { + memset(&expected, 0, sizeof(expected)); + expected.cupsWidth = 256; + expected.cupsHeight = 256; + expected.cupsBytesPerLine = 256; + + if (mode == CUPS_RASTER_WRITE_PWG) + { + strlcpy(expected.MediaClass, "PwgRaster", sizeof(expected.MediaClass)); + expected.cupsInteger[7] = 0xffffff; + } + + if (page & 1) + { + expected.cupsBytesPerLine *= 2; + expected.cupsColorSpace = CUPS_CSPACE_CMYK; + expected.cupsColorOrder = CUPS_ORDER_CHUNKED; + expected.cupsNumColors = 4; + } + else + { + expected.cupsColorSpace = CUPS_CSPACE_K; + expected.cupsColorOrder = CUPS_ORDER_BANDED; + expected.cupsNumColors = 1; + } + + if (page & 2) + { + expected.cupsBytesPerLine *= 2; + expected.cupsBitsPerColor = 16; + expected.cupsBitsPerPixel = (page & 1) ? 64 : 16; + } + else + { + expected.cupsBitsPerColor = 8; + expected.cupsBitsPerPixel = (page & 1) ? 32 : 8; + } + + fputs("cupsRasterReadHeader2: ", stdout); + fflush(stdout); + + if (!cupsRasterReadHeader2(r, &header)) + { + puts("FAIL (read error)"); + errors ++; + break; + } + + if (memcmp(&header, &expected, sizeof(header))) + { + puts("FAIL (bad page header)"); + errors ++; + print_changes(&header, &expected); + } + + fputs("cupsRasterReadPixels: ", stdout); + fflush(stdout); + + for (y = 0; y < 64; y ++) + { + if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) + { + puts("FAIL (read error)"); + errors ++; + break; + } + + if (data[0] != 0 || memcmp(data, data + 1, header.cupsBytesPerLine - 1)) + { + printf("FAIL (raster line %d corrupt)\n", y); + errors ++; + break; + } + } + + if (y == 64) + { + for (y = 0; y < 64; y ++) + { + if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) + { + puts("FAIL (read error)"); + errors ++; + break; + } + + for (x = 0; x < header.cupsBytesPerLine; x ++) + if (data[x] != (x & 255)) + break; + + if (x < header.cupsBytesPerLine) + { + printf("FAIL (raster line %d corrupt)\n", y + 64); + errors ++; + break; + } + } + + if (y == 64) + { + for (y = 0; y < 64; y ++) + { + if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) + { + puts("FAIL (read error)"); + errors ++; + break; + } + + if (data[0] != 255 || memcmp(data, data + 1, header.cupsBytesPerLine - 1)) + { + printf("fail (raster line %d corrupt)\n", y + 128); + errors ++; + break; + } + } + + if (y == 64) + { + for (y = 0; y < 64; y ++) + { + if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) + { + puts("FAIL (read error)"); + errors ++; + break; + } + + for (x = 0; x < header.cupsBytesPerLine; x ++) + if (data[x] != ((x / 4) & 255)) + break; + + if (x < header.cupsBytesPerLine) + { + printf("FAIL (raster line %d corrupt)\n", y + 192); + errors ++; + break; + } + } + + if (y == 64) + puts("PASS"); + } + } + } + } + + cupsRasterClose(r); + fclose(fp); + + return (errors); +} + + +/* + * 'print_changes()' - Print differences in the page header. + */ + +static void +print_changes( + cups_page_header2_t *header, /* I - Actual page header */ + cups_page_header2_t *expected) /* I - Expected page header */ +{ + int i; /* Looping var */ + + + if (strcmp(header->MediaClass, expected->MediaClass)) + printf(" MediaClass (%s), expected (%s)\n", header->MediaClass, + expected->MediaClass); + + if (strcmp(header->MediaColor, expected->MediaColor)) + printf(" MediaColor (%s), expected (%s)\n", header->MediaColor, + expected->MediaColor); + + if (strcmp(header->MediaType, expected->MediaType)) + printf(" MediaType (%s), expected (%s)\n", header->MediaType, + expected->MediaType); + + if (strcmp(header->OutputType, expected->OutputType)) + printf(" OutputType (%s), expected (%s)\n", header->OutputType, + expected->OutputType); + + if (header->AdvanceDistance != expected->AdvanceDistance) + printf(" AdvanceDistance %d, expected %d\n", header->AdvanceDistance, + expected->AdvanceDistance); + + if (header->AdvanceMedia != expected->AdvanceMedia) + printf(" AdvanceMedia %d, expected %d\n", header->AdvanceMedia, + expected->AdvanceMedia); + + if (header->Collate != expected->Collate) + printf(" Collate %d, expected %d\n", header->Collate, + expected->Collate); + + if (header->CutMedia != expected->CutMedia) + printf(" CutMedia %d, expected %d\n", header->CutMedia, + expected->CutMedia); + + if (header->Duplex != expected->Duplex) + printf(" Duplex %d, expected %d\n", header->Duplex, + expected->Duplex); + + if (header->HWResolution[0] != expected->HWResolution[0] || + header->HWResolution[1] != expected->HWResolution[1]) + printf(" HWResolution [%d %d], expected [%d %d]\n", + header->HWResolution[0], header->HWResolution[1], + expected->HWResolution[0], expected->HWResolution[1]); + + if (memcmp(header->ImagingBoundingBox, expected->ImagingBoundingBox, + sizeof(header->ImagingBoundingBox))) + printf(" ImagingBoundingBox [%d %d %d %d], expected [%d %d %d %d]\n", + header->ImagingBoundingBox[0], + header->ImagingBoundingBox[1], + header->ImagingBoundingBox[2], + header->ImagingBoundingBox[3], + expected->ImagingBoundingBox[0], + expected->ImagingBoundingBox[1], + expected->ImagingBoundingBox[2], + expected->ImagingBoundingBox[3]); + + if (header->InsertSheet != expected->InsertSheet) + printf(" InsertSheet %d, expected %d\n", header->InsertSheet, + expected->InsertSheet); + + if (header->Jog != expected->Jog) + printf(" Jog %d, expected %d\n", header->Jog, + expected->Jog); + + if (header->LeadingEdge != expected->LeadingEdge) + printf(" LeadingEdge %d, expected %d\n", header->LeadingEdge, + expected->LeadingEdge); + + if (header->Margins[0] != expected->Margins[0] || + header->Margins[1] != expected->Margins[1]) + printf(" Margins [%d %d], expected [%d %d]\n", + header->Margins[0], header->Margins[1], + expected->Margins[0], expected->Margins[1]); + + if (header->ManualFeed != expected->ManualFeed) + printf(" ManualFeed %d, expected %d\n", header->ManualFeed, + expected->ManualFeed); + + if (header->MediaPosition != expected->MediaPosition) + printf(" MediaPosition %d, expected %d\n", header->MediaPosition, + expected->MediaPosition); + + if (header->MediaWeight != expected->MediaWeight) + printf(" MediaWeight %d, expected %d\n", header->MediaWeight, + expected->MediaWeight); + + if (header->MirrorPrint != expected->MirrorPrint) + printf(" MirrorPrint %d, expected %d\n", header->MirrorPrint, + expected->MirrorPrint); + + if (header->NegativePrint != expected->NegativePrint) + printf(" NegativePrint %d, expected %d\n", header->NegativePrint, + expected->NegativePrint); + + if (header->NumCopies != expected->NumCopies) + printf(" NumCopies %d, expected %d\n", header->NumCopies, + expected->NumCopies); + + if (header->Orientation != expected->Orientation) + printf(" Orientation %d, expected %d\n", header->Orientation, + expected->Orientation); + + if (header->OutputFaceUp != expected->OutputFaceUp) + printf(" OutputFaceUp %d, expected %d\n", header->OutputFaceUp, + expected->OutputFaceUp); + + if (header->PageSize[0] != expected->PageSize[0] || + header->PageSize[1] != expected->PageSize[1]) + printf(" PageSize [%d %d], expected [%d %d]\n", + header->PageSize[0], header->PageSize[1], + expected->PageSize[0], expected->PageSize[1]); + + if (header->Separations != expected->Separations) + printf(" Separations %d, expected %d\n", header->Separations, + expected->Separations); + + if (header->TraySwitch != expected->TraySwitch) + printf(" TraySwitch %d, expected %d\n", header->TraySwitch, + expected->TraySwitch); + + if (header->Tumble != expected->Tumble) + printf(" Tumble %d, expected %d\n", header->Tumble, + expected->Tumble); + + if (header->cupsWidth != expected->cupsWidth) + printf(" cupsWidth %d, expected %d\n", header->cupsWidth, + expected->cupsWidth); + + if (header->cupsHeight != expected->cupsHeight) + printf(" cupsHeight %d, expected %d\n", header->cupsHeight, + expected->cupsHeight); + + if (header->cupsMediaType != expected->cupsMediaType) + printf(" cupsMediaType %d, expected %d\n", header->cupsMediaType, + expected->cupsMediaType); + + if (header->cupsBitsPerColor != expected->cupsBitsPerColor) + printf(" cupsBitsPerColor %d, expected %d\n", header->cupsBitsPerColor, + expected->cupsBitsPerColor); + + if (header->cupsBitsPerPixel != expected->cupsBitsPerPixel) + printf(" cupsBitsPerPixel %d, expected %d\n", header->cupsBitsPerPixel, + expected->cupsBitsPerPixel); + + if (header->cupsBytesPerLine != expected->cupsBytesPerLine) + printf(" cupsBytesPerLine %d, expected %d\n", header->cupsBytesPerLine, + expected->cupsBytesPerLine); + + if (header->cupsColorOrder != expected->cupsColorOrder) + printf(" cupsColorOrder %d, expected %d\n", header->cupsColorOrder, + expected->cupsColorOrder); + + if (header->cupsColorSpace != expected->cupsColorSpace) + printf(" cupsColorSpace %d, expected %d\n", header->cupsColorSpace, + expected->cupsColorSpace); + + if (header->cupsCompression != expected->cupsCompression) + printf(" cupsCompression %d, expected %d\n", header->cupsCompression, + expected->cupsCompression); + + if (header->cupsRowCount != expected->cupsRowCount) + printf(" cupsRowCount %d, expected %d\n", header->cupsRowCount, + expected->cupsRowCount); + + if (header->cupsRowFeed != expected->cupsRowFeed) + printf(" cupsRowFeed %d, expected %d\n", header->cupsRowFeed, + expected->cupsRowFeed); + + if (header->cupsRowStep != expected->cupsRowStep) + printf(" cupsRowStep %d, expected %d\n", header->cupsRowStep, + expected->cupsRowStep); + + if (header->cupsNumColors != expected->cupsNumColors) + printf(" cupsNumColors %d, expected %d\n", header->cupsNumColors, + expected->cupsNumColors); + + if (header->cupsBorderlessScalingFactor != + expected->cupsBorderlessScalingFactor) + printf(" cupsBorderlessScalingFactor %g, expected %g\n", + header->cupsBorderlessScalingFactor, + expected->cupsBorderlessScalingFactor); + + if (header->cupsPageSize[0] != expected->cupsPageSize[0] || + header->cupsPageSize[1] != expected->cupsPageSize[1]) + printf(" cupsPageSize [%g %g], expected [%g %g]\n", + header->cupsPageSize[0], header->cupsPageSize[1], + expected->cupsPageSize[0], expected->cupsPageSize[1]); + + if (header->cupsImagingBBox[0] != expected->cupsImagingBBox[0] || + header->cupsImagingBBox[1] != expected->cupsImagingBBox[1] || + header->cupsImagingBBox[2] != expected->cupsImagingBBox[2] || + header->cupsImagingBBox[3] != expected->cupsImagingBBox[3]) + printf(" cupsImagingBBox [%g %g %g %g], expected [%g %g %g %g]\n", + header->cupsImagingBBox[0], header->cupsImagingBBox[1], + header->cupsImagingBBox[2], header->cupsImagingBBox[3], + expected->cupsImagingBBox[0], expected->cupsImagingBBox[1], + expected->cupsImagingBBox[2], expected->cupsImagingBBox[3]); + + for (i = 0; i < 16; i ++) + if (header->cupsInteger[i] != expected->cupsInteger[i]) + printf(" cupsInteger%d %d, expected %d\n", i, header->cupsInteger[i], + expected->cupsInteger[i]); + + for (i = 0; i < 16; i ++) + if (header->cupsReal[i] != expected->cupsReal[i]) + printf(" cupsReal%d %g, expected %g\n", i, header->cupsReal[i], + expected->cupsReal[i]); + + for (i = 0; i < 16; i ++) + if (strcmp(header->cupsString[i], expected->cupsString[i])) + printf(" cupsString%d (%s), expected (%s)\n", i, + header->cupsString[i], expected->cupsString[i]); + + if (strcmp(header->cupsMarkerType, expected->cupsMarkerType)) + printf(" cupsMarkerType (%s), expected (%s)\n", header->cupsMarkerType, + expected->cupsMarkerType); + + if (strcmp(header->cupsRenderingIntent, expected->cupsRenderingIntent)) + printf(" cupsRenderingIntent (%s), expected (%s)\n", + header->cupsRenderingIntent, + expected->cupsRenderingIntent); + + if (strcmp(header->cupsPageSizeName, expected->cupsPageSizeName)) + printf(" cupsPageSizeName (%s), expected (%s)\n", + header->cupsPageSizeName, + expected->cupsPageSizeName); +} + + +/* + * End of "$Id: testraster.c 7376 2008-03-19 21:07:45Z mike $". + */ diff --git a/install-sh b/install-sh new file mode 100755 index 0000000..41d944e --- /dev/null +++ b/install-sh @@ -0,0 +1,234 @@ +#!/bin/sh +# +# "$Id$" +# +# Install a program, script, or datafile. +# +# Copyright 2008-2012 by Apple Inc. +# +# This script is not compatible with BSD (or any other) install program, as it +# allows owner and group changes to fail with a warning and makes sure that the +# destination directory permissions are as specified - BSD install and the +# original X11 install script did not change permissions of existing +# directories. It also does not support the transform options since CUPS does +# not use them... +# +# Original script from X11R5 (mit/util/scripts/install.sh) +# Copyright 1991 by the Massachusetts Institute of Technology +# +# Permission to use, copy, modify, distribute, and sell this software and its +# documentation for any purpose is hereby granted without fee, 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. +# +# Calling this script install-sh is preferred over install.sh, to prevent +# `make' implicit rules from creating a file called install from it +# when there is no Makefile. + +# set DOITPROG to echo to test this script +# Don't use :- since 4.3BSD and earlier shells don't like it. +doit="${DOITPROG-}" + +# Force umask to 022... +umask 022 + +# put in absolute paths if you don't have them in your path; or use env. vars. +mvprog="${MVPROG-mv}" +cpprog="${CPPROG-cp}" +chmodprog="${CHMODPROG-chmod}" +chownprog="${CHOWNPROG-chown}" +chgrpprog="${CHGRPPROG-chgrp}" +stripprog="${STRIPPROG-strip}" +rmprog="${RMPROG-rm}" +mkdirprog="${MKDIRPROG-mkdir}" +gzipprog="${GZIPPROG-gzip}" + +transformbasename="" +transform_arg="" +instcmd="$mvprog" +chmodcmd="$chmodprog 0755" +chowncmd="" +chgrpcmd="" +stripcmd="" +rmcmd="$rmprog -f" +mvcmd="$mvprog" +src="" +dst="" +dir_arg="" + +gzipcp() { + # gzipcp from to + $gzipprog -9 <"$1" >"$2" +} + +while [ x"$1" != x ]; do + case $1 in + -c) + instcmd="$cpprog" + shift + continue + ;; + + -d) + dir_arg=true + shift + continue + ;; + + -m) + chmodcmd="$chmodprog $2" + shift + shift + continue + ;; + + -o) + chowncmd="$chownprog $2" + shift + shift + continue + ;; + + -g) + chgrpcmd="$chgrpprog $2" + shift + shift + continue + ;; + + -s) + stripcmd="$stripprog" + shift + continue + ;; + + -z) + instcmd="gzipcp" + shift + continue + ;; + + *) + if [ x"$src" = x ]; then + src="$1" + else + dst="$1" + fi + shift + continue + ;; + esac +done + +if [ x"$src" = x ]; then + echo "install-sh: No input file specified" + exit 1 +fi + +if [ x"$dir_arg" != x ]; then + dst="$src" + src="" + + if [ -d "$dst" ]; then + instcmd=: + else + instcmd=$mkdirprog + fi +else + # Waiting for this to be detected by the "$instcmd $src $dsttmp" command + # might cause directories to be created, which would be especially bad + # if $src (and thus $dsttmp) contains '*'. + if [ ! -f "$src" -a ! -d "$src" ]; then + echo "install: $src does not exist" + exit 1 + fi + + if [ x"$dst" = x ]; then + echo "install: No destination specified" + exit 1 + fi + + # If destination is a directory, append the input filename. + if [ -d "$dst" ]; then + dst="$dst/`basename $src`" + fi +fi + +## this sed command emulates the dirname command +dstdir="`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`" + +# Make sure that the destination directory exists. +# This part is taken from Noah Friedman's mkinstalldirs script + +# Skip lots of stat calls in the usual case. +if [ ! -d "$dstdir" ]; then + defaultIFS=' + ' + IFS="${IFS-${defaultIFS}}" + + oIFS="${IFS}" + # Some sh's can't handle IFS=/ for some reason. + IFS='%' + set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` + IFS="${oIFS}" + + pathcomp='' + + while [ $# -ne 0 ] ; do + pathcomp="${pathcomp}${1}" + shift + + if [ ! -d "${pathcomp}" ]; then $doit $mkdirprog "${pathcomp}"; fi + + pathcomp="${pathcomp}/" + done +fi + +if [ x"$dir_arg" != x ]; then + # Make a directory... + $doit $instcmd $dst || exit 1 + + # Allow chown/chgrp to fail, but log a warning + if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst || echo "warning: Unable to change owner of $dst!"; fi + if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst || echo "warning: Unable to change group of $dst!"; fi + if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst || exit 1; fi +else + # Install a file... + dstfile="`basename $dst`" + + # Check the destination file - for libraries just use the "-x" option + # to strip... + case "$dstfile" in + *.a | *.dylib | *.sl | *.sl.* | *.so | *.so.*) + stripopt="-x" + ;; + *) + stripopt="" + ;; + esac + + # Make a temp file name in the proper directory. + dsttmp="$dstdir/#inst.$$#" + + # Move or copy the file name to the temp name + $doit $instcmd $src $dsttmp || exit 1 + + # Update permissions and strip as needed, then move to the final name. + # If the chmod, strip, rm, or mv commands fail, remove the installed + # file... + if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripopt "$dsttmp" || echo "warning: Unable to strip $dst!"; fi + if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp" || echo "warning: Unable to change owner of $dst!"; fi + if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp" || echo "warning: Unable to change group of $dst!"; fi + + trap "rm -f ${dsttmp}" 0 && + if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; fi && + $doit $rmcmd -f "$dstdir/$dstfile" && + $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" +fi + +exit 0