ferret 0.9.6 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (295) hide show
  1. data/MIT-LICENSE +1 -1
  2. data/README +12 -24
  3. data/Rakefile +38 -54
  4. data/TODO +14 -17
  5. data/ext/analysis.c +982 -823
  6. data/ext/analysis.h +133 -76
  7. data/ext/array.c +96 -58
  8. data/ext/array.h +40 -13
  9. data/ext/bitvector.c +476 -118
  10. data/ext/bitvector.h +264 -22
  11. data/ext/compound_io.c +217 -229
  12. data/ext/defines.h +49 -0
  13. data/ext/document.c +107 -317
  14. data/ext/document.h +31 -65
  15. data/ext/except.c +81 -36
  16. data/ext/except.h +117 -55
  17. data/ext/extconf.rb +2 -9
  18. data/ext/ferret.c +211 -104
  19. data/ext/ferret.h +22 -11
  20. data/ext/filter.c +97 -82
  21. data/ext/fs_store.c +348 -367
  22. data/ext/global.c +226 -188
  23. data/ext/global.h +44 -26
  24. data/ext/hash.c +474 -391
  25. data/ext/hash.h +441 -68
  26. data/ext/hashset.c +124 -96
  27. data/ext/hashset.h +169 -20
  28. data/ext/helper.c +56 -5
  29. data/ext/helper.h +7 -0
  30. data/ext/inc/lang.h +29 -49
  31. data/ext/inc/threading.h +31 -0
  32. data/ext/ind.c +288 -278
  33. data/ext/ind.h +68 -0
  34. data/ext/index.c +5688 -0
  35. data/ext/index.h +663 -616
  36. data/ext/lang.h +29 -49
  37. data/ext/libstemmer.c +3 -3
  38. data/ext/mem_pool.c +84 -0
  39. data/ext/mem_pool.h +35 -0
  40. data/ext/posh.c +1006 -0
  41. data/ext/posh.h +1007 -0
  42. data/ext/priorityqueue.c +117 -194
  43. data/ext/priorityqueue.h +135 -39
  44. data/ext/q_boolean.c +1305 -1108
  45. data/ext/q_const_score.c +106 -93
  46. data/ext/q_filtered_query.c +138 -135
  47. data/ext/q_fuzzy.c +206 -242
  48. data/ext/q_match_all.c +94 -80
  49. data/ext/q_multi_term.c +663 -0
  50. data/ext/q_parser.c +667 -593
  51. data/ext/q_phrase.c +992 -555
  52. data/ext/q_prefix.c +72 -61
  53. data/ext/q_range.c +235 -210
  54. data/ext/q_span.c +1480 -1166
  55. data/ext/q_term.c +273 -246
  56. data/ext/q_wildcard.c +127 -114
  57. data/ext/r_analysis.c +1720 -711
  58. data/ext/r_index.c +3049 -0
  59. data/ext/r_qparser.c +433 -146
  60. data/ext/r_search.c +2934 -1993
  61. data/ext/r_store.c +372 -143
  62. data/ext/r_utils.c +941 -0
  63. data/ext/ram_store.c +330 -326
  64. data/ext/search.c +1291 -668
  65. data/ext/search.h +403 -702
  66. data/ext/similarity.c +91 -113
  67. data/ext/similarity.h +45 -30
  68. data/ext/sort.c +721 -484
  69. data/ext/stopwords.c +361 -273
  70. data/ext/store.c +556 -58
  71. data/ext/store.h +706 -126
  72. data/ext/tags +3578 -2780
  73. data/ext/term_vectors.c +352 -0
  74. data/ext/threading.h +31 -0
  75. data/ext/win32.h +54 -0
  76. data/lib/ferret.rb +5 -17
  77. data/lib/ferret/document.rb +130 -2
  78. data/lib/ferret/index.rb +577 -26
  79. data/lib/ferret/number_tools.rb +157 -0
  80. data/lib/ferret_version.rb +3 -0
  81. data/test/test_helper.rb +5 -13
  82. data/test/unit/analysis/tc_analyzer.rb +513 -1
  83. data/test/unit/analysis/{ctc_tokenstream.rb → tc_token_stream.rb} +23 -0
  84. data/test/unit/index/tc_index.rb +183 -240
  85. data/test/unit/index/tc_index_reader.rb +312 -479
  86. data/test/unit/index/tc_index_writer.rb +397 -13
  87. data/test/unit/index/th_doc.rb +269 -206
  88. data/test/unit/query_parser/tc_query_parser.rb +40 -33
  89. data/test/unit/search/tc_filter.rb +59 -71
  90. data/test/unit/search/tc_fuzzy_query.rb +24 -16
  91. data/test/unit/search/tc_index_searcher.rb +23 -201
  92. data/test/unit/search/tc_multi_searcher.rb +78 -226
  93. data/test/unit/search/tc_search_and_sort.rb +93 -81
  94. data/test/unit/search/tc_sort.rb +23 -23
  95. data/test/unit/search/tc_sort_field.rb +7 -7
  96. data/test/unit/search/tc_spans.rb +51 -47
  97. data/test/unit/search/tm_searcher.rb +339 -0
  98. data/test/unit/store/tc_fs_store.rb +1 -1
  99. data/test/unit/store/tm_store_lock.rb +3 -3
  100. data/test/unit/tc_document.rb +81 -0
  101. data/test/unit/ts_analysis.rb +1 -1
  102. data/test/unit/ts_utils.rb +1 -1
  103. data/test/unit/utils/tc_bit_vector.rb +288 -0
  104. data/test/unit/utils/tc_number_tools.rb +117 -0
  105. data/test/unit/utils/tc_priority_queue.rb +106 -0
  106. metadata +140 -301
  107. data/CHANGELOG +0 -9
  108. data/ext/dummy.exe +0 -0
  109. data/ext/field.c +0 -408
  110. data/ext/frtio.h +0 -13
  111. data/ext/inc/except.h +0 -90
  112. data/ext/index_io.c +0 -382
  113. data/ext/index_rw.c +0 -2658
  114. data/ext/lang.c +0 -41
  115. data/ext/nix_io.c +0 -134
  116. data/ext/q_multi_phrase.c +0 -380
  117. data/ext/r_doc.c +0 -582
  118. data/ext/r_index_io.c +0 -1021
  119. data/ext/r_term.c +0 -219
  120. data/ext/term.c +0 -820
  121. data/ext/termdocs.c +0 -611
  122. data/ext/vector.c +0 -637
  123. data/ext/w32_io.c +0 -150
  124. data/lib/ferret/analysis.rb +0 -11
  125. data/lib/ferret/analysis/analyzers.rb +0 -112
  126. data/lib/ferret/analysis/standard_tokenizer.rb +0 -71
  127. data/lib/ferret/analysis/token.rb +0 -100
  128. data/lib/ferret/analysis/token_filters.rb +0 -86
  129. data/lib/ferret/analysis/token_stream.rb +0 -26
  130. data/lib/ferret/analysis/tokenizers.rb +0 -112
  131. data/lib/ferret/analysis/word_list_loader.rb +0 -27
  132. data/lib/ferret/document/document.rb +0 -152
  133. data/lib/ferret/document/field.rb +0 -312
  134. data/lib/ferret/index/compound_file_io.rb +0 -338
  135. data/lib/ferret/index/document_writer.rb +0 -289
  136. data/lib/ferret/index/field_infos.rb +0 -279
  137. data/lib/ferret/index/fields_io.rb +0 -181
  138. data/lib/ferret/index/index.rb +0 -675
  139. data/lib/ferret/index/index_file_names.rb +0 -33
  140. data/lib/ferret/index/index_reader.rb +0 -503
  141. data/lib/ferret/index/index_writer.rb +0 -534
  142. data/lib/ferret/index/multi_reader.rb +0 -377
  143. data/lib/ferret/index/multiple_term_doc_pos_enum.rb +0 -98
  144. data/lib/ferret/index/segment_infos.rb +0 -130
  145. data/lib/ferret/index/segment_merge_info.rb +0 -49
  146. data/lib/ferret/index/segment_merge_queue.rb +0 -16
  147. data/lib/ferret/index/segment_merger.rb +0 -358
  148. data/lib/ferret/index/segment_reader.rb +0 -412
  149. data/lib/ferret/index/segment_term_enum.rb +0 -169
  150. data/lib/ferret/index/segment_term_vector.rb +0 -58
  151. data/lib/ferret/index/term.rb +0 -53
  152. data/lib/ferret/index/term_buffer.rb +0 -83
  153. data/lib/ferret/index/term_doc_enum.rb +0 -291
  154. data/lib/ferret/index/term_enum.rb +0 -52
  155. data/lib/ferret/index/term_info.rb +0 -37
  156. data/lib/ferret/index/term_infos_io.rb +0 -321
  157. data/lib/ferret/index/term_vector_offset_info.rb +0 -20
  158. data/lib/ferret/index/term_vectors_io.rb +0 -553
  159. data/lib/ferret/query_parser.rb +0 -312
  160. data/lib/ferret/query_parser/query_parser.tab.rb +0 -928
  161. data/lib/ferret/search.rb +0 -50
  162. data/lib/ferret/search/boolean_clause.rb +0 -100
  163. data/lib/ferret/search/boolean_query.rb +0 -299
  164. data/lib/ferret/search/boolean_scorer.rb +0 -294
  165. data/lib/ferret/search/caching_wrapper_filter.rb +0 -40
  166. data/lib/ferret/search/conjunction_scorer.rb +0 -99
  167. data/lib/ferret/search/disjunction_sum_scorer.rb +0 -205
  168. data/lib/ferret/search/exact_phrase_scorer.rb +0 -32
  169. data/lib/ferret/search/explanation.rb +0 -41
  170. data/lib/ferret/search/field_cache.rb +0 -215
  171. data/lib/ferret/search/field_doc.rb +0 -31
  172. data/lib/ferret/search/field_sorted_hit_queue.rb +0 -184
  173. data/lib/ferret/search/filter.rb +0 -11
  174. data/lib/ferret/search/filtered_query.rb +0 -130
  175. data/lib/ferret/search/filtered_term_enum.rb +0 -79
  176. data/lib/ferret/search/fuzzy_query.rb +0 -154
  177. data/lib/ferret/search/fuzzy_term_enum.rb +0 -247
  178. data/lib/ferret/search/hit_collector.rb +0 -34
  179. data/lib/ferret/search/hit_queue.rb +0 -11
  180. data/lib/ferret/search/index_searcher.rb +0 -200
  181. data/lib/ferret/search/match_all_query.rb +0 -104
  182. data/lib/ferret/search/multi_phrase_query.rb +0 -216
  183. data/lib/ferret/search/multi_searcher.rb +0 -261
  184. data/lib/ferret/search/multi_term_query.rb +0 -65
  185. data/lib/ferret/search/non_matching_scorer.rb +0 -22
  186. data/lib/ferret/search/phrase_positions.rb +0 -55
  187. data/lib/ferret/search/phrase_query.rb +0 -214
  188. data/lib/ferret/search/phrase_scorer.rb +0 -152
  189. data/lib/ferret/search/prefix_query.rb +0 -54
  190. data/lib/ferret/search/query.rb +0 -140
  191. data/lib/ferret/search/query_filter.rb +0 -51
  192. data/lib/ferret/search/range_filter.rb +0 -103
  193. data/lib/ferret/search/range_query.rb +0 -139
  194. data/lib/ferret/search/req_excl_scorer.rb +0 -125
  195. data/lib/ferret/search/req_opt_sum_scorer.rb +0 -70
  196. data/lib/ferret/search/score_doc.rb +0 -38
  197. data/lib/ferret/search/score_doc_comparator.rb +0 -114
  198. data/lib/ferret/search/scorer.rb +0 -91
  199. data/lib/ferret/search/similarity.rb +0 -278
  200. data/lib/ferret/search/sloppy_phrase_scorer.rb +0 -47
  201. data/lib/ferret/search/sort.rb +0 -112
  202. data/lib/ferret/search/sort_comparator.rb +0 -60
  203. data/lib/ferret/search/sort_field.rb +0 -91
  204. data/lib/ferret/search/spans.rb +0 -12
  205. data/lib/ferret/search/spans/near_spans_enum.rb +0 -304
  206. data/lib/ferret/search/spans/span_first_query.rb +0 -79
  207. data/lib/ferret/search/spans/span_near_query.rb +0 -108
  208. data/lib/ferret/search/spans/span_not_query.rb +0 -130
  209. data/lib/ferret/search/spans/span_or_query.rb +0 -176
  210. data/lib/ferret/search/spans/span_query.rb +0 -25
  211. data/lib/ferret/search/spans/span_scorer.rb +0 -74
  212. data/lib/ferret/search/spans/span_term_query.rb +0 -105
  213. data/lib/ferret/search/spans/span_weight.rb +0 -84
  214. data/lib/ferret/search/spans/spans_enum.rb +0 -44
  215. data/lib/ferret/search/term_query.rb +0 -128
  216. data/lib/ferret/search/term_scorer.rb +0 -183
  217. data/lib/ferret/search/top_docs.rb +0 -36
  218. data/lib/ferret/search/top_field_docs.rb +0 -17
  219. data/lib/ferret/search/weight.rb +0 -54
  220. data/lib/ferret/search/wildcard_query.rb +0 -26
  221. data/lib/ferret/search/wildcard_term_enum.rb +0 -61
  222. data/lib/ferret/stemmers.rb +0 -1
  223. data/lib/ferret/stemmers/porter_stemmer.rb +0 -218
  224. data/lib/ferret/store.rb +0 -5
  225. data/lib/ferret/store/buffered_index_io.rb +0 -190
  226. data/lib/ferret/store/directory.rb +0 -141
  227. data/lib/ferret/store/fs_store.rb +0 -381
  228. data/lib/ferret/store/index_io.rb +0 -245
  229. data/lib/ferret/store/ram_store.rb +0 -286
  230. data/lib/ferret/utils.rb +0 -8
  231. data/lib/ferret/utils/bit_vector.rb +0 -123
  232. data/lib/ferret/utils/date_tools.rb +0 -138
  233. data/lib/ferret/utils/number_tools.rb +0 -91
  234. data/lib/ferret/utils/parameter.rb +0 -41
  235. data/lib/ferret/utils/priority_queue.rb +0 -120
  236. data/lib/ferret/utils/string_helper.rb +0 -47
  237. data/lib/ferret/utils/thread_local.rb +0 -28
  238. data/lib/ferret/utils/weak_key_hash.rb +0 -60
  239. data/lib/rferret.rb +0 -37
  240. data/rake_utils/code_statistics.rb +0 -106
  241. data/test/benchmark/tb_ram_store.rb +0 -76
  242. data/test/benchmark/tb_rw_vint.rb +0 -26
  243. data/test/functional/thread_safety_index_test.rb +0 -81
  244. data/test/functional/thread_safety_test.rb +0 -137
  245. data/test/longrunning/tc_numbertools.rb +0 -60
  246. data/test/longrunning/tm_store.rb +0 -19
  247. data/test/unit/analysis/ctc_analyzer.rb +0 -532
  248. data/test/unit/analysis/data/wordfile +0 -6
  249. data/test/unit/analysis/rtc_letter_tokenizer.rb +0 -20
  250. data/test/unit/analysis/rtc_lower_case_filter.rb +0 -20
  251. data/test/unit/analysis/rtc_lower_case_tokenizer.rb +0 -27
  252. data/test/unit/analysis/rtc_per_field_analyzer_wrapper.rb +0 -39
  253. data/test/unit/analysis/rtc_porter_stem_filter.rb +0 -16
  254. data/test/unit/analysis/rtc_standard_analyzer.rb +0 -20
  255. data/test/unit/analysis/rtc_standard_tokenizer.rb +0 -20
  256. data/test/unit/analysis/rtc_stop_analyzer.rb +0 -20
  257. data/test/unit/analysis/rtc_stop_filter.rb +0 -14
  258. data/test/unit/analysis/rtc_white_space_analyzer.rb +0 -21
  259. data/test/unit/analysis/rtc_white_space_tokenizer.rb +0 -20
  260. data/test/unit/analysis/rtc_word_list_loader.rb +0 -32
  261. data/test/unit/analysis/tc_token.rb +0 -25
  262. data/test/unit/document/rtc_field.rb +0 -28
  263. data/test/unit/document/tc_document.rb +0 -47
  264. data/test/unit/document/tc_field.rb +0 -98
  265. data/test/unit/index/rtc_compound_file_io.rb +0 -107
  266. data/test/unit/index/rtc_field_infos.rb +0 -127
  267. data/test/unit/index/rtc_fields_io.rb +0 -167
  268. data/test/unit/index/rtc_multiple_term_doc_pos_enum.rb +0 -83
  269. data/test/unit/index/rtc_segment_infos.rb +0 -74
  270. data/test/unit/index/rtc_segment_term_docs.rb +0 -17
  271. data/test/unit/index/rtc_segment_term_enum.rb +0 -60
  272. data/test/unit/index/rtc_segment_term_vector.rb +0 -71
  273. data/test/unit/index/rtc_term_buffer.rb +0 -57
  274. data/test/unit/index/rtc_term_info.rb +0 -19
  275. data/test/unit/index/rtc_term_infos_io.rb +0 -192
  276. data/test/unit/index/rtc_term_vectors_io.rb +0 -108
  277. data/test/unit/index/tc_term.rb +0 -27
  278. data/test/unit/index/tc_term_voi.rb +0 -18
  279. data/test/unit/search/rtc_similarity.rb +0 -37
  280. data/test/unit/search/rtc_sort_field.rb +0 -14
  281. data/test/unit/search/tc_multi_searcher2.rb +0 -126
  282. data/test/unit/store/rtc_fs_store.rb +0 -62
  283. data/test/unit/store/rtc_ram_store.rb +0 -15
  284. data/test/unit/store/rtm_store.rb +0 -150
  285. data/test/unit/store/rtm_store_lock.rb +0 -2
  286. data/test/unit/ts_document.rb +0 -2
  287. data/test/unit/utils/rtc_bit_vector.rb +0 -73
  288. data/test/unit/utils/rtc_date_tools.rb +0 -50
  289. data/test/unit/utils/rtc_number_tools.rb +0 -59
  290. data/test/unit/utils/rtc_parameter.rb +0 -40
  291. data/test/unit/utils/rtc_priority_queue.rb +0 -62
  292. data/test/unit/utils/rtc_string_helper.rb +0 -21
  293. data/test/unit/utils/rtc_thread.rb +0 -61
  294. data/test/unit/utils/rtc_weak_key_hash.rb +0 -25
  295. data/test/utils/number_to_spoken.rb +0 -132
data/ext/lang.h CHANGED
@@ -2,60 +2,40 @@
2
2
  #define FRT_LANG_H
3
3
 
4
4
  #include <ruby.h>
5
- #include "hash.h"
5
+ #include <stdarg.h>
6
6
 
7
- #define FERRET_EXT
7
+ #define frt_malloc xmalloc
8
+ #define frt_calloc(n) xcalloc(n, 1)
9
+ #define frt_realloc xrealloc
8
10
 
9
- #define MAX_ERROR_LEN 2048
11
+ #ifdef FRT_HAS_ISO_VARARGS
12
+ /* C99-compliant compiler */
10
13
 
11
- typedef LONG_LONG llong;
12
- typedef unsigned LONG_LONG ullong;
14
+ # define FRT_EXIT(...) frt_rb_raise(__FILE__, __LINE__, __func__, __VA_ARGS__)
15
+ extern void frt_rb_raise(const char *file, int line_num, const char *func,
16
+ const char *err_type, const char *fmt, ...);
13
17
 
14
- #ifdef WIN32
15
- # undef close
16
- # undef rename
17
- extern void eprintf(VALUE etype, const char *fmt, ...);
18
+ # define V_FRT_EXIT(err_type, fmt, args) \
19
+ vfrt_rb_raise(__FILE__, __LINE__, __func__, err_type, fmt, args)
20
+ extern void vfrt_rb_raise(const char *file, int line_num, const char *func,
21
+ const char *err_type, const char *fmt, va_list args);
22
+
23
+ #elif defined(FRT_HAS_GNUC_VARARGS)
24
+ /* gcc has an extension */
25
+
26
+ # define FRT_EXIT(args...) frt_rb_raise(__FILE__, __LINE__, __func__, ##args)
27
+ extern void frt_rb_raise(const char *file, int line_num, const char *func,
28
+ const char *err_type, const char *fmt, ...);
29
+
30
+ # define V_FRT_EXIT(err_type, fmt, args) \
31
+ vfrt_rb_raise(__FILE__, __LINE__, __func__, err_type, fmt, args)
32
+ extern void vfrt_rb_raise(const char *file, int line_num, const char *func,
33
+ const char *err_type, const char *fmt, va_list args);
18
34
  #else
19
- # define eprintf(...) ft_raise(__FILE__, __LINE__, __VA_ARGS__)
35
+ /* Can't do VARARGS */
36
+
37
+ extern void FRT_EXIT(const char *err_type, const char *fmt, ...);
38
+ extern void V_FRT_EXIT(const char *err_type, const char *fmt, va_list args);
20
39
  #endif
21
- extern void ft_raise(char *file, int line_num, VALUE etype, const char *fmt, ...);
22
- extern void weprintf(const char *fmt, ...);
23
- extern char *progname(void);
24
- extern void setprogname(const char *str);
25
-
26
- extern VALUE cQueryParseException;
27
-
28
- #define EXCEPTION_CODE rb_eStandardError
29
- //#define IO_ERROR rb_eIOError
30
- //#define ARG_ERROR rb_eArgError
31
- //#define EOF_ERROR rb_eEOFError
32
- //#define UNSUPPORTED_ERROR rb_eNotImpError
33
- //#define STATE_ERROR rb_eException
34
- //#define PARSE_ERROR cQueryParseException
35
- //#define MEM_ERROR rb_eNoMemError
36
-
37
- typedef void * mutex_t;
38
- typedef struct HshTable * thread_key_t;
39
- typedef int thread_once_t;
40
- #define MUTEX_INITIALIZER NULL
41
- #define MUTEX_RECURSIVE_INITIALIZER NULL
42
- #define THREAD_ONCE_INIT 1;
43
- #define mutex_init(a, b)
44
- #define mutex_lock(a)
45
- #define mutex_trylock(a)
46
- #define mutex_unlock(a)
47
- #define mutex_destroy(a)
48
- #define thread_key_create(a, b) frt_thread_key_create(a, b)
49
- #define thread_key_delete(a) frt_thread_key_delete(a)
50
- #define thread_setspecific(a, b) frt_thread_setspecific(a, b)
51
- #define thread_getspecific(a) frt_thread_getspecific(a)
52
- #define thread_exit(a)
53
- #define thread_once(a, b) frt_thread_once(a, b)
54
-
55
- void frt_thread_once(int *once_control, void (*init_routine) (void));
56
- void frt_thread_key_create(thread_key_t *key, void (*destr_function) (void *));
57
- void frt_thread_key_delete(thread_key_t key);
58
- void frt_thread_setspecific(thread_key_t key, const void *pointer);
59
- void *frt_thread_getspecific(thread_key_t key);
60
40
 
61
41
  #endif
data/ext/libstemmer.c CHANGED
@@ -24,7 +24,7 @@ static stemmer_encoding sb_getenc(const char * charenc)
24
24
  struct stemmer_encoding * encoding;
25
25
  if (charenc == NULL) return ENC_UTF_8;
26
26
  for (encoding = encodings; encoding->name != 0; encoding++) {
27
- if (strcmp(encoding->name, charenc) == 0) break;
27
+ if (strcmp(encoding->name, charenc) == 0) break;
28
28
  }
29
29
  if (encoding->name == NULL) return ENC_UNKNOWN;
30
30
  return encoding->enc;
@@ -36,13 +36,13 @@ sb_stemmer_new(const char * algorithm, const char * charenc)
36
36
  stemmer_encoding enc;
37
37
  struct stemmer_modules * module;
38
38
  struct sb_stemmer * stemmer =
39
- (struct sb_stemmer *) malloc(sizeof(struct sb_stemmer));
39
+ (struct sb_stemmer *) malloc(sizeof(struct sb_stemmer));
40
40
  if (stemmer == NULL) return NULL;
41
41
  enc = sb_getenc(charenc);
42
42
  if (enc == ENC_UNKNOWN) return NULL;
43
43
 
44
44
  for (module = modules; module->name != 0; module++) {
45
- if (strcmp(module->name, algorithm) == 0 && module->enc == enc) break;
45
+ if (strcmp(module->name, algorithm) == 0 && module->enc == enc) break;
46
46
  }
47
47
  if (module->name == NULL) return NULL;
48
48
 
data/ext/mem_pool.c ADDED
@@ -0,0 +1,84 @@
1
+ #include "global.h"
2
+ #include "mem_pool.h"
3
+ #include <string.h>
4
+
5
+ MemoryPool *mp_new_capa(int chuck_size, int init_buf_capa)
6
+ {
7
+ MemoryPool *mp = ALLOC(MemoryPool);
8
+ mp->chunk_size = chuck_size;
9
+ mp->buf_capa = init_buf_capa;
10
+ mp->buffers = ALLOC_N(char *, init_buf_capa);
11
+
12
+ mp->buffers[0] = mp->curr_buffer = emalloc(mp->chunk_size);
13
+ mp->buf_alloc = 1;
14
+ mp->buf_pointer = 0;
15
+ mp->pointer = 0;
16
+ return mp;
17
+ }
18
+
19
+ MemoryPool *mp_new()
20
+ {
21
+ return mp_new_capa(MP_BUF_SIZE, MP_INIT_CAPA);
22
+ }
23
+
24
+ inline void *mp_alloc(MemoryPool *mp, int size)
25
+ {
26
+ char *p;
27
+ p = mp->curr_buffer + mp->pointer;
28
+ mp->pointer += size;
29
+
30
+ if (mp->pointer > mp->chunk_size) {
31
+ mp->buf_pointer++;
32
+ if (mp->buf_pointer >= mp->buf_alloc) {
33
+ mp->buf_alloc++;
34
+ if (mp->buf_alloc >= mp->buf_capa) {
35
+ mp->buf_capa <<= 1;
36
+ REALLOC_N(mp->buffers, char *, mp->buf_capa);
37
+ }
38
+ mp->buffers[mp->buf_pointer] = emalloc(mp->chunk_size);
39
+ }
40
+ p = mp->curr_buffer = mp->buffers[mp->buf_pointer];
41
+ mp->pointer = size;
42
+ }
43
+ return p;
44
+ }
45
+
46
+ char *mp_strdup(MemoryPool *mp, const char *str)
47
+ {
48
+ int len = strlen(str) + 1;
49
+ return memcpy(mp_alloc(mp, len), str, len);
50
+ }
51
+
52
+ char *mp_strndup(MemoryPool *mp, const char *str, int len)
53
+ {
54
+ char *s = memcpy(mp_alloc(mp, len + 1), str, len);
55
+ s[len] = '\0';
56
+ return s;
57
+ }
58
+
59
+ void *mp_memdup(MemoryPool *mp, const void *p, int len)
60
+ {
61
+ return memcpy(mp_alloc(mp, len), p, len);
62
+ }
63
+
64
+ int mp_used(MemoryPool *mp)
65
+ {
66
+ return mp->buf_pointer * mp->chunk_size + mp->pointer;
67
+ }
68
+
69
+ void mp_reset(MemoryPool *mp)
70
+ {
71
+ mp->buf_pointer = 0;
72
+ mp->pointer = 0;
73
+ mp->curr_buffer = mp->buffers[0];
74
+ }
75
+
76
+ void mp_destroy(MemoryPool *mp)
77
+ {
78
+ int i;
79
+ for (i = 0; i < mp->buf_alloc; i++) {
80
+ free(mp->buffers[i]);
81
+ }
82
+ free(mp->buffers);
83
+ free(mp);
84
+ }
data/ext/mem_pool.h ADDED
@@ -0,0 +1,35 @@
1
+ #ifndef FRT_MEM_POOL_H
2
+ #define FRT_MEM_POOL_H
3
+
4
+ #define MP_BUF_SIZE 65536
5
+ #define MP_INIT_CAPA 4
6
+
7
+ typedef struct MemoryPool {
8
+ int buf_alloc;
9
+ int buf_capa;
10
+ int buf_pointer;
11
+ int pointer;
12
+ int chunk_size;
13
+ char *curr_buffer;
14
+ char **buffers;
15
+ } MemoryPool;
16
+
17
+ extern MemoryPool *mp_new();
18
+ extern MemoryPool *mp_new_capa(int chunk_size, int init_capa);
19
+ extern inline void *mp_alloc(MemoryPool *mp, int size);
20
+ extern void mp_reset(MemoryPool *mp);
21
+ extern void mp_destroy(MemoryPool *mp);
22
+ extern char *mp_strdup(MemoryPool *mp, const char *str);
23
+ extern char *mp_strndup(MemoryPool *mp, const char *str, int len);
24
+ extern void *mp_memdup(MemoryPool *mp, const void *p, int len);
25
+ extern int mp_used(MemoryPool *mp);
26
+
27
+ #define MP_ALLOC_N(mp,type,n) (type *)mp_alloc(mp, sizeof(type)*(n))
28
+ #define MP_ALLOC(mp,type) (type *)mp_alloc(mp, sizeof(type))
29
+
30
+ #define MP_ALLOC_AND_ZERO(mp,type)\
31
+ (type*)memset(mp_alloc(mp, sizeof(type)), 0, sizeof(type))
32
+ #define MP_ALLOC_AND_ZERO_N(mp,type,n)\
33
+ (type*)ZEROSET_N(mp_alloc(mp, sizeof(type)*(n)), type, n)
34
+
35
+ #endif
data/ext/posh.c ADDED
@@ -0,0 +1,1006 @@
1
+ /*
2
+ LICENSE:
3
+
4
+ Copyright (c) 2004, Brian Hook
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are
9
+ met:
10
+
11
+ * Redistributions of source code must retain the above copyright
12
+ notice, this list of conditions and the following disclaimer.
13
+
14
+ * Redistributions in binary form must reproduce the above
15
+ copyright notice, this list of conditions and the following
16
+ disclaimer in the documentation and/or other materials provided
17
+ with the distribution.
18
+
19
+ * The names of this package'ss contributors contributors may not
20
+ be used to endorse or promote products derived from this
21
+ software without specific prior written permission.
22
+
23
+
24
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
+ */
36
+ /**
37
+ @file posh.c
38
+ @author Brian Hook
39
+ @date 2002
40
+ @brief Portable Open Source Harness primary source file
41
+ */
42
+ #include "posh.h"
43
+
44
+ #if !defined FORCE_DOXYGEN
45
+
46
+ #if !defined POSH_NO_FLOAT
47
+ # define POSH_FLOAT_STRING "enabled"
48
+ #else
49
+ # define POSH_FLOAT_STRING "disabled"
50
+ #endif
51
+
52
+ #if defined POSH_64BIT_INTEGER
53
+ # define POSH_64BIT_INTEGER_STRING "yes"
54
+ #else
55
+ # define POSH_64BIT_INTEGER_STRING "no"
56
+ #endif
57
+
58
+ #if defined POSH_64BIT_POINTER
59
+ # define POSH_POINTER_STRING "64-bits"
60
+ #else
61
+ # define POSH_POINTER_STRING "32-bits"
62
+ #endif
63
+
64
+ #if defined POSH_LITTLE_ENDIAN
65
+ # define IS_BIG_ENDIAN 0
66
+
67
+ # define NATIVE16 POSH_LittleU16
68
+ # define NATIVE32 POSH_LittleU32
69
+ # define NATIVE64 POSH_LittleU64
70
+ # define FOREIGN16 POSH_BigU16
71
+ # define FOREIGN32 POSH_BigU32
72
+ # define FOREIGN64 POSH_BigU64
73
+ #else
74
+ # define IS_BIG_ENDIAN 1
75
+
76
+ # define NATIVE16 POSH_BigU16
77
+ # define NATIVE32 POSH_BigU32
78
+ # define NATIVE64 POSH_BigU64
79
+ # define FOREIGN16 POSH_LittleU16
80
+ # define FOREIGN32 POSH_LittleU32
81
+ # define FOREIGN64 POSH_LittleU64
82
+ #endif /* POSH_LITTLE_ENDIAN */
83
+
84
+ static
85
+ int
86
+ s_testBigEndian( void )
87
+ {
88
+ union
89
+ {
90
+ posh_byte_t c[ 4 ];
91
+ posh_u32_t i;
92
+ } u;
93
+
94
+ u.i= 1;
95
+
96
+ if ( u.c[ 0 ] == 1 )
97
+ {
98
+ return 0;
99
+ }
100
+ return 1;
101
+ }
102
+
103
+ static
104
+ const char *
105
+ s_testSerialization( void )
106
+ {
107
+ posh_byte_t serbuf[ 8 ];
108
+ posh_u16_t tmp16;
109
+ posh_u32_t tmp32;
110
+
111
+ /* 16-bit serialization */
112
+ POSH_WriteU16ToLittle( serbuf, 0xABCD );
113
+ if ( ( tmp16 = POSH_ReadU16FromLittle( serbuf ) ) != 0xABCD )
114
+ {
115
+ return "*ERROR: failed little-endian 16-bit serialization test";
116
+ }
117
+
118
+ POSH_WriteU16ToBig( serbuf, 0xABCD );
119
+ if ( ( tmp16 = POSH_ReadU16FromBig( serbuf ) ) != 0xABCD )
120
+ {
121
+ return "*ERROR: failed big-endian 16-bit serialization test";
122
+ }
123
+
124
+ /* 32-bit serialization */
125
+ POSH_WriteU32ToLittle( serbuf, 0xABCD1234L );
126
+ if ( ( tmp32 = POSH_ReadU32FromLittle( serbuf ) ) != 0xABCD1234 )
127
+ {
128
+ return "*ERROR: failed little-endian 32-bit serialization test";
129
+ }
130
+
131
+ POSH_WriteU32ToBig( serbuf, 0xABCD1234L );
132
+ if ( ( tmp32 = POSH_ReadU32FromBig( serbuf ) ) != 0xABCD1234 )
133
+ {
134
+ return "*ERROR: failed big-endian 32-bit serialization test";
135
+ }
136
+
137
+ #if defined POSH_64BIT_INTEGER
138
+ {
139
+ #define REF64 POSH_U64(0xFEDCBA9876543210)
140
+
141
+ posh_u64_t tmp64;
142
+
143
+ POSH_WriteU64ToLittle( serbuf, REF64 );
144
+
145
+ if ( ( tmp64 = POSH_ReadU64FromLittle( serbuf ) ) != REF64 )
146
+ {
147
+ return "*ERROR: failed little-endian 64-bit serialization test";
148
+ }
149
+
150
+ POSH_WriteU64ToBig( serbuf, REF64 );
151
+
152
+ if ( ( tmp64 = POSH_ReadU64FromBig( serbuf ) ) != REF64 )
153
+ {
154
+ return "*ERROR: failed big-endian 64-bit serialization test";
155
+ }
156
+ }
157
+ #endif
158
+
159
+ return 0;
160
+ }
161
+
162
+ #if !defined POSH_NO_FLOAT
163
+ static
164
+ const char *
165
+ s_testFloatingPoint( void )
166
+ {
167
+ float fRef = 10.0f/30.0f;
168
+ double dRef = 10.0/30.0;
169
+ posh_byte_t dbuf[ 8 ];
170
+ float fTmp;
171
+ double dTmp;
172
+
173
+ fTmp = POSH_FloatFromLittleBits( POSH_LittleFloatBits( fRef ) );
174
+
175
+ if ( fTmp != fRef )
176
+ {
177
+ return "*ERROR: POSH little endian floating point conversion failed. Please report this to poshlib@poshlib.org!\n";
178
+ }
179
+
180
+ fTmp = POSH_FloatFromBigBits( POSH_BigFloatBits( fRef ) );
181
+ if ( fTmp != fRef )
182
+ {
183
+ return "*ERROR: POSH big endian floating point conversion failed. Please report this to poshlib@poshlib.org!\n";
184
+ }
185
+
186
+ POSH_DoubleBits( dRef, dbuf );
187
+
188
+ dTmp = POSH_DoubleFromBits( dbuf );
189
+
190
+ if ( dTmp != dRef )
191
+ {
192
+ return "*ERROR: POSH double precision floating point serialization failed. Please report this to poshlib@poshlib.org!\n";
193
+ }
194
+
195
+ return 0;
196
+ }
197
+ #endif /* !defined POSH_NO_FLOAT */
198
+
199
+ static
200
+ const char *
201
+ s_testEndianess( void )
202
+ {
203
+ /* check endianess */
204
+ if ( s_testBigEndian() != IS_BIG_ENDIAN )
205
+ {
206
+ return "*ERROR: POSH compile time endianess does not match run-time endianess verification. Please report this to poshlib@poshlib.org!\n";
207
+ }
208
+
209
+ /* make sure our endian swap routines work */
210
+ if ( ( NATIVE32( 0x11223344L ) != 0x11223344L ) ||
211
+ ( FOREIGN32( 0x11223344L ) != 0x44332211L ) ||
212
+ ( NATIVE16( 0x1234 ) != 0x1234 ) ||
213
+ ( FOREIGN16( 0x1234 ) != 0x3412 ) )
214
+ {
215
+ return "*ERROR: POSH endianess macro selection failed. Please report this to poshlib@poshlib.org!\n";
216
+ }
217
+
218
+ /* test serialization routines */
219
+
220
+ return 0;
221
+ }
222
+ #endif /* !defined FORCE_DOXYGEN */
223
+
224
+ /**
225
+ Returns a string describing this platform's basic attributes.
226
+
227
+ POSH_GetArchString() reports on an architecture's statically determined
228
+ attributes. In addition, it will perform run-time verification checks
229
+ to make sure the various platform specific functions work. If an error
230
+ occurs, please contact me at poshlib@poshlib.org so we can try to resolve
231
+ what the specific failure case is.
232
+ @returns a string describing this platform on success, or a string in the
233
+ form "*ERROR: [text]" on failure. You can simply check to see if
234
+ the first character returned is '*' to verify an error condition.
235
+ */
236
+ const char *
237
+ POSH_GetArchString( void )
238
+ {
239
+ const char *err;
240
+ const char *s = "OS:.............."POSH_OS_STRING"\n"
241
+ "CPU:............."POSH_CPU_STRING"\n"
242
+ "endian:.........."POSH_ENDIAN_STRING"\n"
243
+ "ptr size:........"POSH_POINTER_STRING"\n"
244
+ "64-bit ints......"POSH_64BIT_INTEGER_STRING"\n"
245
+ "floating point..."POSH_FLOAT_STRING"\n"
246
+ "compiler........."POSH_COMPILER_STRING"\n";
247
+
248
+ /* test endianess */
249
+ err = s_testEndianess();
250
+
251
+ if ( err != 0 )
252
+ {
253
+ return err;
254
+ }
255
+
256
+ /* test serialization */
257
+ err = s_testSerialization();
258
+
259
+ if ( err != 0 )
260
+ {
261
+ return err;
262
+ }
263
+
264
+ #if !defined POSH_NO_FLOAT
265
+ /* check that our floating point support is correct */
266
+ err = s_testFloatingPoint();
267
+
268
+ if ( err != 0 )
269
+ {
270
+ return err;
271
+ }
272
+
273
+ #endif
274
+
275
+ return s;
276
+ }
277
+
278
+ /* ---------------------------------------------------------------------------*/
279
+ /* BYTE SWAPPING SUPPORT */
280
+ /* ---------------------------------------------------------------------------*/
281
+ /**
282
+ * Byte swaps a 16-bit unsigned value
283
+ *
284
+ @ingroup ByteSwapFunctions
285
+ @param v [in] unsigned 16-bit input value to swap
286
+ @returns a byte swapped version of v
287
+ */
288
+ posh_u16_t
289
+ POSH_SwapU16( posh_u16_t v )
290
+ {
291
+ posh_u16_t swapped;
292
+
293
+ swapped = v << 8;
294
+ swapped |= v >> 8;
295
+
296
+ return swapped;
297
+ }
298
+
299
+ /**
300
+ * Byte swaps a 16-bit signed value
301
+ *
302
+ @ingroup ByteSwapFunctions
303
+ @param v [in] signed 16-bit input value to swap
304
+ @returns a byte swapped version of v
305
+ @remarks This just calls back to the unsigned version, since byte swapping
306
+ is independent of sign. However, we still provide this function to
307
+ avoid signed/unsigned mismatch compiler warnings.
308
+ */
309
+ posh_i16_t
310
+ POSH_SwapI16( posh_i16_t v )
311
+ {
312
+ return ( posh_i16_t ) POSH_SwapU16( v );
313
+ }
314
+
315
+ /**
316
+ * Byte swaps a 32-bit unsigned value
317
+ *
318
+ @ingroup ByteSwapFunctions
319
+ @param v [in] unsigned 32-bit input value to swap
320
+ @returns a byte swapped version of v
321
+ */
322
+ posh_u32_t
323
+ POSH_SwapU32( posh_u32_t v )
324
+ {
325
+ posh_u32_t swapped;
326
+
327
+ swapped = ( v & 0xFF ) << 24;
328
+ swapped |= ( v & 0xFF00 ) << 8;
329
+ swapped |= ( v >> 8 ) & 0xFF00;
330
+ swapped |= ( v >> 24 );
331
+
332
+ return swapped;
333
+ }
334
+
335
+ /**
336
+ * Byte swaps a 32-bit signed value
337
+ *
338
+ @ingroup ByteSwapFunctions
339
+ @param v [in] signed 32-bit input value to swap
340
+ @returns a byte swapped version of v
341
+ @remarks This just calls back to the unsigned version, since byte swapping
342
+ is independent of sign. However, we still provide this function to
343
+ avoid signed/unsigned mismatch compiler warnings.
344
+ */
345
+ posh_i32_t
346
+ POSH_SwapI32( posh_i32_t v )
347
+ {
348
+ return ( posh_i32_t ) POSH_SwapU32( ( posh_u32_t ) v );
349
+ }
350
+
351
+ #if defined POSH_64BIT_INTEGER
352
+ /**
353
+ * Byte swaps a 64-bit unsigned value
354
+
355
+ @param v [in] a 64-bit input value to swap
356
+ @ingroup SixtyFourBit
357
+ @returns a byte swapped version of v
358
+ */
359
+ posh_u64_t
360
+ POSH_SwapU64( posh_u64_t v )
361
+ {
362
+ posh_byte_t tmp;
363
+ union {
364
+ posh_byte_t bytes[ 8 ];
365
+ posh_u64_t u64;
366
+ } u;
367
+
368
+ u.u64 = v;
369
+
370
+ tmp = u.bytes[ 0 ]; u.bytes[ 0 ] = u.bytes[ 7 ]; u.bytes[ 7 ] = tmp;
371
+ tmp = u.bytes[ 1 ]; u.bytes[ 1 ] = u.bytes[ 6 ]; u.bytes[ 6 ] = tmp;
372
+ tmp = u.bytes[ 2 ]; u.bytes[ 2 ] = u.bytes[ 5 ]; u.bytes[ 5 ] = tmp;
373
+ tmp = u.bytes[ 3 ]; u.bytes[ 3 ] = u.bytes[ 4 ]; u.bytes[ 4 ] = tmp;
374
+
375
+ return u.u64;
376
+ }
377
+
378
+ /**
379
+ * Byte swaps a 64-bit signed value
380
+
381
+ @param v [in] a 64-bit input value to swap
382
+ @ingroup SixtyFourBit
383
+ @returns a byte swapped version of v
384
+ */
385
+ posh_i64_t
386
+ POSH_SwapI64( posh_i64_t v )
387
+ {
388
+ return ( posh_i64_t ) POSH_SwapU64( ( posh_u64_t ) v );
389
+ }
390
+
391
+ #endif /* defined POSH_64BIT_INTEGER */
392
+
393
+ /* ---------------------------------------------------------------------------*/
394
+ /* IN-MEMORY SERIALIZATION */
395
+ /* ---------------------------------------------------------------------------*/
396
+
397
+ /**
398
+ * Writes an unsigned 16-bit value to a little endian buffer
399
+
400
+ @ingroup MemoryBuffer
401
+ @param dst [out] pointer to the destination buffer, may not be NULL. Alignment doesn't matter.
402
+ @param value [in] host-endian unsigned 16-bit value
403
+ @returns a pointer to the location two bytes after dst
404
+ @remarks does no validation of the inputs
405
+ */
406
+ posh_u16_t *
407
+ POSH_WriteU16ToLittle( void *dst, posh_u16_t value )
408
+ {
409
+ posh_u16_t *p16 = ( posh_u16_t * ) dst;
410
+ posh_byte_t *p = ( posh_byte_t * ) dst;
411
+
412
+ p[ 0 ] = value & 0xFF;
413
+ p[ 1 ] = ( value & 0xFF00) >> 8;
414
+
415
+ return p16 + 1;
416
+ }
417
+
418
+ /**
419
+ * Writes a signed 16-bit value to a little endian buffer
420
+
421
+ @ingroup MemoryBuffer
422
+ @param dst [out] pointer to the destination buffer, may not be NULL
423
+ @param value [in] host-endian signed 16-bit value
424
+ @returns a pointer to the location two bytes after dst
425
+ @remarks does no validation of the inputs. This simply calls
426
+ POSH_WriteU16ToLittle() with appropriate casting.
427
+ */
428
+ posh_i16_t *
429
+ POSH_WriteI16ToLittle( void *dst, posh_i16_t value )
430
+ {
431
+ return ( posh_i16_t * ) POSH_WriteU16ToLittle( dst, ( posh_u16_t ) value );
432
+ }
433
+
434
+ /**
435
+ * Writes an unsigned 32-bit value to a little endian buffer
436
+
437
+ @ingroup MemoryBuffer
438
+ @param dst [out] pointer to the destination buffer, may not be NULL
439
+ @param value [in] host-endian signed 32-bit value
440
+ @returns a pointer to the location four bytes after dst
441
+ @remarks does no validation of the inputs.
442
+ */
443
+ posh_u32_t *
444
+ POSH_WriteU32ToLittle( void *dst, posh_u32_t value )
445
+ {
446
+ posh_u32_t *p32 = ( posh_u32_t * ) dst;
447
+ posh_byte_t *p = ( posh_byte_t * ) dst;
448
+
449
+ p[ 0 ] = ( value & 0xFF );
450
+ p[ 1 ] = ( value & 0xFF00 ) >> 8;
451
+ p[ 2 ] = ( value & 0xFF0000 ) >> 16;
452
+ p[ 3 ] = ( value & 0xFF000000 ) >> 24;
453
+
454
+ return p32 + 1;
455
+ }
456
+
457
+ /**
458
+ * Writes a signed 32-bit value to a little endian buffer
459
+
460
+ @ingroup MemoryBuffer
461
+ @param dst [out] pointer to the destination buffer, may not be NULL
462
+ @param value [in] host-endian signed 32-bit value
463
+ @returns a pointer to the location four bytes after dst
464
+ @remarks does no validation of the inputs. This simply calls
465
+ POSH_WriteU32ToLittle() with appropriate casting.
466
+ */
467
+ posh_i32_t *
468
+ POSH_WriteI32ToLittle( void *dst, posh_i32_t value )
469
+ {
470
+ return ( posh_i32_t * ) POSH_WriteU32ToLittle( dst, ( posh_u32_t ) value );
471
+ }
472
+
473
+ /**
474
+ * Writes an unsigned 16-bit value to a big endian buffer
475
+
476
+ @ingroup MemoryBuffer
477
+ @param dst [out] pointer to the destination buffer, may not be NULL
478
+ @param value [in] host-endian unsigned 16-bit value
479
+ @returns a pointer to the location two bytes after dst
480
+ @remarks does no validation of the inputs
481
+ */
482
+ posh_u16_t *
483
+ POSH_WriteU16ToBig( void *dst, posh_u16_t value )
484
+ {
485
+ posh_u16_t *p16 = ( posh_u16_t * ) dst;
486
+ posh_byte_t *p = ( posh_byte_t * ) dst;
487
+
488
+ p[ 1 ] = ( value & 0xFF );
489
+ p[ 0 ] = ( value & 0xFF00 ) >> 8;
490
+
491
+ return p16 + 1;
492
+ }
493
+
494
+ /**
495
+ * Writes a signed 16-bit value to a big endian buffer
496
+
497
+ @ingroup MemoryBuffer
498
+ @param dst [out] pointer to the destination buffer, may not be NULL
499
+ @param value [in] host-endian signed 16-bit value
500
+ @returns a pointer to the location two bytes after dst
501
+ @remarks does no validation of the inputs. This simply calls
502
+ POSH_WriteU16ToLittle() with appropriate casting.
503
+ */
504
+ posh_i16_t *
505
+ POSH_WriteI16ToBig( void *dst, posh_i16_t value )
506
+ {
507
+ return ( posh_i16_t * ) POSH_WriteU16ToBig( dst, ( posh_u16_t ) value );
508
+ }
509
+
510
+ /**
511
+ * Writes an unsigned 32-bit value to a big endian buffer
512
+
513
+ @ingroup MemoryBuffer
514
+ @param dst [out] pointer to the destination buffer, may not be NULL
515
+ @param value [in] host-endian unsigned 32-bit value
516
+ @returns a pointer to the location four bytes after dst
517
+ @remarks does no validation of the inputs.
518
+ */
519
+ posh_u32_t *
520
+ POSH_WriteU32ToBig( void *dst, posh_u32_t value )
521
+ {
522
+ posh_u32_t *p32 = ( posh_u32_t * ) dst;
523
+ posh_byte_t *p = ( posh_byte_t * ) dst;
524
+
525
+ p[ 3 ] = ( value & 0xFF );
526
+ p[ 2 ] = ( value & 0xFF00 ) >> 8;
527
+ p[ 1 ] = ( value & 0xFF0000 ) >> 16;
528
+ p[ 0 ] = ( value & 0xFF000000 ) >> 24;
529
+
530
+ return p32 + 1;
531
+ }
532
+
533
+ /**
534
+ * Writes a signed 32-bit value to a big endian buffer
535
+
536
+ @ingroup MemoryBuffer
537
+ @param dst [out] pointer to the destination buffer, may not be NULL
538
+ @param value [in] host-endian signed 32-bit value
539
+ @returns a pointer to the location four bytes after dst
540
+ @remarks does no validation of the inputs. This simply calls
541
+ POSH_WriteU32ToBig() with appropriate casting.
542
+ */
543
+ posh_i32_t *
544
+ POSH_WriteI32ToBig( void *dst, posh_i32_t value )
545
+ {
546
+ return ( posh_i32_t * ) POSH_WriteU32ToBig( dst, ( posh_u32_t ) value );
547
+ }
548
+
549
+ #if defined POSH_64BIT_INTEGER
550
+ /**
551
+ * Writes an unsigned 64-bit value to a little-endian buffer
552
+
553
+ @ingroup SixtyFourBit
554
+ @param dst [out] pointer to the destination buffer, may not be NULL
555
+ @param value [in] host-endian unsigned 64-bit value
556
+ @returns a pointer to the location eight bytes after dst
557
+ @remarks does no validation of the inputs.
558
+ */
559
+ posh_u64_t *
560
+ POSH_WriteU64ToLittle( void *dst, posh_u64_t value )
561
+ {
562
+ posh_u64_t *p64 = ( posh_u64_t * ) dst;
563
+ posh_byte_t *p = ( posh_byte_t * ) dst;
564
+ int i;
565
+
566
+ for ( i = 0; i < 8; i++, value >>= 8 )
567
+ {
568
+ p[ i ] = ( posh_byte_t ) ( value & 0xFF );
569
+ }
570
+
571
+ return p64 + 1;
572
+ }
573
+
574
+ /**
575
+ * Writes a signed 64-bit value to a little-endian buffer
576
+
577
+ @ingroup SixtyFourBit
578
+ @param dst [out] pointer to the destination buffer, may not be NULL
579
+ @param value [in] host-endian unsigned 64-bit value
580
+ @returns a pointer to the location eight bytes after dst
581
+ @remarks does no validation of the inputs.
582
+ */
583
+ posh_i64_t *
584
+ POSH_WriteI64ToLittle( void *dst, posh_i64_t value )
585
+ {
586
+ return ( posh_i64_t * ) POSH_WriteU64ToLittle( dst, ( posh_u64_t ) value );
587
+ }
588
+
589
+ /**
590
+ * Writes an unsigned 64-bit value to a big-endian buffer
591
+
592
+ @ingroup SixtyFourBit
593
+ @param dst [out] pointer to the destination buffer, may not be NULL
594
+ @param value [in] host-endian unsigned 64-bit value
595
+ @returns a pointer to the location eight bytes after dst
596
+ @remarks does no validation of the inputs.
597
+ */
598
+ posh_u64_t *
599
+ POSH_WriteU64ToBig( void *dst, posh_u64_t value )
600
+ {
601
+ posh_u64_t *p64 = ( posh_u64_t * ) dst;
602
+ posh_byte_t *p = ( posh_byte_t * ) dst;
603
+ int i;
604
+
605
+ for ( i = 0; i < 8; i++, value >>= 8 )
606
+ {
607
+ p[ 7-i ] = ( posh_byte_t ) ( value & 0xFF );
608
+ }
609
+
610
+ return p64 + 8;
611
+ }
612
+
613
+ /**
614
+ * Writes a signed 64-bit value to a big-endian buffer
615
+
616
+ @ingroup SixtyFourBit
617
+ @param dst [out] pointer to the destination buffer, may not be NULL
618
+ @param value [in] host-endian signed 64-bit value
619
+ @returns a pointer to the location eight bytes after dst
620
+ @remarks does no validation of the inputs.
621
+ */
622
+ posh_i64_t *
623
+ POSH_WriteI64ToBig( void *dst, posh_i64_t value )
624
+ {
625
+ return ( posh_i64_t * ) POSH_WriteU64ToBig( dst, ( posh_u64_t ) value );
626
+ }
627
+
628
+ #endif /* POSH_64BIT_INTEGER */
629
+
630
+ /* ---------------------------------------------------------------------------*/
631
+ /* IN-MEMORY DESERIALIZATION */
632
+ /* ---------------------------------------------------------------------------*/
633
+
634
+ /**
635
+ * Reads an unsigned 16-bit value from a little-endian buffer
636
+ @ingroup MemoryBuffer
637
+ @param src [in] source buffer
638
+ @returns host-endian unsigned 16-bit value
639
+ */
640
+ posh_u16_t
641
+ POSH_ReadU16FromLittle( const void *src )
642
+ {
643
+ posh_u16_t v = 0;
644
+ posh_byte_t *p = ( posh_byte_t * ) src;
645
+
646
+ v |= p[ 0 ];
647
+ v |= ( ( posh_u16_t ) p[ 1 ] ) << 8;
648
+
649
+ return v;
650
+ }
651
+
652
+ /**
653
+ * Reads a signed 16-bit value from a little-endian buffer
654
+ @ingroup MemoryBuffer
655
+ @param src [in] source buffer
656
+ @returns host-endian signed 16-bit value
657
+ */
658
+ posh_i16_t
659
+ POSH_ReadI16FromLittle( const void *src )
660
+ {
661
+ return ( posh_i16_t ) POSH_ReadU16FromLittle( src );
662
+ }
663
+
664
+ /**
665
+ * Reads an unsigned 32-bit value from a little-endian buffer
666
+ @ingroup MemoryBuffer
667
+ @param src [in] source buffer
668
+ @returns host-endian unsigned 32-bit value
669
+ */
670
+ posh_u32_t
671
+ POSH_ReadU32FromLittle( const void *src )
672
+ {
673
+ posh_u32_t v = 0;
674
+ posh_byte_t *p = ( posh_byte_t * ) src;
675
+
676
+ v |= p[ 0 ];
677
+ v |= ( ( posh_u32_t ) p[ 1 ] ) << 8;
678
+ v |= ( ( posh_u32_t ) p[ 2 ] ) << 16;
679
+ v |= ( ( posh_u32_t ) p[ 3 ] ) << 24;
680
+
681
+ return v;
682
+ }
683
+
684
+ /**
685
+ * Reads a signed 32-bit value from a little-endian buffer
686
+ @ingroup MemoryBuffer
687
+ @param src [in] source buffer
688
+ @returns host-endian signed 32-bit value
689
+ */
690
+ posh_i32_t
691
+ POSH_ReadI32FromLittle( const void *src )
692
+ {
693
+ return ( posh_i32_t ) POSH_ReadU32FromLittle( src );
694
+ }
695
+
696
+
697
+ /**
698
+ * Reads an unsigned 16-bit value from a big-endian buffer
699
+ @ingroup MemoryBuffer
700
+ @param src [in] source buffer
701
+ @returns host-endian unsigned 16-bit value
702
+ */
703
+ posh_u16_t
704
+ POSH_ReadU16FromBig( const void *src )
705
+ {
706
+ posh_u16_t v = 0;
707
+ posh_byte_t *p = ( posh_byte_t * ) src;
708
+
709
+ v |= p[ 1 ];
710
+ v |= ( ( posh_u16_t ) p[ 0 ] ) << 8;
711
+
712
+ return v;
713
+ }
714
+
715
+ /**
716
+ * Reads a signed 16-bit value from a big-endian buffer
717
+ @ingroup MemoryBuffer
718
+ @param src [in] source buffer
719
+ @returns host-endian signed 16-bit value
720
+ */
721
+ posh_i16_t
722
+ POSH_ReadI16FromBig( const void *src )
723
+ {
724
+ return ( posh_i16_t ) POSH_ReadU16FromBig( src );
725
+ }
726
+
727
+ /**
728
+ * Reads an unsigned 32-bit value from a big-endian buffer
729
+ @ingroup MemoryBuffer
730
+ @param src [in] source buffer
731
+ @returns host-endian unsigned 32-bit value
732
+ */
733
+ posh_u32_t
734
+ POSH_ReadU32FromBig( const void *src )
735
+ {
736
+ posh_u32_t v = 0;
737
+ posh_byte_t *p = ( posh_byte_t * ) src;
738
+
739
+ v |= p[ 3 ];
740
+ v |= ( ( posh_u32_t ) p[ 2 ] ) << 8;
741
+ v |= ( ( posh_u32_t ) p[ 1 ] ) << 16;
742
+ v |= ( ( posh_u32_t ) p[ 0 ] ) << 24;
743
+
744
+ return v;
745
+ }
746
+
747
+ /**
748
+ * Reads a signed 32-bit value from a big-endian buffer
749
+ @ingroup MemoryBuffer
750
+ @param src [in] source buffer
751
+ @returns host-endian signed 32-bit value
752
+ */
753
+ posh_i32_t
754
+ POSH_ReadI32FromBig( const void *src )
755
+ {
756
+ return POSH_BigI32( (*(const posh_i32_t*)src ) );
757
+ }
758
+
759
+ #if defined POSH_64BIT_INTEGER
760
+
761
+ /**
762
+ * Reads an unsigned 64-bit value from a little-endian buffer
763
+ @param src [in] source buffer
764
+ @returns host-endian unsigned 32-bit value
765
+ */
766
+ posh_u64_t
767
+ POSH_ReadU64FromLittle( const void *src )
768
+ {
769
+ posh_u64_t v = 0;
770
+ posh_byte_t *p = ( posh_byte_t * ) src;
771
+ int i;
772
+
773
+ for ( i = 0; i < 8; i++ )
774
+ {
775
+ v |= ( ( posh_u64_t ) p[ i ] ) << (i*8);
776
+ }
777
+
778
+ return v;
779
+ }
780
+
781
+ /**
782
+ * Reads a signed 64-bit value from a little-endian buffer
783
+ @param src [in] source buffer
784
+ @returns host-endian signed 32-bit value
785
+ */
786
+ posh_i64_t
787
+ POSH_ReadI64FromLittle( const void *src )
788
+ {
789
+ return ( posh_i64_t ) POSH_ReadU64FromLittle( src );
790
+ }
791
+
792
+ /**
793
+ * Reads an unsigned 64-bit value from a big-endian buffer
794
+ @param src [in] source buffer
795
+ @returns host-endian unsigned 32-bit value
796
+ */
797
+ posh_u64_t
798
+ POSH_ReadU64FromBig( const void *src )
799
+ {
800
+ posh_u64_t v = 0;
801
+ posh_byte_t *p = ( posh_byte_t * ) src;
802
+ int i;
803
+
804
+ for ( i = 0; i < 8; i++ )
805
+ {
806
+ v |= ( ( posh_u64_t ) p[ 7-i ] ) << (i*8);
807
+ }
808
+
809
+ return v;
810
+ }
811
+
812
+ /**
813
+ * Reads an signed 64-bit value from a big-endian buffer
814
+ @param src [in] source buffer
815
+ @returns host-endian signed 32-bit value
816
+ */
817
+ posh_i64_t
818
+ POSH_ReadI64FromBig( const void *src )
819
+ {
820
+ return ( posh_i64_t ) POSH_ReadU64FromBig( src );
821
+ }
822
+
823
+ #endif /* POSH_64BIT_INTEGER */
824
+
825
+ /* ---------------------------------------------------------------------------*/
826
+ /* FLOATING POINT SUPPORT */
827
+ /* ---------------------------------------------------------------------------*/
828
+
829
+ #if !defined POSH_NO_FLOAT
830
+
831
+ /** @ingroup FloatingPoint
832
+ @param[in] f floating point value
833
+ @returns a little-endian bit representation of f
834
+ */
835
+ posh_u32_t
836
+ POSH_LittleFloatBits( float f )
837
+ {
838
+ union
839
+ {
840
+ float f32;
841
+ posh_u32_t u32;
842
+ } u;
843
+
844
+ u.f32 = f;
845
+
846
+ return POSH_LittleU32( u.u32 );
847
+ }
848
+
849
+ /**
850
+ * Extracts raw big-endian bits from a 32-bit floating point value
851
+ *
852
+ @ingroup FloatingPoint
853
+ @param f [in] floating point value
854
+ @returns a big-endian bit representation of f
855
+ */
856
+ posh_u32_t
857
+ POSH_BigFloatBits( float f )
858
+ {
859
+ union
860
+ {
861
+ float f32;
862
+ posh_u32_t u32;
863
+ } u;
864
+
865
+ u.f32 = f;
866
+
867
+ return POSH_BigU32( u.u32 );
868
+ }
869
+
870
+ /**
871
+ * Extracts raw, little-endian bit representation from a 64-bit double.
872
+ *
873
+ @param d [in] 64-bit double precision value
874
+ @param dst [out] 8-byte storage buffer
875
+ @ingroup FloatingPoint
876
+ @returns the raw bits used to represent the value 'd', in the form dst[0]=LSB
877
+ */
878
+ void
879
+ POSH_DoubleBits( double d, posh_byte_t dst[ 8 ] )
880
+ {
881
+ union
882
+ {
883
+ double d64;
884
+ posh_byte_t bytes[ 8 ];
885
+ } u;
886
+
887
+ u.d64 = d;
888
+
889
+ #if defined POSH_LITTLE_ENDIAN
890
+ dst[ 0 ] = u.bytes[ 0 ];
891
+ dst[ 1 ] = u.bytes[ 1 ];
892
+ dst[ 2 ] = u.bytes[ 2 ];
893
+ dst[ 3 ] = u.bytes[ 3 ];
894
+ dst[ 4 ] = u.bytes[ 4 ];
895
+ dst[ 5 ] = u.bytes[ 5 ];
896
+ dst[ 6 ] = u.bytes[ 6 ];
897
+ dst[ 7 ] = u.bytes[ 7 ];
898
+ #else
899
+ dst[ 0 ] = u.bytes[ 7 ];
900
+ dst[ 1 ] = u.bytes[ 6 ];
901
+ dst[ 2 ] = u.bytes[ 5 ];
902
+ dst[ 3 ] = u.bytes[ 4 ];
903
+ dst[ 4 ] = u.bytes[ 3 ];
904
+ dst[ 5 ] = u.bytes[ 2 ];
905
+ dst[ 6 ] = u.bytes[ 1 ];
906
+ dst[ 7 ] = u.bytes[ 0 ];
907
+ #endif
908
+ }
909
+
910
+ /**
911
+ * Creates a double-precision, 64-bit floating point value from a set of raw,
912
+ * little-endian bits
913
+
914
+ @ingroup FloatingPoint
915
+ @param src [in] little-endian byte representation of 64-bit double precision
916
+ floating point value
917
+ @returns double precision floating point representation of the raw bits
918
+ @remarks No error checking is performed, so there are no guarantees that the
919
+ result is a valid number, nor is there any check to ensure that src is
920
+ non-NULL. BE CAREFUL USING THIS.
921
+ */
922
+ double
923
+ POSH_DoubleFromBits( const posh_byte_t src[ 8 ] )
924
+ {
925
+ union
926
+ {
927
+ double d64;
928
+ posh_byte_t bytes[ 8 ];
929
+ } u;
930
+
931
+ #if defined POSH_LITTLE_ENDIAN
932
+ u.bytes[ 0 ] = src[ 0 ];
933
+ u.bytes[ 1 ] = src[ 1 ];
934
+ u.bytes[ 2 ] = src[ 2 ];
935
+ u.bytes[ 3 ] = src[ 3 ];
936
+ u.bytes[ 4 ] = src[ 4 ];
937
+ u.bytes[ 5 ] = src[ 5 ];
938
+ u.bytes[ 6 ] = src[ 6 ];
939
+ u.bytes[ 7 ] = src[ 7 ];
940
+ #else
941
+ u.bytes[ 0 ] = src[ 7 ];
942
+ u.bytes[ 1 ] = src[ 6 ];
943
+ u.bytes[ 2 ] = src[ 5 ];
944
+ u.bytes[ 3 ] = src[ 4 ];
945
+ u.bytes[ 4 ] = src[ 3 ];
946
+ u.bytes[ 5 ] = src[ 2 ];
947
+ u.bytes[ 6 ] = src[ 1 ];
948
+ u.bytes[ 7 ] = src[ 0 ];
949
+ #endif
950
+
951
+ return u.d64;
952
+ }
953
+
954
+ /**
955
+ * Creates a floating point number from little endian bits
956
+ *
957
+ @ingroup FloatingPoint
958
+ @param bits [in] raw floating point bits in little-endian form
959
+ @returns a floating point number based on the given bit representation
960
+ @remarks No error checking is performed, so there are no guarantees that the
961
+ result is a valid number. BE CAREFUL USING THIS.
962
+ */
963
+ float
964
+ POSH_FloatFromLittleBits( posh_u32_t bits )
965
+ {
966
+ union
967
+ {
968
+ float f32;
969
+ posh_u32_t u32;
970
+ } u;
971
+
972
+ u.u32 = bits;
973
+ #if defined POSH_BIG_ENDIAN
974
+ u.u32 = POSH_SwapU32( u.u32 );
975
+ #endif
976
+
977
+ return u.f32;
978
+ }
979
+
980
+ /**
981
+ * Creates a floating point number from big-endian bits
982
+ *
983
+ @ingroup FloatingPoint
984
+ @param bits [in] raw floating point bits in big-endian form
985
+ @returns a floating point number based on the given bit representation
986
+ @remarks No error checking is performed, so there are no guarantees that the
987
+ result is a valid number. BE CAREFUL USING THIS.
988
+ */
989
+ float
990
+ POSH_FloatFromBigBits( posh_u32_t bits )
991
+ {
992
+ union
993
+ {
994
+ float f32;
995
+ posh_u32_t u32;
996
+ } u;
997
+
998
+ u.u32 = bits;
999
+ #if defined POSH_LITTLE_ENDIAN
1000
+ u.u32 = POSH_SwapU32( u.u32 );
1001
+ #endif
1002
+
1003
+ return u.f32;
1004
+ }
1005
+
1006
+ #endif /* !defined POSH_NO_FLOAT */