00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <libheraia.h>
00032
00033 static gboolean bissextile_year(guint32 year);
00034 static void calc_which_month_day(date_and_time_t *mydate, guint32 day, guint tab_ns_months[12]);
00035 static void which_month_day(date_and_time_t *mydate, guint32 day, gboolean bi);
00036 static guint32 remove_days_from_first_january(guint32 base_year, guint8 base_month, guint8 base_day);
00037 static void which_year_month_day(date_and_time_t *mydate, guint32 days, guint32 base_year, guint base_month, guint8 base_day);
00038 static void make_date_and_time(date_and_time_t *mydate, guchar *data, guint8 len, guint64 nbticks, guint32 base_year, guint base_month, guint8 base_day);
00039 static void transform_bcd_to_human(gchar *bcd, guint8 part, guint8 part_number);
00040
00041
00042
00043
00044
00045
00046
00047
00048 gchar *decode_8bits_signed(guchar *data)
00049 {
00050 gint8 total = 0;
00051
00052 if (data == NULL)
00053 {
00054 return NULL;
00055 }
00056 else
00057 {
00058 memcpy(&total, data, sizeof (guchar));
00059 return g_strdup_printf("%d", total);
00060 }
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 gchar *decode_8bits_unsigned(guchar *data)
00072 {
00073 guint8 total = 0;
00074
00075 if (data == NULL)
00076 {
00077 return NULL;
00078 }
00079 else
00080 {
00081 memcpy(&total, data, sizeof (guchar));
00082 return g_strdup_printf("%u", total);
00083 }
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 gchar *decode_16bits_signed(guchar *data)
00096 {
00097 gint16 total = 0;
00098
00099 if (data == NULL)
00100 {
00101 return NULL;
00102 }
00103 else
00104 {
00105 memcpy(&total, data, 2 * sizeof (guchar));
00106 return g_strdup_printf("%d", total);
00107 }
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 gchar *decode_16bits_unsigned(guchar *data)
00119 {
00120 guint16 total = 0;
00121
00122 if (data == NULL)
00123 {
00124 return NULL;
00125 }
00126 else
00127 {
00128 memcpy(&total, data, 2 * sizeof (guchar));
00129 return g_strdup_printf("%u", total);
00130 }
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 gchar *decode_32bits_signed(guchar *data)
00142 {
00143 gint32 total = 0;
00144
00145 if (data == NULL)
00146 {
00147 return NULL;
00148 }
00149 else
00150 {
00151 memcpy(&total, data, 4 * sizeof (guchar));
00152 return g_strdup_printf("%d", total);
00153 }
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 gchar *decode_32bits_unsigned(guchar *data)
00165 {
00166 guint32 total = 0;
00167
00168 if (data == NULL)
00169 {
00170 return NULL;
00171 }
00172 else
00173 {
00174 memcpy(&total, data, 4 * sizeof (guchar));
00175 return g_strdup_printf("%u", total);
00176 }
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186 gchar *decode_64bits_signed(guchar *data)
00187 {
00188 gint64 total = 0;
00189
00190 if (data == NULL)
00191 {
00192 return NULL;
00193 }
00194 else
00195 {
00196 memcpy(&total, data, 8 * sizeof (guchar));
00197 return g_strdup_printf("%lld", total);
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 gchar *decode_64bits_unsigned(guchar *data)
00210 {
00211 guint64 total = 0;
00212
00213 if (data == NULL)
00214 {
00215 return NULL;
00216 }
00217 else
00218 {
00219 memcpy(&total, data, 8 * sizeof (guchar));
00220 return g_strdup_printf("%llu", total);
00221 }
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231 static gboolean bissextile_year(guint32 year)
00232 {
00233 if ((year % 4) == 0)
00234 {
00235 if ((year % 100) == 0)
00236 {
00237 if ((year % 400) == 0)
00238 {
00239 return TRUE;
00240 }
00241 else
00242 {
00243 return FALSE;
00244 }
00245 }
00246 else
00247 {
00248 return TRUE;
00249 }
00250 }
00251 else
00252 {
00253 return FALSE;
00254 }
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 static void calc_which_month_day(date_and_time_t *mydate, guint32 day, guint tab_ns_months[12])
00267 {
00268 gushort i = 0;
00269
00270 while (i<12 && day > tab_ns_months[i])
00271 {
00272 i++;
00273 }
00274
00275 mydate->month = i + 1;
00276
00277 if (i == 0)
00278 {
00279 mydate->day = 1 + day;
00280 }
00281 else
00282 {
00283 mydate->day = (1 + day) - tab_ns_months[i-1];
00284 }
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 static void which_month_day(date_and_time_t *mydate, guint32 day, gboolean bi)
00295 {
00296
00297 if (bi == TRUE)
00298 {
00299 if (day <= 366)
00300 {
00301 guint tab_ns_months[12] = { 31, 60, 91, 121, 152, 182,
00302 213, 244, 274, 305, 335, 366 } ;
00303 calc_which_month_day(mydate, day, tab_ns_months);
00304 }
00305 else
00306 {
00307 mydate->day = 0;
00308 mydate->month = 0;
00309 }
00310 }
00311 else
00312 {
00313 if (day <= 365)
00314 {
00315 guint tab_ns_months[12] = { 31, 59, 90, 120, 151, 181,
00316 212, 243, 273, 304, 334, 365 };
00317 calc_which_month_day(mydate, day, tab_ns_months);
00318 }
00319 else
00320 {
00321 mydate->day = 0;
00322 mydate->month = 0;
00323 }
00324 }
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 static guint32 remove_days_from_first_january(guint32 base_year, guint8 base_month, guint8 base_day)
00337 {
00338 guint tab_ns_months[11];
00339
00340 if (base_day > 0 && base_day < 32)
00341 {
00342 base_day -= 1;
00343 }
00344 else
00345 {
00346 return 0;
00347 }
00348
00349 tab_ns_months[0] = 31;
00350 if (bissextile_year(base_year))
00351 {
00352 tab_ns_months[1] = 60;
00353 tab_ns_months[2] = 91;
00354 tab_ns_months[3] = 121;
00355 tab_ns_months[4] = 152;
00356 tab_ns_months[5] = 182;
00357 tab_ns_months[6] = 213;
00358 tab_ns_months[7] = 244;
00359 tab_ns_months[8] = 274;
00360 tab_ns_months[9] = 305;
00361 tab_ns_months[10] = 335;
00362 }
00363 else
00364 {
00365 tab_ns_months[1] = 59;
00366 tab_ns_months[2] = 90;
00367 tab_ns_months[3] = 120;
00368 tab_ns_months[4] = 151;
00369 tab_ns_months[5] = 181;
00370 tab_ns_months[6] = 212;
00371 tab_ns_months[7] = 243;
00372 tab_ns_months[8] = 273;
00373 tab_ns_months[9] = 304;
00374 tab_ns_months[10] = 334;
00375 }
00376
00377 if (base_month > 1 && base_month < 13)
00378 {
00379 return (tab_ns_months[base_month-2] + base_day);
00380 }
00381 else if (base_month == 1)
00382 {
00383 return base_day;
00384 }
00385 else
00386 {
00387 return 0;
00388 }
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 static void which_year_month_day(date_and_time_t *mydate, guint32 days, guint32 base_year, guint base_month, guint8 base_day)
00412 {
00413 guint32 modulus = 0;
00414 guint32 reste = 0;
00415 guint32 nbdays = 0;
00416
00417 days -= remove_days_from_first_january(base_year, base_month, base_day);
00418
00419 if (days > 146100)
00420 {
00421 modulus = days / 146100;
00422 mydate->year = modulus * 400;
00423 reste = modulus * 3;
00424 days = days % 146100;
00425 }
00426
00427 modulus = days / 1461;
00428 mydate->year += modulus * 4;
00429 reste += (modulus*4) / 100;
00430 reste += days % 1461;
00431
00432 mydate->year += base_year;
00433 if (bissextile_year(mydate->year))
00434 nbdays = 366;
00435 else
00436 nbdays = 365;
00437
00438 while (reste > nbdays)
00439 {
00440 reste -= nbdays;
00441 mydate->year += 1;
00442 if (bissextile_year(mydate->year))
00443 nbdays = 366;
00444 else
00445 nbdays = 365;
00446 }
00447
00448 which_month_day(mydate, reste, bissextile_year(mydate->year));
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 static gchar *date_printf(date_and_time_t *mydate)
00464 {
00465 return g_strdup_printf("%02u/%02u/%04u - %02u:%02u:%02u", mydate->day, mydate->month, mydate->year, mydate->hour, mydate->minutes, mydate->seconds);
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 gchar *decode_dos_date(guchar *data, date_and_time_t *mydate)
00479 {
00480
00481 if (data == NULL)
00482 {
00483 return NULL;
00484 }
00485 else
00486 {
00487 mydate->year = (data[3] >> 1) + 1980;
00488 mydate->month = ((data[3] & 0x01) << 3) + (data[2] >> 5);
00489 mydate->day = data[2] & 0x1F;
00490 mydate->hour = (data[1] & 0xF8) >> 3;
00491 mydate->minutes = ((data[1] & 0x07) << 3) + ((data[0] & 0xE0) >> 5);
00492 mydate->seconds = (data[0] & 0x1F) << 1;
00493
00494 return date_printf(mydate);
00495 }
00496 }
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 static void make_date_and_time(date_and_time_t *mydate, guchar *data, guint8 len, guint64 nbticks, guint32 base_year, guint base_month, guint8 base_day)
00512 {
00513 guint64 total = 0;
00514 guint32 days = 0;
00515
00516 memcpy(&total, data, len * sizeof (guchar));
00517
00518 total = (total / nbticks);
00519 days = (guint32) (total / 86400); ;
00520
00521 which_year_month_day(mydate, days, base_year, base_month, base_day);
00522
00523 mydate->hour = ((total % 86400) / 3600);
00524 mydate->minutes = ((total % 3600) / 60);
00525 mydate->seconds = (total % 60);
00526 }
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 gchar *decode_filetime_date(guchar *data, date_and_time_t *mydate)
00539 {
00540 if (data == NULL)
00541 {
00542 return NULL;
00543 }
00544 else
00545 {
00546 make_date_and_time(mydate, data, 8, 10000000, 1601, 1, 1);
00547 return date_printf(mydate);
00548 }
00549 }
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 gchar *decode_C_date(guchar *data, date_and_time_t *mydate)
00562 {
00563 if (data == NULL)
00564 {
00565 return NULL;
00566 }
00567 else
00568 {
00569 make_date_and_time(mydate, data, 4, 1, 1970, 1, 1);
00570 return date_printf(mydate);
00571 }
00572 }
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583 gchar *decode_HFS_date(guchar *data, date_and_time_t *mydate)
00584 {
00585 if (data == NULL)
00586 {
00587 return NULL;
00588 }
00589 else
00590 {
00591 make_date_and_time(mydate, data, 4, 1, 1904, 1, 1);
00592 return date_printf(mydate);
00593 }
00594 }
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 gchar *decode_to_bits(guchar *data)
00607 {
00608
00609 if (data == NULL)
00610 {
00611 return NULL;
00612 }
00613 else
00614 {
00615 return g_strdup_printf("%1u%1u%1u%1u%1u%1u%1u%1u",
00616 (data[0] & 0x80) > 0 ? 1 : 0,
00617 (data[0] & 0x40) > 0 ? 1 : 0,
00618 (data[0] & 0x20) > 0 ? 1 : 0,
00619 (data[0] & 0x10) > 0 ? 1 : 0,
00620 (data[0] & 0x08) > 0 ? 1 : 0,
00621 (data[0] & 0x04) > 0 ? 1 : 0,
00622 (data[0] & 0x02) > 0 ? 1 : 0,
00623 (data[0] & 0x01));
00624 }
00625 }
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637 static void transform_bcd_to_human(gchar *bcd, guint8 part, guint8 part_number)
00638 {
00639 switch (part)
00640 {
00641 case 0 :
00642 bcd[part_number] = '0';
00643 break;
00644 case 1 :
00645 bcd[part_number] = '1';
00646 break;
00647 case 2 :
00648 bcd[part_number] = '2';
00649 break;
00650 case 3 :
00651 bcd[part_number] = '3';
00652 break;
00653 case 4 :
00654 bcd[part_number] = '4';
00655 break;
00656 case 5 :
00657 bcd[part_number] = '5';
00658 break;
00659 case 6 :
00660 bcd[part_number] = '6';
00661 break;
00662 case 7 :
00663 bcd[part_number] = '7';
00664 break;
00665 case 8 :
00666 bcd[part_number] = '8';
00667 break;
00668 case 9 :
00669 bcd[part_number] = '9';
00670 break;
00671 case 10 :
00672 bcd[part_number] = '*';
00673 break;
00674 case 11 :
00675 bcd[part_number] = '#';
00676 break;
00677 case 12 :
00678 bcd[part_number] = 'a';
00679 break;
00680 case 13 :
00681 bcd[part_number] = 'b';
00682 break;
00683 case 14 :
00684 bcd[part_number] = 'c';
00685 break;
00686 case 15 :
00687 bcd[part_number] = ' ';
00688 break;
00689 default :
00690 bcd[part_number] = '?';
00691 break;
00692 }
00693 }
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704 gchar *decode_packed_BCD(guchar *data)
00705 {
00706 guint8 total = 0;
00707 gchar *bcd = NULL;
00708
00709 if (data == NULL)
00710 {
00711 return NULL;
00712 }
00713 else
00714 {
00715 memcpy(&total, data, sizeof(guchar));
00716 bcd = (gchar *) g_malloc0(3 * sizeof(gchar));
00717 transform_bcd_to_human(bcd, (total & 0x0F), 0);
00718 transform_bcd_to_human(bcd, ((total & 0xF0)>>4), 1);
00719 bcd[2] = '\0';
00720
00721 return bcd;
00722 }
00723 }
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735 gboolean swap_bytes(guchar *to_swap, guint first, guint last)
00736 {
00737 guchar aux;
00738
00739 if (first >= last)
00740 {
00741 return TRUE;
00742 }
00743 else
00744 {
00745 aux = to_swap[first];
00746 to_swap[first] = to_swap[last];
00747 to_swap[last] = aux;
00748 return swap_bytes(to_swap, ++first, --last);
00749 }
00750 }
00751
00752
00753
00754
00755
00756
00757
00758 void reverse_byte_order(guchar *to_reverse)
00759 {
00760 guint8 car = (guint8) to_reverse[0];
00761 guint8 aux = 0;
00762
00763 aux = ((car & 0x80) >> 7);
00764 aux += ((car & 0x40) >> 5);
00765 aux += ((car & 0x20) >> 3);
00766 aux += ((car & 0x10) >> 1);
00767 aux += ((car & 0x08) << 1);
00768 aux += ((car & 0x04) << 3);
00769 aux += ((car & 0x02) << 5);
00770 aux += ((car & 0x01) << 7);
00771
00772 to_reverse[0] = (guchar) aux;
00773 }