-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path51Degrees.c
2298 lines (2081 loc) · 86.9 KB
/
51Degrees.c
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include "snprintf/snprintf.h"
#include <string.h>
#include <limits.h>
#include <math.h>
#include "51Degrees.h"
/* *********************************************************************
* This Source Code Form is copyright of 51Degrees Mobile Experts Limited.
* Copyright � 2014 51Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY
*
* This Source Code Form is the subject of the following patent
* applications, owned by 51Degrees Mobile Experts Limited of 5 Charlotte
* Close, Caversham, Reading, Berkshire, United Kingdom RG4 7BY:
* European Patent Application No. 13192291.6; and
* United States Patent Application Nos. 14/085,223 and 14/085,301.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0.
*
* If a copy of the MPL was not distributed with this file, You can obtain
* one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses", as
* defined by the Mozilla Public License, v. 2.0.
********************************************************************** */
/**
* PROBLEM MEHODS
*/
/* Change snprintf to the Microsoft version */
#ifdef _MSC_FULL_VER
#define snprintf _snprintf
#endif
/**
* DATA STRUCTURES USED ONLY BY FUNCTIONS IN THIS FILE
*/
/* Ranges used when performing a numeric match */
const fiftyoneDegreesRANGE RANGES[] = {
{ 0, 10 },
{ 10, 100 },
{ 100, 1000 },
{ 1000, 10000 },
{ 10000, SHRT_MAX }
};
#define RANGES_COUNT sizeof(RANGES) / sizeof(fiftyoneDegreesRANGE)
const int16_t POWERS[] = { 1, 10, 100, 1000, 10000 };
#define POWERS_COUNT sizeof(POWERS) / sizeof(int32_t)
/**
* DATA FILE READ METHODS
*/
fiftyoneDegreesDataSetInitStatus readStrings(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->strings = (const byte*)malloc(dataSet->header.strings.length + 1);
if (dataSet->strings == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->strings), dataSet->header.strings.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readComponents(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->components = (const fiftyoneDegreesComponent*)malloc(dataSet->header.components.length);
if (dataSet->components == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->components), dataSet->header.components.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readMaps(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->maps = (const fiftyoneDegreesMap*)malloc(dataSet->header.maps.length);
if (dataSet->maps == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->maps), dataSet->header.maps.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readProperties(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->properties = (const fiftyoneDegreesProperty*)malloc(dataSet->header.properties.length);
if (dataSet->properties == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->properties), dataSet->header.properties.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readValues(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->values = (const fiftyoneDegreesValue*)malloc(dataSet->header.values.length);
if (dataSet->values == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->values), dataSet->header.values.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readProfiles(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->profiles = (const byte*)malloc(dataSet->header.profiles.length);
if (dataSet->profiles == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->profiles), dataSet->header.profiles.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readSignatures(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->signatures = (const byte*)malloc(dataSet->header.signatures.length);
if (dataSet->signatures == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->signatures), dataSet->header.signatures.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readRankedSignatureIndexes(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->rankedSignatureIndexes = (const int32_t*)malloc(dataSet->header.rankedSignatureIndexes.length);
if (dataSet->rankedSignatureIndexes == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->rankedSignatureIndexes), dataSet->header.rankedSignatureIndexes.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readNodes(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->nodes = (const byte*)malloc(dataSet->header.nodes.length);
if (dataSet->nodes == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->nodes), dataSet->header.nodes.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readRootNodes(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
int32_t index;
int32_t* rootNodeOffsets;
rootNodeOffsets = (int32_t*)malloc(dataSet->header.rootNodes.length);
if (rootNodeOffsets == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
dataSet->rootNodes = (const fiftyoneDegreesNode**)malloc(dataSet->header.rootNodes.count * sizeof(fiftyoneDegreesNode*));
if (dataSet->rootNodes == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)rootNodeOffsets, dataSet->header.rootNodes.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
for(index = 0; index < dataSet->header.rootNodes.count; index++) {
*(dataSet->rootNodes + index) = (fiftyoneDegreesNode*)(dataSet->nodes + *(rootNodeOffsets + index));
}
free(rootNodeOffsets);
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readProfileOffsets(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
dataSet->profileOffsets = (const fiftyoneDegreesProfileOffset*)malloc(dataSet->header.profileOffsets.length);
if (dataSet->profileOffsets == NULL) {
return DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY;
}
if (fread((void*)(dataSet->profileOffsets), dataSet->header.profileOffsets.length, 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
return DATA_SET_INIT_STATUS_SUCCESS;
}
fiftyoneDegreesDataSetInitStatus readDataSet(FILE *inputFilePtr, fiftyoneDegreesDataSet *dataSet) {
fiftyoneDegreesDataSetInitStatus status = DATA_SET_INIT_STATUS_SUCCESS;
/* Read the data set header */
if (fread((void*)&(dataSet->header), sizeof(fiftyoneDegreesDataSetHeader), 1, inputFilePtr) != 1) {
return DATA_SET_INIT_STATUS_CORRUPT_DATA;
}
/* Check the version of the data file */
if (dataSet->header.versionMajor != 3 ||
dataSet->header.versionMinor != 1) {
return DATA_SET_INIT_STATUS_INCORRECT_VERSION;
}
/* Read the entity lists */
status = readStrings(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readComponents(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readMaps(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readProperties(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readValues(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readProfiles(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readSignatures(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readRankedSignatureIndexes(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readNodes(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readRootNodes(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
status = readProfileOffsets(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) return status;
/* Set some of the constant fields */
((fiftyoneDegreesDataSet*)dataSet)->sizeOfSignature =
((dataSet->header.signatureNodesCount + dataSet->header.signatureProfilesCount) * sizeof(int32_t));
((fiftyoneDegreesDataSet*)dataSet)->signatureStartOfNodes =
(dataSet->header.signatureProfilesCount * sizeof(int32_t));
return DATA_SET_INIT_STATUS_SUCCESS;
}
/**
* METHODS TO RETURN ELEMENTS OF THE DATA SET
*/
/**
* Returns a component pointer from the index provided
* @param dataSet pointer to the data set
* @return pointer to the component
*/
const fiftyoneDegreesComponent* getComponent(fiftyoneDegreesDataSet *dataSet, int32_t componentIndex) {
return dataSet->components + componentIndex;
}
/**
* Returns a pointer to the ascii string at the byte offset provided
* @param dataSet pointer to the data set
* @param offset to the ascii string required
* @return a pointer to the AsciiString at the offset
*/
const fiftyoneDegreesAsciiString* fiftyoneDegreesGetString(const fiftyoneDegreesDataSet *dataSet, int32_t offset) {
return (const fiftyoneDegreesAsciiString*)(dataSet->strings + offset);
}
/**
* Returns a pointer to the profile at the index provided
* @param dataSet pointer to the data set
* @param index of the profile required
* @return pointer to the profile at the index
*/
fiftyoneDegreesProfile* getProfileByIndex(fiftyoneDegreesDataSet *dataSet, int32_t index) {
return (fiftyoneDegreesProfile*)dataSet->profiles + (dataSet->profileOffsets + index)->offset;
}
/**
* Gets the index of the property provided from the dataset
* @param dataSet pointer to the data set containing the property
* @param property pointer to the property
* @return the index of the property
*/
int32_t getPropertyIndex(const fiftyoneDegreesDataSet *dataSet, const fiftyoneDegreesProperty *property) {
return property - dataSet->properties;
}
/**
* Returns the property associated with the name.
* @param dataSet pointer containing the property required
* @param name string of the property required
* @return pointer to the property, or NULL if not found.
*/
const fiftyoneDegreesProperty* fiftyoneDegreesGetPropertyByName(fiftyoneDegreesDataSet *dataSet, char* name) {
int32_t index;
const fiftyoneDegreesProperty *property;
for(index = 0; index < dataSet->header.properties.count; index++) {
property = dataSet->properties + index;
if (strcmp(fiftyoneDegreesGetPropertyName(dataSet, property),
name) == 0)
return property;
}
return NULL;
}
/**
* Returns the name of the value provided.
* @param dataSet pointer to the data set containing the value
* @param value pointer whose name is required
* @return pointer to the char string of the name
*/
const char* fiftyoneDegreesGetValueName(const fiftyoneDegreesDataSet *dataSet, const fiftyoneDegreesValue *value) {
return (char*)(&fiftyoneDegreesGetString(dataSet, value->nameOffset)->firstByte);
}
/**
* Returns the name of the property provided.
* @param dataSet pointer to the data set containing the property
* @param property pointer whose name is required
* @return pointer to the char string of the name
*/
const char* fiftyoneDegreesGetPropertyName(const fiftyoneDegreesDataSet *dataSet, const fiftyoneDegreesProperty *property) {
return (const char*)&(fiftyoneDegreesGetString(dataSet, property->nameOffset)->firstByte);
}
/**
* Returns the first numeric index for the node provided.
* @param node pointer to the node whose numeric indexes are required
* @return pointer to the first numeric index for the node
*/
const fiftyoneDegreesNodeNumericIndex* getFirstNumericIndexForNode(const fiftyoneDegreesNode *node) {
return (const fiftyoneDegreesNodeNumericIndex*)(((byte*)node)
+ (sizeof(fiftyoneDegreesNode)
+ (node->childrenCount * sizeof(fiftyoneDegreesNodeIndex))));
}
/**
* Returns the node associated with the node index
* @param dataSet pointer to the data set
* @param nodeIndex pointer associated with the node required
*/
const fiftyoneDegreesNode* getNodeFromNodeIndex(const fiftyoneDegreesDataSet *dataSet, const fiftyoneDegreesNodeIndex *nodeIndex) {
return (const fiftyoneDegreesNode*)(dataSet->nodes + nodeIndex->relatedNodeOffset);
}
/**
* Returns a pointer to the first node index associated with the node provided
* @param node whose first node index is needed
* @return the first node index of the node
*/
const fiftyoneDegreesNodeIndex* getNodeIndexesForNode(const fiftyoneDegreesNode* node) {
return (fiftyoneDegreesNodeIndex*)(((byte*)node) + sizeof(fiftyoneDegreesNode));
}
/**
* Returns true if the node is a complete one
* @param node pointer to be checked
* @return true if the node is complete, otherwise false
*/
fiftyoneDegreesBool getIsNodeComplete(const fiftyoneDegreesNode* node) {
return node->nextCharacterPosition != SHRT_MIN;
}
/**
* Returns the node pointer at the offset provided.
* @param dataSet pointer to the data set
* @param offset to the node required
* @return pointer to the node at the offset
*/
const fiftyoneDegreesNode* getNodeByOffset(const fiftyoneDegreesDataSet *dataSet, int32_t offset) {
return (const fiftyoneDegreesNode*)(dataSet->nodes + offset);
}
/**
* Returns the root node associated with the node provided
* @param dataSet pointer to the data set
* @param node pointer whose root node is required
* @return node pointer to the root node
*/
const fiftyoneDegreesNode* getRootNode(const fiftyoneDegreesDataSet *dataSet, const fiftyoneDegreesNode *node) {
if (node->parentOffset >= 0) {
return getRootNode(dataSet, getNodeByOffset(dataSet, node->parentOffset));
}
return node;
}
/**
* Returns the length of the signature based on the node offsets
* associated with the signature.
* @param dataSet pointer to the data set
* @param nodeOffsets pointer to the first node offset for the signature
* @return the number of characters the signature contains
*/
int32_t getSignatureLengthFromNodeOffsets(const fiftyoneDegreesDataSet *dataSet, int32_t nodeOffset) {
const fiftyoneDegreesNode *node = getNodeByOffset(dataSet, nodeOffset);
return getRootNode(dataSet, node)->position + 1;
}
/**
* Returns the characters associated with the node by looking them up in the
* strings table.
* @param dataSet pointer to the data set
* @param node pointer for the node whose characters are required
* @return pointer to the ascii string associated with the node
*/
const fiftyoneDegreesAsciiString* getNodeCharacters(const fiftyoneDegreesDataSet *dataSet, const fiftyoneDegreesNode *node) {
return fiftyoneDegreesGetString(dataSet, node->characterStringOffset);
}
/**
* Returns the characters associated with a node index. This is either
* performed using the strings table, or if short by converting the value
* of the node index to a character array. The results are returned in the
* string structure passed into the method.
* @param ws pointer to the workset being used for matching
* @param nodeIndex pointer of the node index being tested
* @param string pointer to return the string
*/
void getCharactersForNodeIndex(fiftyoneDegreesWorkset *ws, const fiftyoneDegreesNodeIndex *nodeIndex, fiftyoneDegreesString *string) {
int16_t index;
const fiftyoneDegreesAsciiString *asciiString;
if (nodeIndex->isString != 0) {
asciiString = fiftyoneDegreesGetString(ws->dataSet, nodeIndex->value.integer);
/* Set the length of the byte array removing the null terminator */
string->length = (int16_t)(asciiString->length - 1);
string->value = (byte*)&(asciiString->firstByte);
}
else {
for(index = 0; index < 4; index++) {
if (nodeIndex->value.characters[index] == 0)
break;
}
string->length = index;
string->value = (byte*)&(nodeIndex->value.characters);
}
}
/**
* Returns the signature at the index provided.
* @param dataSet pointer to the data set
* @param index of the signature required
* @return pointer to the signature at the index
*/
const byte* getSignatureByIndex(const fiftyoneDegreesDataSet *dataSet, int32_t index) {
return dataSet->signatures + (dataSet->sizeOfSignature * index);
}
/**
* Returns the signature at the ranked index provided.
* @param dataSet pointer to the data set
* @param ranked index of the signature required
* @return pointer to the signature at the ranked index
*/
const byte* getSignatureByRankedIndex(const fiftyoneDegreesDataSet *dataSet, int32_t index) {
return getSignatureByIndex(dataSet, dataSet->rankedSignatureIndexes[index]);
}
/**
* Returns the number of node offsets associated with the signature.
* @param dataSet pointer to the data set
* @param nodeOffsets pointer to the node offsets associated with the signature
* @return the number of nodes associated with the signature
*/
int32_t getSignatureNodeOffsetsCount(const fiftyoneDegreesDataSet *dataSet, int32_t *nodeOffsets) {
int32_t count = 0;
while (*(nodeOffsets + count) >= 0 &&
count < dataSet->header.signatureNodesCount)
count++;
return count;
}
/**
* Returns the offset associated with the node pointer provided.
* @param dataSet pointer to the data set
* @return the integer offset to the node in the data structure
*/
int32_t getNodeOffsetFromNode(const fiftyoneDegreesDataSet *dataSet, const fiftyoneDegreesNode *node) {
return (byte*)node - (byte*)(dataSet->nodes);
}
/**
* Returns an integer pointer to the node offsets associated with the
* signature.
* @param dataSet pointer to the data set
* @param signature pointer to the signature whose node offsets are required
* @return pointer to the first integer in the node offsets associated with
* the signature
*/
int32_t* getNodeOffsetsFromSignature(const fiftyoneDegreesDataSet *dataSet, const byte *signature) {
return (int32_t*)(signature + dataSet->signatureStartOfNodes);
}
/**
* Returns an integer pointer to the profile offsets associated with the
* signature.
* @param signature pointer to the signature whose profile offsets are required
* @return pointer to the first integer in the profile offsets associated with
* the signature
*/
int32_t* getProfileOffsetsFromSignature(const byte *signature) {
return (int32_t*)signature;
}
/**
* Returns a pointer to the first signature index of the node
* @param node pointer whose first signature index is required
* @return a pointer to the first signature index
*/
int32_t* getFirstRankedSignatureIndexForNode(const fiftyoneDegreesNode *node) {
return (int32_t*)(((byte*)node) + sizeof(fiftyoneDegreesNode) +
(node->childrenCount * sizeof(fiftyoneDegreesNodeIndex)) +
(node->numericChildrenCount * sizeof(fiftyoneDegreesNodeNumericIndex)));
}
/**
* Returns a pointer to the first signature index of the node
* @param node pointer whose first signature index is required
* @return a pointer to the first signature index
*/
int32_t* getFirstSignatureIndexForNode(const fiftyoneDegreesNode *node) {
return (int32_t*)(((byte*)node) + sizeof(fiftyoneDegreesNode) +
(node->childrenCount * sizeof(fiftyoneDegreesNodeIndex)) +
(node->numericChildrenCount * sizeof(fiftyoneDegreesNodeNumericIndex)));
}
/**
* LINKED LIST METHODS
*/
/**
* Adds the signature index to the linked list with a frequency of 1.
* @param linkedList pointer to the linked list
* @param signatureIndex to be added to the end of the list
*/
void linkedListAdd(fiftyoneDegreesLinkedSignatureList *linkedList, int32_t rankedSignatureIndex) {
fiftyoneDegreesLinkedSignatureListItem *newSignature = (fiftyoneDegreesLinkedSignatureListItem*)(linkedList->items) + linkedList->count;
newSignature->rankedSignatureIndex = rankedSignatureIndex;
newSignature->frequency = 1;
newSignature->next = NULL;
newSignature->previous = linkedList->last;
if (linkedList->first == NULL)
linkedList->first = newSignature;
if (linkedList->last != NULL)
linkedList->last->next = newSignature;
linkedList->last = newSignature;
(linkedList->count)++;
}
/**
* Builds the initial linked list using a single node.
* @param ws pointer to the workset used for the match
* @param node pointer to the node whose signature indexes will be used to
* build the initial linked list.
*/
void buildInitialList(fiftyoneDegreesWorkset *ws, const fiftyoneDegreesNode *node) {
int32_t index;
int32_t *firstSignatureIndex = getFirstRankedSignatureIndexForNode(node);
for (index = 0; index < node->signatureCount; index++) {
linkedListAdd(&(ws->linkedSignatureList), *(firstSignatureIndex + index));
}
}
/**
* Adds the signature index before the item provided.
* @param linkedList pointer to the linked list to be altered
* @param item that the signature index should be added before
*/
void linkedListAddBefore(fiftyoneDegreesLinkedSignatureList *linkedList, fiftyoneDegreesLinkedSignatureListItem *item, int32_t rankedSignatureIndex) {
fiftyoneDegreesLinkedSignatureListItem *newSignature = (fiftyoneDegreesLinkedSignatureListItem*)(linkedList->items + linkedList->count);
newSignature->rankedSignatureIndex = rankedSignatureIndex;
newSignature->frequency = 1;
newSignature->next = item;
newSignature->previous = item->previous;
if (newSignature->previous != NULL) {
newSignature->previous->next = newSignature;
}
item->previous = newSignature;
if (item == linkedList->first)
linkedList->first = newSignature;
linkedList->count++;
}
/**
* Removes the item specified from the linked list.
* @param linkedList pointer to the linked list to be altered
* @param item to be removed from the list
*/
void linkedListRemove(fiftyoneDegreesLinkedSignatureList *linkedList, fiftyoneDegreesLinkedSignatureListItem *item) {
if (item->previous != NULL)
item->previous->next = item->next;
if (item->next != NULL)
item->next->previous = item->previous;
if (item == linkedList->first)
linkedList->first = item->next;
if (item == linkedList->last)
linkedList->last = item->previous;
linkedList->count--;
}
/**
* DATASET SETUP
*/
/**
* Destroys the data set releasing all memory available.
* @param dataSet pointer to the data set being destroyed
*/
void fiftyoneDegreesDestroy(const fiftyoneDegreesDataSet *dataSet) {
free((void*)(dataSet->requiredProperties));
free((void*)(dataSet->strings));
free((void*)(dataSet->components));
free((void*)(dataSet->maps));
free((void*)(dataSet->properties));
free((void*)(dataSet->values));
free((void*)(dataSet->profiles));
free((void*)(dataSet->signatures));
free((void*)(dataSet->nodes));
free((void*)(dataSet->rootNodes));
free((void*)(dataSet->profileOffsets));
}
/**
* Adds all properties in the data set to the required properties list.
* @param dataSet pointer to the data set
*/
void setAllProperties(fiftyoneDegreesDataSet *dataSet) {
int32_t index;
dataSet->requiredPropertyCount = dataSet->header.properties.count;
dataSet->requiredProperties = (const fiftyoneDegreesProperty**)malloc(dataSet->requiredPropertyCount * sizeof(fiftyoneDegreesProperty*));
for(index = 0; index < dataSet->requiredPropertyCount; index++) {
*(dataSet->requiredProperties + index) = dataSet->properties + index;
}
}
/**
* Adds the properties in the array of properties to the list
* of required properties from a match.
* @param dataSet pointer to the data set
* @param properties array of properties to be returned
* @param count number of elements in the properties array
*/
void setProperties(fiftyoneDegreesDataSet *dataSet, char** properties, int32_t count) {
int32_t index, propertyIndex, requiredPropertyLength;
char *requiredPropertyName;
const fiftyoneDegreesAsciiString *propertyName;
// Allocate memory for this number of properties.
dataSet->requiredPropertyCount = 0;
dataSet->requiredProperties = (const fiftyoneDegreesProperty**)malloc(count * sizeof(const fiftyoneDegreesProperty*));
// Add the properties to the list of required properties.
for(propertyIndex = 0; propertyIndex < count; propertyIndex++) {
requiredPropertyName = *(properties + propertyIndex);
requiredPropertyLength = strlen(requiredPropertyName);
for(index = 0; index < dataSet->header.properties.count; index++) {
propertyName = fiftyoneDegreesGetString(dataSet, (dataSet->properties + index)->nameOffset);
if (requiredPropertyLength == propertyName->length - 1 &&
memcmp(requiredPropertyName, &propertyName->firstByte, requiredPropertyLength) == 0) {
*(dataSet->requiredProperties + dataSet->requiredPropertyCount) = (dataSet->properties + index);
dataSet->requiredPropertyCount++;
break;
}
}
}
}
/**
* Gets the number of separators in the char array
* @param input char array containing separated values
* @return number of separators
*/
int32_t getSeparatorCount(char* input) {
int32_t index = 0, count = 0;
if (input != NULL) {
while(*(input + index) != 0) {
if (*(input + index) == ',' ||
*(input + index) == '|' ||
*(input + index) == ' ' ||
*(input + index) == '\t')
count++;
index++;
}
return count + 1;
}
return 0;
}
/**
* Initialises the data set passed to the method with the data from
* the file provided. If required properties is provided the data set
* will only return those listed and separated by comma, pipe, space
* or tab.
* @param fileName of the data source to use for initialisation
* @param dataSet pointer to the data set
* @param requiredProperties char array to the separated list of properties
* the dataSet can return
* @return the number of bytes read from the file
*/
fiftyoneDegreesDataSetInitStatus fiftyoneDegreesInitWithPropertyString(const char *fileName, fiftyoneDegreesDataSet *dataSet, char* requiredProperties) {
int32_t requiredPropertyCount = getSeparatorCount(requiredProperties);
int32_t index, count = 0;
char **requiredPropertiesArray = NULL;
char *last = requiredProperties;
fiftyoneDegreesDataSetInitStatus status;
// Determine if properties were provided.
if (requiredPropertyCount > 0) {
// Allocate pointers for each of the properties.
requiredPropertiesArray = (char**)malloc(requiredPropertyCount * sizeof(char*));
// Change the input string so that the separators are changed to nulls.
for(index = 0; count < requiredPropertyCount; index++) {
if (*(requiredProperties + index) == ',' ||
*(requiredProperties + index) == '|' ||
*(requiredProperties + index) == ' ' ||
*(requiredProperties + index) == '\t' ||
*(requiredProperties + index) == 0) {
*(requiredProperties + index) = 0;
*(requiredPropertiesArray + count) = last;
last = requiredProperties + index + 1;
count++;
}
}
}
status = fiftyoneDegreesInitWithPropertyArray(fileName, dataSet, requiredPropertiesArray, requiredPropertyCount);
if (requiredPropertiesArray != NULL)
free(requiredPropertiesArray);
return status;
}
/**
* Initialises the data set passed to the method with the data from
* the file provided. If required properties is provided the data set
* will only return those contained in the array.
* or tab.
* @param fileName of the data source to use for initialisation
* @param dataSet pointer to the data set
* @param requiredProperties array of strings containing the property names
* @param count the number of elements in the requiredProperties array
* @return the number of bytes read from the file
*/
fiftyoneDegreesDataSetInitStatus fiftyoneDegreesInitWithPropertyArray(const char *fileName, fiftyoneDegreesDataSet *dataSet, char** requiredProperties, int32_t count) {
FILE *inputFilePtr;
fiftyoneDegreesDataSetInitStatus status;
// Open the file and hold on to the pointer.
#ifndef _MSC_FULL_VER
//"C:\\Users\\Mike\\Work\\51Degrees_C\\data\\51Degrees-Lite.dat"
inputFilePtr = fopen(fileName, "rb");
#else
/* If using Microsoft use the fopen_s method to avoid warning */
if (fopen_s(&inputFilePtr, fileName, "rb") != 0) {
return -1;
}
#endif
// If the file didn't open return -1.
if (inputFilePtr == NULL)
return DATA_SET_INIT_STATUS_FILE_NOT_FOUND;
// Read the data set into memory.
status = readDataSet(inputFilePtr, dataSet);
if (status != DATA_SET_INIT_STATUS_SUCCESS) {
return status;
}
// Set the properties that are returned by the data set.
if (requiredProperties == NULL || count == 0) {
setAllProperties(dataSet);
} else {
setProperties(dataSet, requiredProperties, count);
}
return status;
}
/**
* WORKSET METHODS
*/
/**
* Creates a new workset to perform matches using the dataset provided.
* The workset must be destroyed using the freeWorkset method when it's
* finished with to release memory.
* @param dataSet pointer to the data set
* @returns a pointer to the workset created
*/
fiftyoneDegreesWorkset *fiftyoneDegreesCreateWorkset(const fiftyoneDegreesDataSet *dataSet) {
fiftyoneDegreesWorkset *ws = (fiftyoneDegreesWorkset*)malloc(sizeof(fiftyoneDegreesWorkset));
if (ws != NULL) {
ws->dataSet = dataSet;
// Allocate all the memory needed to the workset.
ws->linkedSignatureList.items = (fiftyoneDegreesLinkedSignatureListItem*)malloc(dataSet->header.maxSignaturesClosest * sizeof(fiftyoneDegreesLinkedSignatureListItem));
ws->input = (char*)malloc((dataSet->header.maxUserAgentLength + 1) * sizeof(char));
ws->signatureAsString = (char*)malloc((dataSet->header.maxUserAgentLength + 1) * sizeof(char));
ws->profiles = (const fiftyoneDegreesProfile**)malloc(dataSet->header.components.count * sizeof(const fiftyoneDegreesProfile*));
ws->nodes = (const fiftyoneDegreesNode**)malloc(dataSet->header.maxUserAgentLength * sizeof(const fiftyoneDegreesNode*));
ws->orderedNodes = (const fiftyoneDegreesNode**)malloc(dataSet->header.maxUserAgentLength * sizeof(const fiftyoneDegreesNode*));
ws->targetUserAgentArray = (byte*)malloc(dataSet->header.maxUserAgentLength);
ws->relevantNodes = (char*)malloc(dataSet->header.maxUserAgentLength + 1);
ws->closestNodes = (char*)malloc(dataSet->header.maxUserAgentLength + 1);
ws->values = (const fiftyoneDegreesValue**)malloc(dataSet->header.maxValues * sizeof(const fiftyoneDegreesValue*));
// Check all the memory was allocated correctly.
if (ws->linkedSignatureList.items == NULL ||
ws->input == NULL ||
ws->signatureAsString == NULL ||
ws->profiles == NULL ||
ws->nodes == NULL ||
ws->orderedNodes == NULL ||
ws->targetUserAgentArray == NULL ||
ws->relevantNodes == NULL ||
ws->closestNodes == NULL ||
ws->values == NULL) {
// One or more of the workset memory allocations failed.
// Free any that worked and return NULL.
if (ws->linkedSignatureList.items != NULL) { free((void*)ws->linkedSignatureList.items); }
if (ws->input != NULL) { free((void*)ws->input); }
if (ws->signatureAsString != NULL) { free((void*)ws->signatureAsString); }
if (ws->profiles != NULL) { free((void*)ws->profiles); }
if (ws->nodes != NULL) { free((void*)ws->nodes); }
if (ws->orderedNodes != NULL) { free((void*)ws->orderedNodes); }
if (ws->targetUserAgentArray != NULL) { free((void*)ws->targetUserAgentArray); }
if (ws->relevantNodes != NULL) { free((void*)ws->relevantNodes); }
if (ws->closestNodes != NULL) { free((void*)ws->closestNodes); }
if (ws->values != NULL) { free((void*)ws->values); }
// Free the workset which worked earlier and return NULL.
free(ws);
ws = NULL;
} else {
// Null terminate the strings used to return the relevant and closest nodes.
ws->relevantNodes[dataSet->header.maxUserAgentLength] = 0;
ws->closestNodes[dataSet->header.maxUserAgentLength] = 0;
}
}
return ws;
}
/**
* Releases the memory used by the workset.
* @param pointer to the workset created previously
*/
void fiftyoneDegreesFreeWorkset(const fiftyoneDegreesWorkset *ws) {
free((void*)(ws->input));
free((void*)(ws->signatureAsString));
free((void*)ws->profiles);
free((void*)ws->nodes);
free((void*)ws->orderedNodes);
free((void*)ws->targetUserAgentArray);
free((void*)ws->relevantNodes);
free((void*)ws->closestNodes);
free((void*)ws->values);
free((void*)ws->linkedSignatureList.items);
free((void*)ws);
}
/**
* Adds the node of the signature into the signature string of the work set
* @param ws pointer to the work set used for the match
* @param node from the signature to be added to the string
* @return the right most character returned
*/
int addSignatureNodeToString(fiftyoneDegreesWorkset *ws, const fiftyoneDegreesNode *node) {
int nodeIndex, signatureIndex;
const fiftyoneDegreesAsciiString *characters = fiftyoneDegreesGetString(ws->dataSet, node->characterStringOffset);
for(nodeIndex = 0, signatureIndex = node->position + 1;
nodeIndex < characters->length - 1;
nodeIndex++, signatureIndex++) {
ws->signatureAsString[signatureIndex] = (&(characters->firstByte))[nodeIndex];
}
return signatureIndex;
}
/**
* Sets the signature provided as the string returned by the match.
* @param ws pointer to the work set used for the match
* @param signature pointer to be used as the string for the result
*/
void setSignatureAsString(fiftyoneDegreesWorkset *ws, const byte *signature) {
int *nodeOffsets = getNodeOffsetsFromSignature(ws->dataSet, signature);
int index,
nullPosition = 0,
lastCharacter = 0,
nodeOffsetCount = getSignatureNodeOffsetsCount(ws->dataSet, nodeOffsets);
for(index = 0; index < ws->dataSet->header.maxUserAgentLength; index++) {
ws->signatureAsString[index] = '_';
}
for(index = 0; index < nodeOffsetCount; index++) {
lastCharacter = addSignatureNodeToString(ws, getNodeByOffset(ws->dataSet, *(nodeOffsets + index)));
if (lastCharacter > nullPosition) {
nullPosition = lastCharacter;
}
}
ws->signatureAsString[nullPosition] = 0;
}
/**
* Adds the node to the end of the workset.
* @param ws pointer to the work set used for the match
* @param node pointer to be added to the work set
* @param index the should be added at
*/
void addNodeIntoWorkset(fiftyoneDegreesWorkset *ws, const fiftyoneDegreesNode *node, int32_t index) {
*(ws->nodes + index) = node;
ws->nodeCount++;
}
/**
* Inserts the node at the position provided moving all other nodes down.
* @param ws pointer to the work set used for the match
* @param node pointer to the node being added to the workset
* @param insertIndex the index to insert the node at
*/
void insertNodeIntoWorksetAtIndex(fiftyoneDegreesWorkset *ws, const fiftyoneDegreesNode *node, int32_t insertIndex) {
int32_t index;
for (index = ws->nodeCount - 1; index >= insertIndex; index--) {
*(ws->nodes + index + 1) = *(ws->nodes + index);
}
addNodeIntoWorkset(ws, node, insertIndex);
}
/**
* Inserts the node into the workset considering its position relative to other
* nodes already set.
* @param ws pointer to the work set used for the match
* @param node pointer to be added to the work set
* @return the index the node as added at
*/
int32_t insertNodeIntoWorkSet(fiftyoneDegreesWorkset *ws, const fiftyoneDegreesNode *node) {
int32_t index;
for(index = 0; index < ws->nodeCount; index++) {
if (node > *(ws->nodes + index)) {
// The node to be inserted is greater than the current node.
// Insert the node before this one.
insertNodeIntoWorksetAtIndex(ws, node, index);
return index;
}
}
addNodeIntoWorkset(ws, node, index);
return index;
}
/**
* MATCH METHODS
*/
/**
* Sets the target user agent and resets any other fields to initial values.
* Must be called before a match commences.
* @param ws pointer to the workset to be used for the match
* @param userAgent char pointer to the user agent. Trimmed if longer than
* the maximum allowed for matching
*/
void setTargetUserAgentArray(fiftyoneDegreesWorkset *ws, char* userAgent) {
int32_t index;
ws->targetUserAgent = userAgent;
ws->targetUserAgentArrayLength = strlen(userAgent);
if (ws->targetUserAgentArrayLength > ws->dataSet->header.maxUserAgentLength)
ws->targetUserAgentArrayLength = ws->dataSet->header.maxUserAgentLength;
/* Work out the starting character position */
for(index = 0; index < ws->targetUserAgentArrayLength; index++) {
ws->targetUserAgentArray[index]= userAgent[index];
}
ws->nextCharacterPositionIndex = (int16_t)(ws->targetUserAgentArrayLength - 1);
/* Check to ensure the length of the user agent does not exceed
the number of root nodes. */
if (ws->nextCharacterPositionIndex >= ws->dataSet->header.rootNodes.count) {
ws->nextCharacterPositionIndex = (int16_t)(ws->dataSet->header.rootNodes.count - 1);
}
/* Reset the nodes to zero ready for the new match */
ws->nodeCount = 0;
for(index = 0; index < ws->dataSet->header.signatureNodesCount; index++) {
*(ws->nodes + index) = NULL;
*(ws->orderedNodes + index) = NULL;
}
/* Reset the profiles to space ready for the new match */
ws->profileCount = 0;
for(index = 0; index < ws->dataSet->header.components.count; index++) {
*(ws->profiles + index) = NULL;
}
/* Reset the closest and relevant strings */
for(index = 0; index < ws->dataSet->header.maxUserAgentLength; index++) {