| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package goipp |
| 10 |
|
| 11 |
import ( |
| 12 |
"bytes" |
| 13 |
"encoding/binary" |
| 14 |
"errors" |
| 15 |
"fmt" |
| 16 |
"math" |
| 17 |
"time" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
type Values []struct { |
| 22 |
T Tag |
| 23 |
V Value |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func (values *Values) Add(t Tag, v Value) { |
| 28 |
*values = append(*values, struct { |
| 29 |
T Tag |
| 30 |
V Value |
| 31 |
}{t, v}) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (values Values) String() string { |
| 36 |
if len(values) == 1 { |
| 37 |
return values[0].V.String() |
| 38 |
} |
| 39 |
|
| 40 |
var buf bytes.Buffer |
| 41 |
buf.Write([]byte("[")) |
| 42 |
for i, v := range values { |
| 43 |
if i != 0 { |
| 44 |
buf.Write([]byte(",")) |
| 45 |
} |
| 46 |
buf.Write([]byte(v.V.String())) |
| 47 |
} |
| 48 |
buf.Write([]byte("]")) |
| 49 |
|
| 50 |
return buf.String() |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func (values Values) Equal(values2 Values) bool { |
| 55 |
if len(values) != len(values2) { |
| 56 |
return false |
| 57 |
} |
| 58 |
|
| 59 |
for i, v := range values { |
| 60 |
v2 := values2[i] |
| 61 |
if v.T != v2.T || !ValueEqual(v.V, v2.V) { |
| 62 |
return false |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return true |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
type Value interface { |
| 71 |
String() string |
| 72 |
Type() Type |
| 73 |
encode() ([]byte, error) |
| 74 |
decode([]byte) (Value, error) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func ValueEqual(v1, v2 Value) bool { |
| 79 |
if v1.Type() != v2.Type() { |
| 80 |
return false |
| 81 |
} |
| 82 |
|
| 83 |
switch v1.Type() { |
| 84 |
case TypeDateTime: |
| 85 |
return v1.(Time).Equal(v2.(Time).Time) |
| 86 |
case TypeBinary: |
| 87 |
return bytes.Equal(v1.(Binary), v2.(Binary)) |
| 88 |
case TypeCollection: |
| 89 |
c1 := Attributes(v1.(Collection)) |
| 90 |
c2 := Attributes(v2.(Collection)) |
| 91 |
return c1.Equal(c2) |
| 92 |
} |
| 93 |
|
| 94 |
return v1 == v2 |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
type Void struct{} |
| 102 |
|
| 103 |
|
| 104 |
func (Void) String() string { return "" } |
| 105 |
|
| 106 |
|
| 107 |
func (Void) Type() Type { return TypeVoid } |
| 108 |
|
| 109 |
|
| 110 |
func (v Void) encode() ([]byte, error) { |
| 111 |
return []byte{}, nil |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func (Void) decode([]byte) (Value, error) { |
| 116 |
return Void{}, nil |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
type Integer int32 |
| 123 |
|
| 124 |
|
| 125 |
func (v Integer) String() string { return fmt.Sprintf("%d", int32(v)) } |
| 126 |
|
| 127 |
|
| 128 |
func (Integer) Type() Type { return TypeInteger } |
| 129 |
|
| 130 |
|
| 131 |
func (v Integer) encode() ([]byte, error) { |
| 132 |
return []byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}, nil |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (Integer) decode(data []byte) (Value, error) { |
| 137 |
if len(data) != 4 { |
| 138 |
return nil, errors.New("value must be 4 bytes") |
| 139 |
} |
| 140 |
|
| 141 |
return Integer(binary.BigEndian.Uint32(data)), nil |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
type Boolean bool |
| 148 |
|
| 149 |
|
| 150 |
func (v Boolean) String() string { return fmt.Sprintf("%t", bool(v)) } |
| 151 |
|
| 152 |
|
| 153 |
func (Boolean) Type() Type { return TypeBoolean } |
| 154 |
|
| 155 |
|
| 156 |
func (v Boolean) encode() ([]byte, error) { |
| 157 |
if v { |
| 158 |
return []byte{1}, nil |
| 159 |
} |
| 160 |
return []byte{0}, nil |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (Boolean) decode(data []byte) (Value, error) { |
| 165 |
if len(data) != 1 { |
| 166 |
return nil, errors.New("value must be 1 byte") |
| 167 |
} |
| 168 |
|
| 169 |
return Boolean(data[0] != 0), nil |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
type String string |
| 177 |
|
| 178 |
|
| 179 |
func (v String) String() string { return string(v) } |
| 180 |
|
| 181 |
|
| 182 |
func (String) Type() Type { return TypeString } |
| 183 |
|
| 184 |
|
| 185 |
func (v String) encode() ([]byte, error) { |
| 186 |
return []byte(v), nil |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
func (String) decode(data []byte) (Value, error) { |
| 191 |
return String(data), nil |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
type Time struct{ time.Time } |
| 198 |
|
| 199 |
|
| 200 |
func (v Time) String() string { return v.Time.Format(time.RFC3339) } |
| 201 |
|
| 202 |
|
| 203 |
func (Time) Type() Type { return TypeDateTime } |
| 204 |
|
| 205 |
|
| 206 |
func (v Time) encode() ([]byte, error) { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
year := v.Year() |
| 228 |
_, zone := v.Zone() |
| 229 |
dir := byte('+') |
| 230 |
if zone < 0 { |
| 231 |
zone = -zone |
| 232 |
dir = '-' |
| 233 |
} |
| 234 |
|
| 235 |
return []byte{ |
| 236 |
byte(year >> 8), byte(year), |
| 237 |
byte(v.Month()), |
| 238 |
byte(v.Day()), |
| 239 |
byte(v.Hour()), |
| 240 |
byte(v.Minute()), |
| 241 |
byte(v.Second()), |
| 242 |
byte(v.Nanosecond() / 100000000), |
| 243 |
dir, |
| 244 |
byte(zone / 3600), |
| 245 |
byte((zone / 60) % 60), |
| 246 |
}, nil |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
func (Time) decode(data []byte) (Value, error) { |
| 251 |
|
| 252 |
if len(data) != 9 && len(data) != 11 { |
| 253 |
return nil, errors.New("value must be 9 or 11 bytes") |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
var l *time.Location |
| 258 |
switch { |
| 259 |
case len(data) == 9: |
| 260 |
l = time.UTC |
| 261 |
case data[8] == '+', data[8] == '-': |
| 262 |
name := fmt.Sprintf("UTC%c%d", data[8], data[9]) |
| 263 |
if data[10] != 0 { |
| 264 |
name += fmt.Sprintf(":%d", data[10]) |
| 265 |
} |
| 266 |
|
| 267 |
off := 3600*int(data[9]) + 60*int(data[10]) |
| 268 |
if data[8] == '-' { |
| 269 |
off = -off |
| 270 |
} |
| 271 |
|
| 272 |
l = time.FixedZone(name, off) |
| 273 |
|
| 274 |
default: |
| 275 |
return nil, errors.New("invalid data format") |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
t := time.Date( |
| 280 |
int(binary.BigEndian.Uint16(data[0:2])), |
| 281 |
time.Month(data[2]), |
| 282 |
int(data[3]), |
| 283 |
int(data[4]), |
| 284 |
int(data[5]), |
| 285 |
int(data[6]), |
| 286 |
int(data[7])*100000000, |
| 287 |
l, |
| 288 |
) |
| 289 |
|
| 290 |
return Time{t}, nil |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
type Resolution struct { |
| 297 |
Xres, Yres int |
| 298 |
Units Units |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
func (v Resolution) String() string { |
| 303 |
return fmt.Sprintf("%dx%d%s", v.Xres, v.Yres, v.Units) |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
func (Resolution) Type() Type { return TypeResolution } |
| 308 |
|
| 309 |
|
| 310 |
func (v Resolution) encode() ([]byte, error) { |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
x, y := v.Xres, v.Yres |
| 317 |
|
| 318 |
return []byte{ |
| 319 |
byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x), |
| 320 |
byte(y >> 24), byte(y >> 16), byte(y >> 8), byte(y), |
| 321 |
byte(v.Units), |
| 322 |
}, nil |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
func (Resolution) decode(data []byte) (Value, error) { |
| 327 |
if len(data) != 9 { |
| 328 |
return nil, errors.New("value must be 9 bytes") |
| 329 |
} |
| 330 |
|
| 331 |
return Resolution{ |
| 332 |
Xres: int(binary.BigEndian.Uint32(data[0:4])), |
| 333 |
Yres: int(binary.BigEndian.Uint32(data[4:8])), |
| 334 |
Units: Units(data[8]), |
| 335 |
}, nil |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
type Units uint8 |
| 341 |
|
| 342 |
const ( |
| 343 |
UnitsDpi Units = 3 |
| 344 |
UnitsDpcm Units = 4 |
| 345 |
) |
| 346 |
|
| 347 |
|
| 348 |
func (u Units) String() string { |
| 349 |
switch u { |
| 350 |
case UnitsDpi: |
| 351 |
return "dpi" |
| 352 |
case UnitsDpcm: |
| 353 |
return "dpcm" |
| 354 |
default: |
| 355 |
return fmt.Sprintf("0x%2.2x", uint8(u)) |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
type Range struct { |
| 363 |
Lower, Upper int |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
func (v Range) String() string { |
| 368 |
return fmt.Sprintf("%d-%d", v.Lower, v.Upper) |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
func (Range) Type() Type { return TypeRange } |
| 373 |
|
| 374 |
|
| 375 |
func (v Range) encode() ([]byte, error) { |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
l, u := v.Lower, v.Upper |
| 381 |
|
| 382 |
return []byte{ |
| 383 |
byte(l >> 24), byte(l >> 16), byte(l >> 8), byte(l), |
| 384 |
byte(u >> 24), byte(u >> 16), byte(u >> 8), byte(u), |
| 385 |
}, nil |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
func (Range) decode(data []byte) (Value, error) { |
| 390 |
if len(data) != 8 { |
| 391 |
return nil, errors.New("value must be 9 bytes") |
| 392 |
} |
| 393 |
|
| 394 |
return Range{ |
| 395 |
Lower: int(binary.BigEndian.Uint32(data[0:4])), |
| 396 |
Upper: int(binary.BigEndian.Uint32(data[4:8])), |
| 397 |
}, nil |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
type TextWithLang struct { |
| 406 |
Lang, Text string |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
func (v TextWithLang) String() string { return v.Text + " [" + v.Lang + "]" } |
| 411 |
|
| 412 |
|
| 413 |
func (TextWithLang) Type() Type { return TypeTextWithLang } |
| 414 |
|
| 415 |
|
| 416 |
func (v TextWithLang) encode() ([]byte, error) { |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
lang := []byte(v.Lang) |
| 424 |
text := []byte(v.Text) |
| 425 |
|
| 426 |
if len(lang) > math.MaxUint16 { |
| 427 |
return nil, fmt.Errorf("Lang exceeds %d bytes", math.MaxUint16) |
| 428 |
} |
| 429 |
|
| 430 |
if len(text) > math.MaxUint16 { |
| 431 |
return nil, fmt.Errorf("Text exceeds %d bytes", math.MaxUint16) |
| 432 |
} |
| 433 |
|
| 434 |
data := make([]byte, 2+2+len(lang)+len(text)) |
| 435 |
binary.BigEndian.PutUint16(data, uint16(len(lang))) |
| 436 |
copy(data[2:], []byte(lang)) |
| 437 |
|
| 438 |
data2 := data[2+len(lang):] |
| 439 |
binary.BigEndian.PutUint16(data2, uint16(len(text))) |
| 440 |
copy(data2[2:], []byte(text)) |
| 441 |
|
| 442 |
return data, nil |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
func (TextWithLang) decode(data []byte) (Value, error) { |
| 447 |
var langLen, textLen int |
| 448 |
var lang, text string |
| 449 |
|
| 450 |
|
| 451 |
if len(data) < 2 { |
| 452 |
goto ERROR |
| 453 |
} |
| 454 |
|
| 455 |
langLen = int(binary.BigEndian.Uint16(data[0:2])) |
| 456 |
data = data[2:] |
| 457 |
|
| 458 |
|
| 459 |
if len(data) < langLen { |
| 460 |
goto ERROR |
| 461 |
} |
| 462 |
|
| 463 |
lang = string(data[:langLen]) |
| 464 |
data = data[langLen:] |
| 465 |
|
| 466 |
|
| 467 |
if len(data) < 2 { |
| 468 |
goto ERROR |
| 469 |
} |
| 470 |
|
| 471 |
textLen = int(binary.BigEndian.Uint16(data[0:2])) |
| 472 |
data = data[2:] |
| 473 |
|
| 474 |
|
| 475 |
if len(data) < textLen { |
| 476 |
goto ERROR |
| 477 |
} |
| 478 |
|
| 479 |
text = string(data[:textLen]) |
| 480 |
data = data[textLen:] |
| 481 |
|
| 482 |
|
| 483 |
if len(data) != 0 { |
| 484 |
goto ERROR |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
return TextWithLang{Lang: lang, Text: text}, nil |
| 489 |
|
| 490 |
ERROR: |
| 491 |
return nil, errors.New("invalid data format") |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
type Binary []byte |
| 496 |
|
| 497 |
|
| 498 |
func (v Binary) String() string { |
| 499 |
return fmt.Sprintf("%x", []byte(v)) |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
func (Binary) Type() Type { return TypeBinary } |
| 504 |
|
| 505 |
|
| 506 |
func (v Binary) encode() ([]byte, error) { |
| 507 |
return []byte(v), nil |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
func (Binary) decode(data []byte) (Value, error) { |
| 512 |
return Binary(data), nil |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
type Collection Attributes |
| 519 |
|
| 520 |
|
| 521 |
func (collection *Collection) Add(attr Attribute) { |
| 522 |
*collection = append(*collection, attr) |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
func (c1 Collection) Equal(c2 Attributes) bool { |
| 527 |
return Attributes(c1).Equal(Attributes(c2)) |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
func (v Collection) String() string { |
| 532 |
var buf bytes.Buffer |
| 533 |
buf.Write([]byte("{")) |
| 534 |
for i, attr := range v { |
| 535 |
if i > 0 { |
| 536 |
buf.Write([]byte(" ")) |
| 537 |
} |
| 538 |
fmt.Fprintf(&buf, "%s=%s", attr.Name, attr.Values) |
| 539 |
} |
| 540 |
buf.Write([]byte("}")) |
| 541 |
|
| 542 |
return buf.String() |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
func (Collection) Type() Type { return TypeCollection } |
| 547 |
|
| 548 |
|
| 549 |
func (v Collection) encode() ([]byte, error) { |
| 550 |
|
| 551 |
|
| 552 |
return []byte{}, nil |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
func (Collection) decode(data []byte) (Value, error) { |
| 557 |
panic("internal error") |
| 558 |
} |