2 * IPP data file parsing functions.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include <cups/cups.h>
16 #include "ipp-private.h"
17 #include "string-private.h"
18 #include "debug-internal.h"
22 * '_ippVarsDeinit()' - Free all memory associated with the IPP variables.
26 _ippVarsDeinit(_ipp_vars_t
*v
) /* I - IPP variables */
34 cupsFreeOptions(v
->num_vars
, v
->vars
);
41 * '_ippVarsExpand()' - Expand variables in the source string.
45 _ippVarsExpand(_ipp_vars_t
*v
, /* I - IPP variables */
46 char *dst
, /* I - Destination buffer */
47 const char *src
, /* I - Source string */
48 size_t dstsize
) /* I - Destination buffer size */
50 char *dstptr
, /* Pointer into destination */
51 *dstend
, /* End of destination */
52 temp
[256], /* Temporary string */
53 *tempptr
; /* Pointer into temporary string */
54 const char *value
; /* Value to substitute */
58 dstend
= dst
+ dstsize
- 1;
60 while (*src
&& dstptr
< dstend
)
65 * Substitute a string/number...
68 if (!strncmp(src
, "$$", 2))
73 else if (!strncmp(src
, "$ENV[", 5))
75 strlcpy(temp
, src
+ 5, sizeof(temp
));
77 for (tempptr
= temp
; *tempptr
; tempptr
++)
85 src
+= tempptr
- temp
+ 5;
92 strlcpy(temp
, src
, sizeof(temp
));
93 if ((tempptr
= strchr(temp
, '}')) != NULL
)
96 tempptr
= temp
+ strlen(temp
);
100 strlcpy(temp
, src
+ 1, sizeof(temp
));
102 for (tempptr
= temp
; *tempptr
; tempptr
++)
103 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
110 value
= _ippVarsGet(v
, temp
);
112 src
+= tempptr
- temp
+ 1;
117 strlcpy(dstptr
, value
, (size_t)(dstend
- dstptr
+ 1));
118 dstptr
+= strlen(dstptr
);
130 * '_ippVarsGet()' - Get a variable string.
133 const char * /* O - Value or @code NULL@ if not set */
134 _ippVarsGet(_ipp_vars_t
*v
, /* I - IPP variables */
135 const char *name
) /* I - Variable name */
137 if (!strcmp(name
, "uri"))
139 else if (!strcmp(name
, "uriuser") || !strcmp(name
, "username"))
140 return (v
->username
[0] ? v
->username
: NULL
);
141 else if (!strcmp(name
, "scheme") || !strcmp(name
, "method"))
143 else if (!strcmp(name
, "hostname"))
145 else if (!strcmp(name
, "port"))
147 else if (!strcmp(name
, "resource"))
148 return (v
->resource
);
149 else if (!strcmp(name
, "user"))
152 return (cupsGetOption(name
, v
->num_vars
, v
->vars
));
157 * '_ippVarsInit()' - Initialize .
161 _ippVarsInit(_ipp_vars_t
*v
, /* I - IPP variables */
162 _ipp_fattr_cb_t attrcb
, /* I - Attribute (filter) callback */
163 _ipp_ferror_cb_t errorcb
, /* I - Error callback */
164 _ipp_ftoken_cb_t tokencb
) /* I - Token callback */
166 memset(v
, 0, sizeof(_ipp_vars_t
));
169 v
->errorcb
= errorcb
;
170 v
->tokencb
= tokencb
;
175 * '_ippVarsPasswordCB()' - Password callback using the IPP variables.
178 const char * /* O - Password string or @code NULL@ */
180 const char *prompt
, /* I - Prompt string (not used) */
181 http_t
*http
, /* I - HTTP connection (not used) */
182 const char *method
, /* I - HTTP method (not used) */
183 const char *resource
, /* I - Resource path (not used) */
184 void *user_data
) /* I - IPP variables */
186 _ipp_vars_t
*v
= (_ipp_vars_t
*)user_data
;
187 /* I - IPP variables */
195 if (v
->username
[0] && v
->password
&& v
->password_tries
< 3)
197 v
->password_tries
++;
199 cupsSetUser(v
->username
);
201 return (v
->password
);
211 * '_ippVarsSet()' - Set an IPP variable.
214 int /* O - 1 on success, 0 on failure */
215 _ippVarsSet(_ipp_vars_t
*v
, /* I - IPP variables */
216 const char *name
, /* I - Variable name */
217 const char *value
) /* I - Variable value */
219 if (!strcmp(name
, "uri"))
221 char uri
[1024]; /* New printer URI */
222 http_uri_status_t uri_status
; /* URI status */
224 if ((uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, value
, v
->scheme
, sizeof(v
->scheme
), v
->username
, sizeof(v
->username
), v
->host
, sizeof(v
->host
), &(v
->port
), v
->resource
, sizeof(v
->resource
))) < HTTP_URI_STATUS_OK
)
229 if ((v
->password
= strchr(v
->username
, ':')) != NULL
)
230 *(v
->password
)++ = '\0';
233 snprintf(v
->portstr
, sizeof(v
->portstr
), "%d", v
->port
);
238 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), v
->scheme
, NULL
, v
->host
, v
->port
, v
->resource
);
239 v
->uri
= strdup(uri
);
241 return (v
->uri
!= NULL
);
245 v
->num_vars
= cupsAddOption(name
, value
, v
->num_vars
, &v
->vars
);