File: gp_wgetv.c

package info (click to toggle)
ghostscript 9.06~dfsg-2%2Bdeb8u7
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 62,484 kB
  • sloc: ansic: 440,074; python: 4,915; cpp: 3,565; sh: 2,520; tcl: 1,482; perl: 1,374; makefile: 421; lisp: 407; awk: 66; yacc: 18
file content (246 lines) | stat: -rw-r--r-- 7,815 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Copyright (C) 2001-2012 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
   CA  94903, U.S.A., +1(415)492-9861, for further information.
*/


/* MS Windows implementation of gp_getenv, and gp_serialnumber */

#include "windows_.h"
#include <stdio.h>
#include <stdlib.h>		/* for getenv */
#include <string.h>
#include "gscdefs.h"		/* for gs_productfamily and gs_revision and gs_serialnumber */

#ifdef __WIN32__
/*
 * Get a named registry value.
 * Key = hkeyroot\\key, named value = name.
 * name, ptr, plen and return values are the same as in gp_getenv();
 */
#ifdef WINDOWS_NO_UNICODE
static int
gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name,
    char *ptr, int *plen)
{
    HKEY hkey;
    DWORD cbData;
    BYTE b;
    LONG rc;
    BYTE *bptr = (BYTE *)ptr;

    if (RegOpenKeyEx(hkeyroot, key, 0, KEY_READ, &hkey)
        == ERROR_SUCCESS) {
        cbData = *plen;
        if (bptr == (char *)NULL)
            bptr = &b;	/* Registry API won't return ERROR_MORE_DATA */
                        /* if ptr is NULL */
        rc = RegQueryValueEx(hkey, (char *)name, 0, NULL, bptr, &cbData);
        RegCloseKey(hkey);
        if (rc == ERROR_SUCCESS) {
            *plen = cbData;
            return 0;	/* found environment variable and copied it */
        } else if (rc == ERROR_MORE_DATA) {
            /* buffer wasn't large enough */
            *plen = cbData;
            return -1;
        }
    }
    return 1;	/* not found */
}
#else        /* ifdef WINDOWS_NO_UNICODE */
int
gp_getenv_registry(HKEY hkeyroot, const wchar_t *key, const char *name,
    char *ptr, int *plen)
{
    HKEY hkey;
    DWORD cbData, keytype;
    wchar_t w;
    LONG rc;
    wchar_t *wptr;
    wchar_t *wp = NULL;
    wchar_t *wname;
    int l = -1;

    if (*plen) {
        wp = malloc((*plen)*sizeof(wchar_t));
        if (wp == NULL)
            return 1;
    }
    wptr = wp;

    wname = malloc(utf8_to_wchar(NULL, name)*sizeof(wchar_t));
    if (wname == NULL) {
        if (wp)
            free(wp);
        return 1;
    }
    utf8_to_wchar(wname, name);

    if (RegOpenKeyExW(hkeyroot, key, 0, KEY_READ, &hkey) != ERROR_SUCCESS) {
        free(wname);
        if (wp)
            free(wp);
        return 1; /* Not found */
    }
    keytype = REG_SZ;
    cbData = (*plen) * sizeof(wchar_t);
    if (wptr == NULL)                   /* Registry API won't return */
        wptr = &w;	                /* ERROR_MORE_DATA if ptr is NULL */
    rc = RegQueryValueExW(hkey, wname, 0, &keytype, (BYTE *)wptr, &cbData);
    RegCloseKey(hkey);
    /* Process string keys, ignore keys that are not strings */
    if (keytype == REG_SZ) {
        switch (rc) {
            case ERROR_SUCCESS:
                if (wp) {
                    l = wchar_to_utf8(NULL, wp);
                    if (l <= *plen) {
                        *plen = wchar_to_utf8(ptr, wp);
                        free(wp);
                        free(wname);
                        return 0;       /* found environment variable and copied it */
                    } else;             /* buffer too small, so fall through */
                }                       /* caller only asked for the size, so fall through */
            case ERROR_MORE_DATA:
                /* buffer wasn't large enough or caller only asked for the size */
                if (l >= 0L) {
                    /* buffer size already computed above */
                    *plen = l;
                } else {
                    /* If we're probing for the size, then worst case, it can
                     * take 3 bytes out for every 2 bytes in. */
                    *plen = (cbData*3+1)/2;
                }
                if (wp)
                    free(wp);
                free(wname);
                return -1;              /* found environment variable, but buffer too small */
            default:
                break;
        }
    }
    if (wp)
        free(wp);
    free(wname);
    return 1;                           /* environment variable does not exist */
}
#endif        /* ifdef WINDOWS_NO_UNICODE */
#endif    /* ifdef __WIN32__ */

/* ------ Environment variables ------ */

/* Get the value of an environment variable.  See gp.h for details. */
int
gp_getenv(const char *name, char *ptr, int *plen)
{
    const char *str = getenv(name);

    if (str) {
        int len = strlen(str);

        if (len < *plen) {
            /* string fits */
            strcpy(ptr, str);
            *plen = len + 1;
            return 0;
        }
        /* string doesn't fit */
        *plen = len + 1;
        return -1;
    }
    /* environment variable was not found */

#ifdef __WIN32__
    {
        /* If using Win32, look in the registry for a value with
         * the given name.  The registry value will be under the key
         * HKEY_CURRENT_USER\Software\GPL Ghostscript\N.NN
         * or if that fails under the key
         * HKEY_LOCAL_MACHINE\Software\GPL Ghostscript\N.NN
         * where "GPL Ghostscript" is actually gs_productfamily
         * and N.NN is obtained from gs_revision.
         */
        DWORD version = GetVersion();

        if (!(((HIWORD(version) & 0x8000) != 0)
              && ((HIWORD(version) & 0x4000) == 0))) {
            /* not Win32s */
            int code;
#ifdef WINDOWS_NO_UNICODE
            char key[256];
            char dotversion[16];

            sprintf(dotversion, "%d.%02d", (int)(gs_revision / 100),
                    (int)(gs_revision % 100));
            sprintf(key, "Software\\%s\\%s", gs_productfamily, dotversion);
#else
            wchar_t key[256];
            wchar_t dotversion[16];

            wsprintfW(dotversion, L"%d.%02d", (int)(gs_revision / 100),
                      (int)(gs_revision % 100));
            wsprintfW(key, L"Software\\%hs\\%s", gs_productfamily, dotversion);
#endif
            code = gp_getenv_registry(HKEY_CURRENT_USER, key, name, ptr, plen);
            if ( code <= 0 )
                return code;	/* found it */

            code = gp_getenv_registry(HKEY_LOCAL_MACHINE, key, name, ptr, plen);
            if ( code <= 0 )
                return code;	/* found it */
        }
    }
#endif

    /* nothing found at all */

    if (*plen > 0)
        *ptr = 0;
    *plen = 1;
    return 1;
}

/* If we aren't on WIN32, We don't have a good way to get a serial */
/* number here, so just return what we always used to */
int
gp_serialnumber(void)
{
#ifdef __WIN32__
#define SERIALNUMBER_BUFSIZE 512
    byte buf[SERIALNUMBER_BUFSIZE];
    int buflen = SERIALNUMBER_BUFSIZE;
    int code, i;
#ifdef WINDOWS_NO_UNICODE
    char key[256];

    sprintf(key, "Software\\Microsoft\\MSLicensing\\HardwareID");
#else        /* WINDOWS_NO_UNICODE */
    wchar_t key[256];

    wsprintfW(key, L"Software\\Microsoft\\MSLicensing\\HardwareID");
#endif        /* WINDOWS_NO_UNICODE */
    code = gp_getenv_registry(HKEY_LOCAL_MACHINE, key, "ClientHWID", (char *)buf, &buflen);
    if ( code != 0 )
        return (int)(gs_serialnumber); 	/* error - just return the default */

    /* now turn the buffer into a 31-bit (avoid negative values) integer */
    for (i=0, code=0; i<buflen; i++)
        code += code + (buf[i] ^ i) + (code >> 31);      /* a cheap checksum */

    return code & 0x7fffffff;    /* strip the high bit */

#endif    /* __WIN32__ */

    return (int)(gs_serialnumber);
}