Changeset 2877 in kBuild for trunk/src/kWorker
- Timestamp:
- Sep 5, 2016 3:05:10 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kWorker/kWorker.c
r2876 r2877 31 31 * Header Files * 32 32 *********************************************************************************************************************************/ 33 //#undef NDEBUG 33 34 #include <k/kHlp.h> 34 35 #include <k/kLdr.h> … … 38 39 #include <setjmp.h> 39 40 #include <ctype.h> 41 #include <errno.h> 40 42 41 43 #include "nt/ntstat.h" … … 438 440 wchar_t **winitenv; 439 441 442 /** Size of the array we've allocated (ASSUMES nobody messes with it!). */ 443 KSIZE cEnvVarsAllocated; 440 444 /** The _environ msvcrt variable. */ 441 445 char **environ; 442 446 /** The _wenviron msvcrt variable. */ 443 447 wchar_t **wenviron; 448 /** The shadow _environ msvcrt variable. */ 449 char **papszEnvVars; 450 /** The shadow _wenviron msvcrt variable. */ 451 wchar_t **papwszEnvVars; 444 452 445 453 … … 800 808 801 809 /** 810 * Converts the given string to UTF-16, allocating the buffer. 811 * 812 * @returns Pointer to the new heap allocation containing the UTF-16 version of 813 * the source string. 814 * @param pchSrc The source string. 815 * @param cchSrc The length of the source string. 816 */ 817 static wchar_t *kwStrToUtf16AllocN(const char *pchSrc, KSIZE cchSrc) 818 { 819 DWORD const dwErrSaved = GetLastError(); 820 KSIZE cwcBuf = cchSrc + 1; 821 wchar_t *pwszBuf = (wchar_t *)kHlpAlloc(cwcBuf * sizeof(pwszBuf)); 822 if (pwszBuf) 823 { 824 if (cchSrc > 0) 825 { 826 int cwcRet = MultiByteToWideChar(CP_ACP, 0, pchSrc, (int)cchSrc, pwszBuf, (int)cwcBuf - 1); 827 if (cwcRet > 0) 828 { 829 kHlpAssert(cwcRet < (KSSIZE)cwcBuf); 830 pwszBuf[cwcRet] = '\0'; 831 } 832 else 833 { 834 kHlpFree(pwszBuf); 835 836 /* Figure the length and allocate the right buffer size. */ 837 SetLastError(NO_ERROR); 838 cwcRet = MultiByteToWideChar(CP_ACP, 0, pchSrc, (int)cchSrc, pwszBuf, 0); 839 if (cwcRet) 840 { 841 cwcBuf = cwcRet + 2; 842 pwszBuf = (wchar_t *)kHlpAlloc(cwcBuf * sizeof(pwszBuf)); 843 if (pwszBuf) 844 { 845 SetLastError(NO_ERROR); 846 cwcRet = MultiByteToWideChar(CP_ACP, 0, pchSrc, (int)cchSrc, pwszBuf, (int)cwcBuf - 1); 847 if (cwcRet) 848 { 849 kHlpAssert(cwcRet < (KSSIZE)cwcBuf); 850 pwszBuf[cwcRet] = '\0'; 851 } 852 else 853 { 854 kwErrPrintf("MultiByteToWideChar(,,%*.*s,,) -> dwErr=%d\n", cchSrc, cchSrc, pchSrc, GetLastError()); 855 kHlpFree(pwszBuf); 856 pwszBuf = NULL; 857 } 858 } 859 } 860 else 861 { 862 kwErrPrintf("MultiByteToWideChar(,,%*.*s,,NULL,0) -> dwErr=%d\n", cchSrc, cchSrc, pchSrc, GetLastError()); 863 pwszBuf = NULL; 864 } 865 } 866 } 867 else 868 pwszBuf[0] = '\0'; 869 } 870 SetLastError(dwErrSaved); 871 return pwszBuf; 872 } 873 874 875 /** 802 876 * Converts the given UTF-16 to a normal string. 803 877 * … … 822 896 pszDst[offDst - 1] = '\0'; 823 897 return offDst; 898 } 899 900 901 /** 902 * Converts the given UTF-16 to ASSI, allocating the buffer. 903 * 904 * @returns Pointer to the new heap allocation containing the ANSI version of 905 * the source string. 906 * @param pwcSrc The source string. 907 * @param cwcSrc The length of the source string. 908 */ 909 static char *kwUtf16ToStrAllocN(const wchar_t *pwcSrc, KSIZE cwcSrc) 910 { 911 DWORD const dwErrSaved = GetLastError(); 912 KSIZE cbBuf = cwcSrc + (cwcSrc >> 1) + 1; 913 char *pszBuf = (char *)kHlpAlloc(cbBuf); 914 if (pszBuf) 915 { 916 if (cwcSrc > 0) 917 { 918 int cchRet = WideCharToMultiByte(CP_ACP, 0, pwcSrc, (int)cwcSrc, pszBuf, (int)cbBuf - 1, NULL, NULL); 919 if (cchRet > 0) 920 { 921 kHlpAssert(cchRet < (KSSIZE)cbBuf); 922 pszBuf[cchRet] = '\0'; 923 } 924 else 925 { 926 kHlpFree(pszBuf); 927 928 /* Figure the length and allocate the right buffer size. */ 929 SetLastError(NO_ERROR); 930 cchRet = WideCharToMultiByte(CP_ACP, 0, pwcSrc, (int)cwcSrc, pszBuf, 0, NULL, NULL); 931 if (cchRet) 932 { 933 cbBuf = cchRet + 2; 934 pszBuf = (char *)kHlpAlloc(cbBuf); 935 if (pszBuf) 936 { 937 SetLastError(NO_ERROR); 938 cchRet = WideCharToMultiByte(CP_ACP, 0, pwcSrc, (int)cwcSrc, pszBuf, (int)cbBuf - 1, NULL, NULL); 939 if (cchRet) 940 { 941 kHlpAssert(cchRet < (KSSIZE)cbBuf); 942 pszBuf[cchRet] = '\0'; 943 } 944 else 945 { 946 kwErrPrintf("WideCharToMultiByte(,,%*.*ls,,) -> dwErr=%d\n", cwcSrc, cwcSrc, pwcSrc, GetLastError()); 947 kHlpFree(pszBuf); 948 pszBuf = NULL; 949 } 950 } 951 } 952 else 953 { 954 kwErrPrintf("WideCharToMultiByte(,,%*.*ls,,NULL,0) -> dwErr=%d\n", cwcSrc, cwcSrc, pwcSrc, GetLastError()); 955 pszBuf = NULL; 956 } 957 } 958 } 959 else 960 pszBuf[0] = '\0'; 961 } 962 SetLastError(dwErrSaved); 963 return pszBuf; 824 964 } 825 965 … … 2482 2622 */ 2483 2623 2624 /** Kernel32 - GetEnvironmentStringsA (Watcom uses this one). */ 2625 static LPCH WINAPI kwSandbox_Kernel32_GetEnvironmentStringsA(void) 2626 { 2627 char *pszzEnv; 2628 2629 /* Figure how space much we need first. */ 2630 char *pszCur; 2631 KSIZE cbNeeded = 1; 2632 KSIZE iVar = 0; 2633 while ((pszCur = g_Sandbox.papszEnvVars[iVar++]) != NULL) 2634 cbNeeded += kHlpStrLen(pszCur) + 1; 2635 2636 /* Allocate it. */ 2637 pszzEnv = kHlpAlloc(cbNeeded); 2638 if (pszzEnv) 2639 { 2640 char *psz = pszzEnv; 2641 iVar = 0; 2642 while ((pszCur = g_Sandbox.papszEnvVars[iVar++]) != NULL) 2643 { 2644 KSIZE cbCur = kHlpStrLen(pszCur) + 1; 2645 kHlpAssert((KUPTR)(&psz[cbCur] - pszzEnv) < cbNeeded); 2646 psz = (char *)kHlpMemPCopy(psz, pszCur, cbCur); 2647 } 2648 *psz++ = '\0'; 2649 kHlpAssert(psz - pszzEnv == cbNeeded); 2650 } 2651 2652 KW_LOG(("GetEnvironmentStringsA -> %p [%u]\n", pszzEnv, cbNeeded)); 2653 #if 0 2654 fprintf(stderr, "GetEnvironmentStringsA: %p LB %#x\n", pszzEnv, cbNeeded); 2655 pszCur = pszzEnv; 2656 iVar = 0; 2657 while (*pszCur) 2658 { 2659 fprintf(stderr, " %u:%p=%s<eos>\n\n", iVar, pszCur, pszCur); 2660 iVar++; 2661 pszCur += kHlpStrLen(pszCur) + 1; 2662 } 2663 fprintf(stderr, " %u:%p=<eos>\n\n", iVar, pszCur); 2664 pszCur++; 2665 fprintf(stderr, "ended at %p, after %u bytes (exepcted %u)\n", pszCur, pszCur - pszzEnv, cbNeeded); 2666 #endif 2667 return pszzEnv; 2668 } 2669 2670 2671 /** Kernel32 - GetEnvironmentStrings */ 2672 static LPCH WINAPI kwSandbox_Kernel32_GetEnvironmentStrings(void) 2673 { 2674 KW_LOG(("GetEnvironmentStrings!\n")); 2675 return kwSandbox_Kernel32_GetEnvironmentStringsA(); 2676 } 2677 2678 2679 /** Kernel32 - GetEnvironmentStringsW */ 2680 static LPWCH WINAPI kwSandbox_Kernel32_GetEnvironmentStringsW(void) 2681 { 2682 wchar_t *pwszzEnv; 2683 2684 /* Figure how space much we need first. */ 2685 wchar_t *pwszCur; 2686 KSIZE cwcNeeded = 1; 2687 KSIZE iVar = 0; 2688 while ((pwszCur = g_Sandbox.papwszEnvVars[iVar++]) != NULL) 2689 cwcNeeded += kwUtf16Len(pwszCur) + 1; 2690 2691 /* Allocate it. */ 2692 pwszzEnv = kHlpAlloc(cwcNeeded * sizeof(wchar_t)); 2693 if (pwszzEnv) 2694 { 2695 wchar_t *pwsz = pwszzEnv; 2696 iVar = 0; 2697 while ((pwszCur = g_Sandbox.papwszEnvVars[iVar++]) != NULL) 2698 { 2699 KSIZE cwcCur = kwUtf16Len(pwszCur) + 1; 2700 kHlpAssert((KUPTR)(&pwsz[cwcCur] - pwszzEnv) < cwcNeeded); 2701 pwsz = (wchar_t *)kHlpMemPCopy(pwsz, pwszCur, cwcCur * sizeof(wchar_t)); 2702 } 2703 *pwsz++ = '\0'; 2704 kHlpAssert(pwsz - pwszzEnv == cwcNeeded); 2705 } 2706 2707 KW_LOG(("GetEnvironmentStringsW -> %p [%u]\n", pwszzEnv, cwcNeeded)); 2708 return pwszzEnv; 2709 } 2710 2711 2712 /** Kernel32 - FreeEnvironmentStringsA */ 2713 static BOOL WINAPI kwSandbox_Kernel32_FreeEnvironmentStringsA(LPCH pszzEnv) 2714 { 2715 KW_LOG(("FreeEnvironmentStringsA: %p -> TRUE\n", pszzEnv)); 2716 kHlpFree(pszzEnv); 2717 return TRUE; 2718 } 2719 2720 2721 /** Kernel32 - FreeEnvironmentStringsW */ 2722 static BOOL WINAPI kwSandbox_Kernel32_FreeEnvironmentStringsW(LPWCH pwszzEnv) 2723 { 2724 KW_LOG(("FreeEnvironmentStringsW: %p -> TRUE\n", pwszzEnv)); 2725 kHlpFree(pwszzEnv); 2726 return TRUE; 2727 } 2728 2729 2730 /** 2731 * Grows the environment vectors (KWSANDBOX::environ, KWSANDBOX::papszEnvVars, 2732 * KWSANDBOX::wenviron, and KWSANDBOX::papwszEnvVars). 2733 * 2734 * @returns 0 on success, non-zero on failure. 2735 * @param pSandbox The sandbox. 2736 * @param cMin Minimum size, including terminator. 2737 */ 2738 static int kwSandboxGrowEnv(PKWSANDBOX pSandbox, KSIZE cMin) 2739 { 2740 void *pvNew; 2741 KSIZE const cOld = pSandbox->cEnvVarsAllocated; 2742 KSIZE cNew = cOld + 256; 2743 while (cNew < cMin) 2744 cNew += 256; 2745 2746 2747 pvNew = kHlpRealloc(pSandbox->environ, cNew * sizeof(pSandbox->environ[0])); 2748 if (pvNew) 2749 { 2750 pSandbox->environ = (char **)pvNew; 2751 pSandbox->environ[cOld] = NULL; 2752 2753 pvNew = kHlpRealloc(pSandbox->papszEnvVars, cNew * sizeof(pSandbox->papszEnvVars[0])); 2754 if (pvNew) 2755 { 2756 pSandbox->papszEnvVars = (char **)pvNew; 2757 pSandbox->papszEnvVars[cOld] = NULL; 2758 2759 pvNew = kHlpRealloc(pSandbox->wenviron, cNew * sizeof(pSandbox->wenviron[0])); 2760 if (pvNew) 2761 { 2762 pSandbox->wenviron = (wchar_t **)pvNew; 2763 pSandbox->wenviron[cOld] = NULL; 2764 2765 pvNew = kHlpRealloc(pSandbox->papwszEnvVars, cNew * sizeof(pSandbox->papwszEnvVars[0])); 2766 if (pvNew) 2767 { 2768 pSandbox->papwszEnvVars = (wchar_t **)pvNew; 2769 pSandbox->papwszEnvVars[cOld] = NULL; 2770 2771 pSandbox->cEnvVarsAllocated = cNew; 2772 KW_LOG(("kwSandboxGrowEnv: cNew=%d - crt: %p / %p; shadow: %p, %p\n", 2773 cNew, pSandbox->environ, pSandbox->wenviron, pSandbox->papszEnvVars, pSandbox->papwszEnvVars)); 2774 return 0; 2775 } 2776 } 2777 } 2778 } 2779 kwErrPrintf("kwSandboxGrowEnv ran out of memory! cNew=%u\n", cNew); 2780 return KERR_NO_MEMORY; 2781 } 2782 2783 2784 /** 2785 * Sets an environment variable, ANSI style. 2786 * 2787 * @returns 0 on success, non-zero on failure. 2788 * @param pSandbox The sandbox. 2789 * @param pchVar The variable name. 2790 * @param cchVar The length of the name. 2791 * @param pszValue The value. 2792 */ 2793 static int kwSandboxDoSetEnvA(PKWSANDBOX pSandbox, const char *pchVar, KSIZE cchVar, const char *pszValue) 2794 { 2795 /* Allocate and construct the new strings. */ 2796 KSIZE cchTmp = kHlpStrLen(pszValue); 2797 char *pszNew = (char *)kHlpAlloc(cchVar + 1 + cchTmp + 1); 2798 if (pszNew) 2799 { 2800 wchar_t *pwszNew; 2801 kHlpMemCopy(pszNew, pchVar, cchVar); 2802 pszNew[cchVar] = '='; 2803 kHlpMemCopy(&pszNew[cchVar + 1], pszValue, cchTmp); 2804 cchTmp += cchVar + 1; 2805 pszNew[cchTmp] = '\0'; 2806 2807 pwszNew = kwStrToUtf16AllocN(pszNew, cchTmp); 2808 if (pwszNew) 2809 { 2810 /* Look it up. */ 2811 KSIZE iVar = 0; 2812 char *pszEnv; 2813 while ((pszEnv = pSandbox->papszEnvVars[iVar]) != NULL) 2814 { 2815 if ( _strnicmp(pszEnv, pchVar, cchVar) == 0 2816 && pszEnv[cchVar] == '=') 2817 { 2818 KW_LOG(("kwSandboxDoSetEnvA: Replacing iVar=%d: %p='%s' and %p='%ls'\n" 2819 " iVar=%d: %p='%s' and %p='%ls'\n", 2820 iVar, pSandbox->papszEnvVars[iVar], pSandbox->papszEnvVars[iVar], 2821 pSandbox->papwszEnvVars[iVar], pSandbox->papwszEnvVars[iVar], 2822 iVar, pszNew, pszNew, pwszNew, pwszNew)); 2823 2824 kHlpFree(pSandbox->papszEnvVars[iVar]); 2825 pSandbox->papszEnvVars[iVar] = pszNew; 2826 pSandbox->environ[iVar] = pszNew; 2827 2828 kHlpFree(pSandbox->papwszEnvVars[iVar]); 2829 pSandbox->papwszEnvVars[iVar] = pwszNew; 2830 pSandbox->wenviron[iVar] = pwszNew; 2831 return 0; 2832 } 2833 iVar++; 2834 } 2835 2836 /* Not found, do we need to grow the table first? */ 2837 if (iVar + 1 >= pSandbox->cEnvVarsAllocated) 2838 kwSandboxGrowEnv(pSandbox, iVar + 2); 2839 if (iVar + 1 < pSandbox->cEnvVarsAllocated) 2840 { 2841 KW_LOG(("kwSandboxDoSetEnvA: Adding iVar=%d: %p='%s' and %p='%ls'\n", iVar, pszNew, pszNew, pwszNew, pwszNew)); 2842 2843 pSandbox->papszEnvVars[iVar + 1] = NULL; 2844 pSandbox->papszEnvVars[iVar] = pszNew; 2845 pSandbox->environ[iVar + 1] = NULL; 2846 pSandbox->environ[iVar] = pszNew; 2847 2848 pSandbox->papwszEnvVars[iVar + 1] = NULL; 2849 pSandbox->papwszEnvVars[iVar] = pwszNew; 2850 pSandbox->wenviron[iVar + 1] = NULL; 2851 pSandbox->wenviron[iVar] = pwszNew; 2852 return 0; 2853 } 2854 2855 kHlpFree(pwszNew); 2856 } 2857 kHlpFree(pszNew); 2858 } 2859 KW_LOG(("Out of memory!\n")); 2860 return 0; 2861 } 2862 2863 2864 /** 2865 * Sets an environment variable, UTF-16 style. 2866 * 2867 * @returns 0 on success, non-zero on failure. 2868 * @param pSandbox The sandbox. 2869 * @param pwcVar The variable name. 2870 * @param cwcVar The length of the name. 2871 * @param pwszValue The value. 2872 */ 2873 static int kwSandboxDoSetEnvW(PKWSANDBOX pSandbox, const wchar_t *pwchVar, KSIZE cwcVar, const wchar_t *pwszValue) 2874 { 2875 /* Allocate and construct the new strings. */ 2876 KSIZE cwcTmp = kwUtf16Len(pwszValue); 2877 wchar_t *pwszNew = (wchar_t *)kHlpAlloc((cwcVar + 1 + cwcTmp + 1) * sizeof(wchar_t)); 2878 if (pwszNew) 2879 { 2880 char *pszNew; 2881 kHlpMemCopy(pwszNew, pwchVar, cwcVar * sizeof(wchar_t)); 2882 pwszNew[cwcVar] = '='; 2883 kHlpMemCopy(&pwszNew[cwcVar + 1], pwszValue, cwcTmp * sizeof(wchar_t)); 2884 cwcTmp += cwcVar + 1; 2885 pwszNew[cwcVar] = '\0'; 2886 2887 pszNew = kwUtf16ToStrAllocN(pwszNew, cwcVar); 2888 if (pszNew) 2889 { 2890 /* Look it up. */ 2891 KSIZE iVar = 0; 2892 wchar_t *pwszEnv; 2893 while ((pwszEnv = pSandbox->papwszEnvVars[iVar]) != NULL) 2894 { 2895 if ( _wcsnicmp(pwszEnv, pwchVar, cwcVar) == 0 2896 && pwszEnv[cwcVar] == '=') 2897 { 2898 KW_LOG(("kwSandboxDoSetEnvW: Replacing iVar=%d: %p='%s' and %p='%ls'\n" 2899 " iVar=%d: %p='%s' and %p='%ls'\n", 2900 iVar, pSandbox->papszEnvVars[iVar], pSandbox->papszEnvVars[iVar], 2901 pSandbox->papwszEnvVars[iVar], pSandbox->papwszEnvVars[iVar], 2902 iVar, pszNew, pszNew, pwszNew, pwszNew)); 2903 2904 kHlpFree(pSandbox->papszEnvVars[iVar]); 2905 pSandbox->papszEnvVars[iVar] = pszNew; 2906 pSandbox->environ[iVar] = pszNew; 2907 2908 kHlpFree(pSandbox->papwszEnvVars[iVar]); 2909 pSandbox->papwszEnvVars[iVar] = pwszNew; 2910 pSandbox->wenviron[iVar] = pwszNew; 2911 return 0; 2912 } 2913 iVar++; 2914 } 2915 2916 /* Not found, do we need to grow the table first? */ 2917 if (iVar + 1 >= pSandbox->cEnvVarsAllocated) 2918 kwSandboxGrowEnv(pSandbox, iVar + 2); 2919 if (iVar + 1 < pSandbox->cEnvVarsAllocated) 2920 { 2921 KW_LOG(("kwSandboxDoSetEnvW: Adding iVar=%d: %p='%s' and %p='%ls'\n", iVar, pszNew, pszNew, pwszNew, pwszNew)); 2922 2923 pSandbox->papszEnvVars[iVar + 1] = NULL; 2924 pSandbox->papszEnvVars[iVar] = pszNew; 2925 pSandbox->environ[iVar + 1] = NULL; 2926 pSandbox->environ[iVar] = pszNew; 2927 2928 pSandbox->papwszEnvVars[iVar + 1] = NULL; 2929 pSandbox->papwszEnvVars[iVar] = pwszNew; 2930 pSandbox->wenviron[iVar + 1] = NULL; 2931 pSandbox->wenviron[iVar] = pwszNew; 2932 return 0; 2933 } 2934 2935 kHlpFree(pwszNew); 2936 } 2937 kHlpFree(pszNew); 2938 } 2939 KW_LOG(("Out of memory!\n")); 2940 return 0; 2941 } 2942 2943 2944 /** ANSI unsetenv worker. */ 2945 static int kwSandboxDoUnsetEnvA(PKWSANDBOX pSandbox, const char *pchVar, KSIZE cchVar) 2946 { 2947 KSIZE iVar = 0; 2948 char *pszEnv; 2949 while ((pszEnv = pSandbox->papszEnvVars[iVar]) != NULL) 2950 { 2951 if ( _strnicmp(pszEnv, pchVar, cchVar) == 0 2952 && pszEnv[cchVar] == '=') 2953 { 2954 KSIZE cVars = iVar; 2955 while (pSandbox->papszEnvVars[cVars]) 2956 cVars++; 2957 kHlpAssert(pSandbox->papwszEnvVars[iVar] != NULL); 2958 kHlpAssert(pSandbox->papwszEnvVars[cVars] == NULL); 2959 2960 KW_LOG(("kwSandboxDoUnsetEnvA: Removing iVar=%d: %p='%s' and %p='%ls'; new cVars=%d\n", iVar, 2961 pSandbox->papszEnvVars[iVar], pSandbox->papszEnvVars[iVar], 2962 pSandbox->papwszEnvVars[iVar], pSandbox->papwszEnvVars[iVar], cVars - 1)); 2963 2964 kHlpFree(pSandbox->papszEnvVars[iVar]); 2965 pSandbox->papszEnvVars[iVar] = pSandbox->papszEnvVars[cVars]; 2966 pSandbox->environ[iVar] = pSandbox->papszEnvVars[cVars]; 2967 pSandbox->papszEnvVars[cVars] = NULL; 2968 pSandbox->environ[cVars] = NULL; 2969 2970 kHlpFree(pSandbox->papwszEnvVars[iVar]); 2971 pSandbox->papwszEnvVars[iVar] = pSandbox->papwszEnvVars[cVars]; 2972 pSandbox->wenviron[iVar] = pSandbox->papwszEnvVars[cVars]; 2973 pSandbox->papwszEnvVars[cVars] = NULL; 2974 pSandbox->wenviron[cVars] = NULL; 2975 return 0; 2976 } 2977 iVar++; 2978 } 2979 return KERR_ENVVAR_NOT_FOUND; 2980 } 2981 2982 2983 /** UTF-16 unsetenv worker. */ 2984 static int kwSandboxDoUnsetEnvW(PKWSANDBOX pSandbox, const wchar_t *pwcVar, KSIZE cwcVar) 2985 { 2986 KSIZE iVar = 0; 2987 wchar_t *pwszEnv; 2988 while ((pwszEnv = pSandbox->papwszEnvVars[iVar]) != NULL) 2989 { 2990 if ( _wcsnicmp(pwszEnv, pwcVar, cwcVar) == 0 2991 && pwszEnv[cwcVar] == '=') 2992 { 2993 KSIZE cVars = iVar; 2994 while (pSandbox->papwszEnvVars[cVars]) 2995 cVars++; 2996 kHlpAssert(pSandbox->papszEnvVars[iVar] != NULL); 2997 kHlpAssert(pSandbox->papszEnvVars[cVars] == NULL); 2998 2999 KW_LOG(("kwSandboxDoUnsetEnvA: Removing iVar=%d: %p='%s' and %p='%ls'; new cVars=%d\n", iVar, 3000 pSandbox->papszEnvVars[iVar], pSandbox->papszEnvVars[iVar], 3001 pSandbox->papwszEnvVars[iVar], pSandbox->papwszEnvVars[iVar], cVars - 1)); 3002 3003 kHlpFree(pSandbox->papszEnvVars[iVar]); 3004 pSandbox->papszEnvVars[iVar] = pSandbox->papszEnvVars[cVars]; 3005 pSandbox->environ[iVar] = pSandbox->papszEnvVars[cVars]; 3006 pSandbox->papszEnvVars[cVars] = NULL; 3007 pSandbox->environ[cVars] = NULL; 3008 3009 kHlpFree(pSandbox->papwszEnvVars[iVar]); 3010 pSandbox->papwszEnvVars[iVar] = pSandbox->papwszEnvVars[cVars]; 3011 pSandbox->wenviron[iVar] = pSandbox->papwszEnvVars[cVars]; 3012 pSandbox->papwszEnvVars[cVars] = NULL; 3013 pSandbox->wenviron[cVars] = NULL; 3014 return 0; 3015 } 3016 iVar++; 3017 } 3018 return KERR_ENVVAR_NOT_FOUND; 3019 } 3020 3021 3022 3023 /** ANSI getenv worker. */ 3024 static char *kwSandboxDoGetEnvA(PKWSANDBOX pSandbox, const char *pchVar, KSIZE cchVar) 3025 { 3026 KSIZE iVar = 0; 3027 char *pszEnv; 3028 while ((pszEnv = pSandbox->papszEnvVars[iVar++]) != NULL) 3029 if ( _strnicmp(pszEnv, pchVar, cchVar) == 0 3030 && pszEnv[cchVar] == '=') 3031 return &pszEnv[cchVar + 1]; 3032 return NULL; 3033 } 3034 3035 3036 /** UTF-16 getenv worker. */ 3037 static wchar_t *kwSandboxDoGetEnvW(PKWSANDBOX pSandbox, const wchar_t *pwcVar, KSIZE cwcVar) 3038 { 3039 KSIZE iVar = 0; 3040 wchar_t *pwszEnv; 3041 while ((pwszEnv = pSandbox->papwszEnvVars[iVar++]) != NULL) 3042 if ( _wcsnicmp(pwszEnv, pwcVar, cwcVar) == 0 3043 && pwszEnv[cwcVar] == '=') 3044 return &pwszEnv[cwcVar + 1]; 3045 return NULL; 3046 } 3047 3048 2484 3049 /** Kernel32 - GetEnvironmentVariableA() */ 2485 static DWORD WINAPI kwSandbox_Kernel32_GetEnvironmentVariableA(LPCSTR pwszVar, LPSTR pszValue, DWORD cbValue) 3050 static DWORD WINAPI kwSandbox_Kernel32_GetEnvironmentVariableA(LPCSTR pszVar, LPSTR pszValue, DWORD cbValue) 3051 { 3052 char *pszFoundValue = kwSandboxDoGetEnvA(&g_Sandbox, pszVar, kHlpStrLen(pszVar)); 3053 if (pszFoundValue) 3054 { 3055 DWORD cchRet = kwStrCopyStyle1(pszFoundValue, pszValue, cbValue); 3056 KW_LOG(("GetEnvironmentVariableA: '%s' -> %u (%s)\n", pszVar, cchRet, pszFoundValue)); 3057 return cchRet; 3058 } 3059 KW_LOG(("GetEnvironmentVariableA: '%s' -> 0\n", pszVar)); 3060 SetLastError(ERROR_ENVVAR_NOT_FOUND); 3061 return 0; 3062 } 3063 3064 3065 /** Kernel32 - GetEnvironmentVariableW() */ 3066 static DWORD WINAPI kwSandbox_Kernel32_GetEnvironmentVariableW(LPCWSTR pwszVar, LPWSTR pwszValue, DWORD cwcValue) 3067 { 3068 wchar_t *pwszFoundValue = kwSandboxDoGetEnvW(&g_Sandbox, pwszVar, kwUtf16Len(pwszVar)); 3069 if (pwszFoundValue) 3070 { 3071 DWORD cchRet = kwUtf16CopyStyle1(pwszFoundValue, pwszValue, cwcValue); 3072 KW_LOG(("GetEnvironmentVariableW: '%ls' -> %u (%ls)\n", pwszVar, cchRet, pwszFoundValue)); 3073 return cchRet; 3074 } 3075 KW_LOG(("GetEnvironmentVariableW: '%ls' -> 0\n", pwszVar)); 3076 SetLastError(ERROR_ENVVAR_NOT_FOUND); 3077 return 0; 3078 } 3079 3080 3081 /** Kernel32 - SetEnvironmentVariableA() */ 3082 static BOOL WINAPI kwSandbox_Kernel32_SetEnvironmentVariableA(LPCSTR pszVar, LPCSTR pszValue) 3083 { 3084 int rc; 3085 if (pszValue) 3086 rc = kwSandboxDoSetEnvA(&g_Sandbox, pszVar, kHlpStrLen(pszVar), pszValue); 3087 else 3088 { 3089 kwSandboxDoUnsetEnvA(&g_Sandbox, pszVar, kHlpStrLen(pszVar)); 3090 rc = 0; //?? 3091 } 3092 if (rc == 0) 3093 { 3094 KW_LOG(("SetEnvironmentVariableA(%s,%s) -> TRUE\n", pszVar, pszValue)); 3095 return TRUE; 3096 } 3097 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 3098 KW_LOG(("SetEnvironmentVariableA(%s,%s) -> FALSE!\n", pszVar, pszValue)); 3099 return FALSE; 3100 } 3101 3102 3103 /** Kernel32 - SetEnvironmentVariableW() */ 3104 static BOOL WINAPI kwSandbox_Kernel32_SetEnvironmentVariableW(LPCWSTR pwszVar, LPCWSTR pwszValue) 3105 { 3106 int rc; 3107 if (pwszValue) 3108 rc = kwSandboxDoSetEnvW(&g_Sandbox, pwszVar, kwUtf16Len(pwszVar), pwszValue); 3109 else 3110 { 3111 kwSandboxDoUnsetEnvW(&g_Sandbox, pwszVar, kwUtf16Len(pwszVar)); 3112 rc = 0; //?? 3113 } 3114 if (rc == 0) 3115 { 3116 KW_LOG(("SetEnvironmentVariableA(%ls,%ls) -> TRUE\n", pwszVar, pwszValue)); 3117 return TRUE; 3118 } 3119 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 3120 KW_LOG(("SetEnvironmentVariableA(%ls,%ls) -> FALSE!\n", pwszVar, pwszValue)); 3121 return FALSE; 3122 } 3123 3124 3125 /** Kernel32 - ExpandEnvironmentStringsA() */ 3126 static DWORD WINAPI kwSandbox_Kernel32_ExpandEnvironmentStringsA(LPCSTR pszSrc, LPSTR pwszDst, DWORD cbDst) 2486 3127 { 2487 3128 KWFS_TODO(); … … 2490 3131 2491 3132 2492 /** Kernel32 - GetEnvironmentVariableW() */ 2493 static DWORD WINAPI kwSandbox_Kernel32_GetEnvironmentVariableW(LPCWSTR pwszVar, LPWSTR pwszValue, DWORD cbValue) 2494 { 2495 KW_LOG(("GetEnvironmentVariableW: '%ls'\n", pwszVar)); 2496 //KWFS_TODO(); 2497 //SetLastError(ERROR_ENVVAR_NOT_FOUND); 2498 //return 0; 2499 return GetEnvironmentVariableW(pwszVar, pwszValue, cbValue); 2500 } 2501 2502 2503 /** Kernel32 - SetEnvironmentVariableA() */ 2504 static BOOL WINAPI kwSandbox_Kernel32_SetEnvironmentVariableA(LPCSTR pszVar, LPCSTR pszValue) 2505 { 2506 KWFS_TODO(); 2507 return FALSE; 2508 } 2509 2510 2511 /** Kernel32 - SetEnvironmentVariableW() */ 2512 static BOOL WINAPI kwSandbox_Kernel32_SetEnvironmentVariableW(LPCWSTR pwszVar, LPCWSTR pwszValue) 2513 { 2514 KW_LOG(("SetEnvironmentVariableW: '%ls' = '%ls'\n", pwszVar, pwszValue)); 2515 return SetEnvironmentVariableW(pwszVar, pwszValue); 2516 //KWFS_TODO(); 2517 //return FALSE; 2518 } 2519 2520 2521 /** Kernel32 - ExpandEnvironmentStringsA() */ 2522 static DWORD WINAPI kwSandbox_Kernel32_ExpandEnvironmentStringsA(LPCSTR pszSrc, LPSTR pwszDst, DWORD cbDst) 3133 /** Kernel32 - ExpandEnvironmentStringsW() */ 3134 static DWORD WINAPI kwSandbox_Kernel32_ExpandEnvironmentStringsW(LPCWSTR pwszSrc, LPWSTR pwszDst, DWORD cbDst) 2523 3135 { 2524 3136 KWFS_TODO(); … … 2527 3139 2528 3140 2529 /** Kernel32 - ExpandEnvironmentStringsW() */2530 static DWORD WINAPI kwSandbox_Kernel32_ExpandEnvironmentStringsW(LPCWSTR pwszSrc, LPWSTR pwszDst, DWORD cbDst)2531 {2532 KWFS_TODO();2533 return 0;2534 }2535 2536 2537 3141 /** CRT - _putenv(). */ 2538 3142 static int __cdecl kwSandbox_msvcrt__putenv(const char *pszVarEqualValue) 2539 3143 { 2540 KWFS_TODO(); 2541 return 0; 3144 int rc; 3145 char const *pszEqual = kHlpStrChr(pszVarEqualValue, '='); 3146 if (pszEqual) 3147 { 3148 rc = kwSandboxDoSetEnvA(&g_Sandbox, pszVarEqualValue, pszEqual - pszVarEqualValue, pszEqual + 1); 3149 if (rc == 0) 3150 { } 3151 else 3152 rc = -1; 3153 } 3154 else 3155 { 3156 kwSandboxDoUnsetEnvA(&g_Sandbox, pszVarEqualValue, kHlpStrLen(pszVarEqualValue)); 3157 rc = 0; 3158 } 3159 KW_LOG(("_putenv(%s) -> %d\n", pszVarEqualValue, rc)); 3160 return rc; 2542 3161 } 2543 3162 … … 2546 3165 static int __cdecl kwSandbox_msvcrt__wputenv(const wchar_t *pwszVarEqualValue) 2547 3166 { 2548 KWFS_TODO(); 2549 return 0; 3167 int rc; 3168 wchar_t const *pwszEqual = wcschr(pwszVarEqualValue, '='); 3169 if (pwszEqual) 3170 { 3171 rc = kwSandboxDoSetEnvW(&g_Sandbox, pwszVarEqualValue, pwszEqual - pwszVarEqualValue, pwszEqual + 1); 3172 if (rc == 0) 3173 { } 3174 else 3175 rc = -1; 3176 } 3177 else 3178 { 3179 kwSandboxDoUnsetEnvW(&g_Sandbox, pwszVarEqualValue, kwUtf16Len(pwszVarEqualValue)); 3180 rc = 0; 3181 } 3182 KW_LOG(("_wputenv(%ls) -> %d\n", pwszVarEqualValue, rc)); 3183 return rc; 2550 3184 } 2551 3185 … … 2554 3188 static errno_t __cdecl kwSandbox_msvcrt__putenv_s(const char *pszVar, const char *pszValue) 2555 3189 { 2556 KWFS_TODO(); 2557 return 0; 3190 char const *pszEqual = kHlpStrChr(pszVar, '='); 3191 if (pszEqual == NULL) 3192 { 3193 if (pszValue) 3194 { 3195 int rc = kwSandboxDoSetEnvA(&g_Sandbox, pszVar, kHlpStrLen(pszVar), pszValue); 3196 if (rc == 0) 3197 { 3198 KW_LOG(("_putenv_s(%s,%s) -> 0\n", pszVar, pszValue)); 3199 return 0; 3200 } 3201 } 3202 else 3203 { 3204 kwSandboxDoUnsetEnvA(&g_Sandbox, pszVar, kHlpStrLen(pszVar)); 3205 KW_LOG(("_putenv_s(%ls,NULL) -> 0\n", pszVar)); 3206 return 0; 3207 } 3208 KW_LOG(("_putenv_s(%s,%s) -> ENOMEM\n", pszVar, pszValue)); 3209 return ENOMEM; 3210 } 3211 KW_LOG(("_putenv_s(%s,%s) -> EINVAL\n", pszVar, pszValue)); 3212 return EINVAL; 2558 3213 } 2559 3214 … … 2562 3217 static errno_t __cdecl kwSandbox_msvcrt__wputenv_s(const wchar_t *pwszVar, const wchar_t *pwszValue) 2563 3218 { 2564 KW_LOG(("_wputenv_s: '%ls' = '%ls'\n", pwszVar, pwszValue)); 2565 //KWFS_TODO(); 2566 return SetEnvironmentVariableW(pwszVar, pwszValue) ? 0 : -1; 3219 wchar_t const *pwszEqual = wcschr(pwszVar, '='); 3220 if (pwszEqual == NULL) 3221 { 3222 if (pwszValue) 3223 { 3224 int rc = kwSandboxDoSetEnvW(&g_Sandbox, pwszVar, kwUtf16Len(pwszVar), pwszValue); 3225 if (rc == 0) 3226 { 3227 KW_LOG(("_wputenv_s(%ls,%ls) -> 0\n", pwszVar, pwszValue)); 3228 return 0; 3229 } 3230 } 3231 else 3232 { 3233 kwSandboxDoUnsetEnvW(&g_Sandbox, pwszVar, kwUtf16Len(pwszVar)); 3234 KW_LOG(("_wputenv_s(%ls,NULL) -> 0\n", pwszVar)); 3235 return 0; 3236 } 3237 KW_LOG(("_wputenv_s(%ls,%ls) -> ENOMEM\n", pwszVar, pwszValue)); 3238 return ENOMEM; 3239 } 3240 KW_LOG(("_wputenv_s(%ls,%ls) -> EINVAL\n", pwszVar, pwszValue)); 3241 return EINVAL; 2567 3242 } 2568 3243 … … 2571 3246 static char *** __cdecl kwSandbox_msvcrt___p___initenv(void) 2572 3247 { 3248 KW_LOG(("__p___initenv\n")); 3249 KWFS_TODO(); 2573 3250 return &g_Sandbox.initenv; 2574 3251 } … … 2578 3255 static wchar_t *** __cdecl kwSandbox_msvcrt___p___winitenv(void) 2579 3256 { 3257 KW_LOG(("__p___winitenv\n")); 3258 KWFS_TODO(); 2580 3259 return &g_Sandbox.winitenv; 2581 3260 } … … 2585 3264 static char *** __cdecl kwSandbox_msvcrt___p__environ(void) 2586 3265 { 3266 KW_LOG(("__p__environ\n")); 2587 3267 return &g_Sandbox.environ; 2588 3268 } … … 2592 3272 static wchar_t *** __cdecl kwSandbox_msvcrt___p__wenviron(void) 2593 3273 { 3274 KW_LOG(("__p__wenviron\n")); 2594 3275 return &g_Sandbox.wenviron; 2595 3276 } … … 2600 3281 static KUPTR /*void*/ __cdecl kwSandbox_msvcrt__get_environ(char ***ppapszEnviron) 2601 3282 { 2602 KWFS_TODO(); /** @todo check the callers expec ations! */3283 KWFS_TODO(); /** @todo check the callers expectations! */ 2603 3284 *ppapszEnviron = g_Sandbox.environ; 2604 3285 return 0; … … 2610 3291 static KUPTR /*void*/ __cdecl kwSandbox_msvcrt__get_wenviron(wchar_t ***ppapwszEnviron) 2611 3292 { 2612 KWFS_TODO(); /** @todo check the callers expec ations! */3293 KWFS_TODO(); /** @todo check the callers expectations! */ 2613 3294 *ppapwszEnviron = g_Sandbox.wenviron; 2614 3295 return 0; … … 2984 3665 KU32_MAX /*iSymbol*/, 2985 3666 pszProc, 2986 strlen(pszProc),3667 kHlpStrLen(pszProc), 2987 3668 NULL /*pszVersion*/, 2988 3669 NULL /*pfnGetForwarder*/, NULL /*pvUser*/, … … 4791 5472 { TUPLE("CreateThread"), NULL, (KUPTR)kwSandbox_Kernel32_CreateThread }, 4792 5473 5474 { TUPLE("GetEnvironmentStrings"), NULL, (KUPTR)kwSandbox_Kernel32_GetEnvironmentStrings }, 5475 { TUPLE("GetEnvironmentStringsA"), NULL, (KUPTR)kwSandbox_Kernel32_GetEnvironmentStringsA }, 5476 { TUPLE("GetEnvironmentStringsW"), NULL, (KUPTR)kwSandbox_Kernel32_GetEnvironmentStringsW }, 5477 { TUPLE("FreeEnvironmentStringsA"), NULL, (KUPTR)kwSandbox_Kernel32_FreeEnvironmentStringsA }, 5478 { TUPLE("FreeEnvironmentStringsW"), NULL, (KUPTR)kwSandbox_Kernel32_FreeEnvironmentStringsW }, 4793 5479 { TUPLE("GetEnvironmentVariableA"), NULL, (KUPTR)kwSandbox_Kernel32_GetEnvironmentVariableA }, 4794 5480 { TUPLE("GetEnvironmentVariableW"), NULL, (KUPTR)kwSandbox_Kernel32_GetEnvironmentVariableW }, … … 5134 5820 cbCmdLine = 0; 5135 5821 for (i = 0; i < cArgs; i++) 5136 cbCmdLine += strlen(papszQuotedArgs[i]) + 1;5822 cbCmdLine += kHlpStrLen(papszQuotedArgs[i]) + 1; 5137 5823 *pcbCmdLine = cbCmdLine; 5138 5824 … … 5172 5858 KSIZE cbCmdLine; 5173 5859 KU32 i; 5860 int rc; 5174 5861 5175 5862 /* Simple stuff. */ … … 5197 5884 *pwcPool++ = pSandbox->papszArgs[i][-1]; /* flags */ 5198 5885 pSandbox->papwszArgs[i] = pwcPool; 5199 pwcPool += kwStrToUtf16(pSandbox->papszArgs[i], pwcPool, ( strlen(pSandbox->papszArgs[i]) + 1) * 2);5886 pwcPool += kwStrToUtf16(pSandbox->papszArgs[i], pwcPool, (kHlpStrLen(pSandbox->papszArgs[i]) + 1) * 2); 5200 5887 pwcPool++; 5201 5888 } … … 5217 5904 5218 5905 /* 5906 * Setup the enviornment. 5907 */ 5908 rc = kwSandboxGrowEnv(pSandbox, cEnvVars + 2); 5909 if (rc == 0) 5910 { 5911 KU32 iDst = 0; 5912 for (i = 0; i < cEnvVars; i++) 5913 { 5914 const char *pszVar = papszEnvVars[i]; 5915 KSIZE cchVar = kHlpStrLen(pszVar); 5916 if ( cchVar > 0 5917 && kHlpMemChr(pszVar, '=', cchVar) != NULL) 5918 { 5919 char *pszCopy = kHlpDup(pszVar, cchVar + 1); 5920 wchar_t *pwszCopy = kwStrToUtf16AllocN(pszVar, cchVar + 1); 5921 if (pszCopy && pwszCopy) 5922 { 5923 pSandbox->papszEnvVars[iDst] = pszCopy; 5924 pSandbox->environ[iDst] = pszCopy; 5925 pSandbox->papwszEnvVars[iDst] = pwszCopy; 5926 pSandbox->wenviron[iDst] = pwszCopy; 5927 iDst++; 5928 } 5929 else 5930 { 5931 kHlpFree(pszCopy); 5932 kHlpFree(pwszCopy); 5933 return kwErrPrintfRc(KERR_NO_MEMORY, "Out of memory setting up env vars!\n"); 5934 } 5935 } 5936 else 5937 kwErrPrintf("kwSandboxInit: Skipping bad env var '%s'\n", pszVar); 5938 } 5939 pSandbox->papszEnvVars[iDst] = NULL; 5940 pSandbox->environ[iDst] = NULL; 5941 pSandbox->papwszEnvVars[iDst] = NULL; 5942 pSandbox->wenviron[iDst] = NULL; 5943 } 5944 else 5945 return kwErrPrintfRc(KERR_NO_MEMORY, "Error setting up environment variables: %d\n", rc); 5946 5947 /* 5219 5948 * Invalidate the volatile parts of cache (kBuild output directory, 5220 5949 * temporary directory, whatever). … … 5289 6018 } 5290 6019 6020 /* Free the environment. */ 6021 if (pSandbox->papszEnvVars) 6022 { 6023 KU32 i; 6024 for (i = 0; pSandbox->papszEnvVars[i]; i++) 6025 kHlpFree(pSandbox->papszEnvVars[i]); 6026 pSandbox->environ[0] = NULL; 6027 pSandbox->papszEnvVars[0] = NULL; 6028 6029 for (i = 0; pSandbox->papwszEnvVars[i]; i++) 6030 kHlpFree(pSandbox->papwszEnvVars[i]); 6031 pSandbox->wenviron[0] = NULL; 6032 pSandbox->papwszEnvVars[0] = NULL; 6033 } 5291 6034 } 5292 6035 … … 5484 6227 */ 5485 6228 const char *pszExecutable; 5486 size_tcbTmp;6229 KSIZE cbTmp; 5487 6230 5488 6231 pszMsg += sizeof("JOB"); … … 5491 6234 /* Executable name. */ 5492 6235 pszExecutable = pszMsg; 5493 cbTmp = strlen(pszMsg) + 1;6236 cbTmp = kHlpStrLen(pszMsg) + 1; 5494 6237 pszMsg += cbTmp; 5495 6238 if ( cbTmp < cbMsg … … 5501 6244 /* Current working directory. */ 5502 6245 pszCwd = pszMsg; 5503 cbTmp = strlen(pszMsg) + 1;6246 cbTmp = kHlpStrLen(pszMsg) + 1; 5504 6247 pszMsg += cbTmp; 5505 6248 if ( cbTmp + sizeof(KU32) < cbMsg … … 5524 6267 { 5525 6268 papszArgs[i] = pszMsg + 1; /* First byte is expansion flags for MSC & EMX. */ 5526 cbTmp = 1 + strlen(pszMsg + 1) + 1;6269 cbTmp = 1 + kHlpStrLen(pszMsg + 1) + 1; 5527 6270 pszMsg += cbTmp; 5528 6271 if (cbTmp < cbMsg) … … 5555 6298 { 5556 6299 papszEnvVars[i] = pszMsg; 5557 cbTmp = strlen(pszMsg) + 1;6300 cbTmp = kHlpStrLen(pszMsg) + 1; 5558 6301 pszMsg += cbTmp; 5559 6302 if (cbTmp < cbMsg)
Note:
See TracChangeset
for help on using the changeset viewer.