github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/encoder/filename/init.go (about) 1 // Package filename provides utilities for encoder. 2 package filename 3 4 import ( 5 "encoding/base64" 6 "sync" 7 8 "github.com/klauspost/compress/huff0" 9 ) 10 11 // encodeURL is base64 url encoding values. 12 const encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" 13 14 // decodeMap will return x = decodeMap[encodeURL[byte(x)]] - 1 if x >= 0 and x < 64, otherwise -1 is returned. 15 var decodeMap [256]byte 16 17 // maxLength is the maximum length that will be attempted to be compressed. 18 const maxLength = 256 19 20 var ( 21 initOnce sync.Once // Used to control init of tables. 22 23 encTables [64]*huff0.Scratch // Encoders. 24 encTableLocks [64]sync.Mutex // Temporary locks for encoders since they are stateful. 25 decTables [64]*huff0.Decoder // Stateless decoders. 26 ) 27 28 const ( 29 tableUncompressed = 0 30 31 tableSCSU = 59 32 tableSCSUPlain = 60 33 tableRLE = 61 34 tableCustom = 62 35 tableReserved = 63 36 ) 37 38 // predefined tables as base64 URL encoded string. 39 var tablesData = [64]string{ 40 // Uncompressed 41 tableUncompressed: "", 42 // ncw home directory 43 1: "MRDIEtAAMAzDMAzDSjX_ybu0w97bb-L3b2mR-rUl5LXW3lZII43kIDMzM1NXu3okgQs=", 44 // ncw images 45 2: "IhDIAEAA______-Pou_4Sf5z-uS-39MVWjullFLKM7EBECs=", 46 // ncw Google Drive: 47 3: "JxDQAIIBMDMzMwOzbv7nJJCyd_m_9D2llCarnQX33nvvlFKEhUxAAQ==", 48 // Hex 49 4: "ExDoSTD___-tfXfhJ0hKSkryTxU=", 50 // Base64 51 5: "JRDIcQf_______8PgIiIiIgINkggARHlkQwSSCCBxHFYINHdfXI=", 52 // Hex plus a bit... 53 6: "E5CxwAHm9sYcAlmWZVvMHA4Y5jw=", 54 // Hex, upper case letters. 55 7: "FICxgAMMAGC3YwMthe3DWM_wDAAQ", 56 57 // Special tables: 58 // SCSU and a fairly generic table: 59 tableSCSU: "UxAgZmEB-RYPU8hrnAk6uMgpTNQMB5MGRBx0D3T0JjyUyY-yOi5CoGgktbAktSh7d36HtPTFu7SXJ7FYw_AYmA74ZH2vWgc8O6Z5jLnWnsFqU_4B", 60 // SCSU with no table... 61 tableSCSUPlain: "", 62 // Compressed data has its own table. 63 tableCustom: "", 64 // Reserved for extension. 65 tableReserved: "", 66 } 67 68 func initCoders() { 69 initOnce.Do(func() { 70 // Init base 64 decoder. 71 for i, v := range encodeURL { 72 decodeMap[v] = byte(i) + 1 73 } 74 75 // Initialize encoders and decoders. 76 for i, dataString := range tablesData { 77 if len(dataString) == 0 { 78 continue 79 } 80 data, err := base64.URLEncoding.DecodeString(dataString) 81 if err != nil { 82 panic(err) 83 } 84 s, _, err := huff0.ReadTable(data, nil) 85 if err != nil { 86 panic(err) 87 } 88 89 // We want to save at least len(in) >> 5 90 s.WantLogLess = 5 91 s.Reuse = huff0.ReusePolicyMust 92 encTables[i] = s 93 decTables[i] = s.Decoder() 94 } 95 // Add custom table type. 96 var s huff0.Scratch 97 s.Reuse = huff0.ReusePolicyNone 98 encTables[tableCustom] = &s 99 decTables[tableCustom] = nil 100 }) 101 }