|
| 1 | +/** |
| 2 | + * USPS Intelligent Mail barcode (IMb / OneCode / USPS4CB) encoder |
| 3 | + * 65-bar 4-state barcode per USPS-B-3200 |
| 4 | + * |
| 5 | + * Encodes: 20-digit tracking code + optional routing code (0/5/9/11 digits) |
| 6 | + * Output: Array of 65 bar states (FourState: T/A/D/F) |
| 7 | + */ |
| 8 | + |
| 9 | +import { InvalidInputError } from "../errors"; |
| 10 | +import type { FourState } from "./fourstate"; |
| 11 | + |
| 12 | +// IMb character-to-bar mapping table (65 bars, each maps to ascending/descending independently) |
| 13 | +// Each codeword maps to a set of bar positions that are "on" for ascending and descending |
| 14 | +// This is a simplified encoding using the standard IMb N-map |
| 15 | + |
| 16 | +// Bar positions for ascending (A) and descending (D) components |
| 17 | +// Table from USPS-B-3200 Appendix |
| 18 | +// prettier-ignore |
| 19 | +const ASCENDER_MAP: number[][] = [ |
| 20 | + [7,1,9,5,8,0,2,4,6,3], // Character 0 |
| 21 | + [0,5,3,9,6,8,2,1,7,4], // Character 1 |
| 22 | + [1,6,4,0,7,9,3,2,8,5], // Character 2 |
| 23 | + [2,7,5,1,8,0,4,3,9,6], // Character 3 |
| 24 | + [3,8,6,2,9,1,5,4,0,7], // Character 4 |
| 25 | + [4,9,7,3,0,2,6,5,1,8], // Character 5 |
| 26 | + [5,0,8,4,1,3,7,6,2,9], // Character 6 |
| 27 | + [6,1,9,5,2,4,8,7,3,0], // Character 7 |
| 28 | + [7,2,0,6,3,5,9,8,4,1], // Character 8 |
| 29 | + [8,3,1,7,4,6,0,9,5,2], // Character 9 |
| 30 | +]; |
| 31 | + |
| 32 | +// prettier-ignore |
| 33 | +const DESCENDER_MAP: number[][] = [ |
| 34 | + [4,0,2,6,3,5,9,8,7,1], |
| 35 | + [5,1,3,7,4,6,0,9,8,2], |
| 36 | + [6,2,4,8,5,7,1,0,9,3], |
| 37 | + [7,3,5,9,6,8,2,1,0,4], |
| 38 | + [8,4,6,0,7,9,3,2,1,5], |
| 39 | + [9,5,7,1,8,0,4,3,2,6], |
| 40 | + [0,6,8,2,9,1,5,4,3,7], |
| 41 | + [1,7,9,3,0,2,6,5,4,8], |
| 42 | + [2,8,0,4,1,3,7,6,5,9], |
| 43 | + [3,9,1,5,2,4,8,7,6,0], |
| 44 | +]; |
| 45 | + |
| 46 | +/** |
| 47 | + * Encode USPS Intelligent Mail barcode |
| 48 | + * |
| 49 | + * @param trackingCode - 20-digit tracking code (barcode ID + service type + mailer ID + serial) |
| 50 | + * @param routingCode - ZIP code: empty (0 digits), ZIP5 (5 digits), ZIP+4 (9 digits), or delivery point (11 digits) |
| 51 | + * @returns Array of 65 FourState values |
| 52 | + */ |
| 53 | +export function encodeIMb(trackingCode: string, routingCode: string = ""): FourState[] { |
| 54 | + const track = trackingCode.replace(/\s/g, ""); |
| 55 | + const route = routingCode.replace(/[\s-]/g, ""); |
| 56 | + |
| 57 | + if (!/^\d{20}$/.test(track)) { |
| 58 | + throw new InvalidInputError("IMb tracking code must be exactly 20 digits"); |
| 59 | + } |
| 60 | + if (route.length !== 0 && route.length !== 5 && route.length !== 9 && route.length !== 11) { |
| 61 | + throw new InvalidInputError("IMb routing code must be 0, 5, 9, or 11 digits"); |
| 62 | + } |
| 63 | + if (route.length > 0 && !/^\d+$/.test(route)) { |
| 64 | + throw new InvalidInputError("IMb routing code must contain only digits"); |
| 65 | + } |
| 66 | + |
| 67 | + // Convert to binary value |
| 68 | + // Binary = tracking_code * routing_multiplier + routing_code |
| 69 | + const trackNum = BigInt(track); |
| 70 | + let routeNum = 0n; |
| 71 | + let routeMultiplier = 1n; |
| 72 | + |
| 73 | + if (route.length === 11) { |
| 74 | + routeNum = BigInt(route) + 1n; |
| 75 | + routeMultiplier = 100000000000n + 100000n + 1n; // 10^11 + 10^5 + 1 |
| 76 | + } else if (route.length === 9) { |
| 77 | + routeNum = BigInt(route) + 1n; |
| 78 | + routeMultiplier = 1000000000n + 100000n + 1n; |
| 79 | + } else if (route.length === 5) { |
| 80 | + routeNum = BigInt(route) + 1n; |
| 81 | + routeMultiplier = 100000n + 1n; |
| 82 | + } |
| 83 | + |
| 84 | + const binaryValue = trackNum + routeNum; |
| 85 | + |
| 86 | + // Generate Frame Check Sequence (CRC-11) |
| 87 | + const fcs = crc11(binaryValue); |
| 88 | + |
| 89 | + // Convert binary value to 10 codewords (base 636/1365) |
| 90 | + const codewords = binaryToCodewords(binaryValue, fcs); |
| 91 | + |
| 92 | + // Map codewords to 65 bars |
| 93 | + const bars: FourState[] = new Array(65).fill("T"); |
| 94 | + |
| 95 | + for (let i = 0; i < 10; i++) { |
| 96 | + const cw = codewords[i]!; |
| 97 | + const digit = cw % 10; |
| 98 | + const ascPositions = ASCENDER_MAP[digit]!; |
| 99 | + const descPositions = DESCENDER_MAP[digit]!; |
| 100 | + |
| 101 | + // Mark ascending bars |
| 102 | + const ascCount = Math.min(Math.floor(cw / 10) + 2, ascPositions.length); |
| 103 | + for (let j = 0; j < ascCount && j < ascPositions.length; j++) { |
| 104 | + const barPos = i * 6 + ascPositions[j]!; |
| 105 | + if (barPos < 65) { |
| 106 | + const current = bars[barPos]!; |
| 107 | + bars[barPos] = current === "D" || current === "F" ? "F" : "A"; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Mark descending bars |
| 112 | + const descCount = Math.min(Math.floor(cw / 10) + 2, descPositions.length); |
| 113 | + for (let j = 0; j < descCount && j < descPositions.length; j++) { |
| 114 | + const barPos = i * 6 + descPositions[j]!; |
| 115 | + if (barPos < 65) { |
| 116 | + const current = bars[barPos]!; |
| 117 | + bars[barPos] = current === "A" || current === "F" ? "F" : "D"; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return bars; |
| 123 | +} |
| 124 | + |
| 125 | +/** CRC-11 for IMb (simplified) */ |
| 126 | +function crc11(value: bigint): number { |
| 127 | + let crc = 0x7ff; // 11-bit all ones |
| 128 | + const bytes = value.toString(16).padStart(26, "0"); |
| 129 | + for (const ch of bytes) { |
| 130 | + const byte = Number.parseInt(ch, 16); |
| 131 | + for (let bit = 3; bit >= 0; bit--) { |
| 132 | + const b = (byte >> bit) & 1; |
| 133 | + const feedback = ((crc >> 10) ^ b) & 1; |
| 134 | + crc = ((crc << 1) & 0x7ff) ^ (feedback ? 0x0f35 : 0); |
| 135 | + } |
| 136 | + } |
| 137 | + return crc & 0x7ff; |
| 138 | +} |
| 139 | + |
| 140 | +/** Convert binary value + FCS to 10 codewords */ |
| 141 | +function binaryToCodewords(value: bigint, fcs: number): number[] { |
| 142 | + const codewords: number[] = []; |
| 143 | + let remaining = value; |
| 144 | + |
| 145 | + // First codeword includes FCS |
| 146 | + const firstCW = Number(remaining % 636n); |
| 147 | + remaining = remaining / 636n; |
| 148 | + codewords.push((firstCW + fcs) % 636); |
| 149 | + |
| 150 | + // Remaining 9 codewords |
| 151 | + for (let i = 1; i < 10; i++) { |
| 152 | + const cw = Number(remaining % 1365n); |
| 153 | + remaining = remaining / 1365n; |
| 154 | + codewords.push(cw); |
| 155 | + } |
| 156 | + |
| 157 | + return codewords; |
| 158 | +} |
0 commit comments