4 * SNMP supplies functions for CUPS.
6 * Copyright 2008-2011 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * backendSNMPSupplies() - Get the current supplies for a device.
19 * backend_init_supplies() - Initialize the supplies list.
20 * backend_walk_cb() - Interpret the supply value responses.
21 * utf16_to_utf8() - Convert UTF-16 text to UTF-8.
25 * Include necessary headers.
28 #include "backend-private.h"
29 #include <cups/array.h>
36 #define CUPS_MAX_SUPPLIES 32 /* Maximum number of supplies for a printer */
37 #define CUPS_SUPPLY_TIMEOUT 2.0 /* Timeout for SNMP lookups */
39 #define CUPS_DEVELOPER_LOW 1
40 #define CUPS_DEVELOPER_EMPTY 2
41 #define CUPS_MARKER_SUPPLY_LOW 4
42 #define CUPS_MARKER_SUPPLY_EMPTY 8
43 #define CUPS_MARKER_WASTE_ALMOST_FULL 16
44 #define CUPS_MARKER_WASTE_FULL 32
45 #define CUPS_OPC_NEAR_EOL 64
46 #define CUPS_OPC_LIFE_OVER 128
47 #define CUPS_TONER_LOW 256
48 #define CUPS_TONER_EMPTY 512
55 typedef struct /**** Printer supply data ****/
57 char name
[CUPS_SNMP_MAX_STRING
], /* Name of supply */
58 color
[8]; /* Color: "#RRGGBB" or "none" */
59 int colorant
, /* Colorant index */
60 type
, /* Supply type */
61 max_capacity
, /* Maximum capacity */
62 level
; /* Current level value */
65 typedef struct /**** Printer state table ****/
67 int bit
; /* State bit */
68 const char *keyword
; /* IPP printer-state-reasons keyword */
76 static http_addr_t current_addr
; /* Current address */
77 static int current_state
= -1;
78 /* Current device state bits */
79 static int charset
= -1; /* Character set for supply names */
80 static int num_supplies
= 0;
81 /* Number of supplies found */
82 static backend_supplies_t supplies
[CUPS_MAX_SUPPLIES
];
83 /* Supply information */
84 static int supply_state
= -1;
85 /* Supply state info */
87 static const int hrDeviceDescr
[] =
88 { CUPS_OID_hrDeviceDescr
, 1, -1 };
89 /* Device description OID */
90 static const int hrPrinterStatus
[] =
91 { CUPS_OID_hrPrinterStatus
, 1, -1 };
92 /* Current state OID */
93 static const int hrPrinterDetectedErrorState
[] =
94 { CUPS_OID_hrPrinterDetectedErrorState
, 1, -1 };
95 /* Current printer state bits OID */
96 static const int prtGeneralCurrentLocalization
[] =
97 { CUPS_OID_prtGeneralCurrentLocalization
, 1, -1 };
98 static const int prtLocalizationCharacterSet
[] =
99 { CUPS_OID_prtLocalizationCharacterSet
, 1, 1, -1 },
100 prtLocalizationCharacterSetOffset
=
101 (sizeof(prtLocalizationCharacterSet
) /
102 sizeof(prtLocalizationCharacterSet
[0]));
103 static const int prtMarkerColorantValue
[] =
104 { CUPS_OID_prtMarkerColorantValue
, -1 },
106 prtMarkerColorantValueOffset
=
107 (sizeof(prtMarkerColorantValue
) /
108 sizeof(prtMarkerColorantValue
[0]));
109 /* Offset to colorant index */
110 static const int prtMarkerLifeCount
[] =
111 { CUPS_OID_prtMarkerLifeCount
, 1, 1, -1 };
112 /* Page counter OID */
113 static const int prtMarkerSuppliesEntry
[] =
114 { CUPS_OID_prtMarkerSuppliesEntry
, -1 };
116 static const int prtMarkerSuppliesColorantIndex
[] =
117 { CUPS_OID_prtMarkerSuppliesColorantIndex
, -1 },
118 /* Colorant index OID */
119 prtMarkerSuppliesColorantIndexOffset
=
120 (sizeof(prtMarkerSuppliesColorantIndex
) /
121 sizeof(prtMarkerSuppliesColorantIndex
[0]));
122 /* Offset to supply index */
123 static const int prtMarkerSuppliesDescription
[] =
124 { CUPS_OID_prtMarkerSuppliesDescription
, -1 },
125 /* Description OID */
126 prtMarkerSuppliesDescriptionOffset
=
127 (sizeof(prtMarkerSuppliesDescription
) /
128 sizeof(prtMarkerSuppliesDescription
[0]));
129 /* Offset to supply index */
130 static const int prtMarkerSuppliesLevel
[] =
131 { CUPS_OID_prtMarkerSuppliesLevel
, -1 },
133 prtMarkerSuppliesLevelOffset
=
134 (sizeof(prtMarkerSuppliesLevel
) /
135 sizeof(prtMarkerSuppliesLevel
[0]));
136 /* Offset to supply index */
137 static const int prtMarkerSuppliesMaxCapacity
[] =
138 { CUPS_OID_prtMarkerSuppliesMaxCapacity
, -1 },
139 /* Max capacity OID */
140 prtMarkerSuppliesMaxCapacityOffset
=
141 (sizeof(prtMarkerSuppliesMaxCapacity
) /
142 sizeof(prtMarkerSuppliesMaxCapacity
[0]));
143 /* Offset to supply index */
144 static const int prtMarkerSuppliesType
[] =
145 { CUPS_OID_prtMarkerSuppliesType
, -1 },
147 prtMarkerSuppliesTypeOffset
=
148 (sizeof(prtMarkerSuppliesType
) /
149 sizeof(prtMarkerSuppliesType
[0]));
150 /* Offset to supply index */
152 static const backend_state_t
const printer_states
[] =
154 { CUPS_TC_lowPaper
, "media-low-report" },
155 { CUPS_TC_noPaper
| CUPS_TC_inputTrayEmpty
, "media-empty-warning" },
156 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
157 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
158 { CUPS_TC_doorOpen
, "door-open-report" },
159 { CUPS_TC_jammed
, "media-jam-warning" },
160 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
161 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
162 { CUPS_TC_inputTrayMissing
, "input-tray-missing-warning" },
163 { CUPS_TC_outputTrayMissing
, "output-tray-missing-warning" },
164 { CUPS_TC_markerSupplyMissing
, "marker-supply-missing-warning" },
165 { CUPS_TC_outputNearFull
, "output-area-almost-full-report" },
166 { CUPS_TC_outputFull
, "output-area-full-warning" }
169 static const backend_state_t
const supply_states
[] =
171 { CUPS_DEVELOPER_LOW
, "developer-low-report" },
172 { CUPS_DEVELOPER_EMPTY
, "developer-empty-warning" },
173 { CUPS_MARKER_SUPPLY_LOW
, "marker-supply-low-report" },
174 { CUPS_MARKER_SUPPLY_EMPTY
, "marker-supply-empty-warning" },
175 { CUPS_MARKER_WASTE_ALMOST_FULL
, "marker-waste-almost-full-report" },
176 { CUPS_MARKER_WASTE_FULL
, "marker-waste-full-warning" },
177 { CUPS_OPC_NEAR_EOL
, "opc-near-eol-report" },
178 { CUPS_OPC_LIFE_OVER
, "opc-life-over-warning" },
179 { CUPS_TONER_LOW
, "toner-low-report" },
180 { CUPS_TONER_EMPTY
, "toner-empty-warning" }
188 static void backend_init_supplies(int snmp_fd
, http_addr_t
*addr
);
189 static void backend_walk_cb(cups_snmp_t
*packet
, void *data
);
190 static void utf16_to_utf8(cups_utf8_t
*dst
, const unsigned char *src
,
191 size_t srcsize
, size_t dstsize
, int le
);
195 * 'backendSNMPSupplies()' - Get the current supplies for a device.
198 int /* O - 0 on success, -1 on error */
200 int snmp_fd
, /* I - SNMP socket */
201 http_addr_t
*addr
, /* I - Printer address */
202 int *page_count
, /* O - Page count */
203 int *printer_state
) /* O - Printer state */
205 if (!httpAddrEqual(addr
, ¤t_addr
))
206 backend_init_supplies(snmp_fd
, addr
);
207 else if (num_supplies
> 0)
208 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
209 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel
,
210 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
218 if (num_supplies
> 0)
220 int i
, /* Looping var */
221 percent
, /* Percent full */
222 new_state
, /* New state value */
223 change_state
, /* State change */
224 new_supply_state
= 0; /* Supply state */
225 char value
[CUPS_MAX_SUPPLIES
* 4],
226 /* marker-levels value string */
227 *ptr
; /* Pointer into value string */
228 cups_snmp_t packet
; /* SNMP response packet */
231 * Generate the marker-levels value string...
234 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
236 percent
= 100 * supplies
[i
].level
/ supplies
[i
].max_capacity
;
240 switch (supplies
[i
].type
)
243 case CUPS_TC_tonerCartridge
:
245 new_supply_state
|= CUPS_TONER_EMPTY
;
247 new_supply_state
|= CUPS_TONER_LOW
;
249 case CUPS_TC_wasteToner
:
250 case CUPS_TC_wasteInk
:
252 new_supply_state
|= CUPS_MARKER_WASTE_FULL
;
254 new_supply_state
|= CUPS_MARKER_WASTE_ALMOST_FULL
;
257 case CUPS_TC_inkCartridge
:
258 case CUPS_TC_inkRibbon
:
259 case CUPS_TC_solidWax
:
260 case CUPS_TC_ribbonWax
:
262 new_supply_state
|= CUPS_MARKER_SUPPLY_EMPTY
;
264 new_supply_state
|= CUPS_MARKER_SUPPLY_LOW
;
266 case CUPS_TC_developer
:
268 new_supply_state
|= CUPS_DEVELOPER_EMPTY
;
270 new_supply_state
|= CUPS_DEVELOPER_LOW
;
272 case CUPS_TC_coronaWire
:
275 case CUPS_TC_transferUnit
:
277 new_supply_state
|= CUPS_OPC_LIFE_OVER
;
279 new_supply_state
|= CUPS_OPC_NEAR_EOL
;
287 if (supplies
[i
].max_capacity
> 0)
288 sprintf(ptr
, "%d", percent
);
293 fprintf(stderr
, "ATTR: marker-levels=%s\n", value
);
295 if (supply_state
< 0)
296 change_state
= 0xffff;
298 change_state
= supply_state
^ new_supply_state
;
300 fprintf(stderr
, "DEBUG: new_supply_state=%x, change_state=%x\n",
301 new_supply_state
, change_state
);
304 i
< (int)(sizeof(supply_states
) / sizeof(supply_states
[0]));
306 if (change_state
& supply_states
[i
].bit
)
308 fprintf(stderr
, "STATE: %c%s\n",
309 (new_supply_state
& supply_states
[i
].bit
) ? '+' : '-',
310 supply_states
[i
].keyword
);
313 supply_state
= new_supply_state
;
316 * Get the current printer status bits...
319 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
320 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
321 hrPrinterDetectedErrorState
))
324 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
325 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
328 if (packet
.object_value
.string
.num_bytes
== 2)
329 new_state
= (packet
.object_value
.string
.bytes
[0] << 8) |
330 packet
.object_value
.string
.bytes
[1];
331 else if (packet
.object_value
.string
.num_bytes
== 1)
332 new_state
= (packet
.object_value
.string
.bytes
[0] << 8);
336 if (current_state
< 0)
337 change_state
= 0xffff;
339 change_state
= current_state
^ new_state
;
341 fprintf(stderr
, "DEBUG: new_state=%x, change_state=%x\n", new_state
,
345 i
< (int)(sizeof(printer_states
) / sizeof(printer_states
[0]));
347 if (change_state
& printer_states
[i
].bit
)
349 fprintf(stderr
, "STATE: %c%s\n",
350 (new_state
& printer_states
[i
].bit
) ? '+' : '-',
351 printer_states
[i
].keyword
);
354 current_state
= new_state
;
357 * Get the current printer state...
362 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
363 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
367 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
368 packet
.object_type
!= CUPS_ASN1_INTEGER
)
371 *printer_state
= packet
.object_value
.integer
;
375 * Get the current page count...
380 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
381 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
385 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
386 packet
.object_type
!= CUPS_ASN1_COUNTER
)
389 *page_count
= packet
.object_value
.counter
;
400 * 'backend_init_supplies()' - Initialize the supplies list.
404 backend_init_supplies(
405 int snmp_fd
, /* I - SNMP socket */
406 http_addr_t
*addr
) /* I - Printer address */
408 int i
, /* Looping var */
409 type
; /* Current marker type */
410 cups_file_t
*cachefile
; /* Cache file */
411 const char *cachedir
; /* CUPS_CACHEDIR value */
412 char addrstr
[1024], /* Address string */
413 cachefilename
[1024], /* Cache filename */
414 description
[CUPS_SNMP_MAX_STRING
],
415 /* Device description string */
416 value
[CUPS_MAX_SUPPLIES
* (CUPS_SNMP_MAX_STRING
* 2 + 3)],
418 *ptr
, /* Pointer into value string */
419 *name_ptr
; /* Pointer into name string */
420 cups_snmp_t packet
; /* SNMP response packet */
421 ppd_file_t
*ppd
; /* PPD file for this queue */
422 ppd_attr_t
*ppdattr
; /* cupsSNMPSupplies attribute */
423 static const char * const types
[] = /* Supply types */
463 * Reset state information...
466 current_addr
= *addr
;
471 memset(supplies
, 0, sizeof(supplies
));
474 * See if we should be getting supply levels via SNMP...
477 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
||
478 ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPSupplies", NULL
)) != NULL
&&
479 ppdattr
->value
&& strcasecmp(ppdattr
->value
, "true")))
488 * Get the device description...
491 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
492 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
496 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
497 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
499 strlcpy(description
, "Unknown", sizeof(description
));
503 strlcpy(description
, (char *)packet
.object_value
.string
.bytes
,
504 sizeof(description
));
506 fprintf(stderr
, "DEBUG2: hrDeviceDesc=\"%s\"\n", description
);
509 * See if we have already queried this device...
512 httpAddrString(addr
, addrstr
, sizeof(addrstr
));
514 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
515 cachedir
= CUPS_CACHEDIR
;
517 snprintf(cachefilename
, sizeof(cachefilename
), "%s/%s.snmp", cachedir
,
520 if ((cachefile
= cupsFileOpen(cachefilename
, "r")) != NULL
)
523 * Yes, read the cache file:
525 * 2 num_supplies charset
527 * supply structures...
530 if (cupsFileGets(cachefile
, value
, sizeof(value
)))
532 if (sscanf(value
, "2 %d%d", &num_supplies
, &charset
) == 2 &&
533 num_supplies
<= CUPS_MAX_SUPPLIES
&&
534 cupsFileGets(cachefile
, value
, sizeof(value
)))
536 if (!strcmp(description
, value
))
537 cupsFileRead(cachefile
, (char *)supplies
,
538 num_supplies
* sizeof(backend_supplies_t
));
552 cupsFileClose(cachefile
);
556 * If the cache information isn't correct, scan for supplies...
562 * Get the configured character set...
565 int oid
[CUPS_SNMP_MAX_OID
]; /* OID for character set */
568 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
569 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
570 prtGeneralCurrentLocalization
))
573 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
574 packet
.object_type
!= CUPS_ASN1_INTEGER
)
577 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
578 packet
.object_type
, CUPS_ASN1_INTEGER
);
582 fprintf(stderr
, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
583 packet
.object_value
.integer
);
585 _cupsSNMPCopyOID(oid
, prtLocalizationCharacterSet
, CUPS_SNMP_MAX_OID
);
586 oid
[prtLocalizationCharacterSetOffset
- 2] = packet
.object_value
.integer
;
589 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
590 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
594 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
595 packet
.object_type
!= CUPS_ASN1_INTEGER
)
598 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
599 packet
.object_type
, CUPS_ASN1_INTEGER
);
603 fprintf(stderr
, "DEBUG2: prtLocalizationCharacterSet=%d\n",
604 packet
.object_value
.integer
);
605 charset
= packet
.object_value
.integer
;
608 if (num_supplies
< 0)
611 * Walk the printer configuration information...
614 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
615 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesEntry
,
616 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
620 * Save the cached information...
623 if (num_supplies
< 0)
626 if ((cachefile
= cupsFileOpen(cachefilename
, "w")) != NULL
)
628 cupsFilePrintf(cachefile
, "2 %d %d\n", num_supplies
, charset
);
629 cupsFilePrintf(cachefile
, "%s\n", description
);
631 if (num_supplies
> 0)
632 cupsFileWrite(cachefile
, (char *)supplies
,
633 num_supplies
* sizeof(backend_supplies_t
));
635 cupsFileClose(cachefile
);
638 if (num_supplies
<= 0)
645 for (i
= 0; i
< num_supplies
; i
++)
646 strcpy(supplies
[i
].color
, "none");
648 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
649 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue
,
650 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
653 * Output the marker-colors attribute...
656 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
661 strcpy(ptr
, supplies
[i
].color
);
664 fprintf(stderr
, "ATTR: marker-colors=%s\n", value
);
667 * Output the marker-names attribute...
670 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++)
676 for (name_ptr
= supplies
[i
].name
; *name_ptr
;)
678 if (*name_ptr
== '\\' || *name_ptr
== '\"')
681 *ptr
++ = *name_ptr
++;
688 fprintf(stderr
, "ATTR: marker-names=%s\n", value
);
691 * Output the marker-types attribute...
694 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
699 type
= supplies
[i
].type
;
701 if (type
< CUPS_TC_other
|| type
> CUPS_TC_covers
)
702 strcpy(ptr
, "unknown");
704 strcpy(ptr
, types
[type
- CUPS_TC_other
]);
707 fprintf(stderr
, "ATTR: marker-types=%s\n", value
);
712 * 'backend_walk_cb()' - Interpret the supply value responses.
716 backend_walk_cb(cups_snmp_t
*packet
, /* I - SNMP packet */
717 void *data
) /* I - User data (unused) */
719 int i
, j
, k
; /* Looping vars */
720 static const char * const colors
[8][2] =
721 { /* Standard color names */
722 { "black", "#000000" },
723 { "blue", "#0000FF" },
724 { "cyan", "#00FFFF" },
725 { "green", "#00FF00" },
726 { "magenta", "#FF00FF" },
727 { "red", "#FF0000" },
728 { "white", "#FFFFFF" },
729 { "yellow", "#FFFF00" }
735 if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerColorantValue
) &&
736 packet
->object_type
== CUPS_ASN1_OCTET_STRING
)
742 i
= packet
->object_name
[prtMarkerColorantValueOffset
];
744 fprintf(stderr
, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i
,
745 (char *)packet
->object_value
.string
.bytes
);
747 for (j
= 0; j
< num_supplies
; j
++)
748 if (supplies
[j
].colorant
== i
)
750 for (k
= 0; k
< (int)(sizeof(colors
) / sizeof(colors
[0])); k
++)
751 if (!strcmp(colors
[k
][0], (char *)packet
->object_value
.string
.bytes
))
753 strcpy(supplies
[j
].color
, colors
[k
][1]);
758 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesColorantIndex
))
761 * Get colorant index...
764 i
= packet
->object_name
[prtMarkerSuppliesColorantIndexOffset
];
765 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
766 packet
->object_type
!= CUPS_ASN1_INTEGER
)
769 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i
,
770 packet
->object_value
.integer
);
772 if (i
> num_supplies
)
775 supplies
[i
- 1].colorant
= packet
->object_value
.integer
;
777 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesDescription
))
780 * Get supply name/description...
783 i
= packet
->object_name
[prtMarkerSuppliesDescriptionOffset
];
784 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
785 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
788 if (i
> num_supplies
)
793 case CUPS_TC_csASCII
:
794 case CUPS_TC_csUTF8
:
795 case CUPS_TC_csUnicodeASCII
:
796 strlcpy(supplies
[i
- 1].name
,
797 (char *)packet
->object_value
.string
.bytes
,
798 sizeof(supplies
[0].name
));
801 case CUPS_TC_csISOLatin1
:
802 case CUPS_TC_csUnicodeLatin1
:
803 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
804 (char *)packet
->object_value
.string
.bytes
,
805 sizeof(supplies
[0].name
), CUPS_ISO8859_1
);
808 case CUPS_TC_csShiftJIS
:
809 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
810 (char *)packet
->object_value
.string
.bytes
,
811 sizeof(supplies
[0].name
), CUPS_JIS_X0213
);
814 case CUPS_TC_csUCS4
:
815 case CUPS_TC_csUTF32
:
816 case CUPS_TC_csUTF32BE
:
817 case CUPS_TC_csUTF32LE
:
818 cupsUTF32ToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
819 (cups_utf32_t
*)packet
->object_value
.string
.bytes
,
820 sizeof(supplies
[0].name
));
823 case CUPS_TC_csUnicode
:
824 case CUPS_TC_csUTF16BE
:
825 case CUPS_TC_csUTF16LE
:
826 utf16_to_utf8((cups_utf8_t
*)supplies
[i
- 1].name
,
827 packet
->object_value
.string
.bytes
,
828 packet
->object_value
.string
.num_bytes
,
829 sizeof(supplies
[0].name
), charset
== CUPS_TC_csUTF16LE
);
834 * If we get here, the printer is using an unknown character set and
835 * we just want to copy characters that look like ASCII...
839 char *src
, *dst
; /* Pointers into strings */
843 * Loop safe because both the object_value and supplies char arrays
844 * are CUPS_SNMP_MAX_STRING elements long.
847 for (src
= (char *)packet
->object_value
.string
.bytes
,
848 dst
= supplies
[i
- 1].name
;
852 if ((*src
& 0x80) || *src
< ' ' || *src
== 0x7f)
863 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i
,
864 supplies
[i
- 1].name
);
867 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesLevel
))
873 i
= packet
->object_name
[prtMarkerSuppliesLevelOffset
];
874 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
875 packet
->object_type
!= CUPS_ASN1_INTEGER
)
878 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i
,
879 packet
->object_value
.integer
);
881 if (i
> num_supplies
)
884 supplies
[i
- 1].level
= packet
->object_value
.integer
;
886 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesMaxCapacity
))
889 * Get max capacity...
892 i
= packet
->object_name
[prtMarkerSuppliesMaxCapacityOffset
];
893 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
894 packet
->object_type
!= CUPS_ASN1_INTEGER
)
897 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i
,
898 packet
->object_value
.integer
);
900 if (i
> num_supplies
)
903 supplies
[i
- 1].max_capacity
= packet
->object_value
.integer
;
905 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesType
))
911 i
= packet
->object_name
[prtMarkerSuppliesTypeOffset
];
912 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
913 packet
->object_type
!= CUPS_ASN1_INTEGER
)
916 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i
,
917 packet
->object_value
.integer
);
919 if (i
> num_supplies
)
922 supplies
[i
- 1].type
= packet
->object_value
.integer
;
928 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
933 cups_utf8_t
*dst
, /* I - Destination buffer */
934 const unsigned char *src
, /* I - Source string */
935 size_t srcsize
, /* I - Size of source string */
936 size_t dstsize
, /* I - Size of destination buffer */
937 int le
) /* I - Source is little-endian? */
939 cups_utf32_t ch
, /* Current character */
940 temp
[CUPS_SNMP_MAX_STRING
],
942 *ptr
; /* Pointer into UTF-32 string */
945 for (ptr
= temp
; srcsize
>= 2;)
948 ch
= src
[0] | (src
[1] << 8);
950 ch
= (src
[0] << 8) | src
[1];
955 if (ch
>= 0xd800 && ch
<= 0xdbff && srcsize
>= 2)
958 * Multi-word UTF-16 char...
961 int lch
; /* Lower word */
965 lch
= src
[0] | (src
[1] << 8);
967 lch
= (src
[0] << 8) | src
[1];
969 if (lch
>= 0xdc00 && lch
<= 0xdfff)
974 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
978 if (ptr
< (temp
+ CUPS_SNMP_MAX_STRING
- 1))
984 cupsUTF32ToUTF8(dst
, temp
, dstsize
);