
/* Example of use 


                 
                  void main(){

                              char tmp_name[200];
                              strcpy(tmp_name,"styl.css");

                              struct safeObject myObject;

                              createSafeObjectFromFile(&myObject,tmp_name);

                              char *ptr=myObject.sObjectStart;
                              for(int i=0;i<myObject.sSize;i++){
                              printf("%c",*ptr);ptr++;}
                              

                              ptr=myObject.sObjectStart;
                              free(ptr);}                                       */





char safeChars []={
                                                      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
                                                      'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                                                      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
                                                      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                                                      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','-', '_'};



long long getFileSize(const char *fileName) {
                                             FILE *file = fopen(fileName, "rb");
                                             if (!file) {
                                                 perror("Could not open the file");
                                                 return -1; // Wartość -1 oznacza błąd
                                             }
    
                                             fseek(file, 0, SEEK_END);
                                             long long fileSize = ftell(file);
                                             fclose(file);
    
                                             return fileSize;}


char getRandomSafeChar() {
                          const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                          return charset[rand() % (sizeof(charset) - 1)];}



char getRandomChar() {
                      return rand() % 126;}



struct safeObject{
                  long long sSize;
                  char *sObjectStart;
                  char *sObjectEnd;};


int getSafeCharCode(char *ptr){
                               char nr_1,nr_2;
                               for(int i=0;i<70;i++){
                                                      if(*ptr == safeChars[i]){nr_2=i;break;}}
                               ptr++;
                               for(int i=0;i<70;i++){
                                                      if(*ptr== safeChars[i]){nr_1=i;break;}}  
                               return (nr_1 << 4) | (nr_2 & 0x0F);
                               }




void createSafeObjectFromFile(struct safeObject *myObject,char *myFile){
                                                                 char tmp;
                                                                 char tmp_2;
                                                                 char low_bits,high_bits;
                                                                 long long size = getFileSize(myFile);

                                                                 myObject->sSize = size*2;
                                                                 printf("Rozmiar - %s  %lld\n",myFile,myObject->sSize);
                                                                 
                                                                 FILE *tmpFile = fopen(myFile,"rb");
                                                                 myObject->sObjectStart = (char*) malloc(((myObject->sSize)+1) * sizeof(char));
                                                                 char *tmp_ptr = myObject->sObjectStart; 
                                                                 while(!feof(tmpFile)){
                                                                                       tmp = fgetc(tmpFile);//printf("\n1-\n %d\n\n",tmp);
                                                                                       tmp_2 = tmp;
                                                                                       low_bits = tmp & 0x0F;
                                                                                       *tmp_ptr = safeChars[low_bits];
                                                                                       tmp_ptr++;
                                                                                       high_bits = (tmp_2 >> 4) & 0x0F;
                                                                                       *tmp_ptr =  safeChars[high_bits];
                                                                                       tmp_ptr++;}
                                                                 myObject->sObjectEnd = tmp_ptr-1;                      
                                                                 fclose(tmpFile);}



void createSafeObjectFromString(struct safeObject *myObject,char *string){ 
                                                                 char tmp;
                                                                 char tmp_2;char *tmp_ptr=string;
                                                               
                                                                 char low_bits,high_bits;
                                                                 long long size = strlen(string);

                                                                 myObject->sSize = size*2;
                                                                 //printf("Rozmiar - %s  %lld\n",myFile,myObject->sSize);
                                                                 
                                                                 //FILE *tmpFile = fopen(myFile,"rb");
                                                                 myObject->sObjectStart = (char*) malloc(((myObject->sSize)+1) * sizeof(char));
                                                                 
                                                                 tmp_ptr = myObject->sObjectStart;
                                                                 char *ptt = string;
                                                                 for(int z=0;z<strlen(string);z++){
                                                                                       tmp = *ptt;
                                                                                       tmp_2 = *ptt;
                                                                                       low_bits = tmp_2 & 0x0F;
                                                                                       *tmp_ptr = safeChars[low_bits];
                                                                                       tmp_ptr++;
                                                                                       high_bits = (tmp >> 4) & 0x0F;
                                                                                       *tmp_ptr =  safeChars[high_bits];
                                                                                       tmp_ptr++;ptt++;}
                                                                
                                                                   
                                                                 myObject->sObjectEnd = tmp_ptr-1;                      
                                                                 }

void stringFromLong(long long value, char *out) {
                                                 int dl = 0;
                                                 char tab[20]; // Powiększono tablicę, aby pomieścić większe liczby

                                                 if (value == 0) {
                                                     tab[dl++] = '0';
                                                 } else {
                                                     while (value > 0) {
                                                         tab[dl++] = (value % 10) + '0';  // Pobierz ostatnią cyfrę i zapisz jako znak
                                                         value /= 10;  // Usuń ostatnią cyfrę z liczby
                                                     }
                                                 }

                                                 // Odwrócenie tablicy, aby wynikowy string był w poprawnym formacie
                                                 for (int i = dl - 1; i >= 0; i--) {
                                                     *out++ = tab[i];
                                                 }

                                                 // Dodanie długości stringa na koniec
                                                 *out++ = dl + '0';

                                                 // Zakończenie stringa null-terminatorem
                                                 *out = '\0';
                                             }




                                                               







