summaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/arrow_next/cpp/src/arrow/vendored/datetime/ios.mm
blob: a532373d69b993b60ffc8a0194dcc0a488f313f8 (plain) (blame)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Kormanovsky
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

#include "ios.h"

#if TARGET_OS_IPHONE

#include <Foundation/Foundation.h>

#include <fstream>
#include <zlib.h>
#include <sys/stat.h>

#ifndef TAR_DEBUG
#  define TAR_DEBUG 0
#endif

#define INTERNAL_DIR        "Library"
#define TZDATA_DIR          "tzdata"
#define TARGZ_EXTENSION     "tar.gz"

#define TAR_BLOCK_SIZE                  512
#define TAR_TYPE_POSITION               156
#define TAR_NAME_POSITION               0
#define TAR_NAME_SIZE                   100
#define TAR_SIZE_POSITION               124
#define TAR_SIZE_SIZE                   12

namespace arrow20_vendored::date
{
    namespace iOSUtils
    {

        struct TarInfo
        {
            char objType;
            std::string objName;
            size_t realContentSize; // writable size without padding zeroes
            size_t blocksContentSize; // adjusted size to 512 bytes blocks
            bool success;
        };

        std::string convertCFStringRefPathToCStringPath(CFStringRef ref);
        bool extractTzdata(CFURLRef homeUrl, CFURLRef archiveUrl, std::string destPath);
        TarInfo getTarObjectInfo(std::ifstream &readStream);
        std::string getTarObject(std::ifstream &readStream, int64_t size);
        bool writeFile(const std::string &tzdataPath, const std::string &fileName,
                       const std::string &data, size_t realContentSize);

        std::string
        get_current_timezone()
        {
            CFTimeZoneRef tzRef = CFTimeZoneCopySystem();
            CFStringRef tzNameRef = CFTimeZoneGetName(tzRef);
            CFIndex bufferSize = CFStringGetLength(tzNameRef) + 1;
            char buffer[bufferSize];

            if (CFStringGetCString(tzNameRef, buffer, bufferSize, kCFStringEncodingUTF8))
            {
                CFRelease(tzRef);
                return std::string(buffer);
            }

            CFRelease(tzRef);

            return "";
        }

        std::string
        get_tzdata_path()
        {
            CFURLRef homeUrlRef = CFCopyHomeDirectoryURL();
            CFStringRef homePath = CFURLCopyPath(homeUrlRef);
            std::string path(std::string(convertCFStringRefPathToCStringPath(homePath)) +
                             INTERNAL_DIR + "/" + TZDATA_DIR);
            std::string result_path(std::string(convertCFStringRefPathToCStringPath(homePath)) +
                                    INTERNAL_DIR);

            if (access(path.c_str(), F_OK) == 0)
            {
#if TAR_DEBUG
                printf("tzdata dir exists\n");
#endif
                CFRelease(homeUrlRef);
                CFRelease(homePath);

                return result_path;
            }

            CFBundleRef mainBundle = CFBundleGetMainBundle();
            CFArrayRef paths = CFBundleCopyResourceURLsOfType(mainBundle, CFSTR(TARGZ_EXTENSION),
                                                              NULL);

            if (CFArrayGetCount(paths) != 0)
            {
                // get archive path, assume there is no other tar.gz in bundle
                CFURLRef archiveUrl = static_cast<CFURLRef>(CFArrayGetValueAtIndex(paths, 0));
                CFStringRef archiveName = CFURLCopyPath(archiveUrl);
                archiveUrl = CFBundleCopyResourceURL(mainBundle, archiveName, NULL, NULL);

                extractTzdata(homeUrlRef, archiveUrl, path);

                CFRelease(archiveUrl);
                CFRelease(archiveName);
            }

            CFRelease(homeUrlRef);
            CFRelease(homePath);
            CFRelease(paths);

            return result_path;
        }

        std::string
        convertCFStringRefPathToCStringPath(CFStringRef ref)
        {
            CFIndex bufferSize = CFStringGetMaximumSizeOfFileSystemRepresentation(ref);
            char *buffer = new char[bufferSize];
            CFStringGetFileSystemRepresentation(ref, buffer, bufferSize);
            auto result = std::string(buffer);
            delete[] buffer;
            return result;
        }

        bool
        extractTzdata(CFURLRef homeUrl, CFURLRef archiveUrl, std::string destPath)
        {
            std::string TAR_TMP_PATH = "/tmp.tar";

            CFStringRef homeStringRef = CFURLCopyPath(homeUrl);
            auto homePath = convertCFStringRefPathToCStringPath(homeStringRef);
            CFRelease(homeStringRef);

            CFStringRef archiveStringRef = CFURLCopyPath(archiveUrl);
            auto archivePath = convertCFStringRefPathToCStringPath(archiveStringRef);
            CFRelease(archiveStringRef);

            // create Library path
            auto libraryPath = homePath + INTERNAL_DIR;

            // create tzdata path
            auto tzdataPath = libraryPath + "/" + TZDATA_DIR;

            // -- replace %20 with " "
            const std::string search = "%20";
            const std::string replacement = " ";
            size_t pos = 0;

            while ((pos = archivePath.find(search, pos)) != std::string::npos) {
                archivePath.replace(pos, search.length(), replacement);
                pos += replacement.length();
            }

            gzFile tarFile = gzopen(archivePath.c_str(), "rb");

            // create tar unpacking path
            auto tarPath = libraryPath + TAR_TMP_PATH;

            // create tzdata directory
            mkdir(destPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

            // ======= extract tar ========

            std::ofstream os(tarPath.c_str(), std::ofstream::out | std::ofstream::app);
            unsigned int bufferLength = 1024 * 256;  // 256Kb
            unsigned char *buffer = (unsigned char *)malloc(bufferLength);
            bool success = true;

            while (true)
            {
                int readBytes = gzread(tarFile, buffer, bufferLength);

                if (readBytes > 0)
                {
                    os.write((char *) &buffer[0], readBytes);
                }
                else
                    if (readBytes == 0)
                    {
                        break;
                    }
                    else
                        if (readBytes == -1)
                        {
                            printf("decompression failed\n");
                            success = false;
                            break;
                        }
                        else
                        {
                            printf("unexpected zlib state\n");
                            success = false;
                            break;
                        }
            }

            os.close();
            free(buffer);
            gzclose(tarFile);

            if (!success)
            {
                remove(tarPath.c_str());
                return false;
            }

            // ======== extract files =========

            uint64_t location = 0; // Position in the file

            // get file size
            struct stat stat_buf;
            int res = stat(tarPath.c_str(), &stat_buf);
            if (res != 0)
            {
                printf("error file size\n");
                remove(tarPath.c_str());
                return false;
            }
            int64_t tarSize = stat_buf.st_size;

            // create read stream
            std::ifstream is(tarPath.c_str(), std::ifstream::in | std::ifstream::binary);

            // process files
            while (location < tarSize)
            {
                TarInfo info = getTarObjectInfo(is);

                if (!info.success || info.realContentSize == 0)
                {
                    break; // something wrong or all files are read
                }

                switch (info.objType)
                {
                    case '0':   // file
                    case '\0':  //
                    {
                        std::string obj = getTarObject(is, info.blocksContentSize);
#if TAR_DEBUG
                        size += info.realContentSize;
                        printf("#%i %s file size %lld written total %ld from %lld\n", ++count,
                               info.objName.c_str(), info.realContentSize, size, tarSize);
#endif
                        writeFile(tzdataPath, info.objName, obj, info.realContentSize);
                        location += info.blocksContentSize;

                        break;
                    }
                }
            }

            remove(tarPath.c_str());

            return true;
        }

        TarInfo
        getTarObjectInfo(std::ifstream &readStream)
        {
            int64_t length = TAR_BLOCK_SIZE;
            char buffer[length];
            char type;
            char name[TAR_NAME_SIZE + 1];
            char sizeBuf[TAR_SIZE_SIZE + 1];

            readStream.read(buffer, length);

            memcpy(&type, &buffer[TAR_TYPE_POSITION], 1);

            memset(&name, '\0', TAR_NAME_SIZE + 1);
            memcpy(&name, &buffer[TAR_NAME_POSITION], TAR_NAME_SIZE);

            memset(&sizeBuf, '\0', TAR_SIZE_SIZE + 1);
            memcpy(&sizeBuf, &buffer[TAR_SIZE_POSITION], TAR_SIZE_SIZE);
            size_t realSize = strtol(sizeBuf, NULL, 8);
            size_t blocksSize = realSize + (TAR_BLOCK_SIZE - (realSize % TAR_BLOCK_SIZE));

            return {type, std::string(name), realSize, blocksSize, true};
        }

        std::string
        getTarObject(std::ifstream &readStream, int64_t size)
        {
            char buffer[size];
            readStream.read(buffer, size);
            return std::string(buffer);
        }

        bool
        writeFile(const std::string &tzdataPath, const std::string &fileName, const std::string &data,
                  size_t realContentSize)
        {
            std::ofstream os(tzdataPath + "/" + fileName, std::ofstream::out | std::ofstream::binary);

            if (!os) {
                return false;
            }

            // trim empty space
            char trimmedData[realContentSize + 1];
            memset(&trimmedData, '\0', realContentSize);
            memcpy(&trimmedData, data.c_str(), realContentSize);

            // write
            os.write(trimmedData, realContentSize);
            os.close();

            return true;
        }

    }  // namespace iOSUtils
}  // namespace arrow20_vendored::date

#endif  // TARGET_OS_IPHONE