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 #include "heraia_types.h"
00026
00027 static gboolean bissextile_year(guint32 year);
00028 static void calc_which_month_day(date_and_time_t *mydate, guint32 day, guint tab_ns_months[12]);
00029 static void which_month_day(date_and_time_t *mydate, guint32 day, gboolean bi);
00030 static guint32 remove_days_from_first_january(guint32 base_year, guint8 base_month, guint8 base_day);
00031 static void which_year_month_day(date_and_time_t *mydate, guint32 days, guint32 base_year, guint base_month, guint8 base_day);
00032 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);
00033 static void transform_bcd_to_human(gchar *bcd, guint8 part, guint8 part_number);
00034
00038 static gboolean bissextile_year(guint32 year)
00039 {
00040
00041 if ((year % 4) == 0)
00042 {
00043 if ((year % 100) == 0)
00044 {
00045 if ((year % 400) == 0)
00046 {
00047 return TRUE;
00048 }
00049 else
00050 {
00051 return FALSE;
00052 }
00053 }
00054 else
00055 {
00056 return TRUE;
00057 }
00058 }
00059 else
00060 {
00061 return FALSE;
00062 }
00063 }
00064
00069 static void calc_which_month_day(date_and_time_t *mydate, guint32 day, guint tab_ns_months[12])
00070 {
00071 gushort i = 0;
00072
00073 while (i<12 && day > tab_ns_months[i])
00074 {
00075 i++;
00076 }
00077
00078 mydate->month = i + 1;
00079
00080 if (i == 0)
00081 {
00082 mydate->day = 1 + day;
00083 }
00084 else
00085 {
00086 mydate->day = (1 + day) - tab_ns_months[i-1];
00087 }
00088 }
00089
00093 static void which_month_day(date_and_time_t *mydate, guint32 day, gboolean bi)
00094 {
00095
00096 if (bi == TRUE)
00097 {
00098 if (day <= 366)
00099 {
00100 guint tab_ns_months[12] = { 31, 60, 91, 121, 152, 182,
00101 213, 244, 274, 305, 335, 366 } ;
00102 calc_which_month_day(mydate, day, tab_ns_months);
00103 }
00104 else
00105 {
00106 mydate->day = 0;
00107 mydate->month = 0;
00108 }
00109 }
00110 else
00111 {
00112 if (day <= 365)
00113 {
00114 guint tab_ns_months[12] = { 31, 59, 90, 120, 151, 181,
00115 212, 243, 273, 304, 334, 365 };
00116 calc_which_month_day(mydate, day, tab_ns_months);
00117 }
00118 else
00119 {
00120 mydate->day = 0;
00121 mydate->month = 0;
00122 }
00123 }
00124 }
00125
00130 static guint32 remove_days_from_first_january(guint32 base_year, guint8 base_month, guint8 base_day)
00131 {
00132 guint tab_ns_months[11];
00133
00134 if (base_day > 0 && base_day < 32)
00135 {
00136 base_day -= 1;
00137 }
00138 else
00139 {
00140 return 0;
00141 }
00142
00143 tab_ns_months[0] = 31;
00144 if (bissextile_year(base_year))
00145 {
00146 tab_ns_months[1] = 60;
00147 tab_ns_months[2] = 91;
00148 tab_ns_months[3] = 121;
00149 tab_ns_months[4] = 152;
00150 tab_ns_months[5] = 182;
00151 tab_ns_months[6] = 213;
00152 tab_ns_months[7] = 244;
00153 tab_ns_months[8] = 274;
00154 tab_ns_months[9] = 305;
00155 tab_ns_months[10] = 335;
00156 }
00157 else
00158 {
00159 tab_ns_months[1] = 59;
00160 tab_ns_months[2] = 90;
00161 tab_ns_months[3] = 120;
00162 tab_ns_months[4] = 151;
00163 tab_ns_months[5] = 181;
00164 tab_ns_months[6] = 212;
00165 tab_ns_months[7] = 243;
00166 tab_ns_months[8] = 273;
00167 tab_ns_months[9] = 304;
00168 tab_ns_months[10] = 334;
00169 }
00170
00171 if (base_month > 1 && base_month < 13)
00172 {
00173 return (tab_ns_months[base_month-2] + base_day);
00174 }
00175 else if (base_month == 1)
00176 {
00177 return base_day;
00178 }
00179 else
00180 {
00181 return 0;
00182 }
00183 }
00184
00185
00186
00199 static void which_year_month_day (date_and_time_t *mydate, guint32 days, guint32 base_year, guint base_month, guint8 base_day)
00200 {
00201 guint32 modulus = 0;
00202 guint32 reste = 0;
00203 guint32 nbdays = 0;
00204
00205 days -= remove_days_from_first_january(base_year, base_month, base_day);
00206
00207 if (days > 146100)
00208 {
00209 modulus = days / 146100;
00210 mydate->year = modulus * 400;
00211 reste = modulus * 3;
00212 days = days % 146100;
00213 }
00214
00215 modulus = days / 1461;
00216 mydate->year += modulus * 4;
00217 reste += (modulus*4) / 100;
00218 reste += days % 1461;
00219
00220 mydate->year += base_year;
00221 if (bissextile_year(mydate->year))
00222 nbdays = 366;
00223 else
00224 nbdays = 365;
00225
00226 while (reste > nbdays)
00227 {
00228 reste -= nbdays;
00229 mydate->year += 1;
00230 if (bissextile_year(mydate->year))
00231 nbdays = 366;
00232 else
00233 nbdays = 365;
00234 }
00235
00236 which_month_day(mydate, reste, bissextile_year(mydate->year));
00237 }
00238
00239
00247 static gchar *date_printf(date_and_time_t *mydate)
00248 {
00249 return g_strdup_printf("%02u/%02u/%04u - %02u:%02u:%02u", mydate->day, mydate->month, mydate->year, mydate->hour, mydate->minutes, mydate->seconds);
00250 }
00251
00252
00260 gchar *decode_dos_date(guchar *data, date_and_time_t *mydate)
00261 {
00262
00263 if (data == NULL)
00264 {
00265 return NULL;
00266 }
00267 else
00268 {
00269 mydate->year = (data[3] >> 1) + 1980;
00270 mydate->month = ((data[3] & 0x01) << 3) + (data[2] >> 5);
00271 mydate->day = data[2] & 0x1F;
00272 mydate->hour = (data[1] & 0xF8) >> 3;
00273 mydate->minutes = ((data[1] & 0x07) << 3) + ((data[0] & 0xE0) >> 5);
00274 mydate->seconds = (data[0] & 0x1F) << 1;
00275
00276 return date_printf(mydate);
00277 }
00278 }
00279
00280
00292 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)
00293 {
00294 guint64 total = 0;
00295 guint32 days = 0;
00296
00297 memcpy(&total, data, len * sizeof (guchar));
00298
00299 total = (total / nbticks);
00300 days = (guint32) (total / 86400); ;
00301
00302 which_year_month_day(mydate, days, base_year, base_month, base_day);
00303
00304 mydate->hour = ((total % 86400) / 3600);
00305 mydate->minutes = ((total % 3600) / 60);
00306 mydate->seconds = (total % 60);
00307 }
00308
00309
00317 gchar *decode_filetime_date(guchar *data, date_and_time_t *mydate)
00318 {
00319 if (data == NULL)
00320 {
00321 return NULL;
00322 }
00323 else
00324 {
00325 make_date_and_time(mydate, data, 8, 10000000, 1601, 1, 1);
00326 return date_printf(mydate);
00327 }
00328 }
00329
00330
00338 gchar *decode_C_date(guchar *data, date_and_time_t *mydate)
00339 {
00340 if (data == NULL)
00341 {
00342 return NULL;
00343 }
00344 else
00345 {
00346 make_date_and_time(mydate, data, 4, 1, 1970, 1, 1);
00347 return date_printf(mydate);
00348 }
00349 }
00350
00358 gchar *decode_HFS_date(guchar *data, date_and_time_t *mydate)
00359 {
00360 if (data == NULL)
00361 {
00362 return NULL;
00363 }
00364 else
00365 {
00366 make_date_and_time(mydate, data, 4, 1, 1904, 1, 1);
00367 return date_printf(mydate);
00368 }
00369 }
00370
00371
00372
00377 gchar *decode_to_bits(guchar *data)
00378 {
00379
00380 if (data == NULL)
00381 {
00382 return NULL;
00383 }
00384 else
00385 {
00386 return g_strdup_printf("%1u%1u%1u%1u%1u%1u%1u%1u",
00387 (data[0] & 0x80) > 0 ? 1 : 0,
00388 (data[0] & 0x40) > 0 ? 1 : 0,
00389 (data[0] & 0x20) > 0 ? 1 : 0,
00390 (data[0] & 0x10) > 0 ? 1 : 0,
00391 (data[0] & 0x08) > 0 ? 1 : 0,
00392 (data[0] & 0x04) > 0 ? 1 : 0,
00393 (data[0] & 0x02) > 0 ? 1 : 0,
00394 (data[0] & 0x01));
00395 }
00396 }
00397
00398
00405 static void transform_bcd_to_human(gchar *bcd, guint8 part, guint8 part_number)
00406 {
00407 switch (part)
00408 {
00409 case 0 :
00410 bcd[part_number] = '0';
00411 break;
00412 case 1 :
00413 bcd[part_number] = '1';
00414 break;
00415 case 2 :
00416 bcd[part_number] = '2';
00417 break;
00418 case 3 :
00419 bcd[part_number] = '3';
00420 break;
00421 case 4 :
00422 bcd[part_number] = '4';
00423 break;
00424 case 5 :
00425 bcd[part_number] = '5';
00426 break;
00427 case 6 :
00428 bcd[part_number] = '6';
00429 break;
00430 case 7 :
00431 bcd[part_number] = '7';
00432 break;
00433 case 8 :
00434 bcd[part_number] = '8';
00435 break;
00436 case 9 :
00437 bcd[part_number] = '9';
00438 break;
00439 case 10 :
00440 bcd[part_number] = '*';
00441 break;
00442 case 11 :
00443 bcd[part_number] = '#';
00444 break;
00445 case 12 :
00446 bcd[part_number] = 'a';
00447 break;
00448 case 13 :
00449 bcd[part_number] = 'b';
00450 break;
00451 case 14 :
00452 bcd[part_number] = 'c';
00453 break;
00454 case 15 :
00455 bcd[part_number] = ' ';
00456 break;
00457 default :
00458 bcd[part_number] = '?';
00459 break;
00460 }
00461 }
00462
00463
00469 gchar *decode_packed_BCD(guchar *data)
00470 {
00471 guint8 total = 0;
00472 gchar *bcd = NULL;
00473
00474 if (data == NULL)
00475 {
00476 return NULL;
00477 }
00478 else
00479 {
00480 memcpy(&total, data, sizeof(guchar));
00481 bcd = (gchar *) g_malloc0(3 * sizeof(gchar));
00482 transform_bcd_to_human(bcd, (total & 0x0F), 0);
00483 transform_bcd_to_human(bcd, ((total & 0xF0)>>4), 1);
00484 bcd[2] = '\0';
00485
00486 return bcd;
00487 }
00488 }
00489
00490
00497 gchar *decode_8bits_signed(guchar *data)
00498 {
00499 gint8 total = 0;
00500
00501 if (data == NULL)
00502 {
00503 return NULL;
00504 }
00505 else
00506 {
00507 memcpy(&total, data, sizeof (guchar));
00508 return g_strdup_printf("%d", total);
00509 }
00510 }
00511
00512
00519 gchar *decode_8bits_unsigned(guchar *data)
00520 {
00521 guint8 total = 0;
00522
00523 if (data == NULL)
00524 {
00525 return NULL;
00526 }
00527 else
00528 {
00529 memcpy(&total, data, sizeof (guchar));
00530 return g_strdup_printf("%u", total);
00531 }
00532 }
00533
00534
00541 gchar *decode_16bits_signed(guchar *data)
00542 {
00543 gint16 total = 0;
00544
00545 if (data == NULL)
00546 {
00547 return NULL;
00548 }
00549 else
00550 {
00551 memcpy(&total, data, 2 * sizeof (guchar));
00552 return g_strdup_printf("%d", total);
00553 }
00554 }
00555
00556
00563 gchar *decode_16bits_unsigned(guchar *data)
00564 {
00565 guint16 total = 0;
00566
00567 if (data == NULL)
00568 {
00569 return NULL;
00570 }
00571 else
00572 {
00573 memcpy(&total, data, 2 * sizeof (guchar));
00574 return g_strdup_printf("%u", total);
00575 }
00576 }
00577
00578
00585 gchar *decode_32bits_signed(guchar *data)
00586 {
00587 gint32 total = 0;
00588
00589 if (data == NULL)
00590 {
00591 return NULL;
00592 }
00593 else
00594 {
00595 memcpy(&total, data, 4 * sizeof (guchar));
00596 return g_strdup_printf("%d", total);
00597 }
00598 }
00599
00600
00607 gchar *decode_32bits_unsigned(guchar *data)
00608 {
00609 guint32 total = 0;
00610
00611 if (data == NULL)
00612 {
00613 return NULL;
00614 }
00615 else
00616 {
00617 memcpy(&total, data, 4 * sizeof (guchar));
00618 return g_strdup_printf("%u", total);
00619 }
00620 }
00621
00628 gchar *decode_64bits_signed(guchar *data)
00629 {
00630 gint64 total = 0;
00631
00632 if (data == NULL)
00633 {
00634 return NULL;
00635 }
00636 else
00637 {
00638 memcpy(&total, data, 8 * sizeof (guchar));
00639 return g_strdup_printf("%lld", total);
00640 }
00641 }
00642
00643
00650 gchar *decode_64bits_unsigned(guchar *data)
00651 {
00652 guint64 total = 0;
00653
00654 if (data == NULL)
00655 {
00656 return NULL;
00657 }
00658 else
00659 {
00660 memcpy(&total, data, 8 * sizeof (guchar));
00661 return g_strdup_printf("%llu", total);
00662 }
00663 }
00664
00665
00671 gboolean swap_bytes(guchar *to_swap, guint first, guint last)
00672 {
00673 guchar aux;
00674
00675 if (first >= last)
00676 {
00677 return TRUE;
00678 }
00679 else
00680 {
00681 aux = to_swap[first];
00682 to_swap[first] = to_swap[last];
00683 to_swap[last] = aux;
00684 return swap_bytes(to_swap, ++first, --last);
00685 }
00686 }
00687
00692 void reverse_byte_order(guchar *to_reverse)
00693 {
00694 guint8 car = (guint8) to_reverse[0];
00695 guint8 aux = 0;
00696
00697 aux = ((car & 0x80) >> 7);
00698 aux += ((car & 0x40) >> 5);
00699 aux += ((car & 0x20) >> 3);
00700 aux += ((car & 0x10) >> 1);
00701 aux += ((car & 0x08) << 1);
00702 aux += ((car & 0x04) << 3);
00703 aux += ((car & 0x02) << 5);
00704 aux += ((car & 0x01) << 7);
00705
00706 to_reverse[0] = (guchar) aux;
00707 }