Changeset 1ecde20 in tol
- Timestamp:
- Oct 5, 2010, 1:47:18 PM (16 years ago)
- Branches:
- 1941-docker-image-to-test, devel, master
- Children:
- 7d3cadb
- Parents:
- c3a68a9
- Location:
- tol
- Files:
-
- 3 edited
-
bbasic/tol_btext.h (modified) (2 diffs)
-
bbasic/txtalgeb.cpp (modified) (3 diffs)
-
btol/text_type/txtgra.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
tol/bbasic/tol_btext.h
rc3a68a9 r1ecde20 485 485 void ReallocateBuffer(BInt newSize); 486 486 487 void base64_encode(const BArray<unsigned char>& binary); 488 int base64_decode(BArray<unsigned char>& binary) const; 489 void base64_encode_from_file(const BText& path); 490 int base64_decode_to_file(const BText& path) const; 491 487 492 private: 488 493 … … 706 711 } 707 712 713 std::string base64_encode(const BArray<unsigned char>& binary); 714 int base64_decode(BArray<unsigned char>& binary, std::string const& encoded_string); 715 708 716 #endif // TOL_BTEXT_H 709 717 -
tol/bbasic/txtalgeb.cpp
rc3a68a9 r1ecde20 28 28 #include <tol/tol_btimer.h> 29 29 #include <tol/tol_barray.h> 30 #include <tol/tol_bdir.h> 30 31 //#include <tol/tol_bfilter.h> no parece que haga falta aqui 31 32 … … 1598 1599 } 1599 1600 length_ = 0; 1600 BChar ch = 0, c ;1601 BChar ch = 0, c = 0; 1601 1602 bool untilIsLF = until=='\n'; 1602 1603 for(;(length_<=maxChars)&&(ch!=until)&&(!feof(file)); length_++) … … 1798 1799 1799 1800 1801 1802 static const std::string base64_chars = 1803 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 1804 "abcdefghijklmnopqrstuvwxyz" 1805 "0123456789+/"; 1806 1807 1808 //-------------------------------------------------------------------- 1809 static inline bool is_base64(unsigned char c) 1810 //-------------------------------------------------------------------- 1811 { 1812 return (isalnum(c) || (c == '+') || (c == '/')); 1813 } 1814 1815 //-------------------------------------------------------------------- 1816 std::string base64_encode(const BArray<unsigned char>& binary) 1817 //-------------------------------------------------------------------- 1818 { 1819 unsigned char const* bytes_to_encode = binary.Buffer(); 1820 unsigned int in_len = binary.Size(); 1821 std::string ret; 1822 int i = 0; 1823 int j = 0; 1824 unsigned char char_array_3[3]; 1825 unsigned char char_array_4[4]; 1826 1827 while (in_len--) { 1828 char_array_3[i++] = *(bytes_to_encode++); 1829 if (i == 3) { 1830 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 1831 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 1832 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 1833 char_array_4[3] = char_array_3[2] & 0x3f; 1834 1835 for(i = 0; (i <4) ; i++) 1836 ret += base64_chars[char_array_4[i]]; 1837 i = 0; 1838 } 1839 } 1840 1841 if (i) 1842 { 1843 for(j = i; j < 3; j++) 1844 char_array_3[j] = '\0'; 1845 1846 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 1847 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 1848 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 1849 char_array_4[3] = char_array_3[2] & 0x3f; 1850 1851 for (j = 0; (j < i + 1); j++) 1852 ret += base64_chars[char_array_4[j]]; 1853 1854 while((i++ < 3)) 1855 ret += '='; 1856 1857 } 1858 1859 return ret; 1860 1861 } 1862 1863 //-------------------------------------------------------------------- 1864 int base64_decode(BArray<unsigned char>& binary, std::string const& encoded_string) 1865 //-------------------------------------------------------------------- 1866 { 1867 int in_len = encoded_string.size(); 1868 int i = 0; 1869 int j = 0; 1870 int in_ = 0; 1871 unsigned char char_array_4[4], char_array_3[3]; 1872 int bin_len = (in_len * 2)/3 +1; 1873 binary.AllocBuffer(bin_len); 1874 binary.ReallocBuffer(0); 1875 1876 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { 1877 char_array_4[i++] = encoded_string[in_]; in_++; 1878 if (i ==4) { 1879 for (i = 0; i <4; i++) 1880 char_array_4[i] = base64_chars.find(char_array_4[i]); 1881 1882 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 1883 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 1884 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 1885 1886 for (i = 0; (i < 3); i++) 1887 binary.Add(char_array_3[i]); 1888 i = 0; 1889 } 1890 } 1891 if (i) { 1892 for (j = i; j <4; j++) 1893 char_array_4[j] = 0; 1894 1895 for (j = 0; j <4; j++) 1896 char_array_4[j] = base64_chars.find(char_array_4[j]); 1897 1898 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 1899 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 1900 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 1901 1902 for (j = 0; (j < i - 1); j++) { binary.Add(char_array_3[j]); } 1903 } 1904 return binary.Size(); 1905 } 1906 1907 //-------------------------------------------------------------------- 1908 void BText::base64_encode(const BArray<unsigned char>& binary) 1909 //-------------------------------------------------------------------- 1910 { 1911 std::string str = ::base64_encode(binary); 1912 Copy(str.c_str(),str.length()); 1913 } 1914 1915 //-------------------------------------------------------------------- 1916 int BText::base64_decode(BArray<unsigned char>& binary) const 1917 //-------------------------------------------------------------------- 1918 { 1919 std::string str(String(), Length()); 1920 return(::base64_decode(binary, str)); 1921 } 1922 1923 //-------------------------------------------------------------------- 1924 void BText::base64_encode_from_file(const BText& path) 1925 //-------------------------------------------------------------------- 1926 { 1927 BDir dir(path); 1928 bool ok = false; 1929 if(dir.Exist() && dir.IsFile()) 1930 { 1931 BArray<unsigned char> binary; 1932 binary.AllocBuffer(dir.Bytes()); 1933 FILE *stream = fopen(path.String(),"rb"); 1934 if(stream) 1935 { 1936 fread(binary.GetBuffer(),1,binary.Size(),stream); 1937 fclose(stream); 1938 ok = true; 1939 base64_encode(binary); 1940 } 1941 } 1942 if(!ok) 1943 { 1944 Error(BText("[base64_encode_from_file]: " 1945 "cannot open file for reading:")+path); 1946 ReallocateBuffer(0); 1947 } 1948 } 1949 1950 //-------------------------------------------------------------------- 1951 int BText::base64_decode_to_file(const BText& path) const 1952 //-------------------------------------------------------------------- 1953 { 1954 BArray<unsigned char> binary; 1955 int size = 0; 1956 FILE *stream = fopen(path.String(),"wb"); 1957 if(stream) 1958 { 1959 size = base64_decode(binary); 1960 fwrite(binary.Buffer(),1,binary.Size(),stream); 1961 fclose(stream); 1962 } 1963 else 1964 { 1965 Error(BText("[base64_decode_to_file]: " 1966 "cannot open file for writing:")+path); 1967 } 1968 return(size); 1969 } 1970 1971 -
tol/btol/text_type/txtgra.cpp
rc3a68a9 r1ecde20 1907 1907 } 1908 1908 1909 //-------------------------------------------------------------------- 1910 DeclareContensClass(BText, BTxtTemporary, BTxtEncodeBase64FromFile); 1911 DefExtOpr(1, BTxtEncodeBase64FromFile, "EncodeBase64FromFile", 1, 1, 1912 "Text", 1913 I2("(Text fileName)", 1914 "(Text nombreFichero)"), 1915 I2("Returns the contents of a binary file decoded with Base64 " 1916 "algorithm (http://en.wikipedia.org/wiki/Base64). " 1917 "If the file not exists returns " 1918 "the void string and displays a warning message.", 1919 "Devuelve el contenido de un fichero binario codificado con " 1920 "el algoritmo Base64 (http://es.wikipedia.org/wiki/Base64). " 1921 "Si el fichero no existe " 1922 "devuelve la cadena vacía y muestra un mensaje de advertencia."), 1923 BOperClassify::Text_); 1924 1925 //-------------------------------------------------------------------- 1926 void BTxtEncodeBase64FromFile::CalcContens() 1927 //-------------------------------------------------------------------- 1928 { 1929 BText& path = Text(Arg(1)); 1930 contens_.base64_encode_from_file(path); 1931 } 1932 1933 1934 //-------------------------------------------------------------------- 1935 DeclareContensClass(BDat, BDatTemporary, BDatDecodeBase64ToFile); 1936 DefExtOpr(1, BDatDecodeBase64ToFile, "DecodeBase64ToFile", 2, 2, 1937 "Text Text", 1938 I2("(Text fileName, Text encoded)", 1939 "(Text nombreFichero, Text encoded)"), 1940 I2("Writes a binary a file with the result of decoding a text " 1941 "using Base64 algorithm (http://en.wikipedia.org/wiki/Base64). " 1942 "It returns the binary file size.", 1943 "Escribe un fichero binario con el resultado de descodificar un " 1944 "texto usando el algoritmo Base64 " 1945 "(http://es.wikipedia.org/wiki/Base64). Develve el tamaño del " 1946 "fichero creado."), 1947 BOperClassify::Text_); 1948 1949 //-------------------------------------------------------------------- 1950 void BDatDecodeBase64ToFile::CalcContens() 1951 //-------------------------------------------------------------------- 1952 { 1953 BText& path = Text(Arg(1)); 1954 BText& encoded = Text(Arg(2)); 1955 contens_ = encoded.base64_decode_to_file(path); 1956 } 1909 1957 1910 1958 //--------------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.
