internal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_private.h"
  17. #include "fspr_arch_misc.h"
  18. #include "fspr_arch_file_io.h"
  19. #include <crtdbg.h>
  20. #include <assert.h>
  21. /* This module is the source of -static- helper functions that are
  22. * entirely internal to apr. If the fn is exported - it does not
  23. * belong here.
  24. *
  25. * Namespace decoration is still required to protect us from symbol
  26. * clashes in static linkages.
  27. */
  28. /* Shared by fspr_app.c and start.c
  29. *
  30. * An internal apr function to convert an array of strings (either
  31. * a counted or NULL terminated list, such as an argv[argc] or env[]
  32. * list respectively) from wide Unicode strings to narrow utf-8 strings.
  33. * These are allocated from the MSVCRT's _CRT_BLOCK to trick the system
  34. * into trusting our store.
  35. */
  36. int fspr_wastrtoastr(char const * const * *retarr,
  37. wchar_t const * const *arr, int args)
  38. {
  39. fspr_size_t elesize = 0;
  40. char **newarr;
  41. char *elements;
  42. char *ele;
  43. int arg;
  44. if (args < 0) {
  45. for (args = 0; arr[args]; ++args)
  46. ;
  47. }
  48. newarr = _malloc_dbg((args + 1) * sizeof(char *),
  49. _CRT_BLOCK, __FILE__, __LINE__);
  50. for (arg = 0; arg < args; ++arg) {
  51. newarr[arg] = (void*)(wcslen(arr[arg]) + 1);
  52. elesize += (fspr_size_t)newarr[arg];
  53. }
  54. /* This is a safe max allocation, we will realloc after
  55. * processing and return the excess to the free store.
  56. * 3 ucs bytes hold any single wchar_t value (16 bits)
  57. * 4 ucs bytes will hold a wchar_t pair value (20 bits)
  58. */
  59. elesize = elesize * 3 + 1;
  60. ele = elements = _malloc_dbg(elesize * sizeof(char),
  61. _CRT_BLOCK, __FILE__, __LINE__);
  62. for (arg = 0; arg < args; ++arg) {
  63. fspr_size_t len = (fspr_size_t)newarr[arg];
  64. fspr_size_t newlen = elesize;
  65. newarr[arg] = ele;
  66. (void)fspr_conv_ucs2_to_utf8(arr[arg], &len,
  67. newarr[arg], &elesize);
  68. newlen -= elesize;
  69. ele += newlen;
  70. assert(elesize && (len == 0));
  71. }
  72. newarr[arg] = NULL;
  73. *(ele++) = '\0';
  74. /* Return to the free store if the heap realloc is the least bit optimized
  75. */
  76. ele = _realloc_dbg(elements, ele - elements,
  77. _CRT_BLOCK, __FILE__, __LINE__);
  78. if (ele != elements) {
  79. fspr_size_t diff = ele - elements;
  80. for (arg = 0; arg < args; ++arg) {
  81. newarr[arg] += diff;
  82. }
  83. }
  84. *retarr = newarr;
  85. return args;
  86. }